[lxc-devel] [PATCH] lxc-create: use posix shell instead of bash
Serge Hallyn
serge.hallyn at canonical.com
Mon Nov 19 16:36:46 UTC 2012
Quoting Natanael Copa (ncopa at alpinelinux.org):
> - use '[ -x /path/prog ]' instead of 'type /path/prog'
> - avoid getopt --longoptions
> - add \ at after && and || when those are at end of line
> - make sure condition expands to empty string if variable is empty
>
looks good to me, thanks.
Acked-by: Serge E. Hallyn <serge.hallyn at ubuntu.com>
> Signed-off-by: Natanael Copa <ncopa at alpinelinux.org>
> ---
> I have tested it with 'lxc-create -t debian ...' with busybox as /bin/sh
>
> src/lxc/lxc-create.in | 70 +++++++++++++++++++++++++++++----------------------
> 1 file changed, 40 insertions(+), 30 deletions(-)
>
> diff --git a/src/lxc/lxc-create.in b/src/lxc/lxc-create.in
> index 101e489..e8056e9 100644
> --- a/src/lxc/lxc-create.in
> +++ b/src/lxc/lxc-create.in
> @@ -1,4 +1,4 @@
> -#!/bin/bash
> +#!/bin/sh
>
> #
> # lxc: linux Container library
> @@ -54,16 +54,26 @@ help() {
> echo " $(basename $0) -t ubuntu -h" >&2
> exit 0
> fi
> - type ${templatedir}/lxc-$lxc_template 2>/dev/null
> - if [ $? -eq 0 ]; then
> + if [ -x ${templatedir}/lxc-$lxc_template ]; then
> echo >&2
> echo "Template-specific options (TEMPLATE_OPTIONS):" >&2
> ${templatedir}/lxc-$lxc_template -h
> fi
> }
>
> -shortoptions='hn:f:t:B:'
> -longoptions='help,name:,config:,template:,backingstore:,fstype:,dir:,lvname:,vgname:,fssize:'
> +usage_err() {
> + [ -n "$1" ] && echo "$1" >&2
> + usage
> + exit 1
> +}
> +
> +optarg_check() {
> + local opt="$1" optarg="$2"
> + if [ -z "$optarg" ]; then
> + usage_err "option '$opt' requires an argument"
> + fi
> +}
> +
> lxc_path=@LXCPATH@
> bindir=@BINDIR@
> templatedir=@LXCTEMPLATEDIR@
> @@ -73,68 +83,69 @@ fssize=500M
> vgname=lxc
> custom_rootfs=""
>
> -getopt=$(getopt -o $shortoptions --longoptions $longoptions -- "$@")
> -if [ $? != 0 ]; then
> - usage
> - exit 1;
> -fi
> -
> -eval set -- "$getopt"
> -
> -while true; do
> - case "$1" in
> +while [ $# -gt 0 ]; do
> + local opt="$1"
> + shift
> + case "$opt" in
> -h|--help)
> help
> exit 1
> ;;
> -n|--name)
> - shift
> + optarg_check $opt "$1"
> lxc_name=$1
> shift
> ;;
> -f|--config)
> - shift
> + optarg_check $opt "$1"
> lxc_config=$1
> shift
> ;;
> -t|--template)
> - shift
> + optarg_check $opt "$1"
> lxc_template=$1
> shift
> ;;
> -B|--backingstore)
> - shift
> + optarg_check $opt "$1"
> backingstore=$1
> shift
> ;;
> --dir)
> - shift
> + optarg_check $opt "$1"
> custom_rootfs=$1
> shift
> ;;
> --lvname)
> - shift
> + optarg_check $opt "$1"
> lvname=$1
> shift
> ;;
> --vgname)
> - shift
> + optarg_check $opt "$1"
> vgname=$1
> shift
> ;;
> --fstype)
> - shift
> + optarg_check $opt "$1"
> fstype=$1
> shift
> ;;
> --fssize)
> - shift
> + optarg_check $opt "$1"
> fssize=$1
> shift
> ;;
> --)
> shift
> break;;
> + -?)
> + usage_err "unknown option '$opt'"
> + ;;
> + -*)
> + # split opts -abc into -a -b -c
> + set -- $(echo "${opt#-}" | sed 's/\(.\)/ -\1/g') "$@"
> + ;;
> *)
> usage
> exit 1
> @@ -200,7 +211,7 @@ rootfs="$lxc_path/$lxc_name/rootfs"
>
> if [ "$backingstore" = "_unset" -o "$backingstore" = "btrfs" ]; then
> # 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
> backingstore="btrfs"
> else
> @@ -246,10 +257,10 @@ elif [ "$backingstore" = "btrfs" ]; then
> fi
>
> cleanup() {
> - if [ $backingstore = "lvm" ]; then
> + if [ "$backingstore" = "lvm" ]; then
> umount $rootfs
> lvremove -f $rootdev
> - elif [ $backingstore = "btrfs" ]; then
> + elif [ "$backingstore" = "btrfs" ]; then
> btrfs subvolume delete "$rootfs"
> fi
> ${bindir}/lxc-destroy -n $lxc_name
> @@ -299,8 +310,7 @@ if [ ! -z $lxc_template ]; then
> template_path=${templatedir}/lxc-$lxc_template
> fi
>
> - type $template_path 2>/dev/null
> - if [ $? -ne 0 ]; then
> + if ! [ -x "$template_path" ]; then
> echo "$(basename $0): unknown template '$lxc_template'" >&2
> cleanup
> fi
> @@ -314,7 +324,7 @@ if [ ! -z $lxc_template ]; then
> echo "'$lxc_template' template installed"
> fi
>
> -if [ $backingstore = "lvm" ]; then
> +if [ "$backingstore" = "lvm" ]; then
> echo "Unmounting LVM"
> umount $rootfs
>
> --
> 1.8.0
>
>
> ------------------------------------------------------------------------------
> Monitor your physical, virtual and cloud infrastructure from a single
> web console. Get in-depth insight into apps, servers, databases, vmware,
> SAP, cloud infrastructure, etc. Download 30-day Free Trial.
> Pricing starts from $795 for 25 servers or applications!
> http://p.sf.net/sfu/zoho_dev2dev_nov
> _______________________________________________
> Lxc-devel mailing list
> Lxc-devel at lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/lxc-devel
More information about the lxc-devel
mailing list