[lxc-devel] [PATCH] lxc-create: Script cleanup

Stéphane Graber stgraber at ubuntu.com
Mon Dec 3 14:31:44 UTC 2012


 - 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: Stéphane Graber <stgraber at ubuntu.com>
---
 src/lxc/lxc-create.in | 175
+++++++++++++++++++++++++-------------------------
 1 file changed, 89 insertions(+), 86 deletions(-)

diff --git a/src/lxc/lxc-create.in b/src/lxc/lxc-create.in
index 7a50633..650a245 100644
--- a/src/lxc/lxc-create.in
+++ b/src/lxc/lxc-create.in
@@ -49,7 +49,7 @@ help() {
     echo "  --fstype FS_TYPE   specify the filesystem type (default:
ext4)" >&2
     echo "  --fssize FS_SIZE   specify the filesystem size (default:
500M)" >&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 "  $(basename $0) -t ubuntu -h" >&2
         exit 0
@@ -83,83 +83,82 @@ vgname=lxc
 custom_rootfs=""
  while [ $# -gt 0 ]; do
-        opt="$1"
-        shift
-        case "$opt" in
-	    -h|--help)
-		help
-		exit 1
-		;;
-	    -n|--name)
-		optarg_check $opt "$1"
-		lxc_name=$1
-		shift
-		;;
-	    -f|--config)
-		optarg_check $opt "$1"
-		lxc_config=$1
-		shift
-		;;
-	    -t|--template)
-		optarg_check $opt "$1"
-		lxc_template=$1
-		shift
-		;;
-	    -B|--backingstore)
-		optarg_check $opt "$1"
-		backingstore=$1
-		shift
-		;;
-	    --dir)
-		optarg_check $opt "$1"
-		custom_rootfs=$1
-		shift
-		;;
-	    --lvname)
-		optarg_check $opt "$1"
-		lvname=$1
-		shift
-		;;
-	    --vgname)
-		optarg_check $opt "$1"
-		vgname=$1
-		shift
-		;;
-	    --fstype)
-		optarg_check $opt "$1"
-		fstype=$1
-		shift
-		;;
-	    --fssize)
-		optarg_check $opt "$1"
-		fssize=$1
-		shift
-		;;
-            --)
-		break;;
-	    -?)
-		usage_err "unknown option '$opt'"
-		;;
-	    -*)
-		# split opts -abc into -a -b -c
-		set -- $(echo "${opt#-}" | sed 's/\(.\)/ -\1/g') "$@"
-		;;
-            *)
-		usage
-		exit 1
-		;;
-        esac
+    opt="$1"
+    shift
+    case "$opt" in
+        -h|--help)
+            help
+            exit 1
+            ;;
+        -n|--name)
+            optarg_check $opt "$1"
+            lxc_name=$1
+            shift
+            ;;
+        -f|--config)
+            optarg_check $opt "$1"
+            lxc_config=$1
+            shift
+            ;;
+        -t|--template)
+            optarg_check $opt "$1"
+            lxc_template=$1
+            shift
+            ;;
+        -B|--backingstore)
+            optarg_check $opt "$1"
+            backingstore=$1
+            shift
+            ;;
+        --dir)
+            optarg_check $opt "$1"
+            custom_rootfs=$1
+            shift
+            ;;
+        --lvname)
+            optarg_check $opt "$1"
+            lvname=$1
+            shift
+            ;;
+        --vgname)
+            optarg_check $opt "$1"
+            vgname=$1
+            shift
+            ;;
+        --fstype)
+            optarg_check $opt "$1"
+            fstype=$1
+            shift
+            ;;
+        --fssize)
+            optarg_check $opt "$1"
+            fssize=$1
+            shift
+            ;;
+        --)
+            break;;
+        -?)
+            usage_err "unknown option '$opt'"
+            ;;
+        -*)
+            # split opts -abc into -a -b -c
+            set -- $(echo "${opt#-}" | sed 's/\(.\)/ -\1/g') "$@"
+            ;;
+        *)
+            usage
+            exit 1
+            ;;
+    esac
 done
  # If -h or --help was passed into the container, we'll want to cleanup
 # afterward
 wantedhelp=0
-for var in "$@"
-do
-if [ "$var" = "-h" -o "$var" = "--help" ]; then
-    help
-    exit 1
-fi
+for var in "$@"; do
+    if [ "$var" = "-h" ] || [ "$var" = "--help" ]; then
+        help
+        exit 1
+    fi
 done
  @@ -188,13 +187,14 @@ if [ "$(id -u)" != "0" ]; then
    exit 1
 fi
 -if [ -n "$custom_rootfs" -a "$backingstore" != "dir" ]; then
+if [ -n "$custom_rootfs" ] && [ "$backingstore" != "dir" ]; then
    echo "--dir is only valid with -B dir"
 fi
  case "$backingstore" in
     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
         exit 1
         ;;
@@ -207,7 +207,7 @@ fi
  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 which btrfs >/dev/null 2>&1 && \
         btrfs filesystem df "$lxc_path/" >/dev/null 2>&1; then
@@ -221,12 +221,13 @@ if [ "$backingstore" = "_unset" -o "$backingstore"
= "btrfs" ]; then
     fi
 fi
 -if [ $backingstore = "lvm" ]; then
+if [ "$backingstore" = "lvm" ]; then
     which vgscan > /dev/null
     if [ $? -ne 0 ]; then
         echo "$(basename $0): vgscan not found (is lvm2 installed?)" >&2
         exit 1
     fi
+
     grep -q "\<$fstype\>" /proc/filesystems
     if [ $? -ne 0 ]; then
         echo "$(basename $0): $fstype is not listed in
/proc/filesystems" >&2
@@ -246,6 +247,7 @@ if [ $backingstore = "lvm" ]; then
         echo "please delete it (using \"lvremove $rootdev\") and try
again" >&2
         exit 1
     fi
+
 elif [ "$backingstore" = "btrfs" ]; then
     mkdir "$lxc_path/$lxc_name"
     if ! out=$(btrfs subvolume create "$rootfs" 2>&1); then
@@ -261,6 +263,7 @@ cleanup() {
     elif [ "$backingstore" = "btrfs" ]; then
         btrfs subvolume delete "$rootfs"
     fi
+
     ${bindir}/lxc-destroy -n $lxc_name
     echo "$(basename $0): aborted" >&2
     exit 1
@@ -282,7 +285,7 @@ if [ ! -r "$lxc_config" ]; then
 fi
  # 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
 else
     template_path=${templatedir}/lxc-$lxc_template
@@ -293,7 +296,7 @@ if ! [ -x "$template_path" ]; then
     cleanup
 fi
 -if [ ! -z $lxc_template ]; then
+if [ ! -z "$lxc_template" ]; then
     sum=$(sha1sum $template_path | cut -d ' ' -f1)
     echo "# Template used to create this container: $lxc_template" >>
$lxc_path/$lxc_name/config
     if [ -n "$*" ]; then
@@ -306,15 +309,15 @@ fi
 cat $lxc_config >> $lxc_path/$lxc_name/config
  if [ -n "$custom_rootfs" ]; then
-	if grep -q "lxc.rootfs" $lxc_path/$lxc_name/config ; then
-		echo "configuration file already specifies a lxc.rootfs"
-		exit 1
-	fi
-	echo "lxc.rootfs = $custom_rootfs" >> $lxc_path/$lxc_name/config
+    if grep -q "lxc.rootfs" $lxc_path/$lxc_name/config ; then
+        echo "configuration file already specifies a lxc.rootfs"
+        exit 1
+    fi
+    echo "lxc.rootfs = $custom_rootfs" >> $lxc_path/$lxc_name/config
 fi
  # Create the fs as needed
-if [ $backingstore = "lvm" ]; then
+if [ "$backingstore" = "lvm" ]; then
     [ -d "$rootfs" ] || mkdir $rootfs
     lvcreate -L $fssize -n $lvname $vgname || exit 1
     udevadm settle
@@ -322,7 +325,7 @@ if [ $backingstore = "lvm" ]; then
     mount -t $fstype $rootdev $rootfs
 fi
 -if [ ! -z $lxc_template ]; then
+if [ ! -z "$lxc_template" ]; then
     $template_path --path=$lxc_path/$lxc_name --name=$lxc_name $*
     if [ $? -ne 0 ]; then
         echo "$(basename $0): failed to execute template
'$lxc_template'" >&2
-- 
1.8.0


-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 899 bytes
Desc: OpenPGP digital signature
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20121203/1686eba2/attachment.pgp>


More information about the lxc-devel mailing list