lxc-create: use posix shell instead of bash

- 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

Signed-off-by: Natanael Copa <ncopa@alpinelinux.org>
Acked-by: Serge E. Hallyn <serge.hallyn@ubuntu.com>
Acked-by: Stéphane Graber <stgraber@ubuntu.com>
This commit is contained in:
Natanael Copa 2012-11-16 17:01:55 +01:00 committed by Stéphane Graber
parent c3752c0b59
commit e60a8164c1

View File

@ -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