Commit 1d4f0646 by Stéphane Graber

lxc-create: Script cleanup

- Removes the mixed tabs/spaces, replacing by standard 4 spaces indent. - Fix a bunch of bashisms. - Use shell syntax for and/or in if statements instead of the "test" syntax. - Improve block spacing a bit. Signed-off-by: 's avatarStéphane Graber <stgraber@ubuntu.com> Acked-by: 's avatarSerge E. Hallyn <serge.hallyn@ubuntu.com>
parent fa0e6e1b
...@@ -49,7 +49,7 @@ help() { ...@@ -49,7 +49,7 @@ help() {
echo " --fstype FS_TYPE specify the filesystem type (default: ext4)" >&2 echo " --fstype FS_TYPE specify the filesystem type (default: ext4)" >&2
echo " --fssize FS_SIZE specify the filesystem size (default: 500M)" >&2 echo " --fssize FS_SIZE specify the filesystem size (default: 500M)" >&2
echo >&2 echo >&2
if [ -z $lxc_template ]; then if [ -z "$lxc_template" ]; then
echo "To see template-specific options, specify a template. For example:" >&2 echo "To see template-specific options, specify a template. For example:" >&2
echo " $(basename $0) -t ubuntu -h" >&2 echo " $(basename $0) -t ubuntu -h" >&2
exit 0 exit 0
...@@ -83,83 +83,82 @@ vgname=lxc ...@@ -83,83 +83,82 @@ vgname=lxc
custom_rootfs="" custom_rootfs=""
while [ $# -gt 0 ]; do while [ $# -gt 0 ]; do
opt="$1" opt="$1"
shift shift
case "$opt" in case "$opt" in
-h|--help) -h|--help)
help help
exit 1 exit 1
;; ;;
-n|--name) -n|--name)
optarg_check $opt "$1" optarg_check $opt "$1"
lxc_name=$1 lxc_name=$1
shift shift
;; ;;
-f|--config) -f|--config)
optarg_check $opt "$1" optarg_check $opt "$1"
lxc_config=$1 lxc_config=$1
shift shift
;; ;;
-t|--template) -t|--template)
optarg_check $opt "$1" optarg_check $opt "$1"
lxc_template=$1 lxc_template=$1
shift shift
;; ;;
-B|--backingstore) -B|--backingstore)
optarg_check $opt "$1" optarg_check $opt "$1"
backingstore=$1 backingstore=$1
shift shift
;; ;;
--dir) --dir)
optarg_check $opt "$1" optarg_check $opt "$1"
custom_rootfs=$1 custom_rootfs=$1
shift shift
;; ;;
--lvname) --lvname)
optarg_check $opt "$1" optarg_check $opt "$1"
lvname=$1 lvname=$1
shift shift
;; ;;
--vgname) --vgname)
optarg_check $opt "$1" optarg_check $opt "$1"
vgname=$1 vgname=$1
shift shift
;; ;;
--fstype) --fstype)
optarg_check $opt "$1" optarg_check $opt "$1"
fstype=$1 fstype=$1
shift shift
;; ;;
--fssize) --fssize)
optarg_check $opt "$1" optarg_check $opt "$1"
fssize=$1 fssize=$1
shift shift
;; ;;
--) --)
break;; break;;
-?) -?)
usage_err "unknown option '$opt'" usage_err "unknown option '$opt'"
;; ;;
-*) -*)
# split opts -abc into -a -b -c # split opts -abc into -a -b -c
set -- $(echo "${opt#-}" | sed 's/\(.\)/ -\1/g') "$@" set -- $(echo "${opt#-}" | sed 's/\(.\)/ -\1/g') "$@"
;; ;;
*) *)
usage usage
exit 1 exit 1
;; ;;
esac esac
done done
# If -h or --help was passed into the container, we'll want to cleanup # If -h or --help was passed into the container, we'll want to cleanup
# afterward # afterward
wantedhelp=0 wantedhelp=0
for var in "$@" for var in "$@"; do
do if [ "$var" = "-h" ] || [ "$var" = "--help" ]; then
if [ "$var" = "-h" -o "$var" = "--help" ]; then help
help exit 1
exit 1 fi
fi
done done
...@@ -188,13 +187,14 @@ if [ "$(id -u)" != "0" ]; then ...@@ -188,13 +187,14 @@ if [ "$(id -u)" != "0" ]; then
exit 1 exit 1
fi fi
if [ -n "$custom_rootfs" -a "$backingstore" != "dir" ]; then if [ -n "$custom_rootfs" ] && [ "$backingstore" != "dir" ]; then
echo "--dir is only valid with -B dir" echo "--dir is only valid with -B dir"
fi fi
case "$backingstore" in case "$backingstore" in
dir|lvm|none|btrfs|_unset) :;; dir|lvm|none|btrfs|_unset) :;;
*) echo "$(basename $0): '$backingstore' is not known (try 'none', 'dir', 'lvm', 'btrfs')" >&2 *)
echo "$(basename $0): '$backingstore' is not known (try 'none', 'dir', 'lvm', 'btrfs')" >&2
usage usage
exit 1 exit 1
;; ;;
...@@ -207,7 +207,7 @@ fi ...@@ -207,7 +207,7 @@ fi
rootfs="$lxc_path/$lxc_name/rootfs" rootfs="$lxc_path/$lxc_name/rootfs"
if [ "$backingstore" = "_unset" -o "$backingstore" = "btrfs" ]; then if [ "$backingstore" = "_unset" ] || [ "$backingstore" = "btrfs" ]; then
# if no backing store was given, then see if btrfs would work # if no backing store was given, then see if btrfs would work
if which btrfs >/dev/null 2>&1 && \ if which btrfs >/dev/null 2>&1 && \
btrfs filesystem df "$lxc_path/" >/dev/null 2>&1; then btrfs filesystem df "$lxc_path/" >/dev/null 2>&1; then
...@@ -221,12 +221,13 @@ if [ "$backingstore" = "_unset" -o "$backingstore" = "btrfs" ]; then ...@@ -221,12 +221,13 @@ if [ "$backingstore" = "_unset" -o "$backingstore" = "btrfs" ]; then
fi fi
fi fi
if [ $backingstore = "lvm" ]; then if [ "$backingstore" = "lvm" ]; then
which vgscan > /dev/null which vgscan > /dev/null
if [ $? -ne 0 ]; then if [ $? -ne 0 ]; then
echo "$(basename $0): vgscan not found (is lvm2 installed?)" >&2 echo "$(basename $0): vgscan not found (is lvm2 installed?)" >&2
exit 1 exit 1
fi fi
grep -q "\<$fstype\>" /proc/filesystems grep -q "\<$fstype\>" /proc/filesystems
if [ $? -ne 0 ]; then if [ $? -ne 0 ]; then
echo "$(basename $0): $fstype is not listed in /proc/filesystems" >&2 echo "$(basename $0): $fstype is not listed in /proc/filesystems" >&2
...@@ -246,6 +247,7 @@ if [ $backingstore = "lvm" ]; then ...@@ -246,6 +247,7 @@ if [ $backingstore = "lvm" ]; then
echo "please delete it (using \"lvremove $rootdev\") and try again" >&2 echo "please delete it (using \"lvremove $rootdev\") and try again" >&2
exit 1 exit 1
fi fi
elif [ "$backingstore" = "btrfs" ]; then elif [ "$backingstore" = "btrfs" ]; then
mkdir "$lxc_path/$lxc_name" mkdir "$lxc_path/$lxc_name"
if ! out=$(btrfs subvolume create "$rootfs" 2>&1); then if ! out=$(btrfs subvolume create "$rootfs" 2>&1); then
...@@ -261,6 +263,7 @@ cleanup() { ...@@ -261,6 +263,7 @@ cleanup() {
elif [ "$backingstore" = "btrfs" ]; then elif [ "$backingstore" = "btrfs" ]; then
btrfs subvolume delete "$rootfs" btrfs subvolume delete "$rootfs"
fi fi
${bindir}/lxc-destroy -n $lxc_name ${bindir}/lxc-destroy -n $lxc_name
echo "$(basename $0): aborted" >&2 echo "$(basename $0): aborted" >&2
exit 1 exit 1
...@@ -282,7 +285,7 @@ if [ ! -r "$lxc_config" ]; then ...@@ -282,7 +285,7 @@ if [ ! -r "$lxc_config" ]; then
fi fi
# Allow for a path to be provided as the template name # Allow for a path to be provided as the template name
if [ -x $lxc_template ]; then if [ -x "$lxc_template" ]; then
template_path=$lxc_template template_path=$lxc_template
else else
template_path=${templatedir}/lxc-$lxc_template template_path=${templatedir}/lxc-$lxc_template
...@@ -293,7 +296,7 @@ if ! [ -x "$template_path" ]; then ...@@ -293,7 +296,7 @@ if ! [ -x "$template_path" ]; then
cleanup cleanup
fi fi
if [ ! -z $lxc_template ]; then if [ ! -z "$lxc_template" ]; then
sum=$(sha1sum $template_path | cut -d ' ' -f1) sum=$(sha1sum $template_path | cut -d ' ' -f1)
echo "# Template used to create this container: $lxc_template" >> $lxc_path/$lxc_name/config echo "# Template used to create this container: $lxc_template" >> $lxc_path/$lxc_name/config
if [ -n "$*" ]; then if [ -n "$*" ]; then
...@@ -306,15 +309,15 @@ fi ...@@ -306,15 +309,15 @@ fi
cat $lxc_config >> $lxc_path/$lxc_name/config cat $lxc_config >> $lxc_path/$lxc_name/config
if [ -n "$custom_rootfs" ]; then if [ -n "$custom_rootfs" ]; then
if grep -q "lxc.rootfs" $lxc_path/$lxc_name/config ; then if grep -q "lxc.rootfs" $lxc_path/$lxc_name/config ; then
echo "configuration file already specifies a lxc.rootfs" echo "configuration file already specifies a lxc.rootfs"
exit 1 exit 1
fi fi
echo "lxc.rootfs = $custom_rootfs" >> $lxc_path/$lxc_name/config echo "lxc.rootfs = $custom_rootfs" >> $lxc_path/$lxc_name/config
fi fi
# Create the fs as needed # Create the fs as needed
if [ $backingstore = "lvm" ]; then if [ "$backingstore" = "lvm" ]; then
[ -d "$rootfs" ] || mkdir $rootfs [ -d "$rootfs" ] || mkdir $rootfs
lvcreate -L $fssize -n $lvname $vgname || exit 1 lvcreate -L $fssize -n $lvname $vgname || exit 1
udevadm settle udevadm settle
...@@ -322,7 +325,7 @@ if [ $backingstore = "lvm" ]; then ...@@ -322,7 +325,7 @@ if [ $backingstore = "lvm" ]; then
mount -t $fstype $rootdev $rootfs mount -t $fstype $rootdev $rootfs
fi fi
if [ ! -z $lxc_template ]; then if [ ! -z "$lxc_template" ]; then
$template_path --path=$lxc_path/$lxc_name --name=$lxc_name $* $template_path --path=$lxc_path/$lxc_name --name=$lxc_name $*
if [ $? -ne 0 ]; then if [ $? -ne 0 ]; then
echo "$(basename $0): failed to execute template '$lxc_template'" >&2 echo "$(basename $0): failed to execute template '$lxc_template'" >&2
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment