On Wed, 14 Nov 2012 08:40:39 +0100 Natanael Copa <nc...@alpinelinux.org> wrote:
> I wonder if it there are any interest to make the scripts posix shell > compliant so they can run with for example busybox ash and dash. I should maybe add that I think the biggest challenge will be the longopts parsing. That said I have managed to make an alternative that should make it possible to be compatible with the current option parsing so we would not need break scripts that uses current lcx-* scripts. I also think it might not be worth convert the template scripts since those probably will depend on bash, perl and other stuff anyways. The scripts I'm interested in make posix compliant are: lxc-checkconfig.in lxc-clone.in lxc-create.in lxc-destroy.in lxc-ls.in lxc-netstat.in lxc-ps.in lxc-setcap.in lxc-setuid.in lxc-shutdown.in lxc-version.in I have made a few of them already as shown below (mostly untested). I have a solution for: 'prog arg1 --opt1 -- arg2' in case that would ever be needed. I think it would be nice with some kind of feedback before I continue. commit 6b65cb9e5a33ce3cc1156329b8e775c0b6aa36ee Author: Natanael Copa <nc...@alpinelinux.org> Date: Wed Nov 14 13:55:21 2012 +0100 lxc-ls: use posix shell instead of bash Signed-off-by: Natanael Copa <nc...@alpinelinux.org> diff --git a/src/lxc/lxc-ls.in b/src/lxc/lxc-ls.in index 9293323..948eef9 100644 --- a/src/lxc/lxc-ls.in +++ b/src/lxc/lxc-ls.in @@ -1,4 +1,4 @@ -#!/bin/bash +#!/bin/sh # # lxc: linux Container library @@ -18,10 +18,11 @@ # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA lxc_path=@LXCPATH@ +program=${0##*/} usage() { - echo "usage: $(basename $0) [--active] [--] [LS_OPTIONS...]" >&2 + echo "usage: $program [--active] [--] [LS_OPTIONS...]" >&2 } help() { @@ -61,11 +62,10 @@ get_parent_cgroup() # Return the absolute path to the containers' parent cgroup # (do not append '/lxc' if the hierarchy contains the 'ns' subsystem) - if [[ ",$subsystems," == *,ns,* ]]; then - parent_cgroup="${mountpoint}${init_cgroup%/}" - else - parent_cgroup="${mountpoint}${init_cgroup%/}/lxc" - fi + case ",$subsystems," in + *,ns,*) parent_cgroup="${mountpoint}${init_cgroup%/}";; + *) parent_cgroup="${mountpoint}${init_cgroup%/}/lxc";; + esac break done } @@ -91,7 +91,7 @@ if [ ! -z "$directory" ]; then fi if [ -z "$containers" ]; then - echo "$(basename $0): no containers found" >&2 + echo "$program: no containers found" >&2 exit 1 fi commit bc4c995b12bc6d0df563296d929ccf5266f657d8 Author: Natanael Copa <nc...@alpinelinux.org> Date: Wed Nov 14 13:54:04 2012 +0100 lxc-create: use posix shell instead of bash Signed-off-by: Natanael Copa <nc...@alpinelinux.org> diff --git a/src/lxc/lxc-create.in b/src/lxc/lxc-create.in index b21cdc3..98bf380 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 @@ -20,8 +20,9 @@ # License along with this library; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +program=${0##*/} usage() { - echo "usage: $(basename $0) -n NAME [-f CONFIG_FILE] [-t TEMPLATE] [FS_OPTIONS] --" >&2 + echo "usage: $program -n NAME [-f CONFIG_FILE] [-t TEMPLATE] [FS_OPTIONS] --" >&2 echo " [TEMPLATE_OPTIONS]" >&2 echo >&2 echo "where FS_OPTIONS is one of:" >&2 @@ -49,7 +50,7 @@ help() { echo >&2 if [ -z $lxc_template ]; then echo "To see template-specific options, specify a template. For example:" >&2 - echo " $(basename $0) -t ubuntu -h" >&2 + echo " $program -t ubuntu -h" >&2 exit 0 fi type ${templatedir}/lxc-$lxc_template 2>/dev/null @@ -60,8 +61,19 @@ help() { fi } -shortoptions='hn:f:t:B:' -longoptions='help,name:,config:,template:,backingstore:,fstype:,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@ @@ -70,66 +82,65 @@ fstype=ext4 fssize=500M vgname=lxc -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 ;; --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 + usage_err ;; esac done @@ -147,19 +158,17 @@ done if [ -z "$lxc_path" ]; then - echo "$(basename $0): no configuration path defined" >&2 + echo "$program: no configuration path defined" >&2 exit 1 fi if [ ! -r $lxc_path ]; then - echo "$(basename $0): configuration path '$lxc_path' not found" >&2 + echo "$program: configuration path '$lxc_path' not found" >&2 exit 1 fi if [ -z "$lxc_name" ]; then - echo "$(basename $0): no container name specified" >&2 - usage - exit 1 + usage_err "$program: no container name specified" fi if [ -z "$lvname" ]; then @@ -167,20 +176,19 @@ if [ -z "$lvname" ]; then fi if [ "$(id -u)" != "0" ]; then - echo "$(basename $0): must be run as root" >&2 + echo "$program: must be run as root" >&2 exit 1 fi case "$backingstore" in lvm|none|btrfs|_unset) :;; - *) echo "$(basename $0): '$backingstore' is not known (try 'none', 'lvm', 'btrfs')" >&2 - usage - exit 1 + *) echo "$program: '$backingstore' is not known (try 'none', 'lvm', 'btrfs')" >&2 + usage_err ;; esac if [ -d "$lxc_path/$lxc_name" ]; then - echo "$(basename $0): '$lxc_name' already exists" >&2 + echo "$program: '$lxc_name' already exists" >&2 exit 1 fi @@ -188,12 +196,12 @@ 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 if [ "$backingstore" = "btrfs" ]; then - echo "$(basename $0): missing 'btrfs' command or $lxc_path is not btrfs" >&2 + echo "$program: missing 'btrfs' command or $lxc_path is not btrfs" >&2 exit 1; fi backingstore="none" @@ -203,32 +211,32 @@ fi if [ $backingstore = "lvm" ]; then which vgscan > /dev/null if [ $? -ne 0 ]; then - echo "$(basename $0): vgscan not found (is lvm2 installed?)" >&2 + echo "$program: 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 + echo "$program: $fstype is not listed in /proc/filesystems" >&2 exit 1 fi vgscan | grep -q "Found volume group \"$vgname\"" if [ $? -ne 0 ]; then - echo "$(basename $0): could not find volume group \"$vgname\"" >&2 + echo "$program: could not find volume group \"$vgname\"" >&2 exit 1 fi rootdev=/dev/$vgname/$lvname lvdisplay $rootdev > /dev/null 2>&1 if [ $? -eq 0 ]; then - echo "$(basename $0): backing store already exists: $rootdev" >&2 + echo "$program: backing store already exists: $rootdev" >&2 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 - echo "$(basename $0): failed to create subvolume in $rootfs: $out" >&2 + echo "$program: failed to create subvolume in $rootfs: $out" >&2 exit 1; fi fi @@ -239,7 +247,7 @@ cleanup() { lvremove -f $rootdev fi ${bindir}/lxc-destroy -n $lxc_name - echo "$(basename $0): aborted" >&2 + echo "$program: aborted" >&2 exit 1 } @@ -251,7 +259,7 @@ if [ -z "$lxc_config" ]; then touch $lxc_path/$lxc_name/config else if [ ! -r "$lxc_config" ]; then - echo "$(basename $0): '$lxc_config' configuration file not found" >&2 + echo "$program: '$lxc_config' configuration file not found" >&2 exit 1 fi @@ -259,19 +267,19 @@ else fi # Create the fs as needed -if [ $backingstore = "lvm" ]; then - [ -d "$rootfs" ] || mkdir $rootfs +if [ "$backingstore" = "lvm" ]; then + [ -d "$rootfs" ] || mkdir "$rootfs" lvcreate -L $fssize -n $lvname $vgname || exit 1 udevadm settle mkfs -t $fstype $rootdev || exit 1 mount -t $fstype $rootdev $rootfs fi -if [ ! -z $lxc_template ]; then +if [ ! -z "$lxc_template" ]; then type ${templatedir}/lxc-$lxc_template 2>/dev/null if [ $? -ne 0 ]; then - echo "$(basename $0): unknown template '$lxc_template'" >&2 + echo "$program: unknown template '$lxc_template'" >&2 cleanup fi @@ -284,7 +292,7 @@ if [ ! -z $lxc_template ]; then ${templatedir}/lxc-$lxc_template --path=$lxc_path/$lxc_name --name=$lxc_name $* if [ $? -ne 0 ]; then - echo "$(basename $0): failed to execute template '$lxc_template'" >&2 + echo "$program: failed to execute template '$lxc_template'" >&2 cleanup fi commit 5900a83d621cb2871a4a8376a1387ac389b98065 Author: Natanael Copa <nc...@alpinelinux.org> Date: Wed Nov 14 13:53:34 2012 +0100 lxc-clone: use posix shell instead of bash Signed-off-by: Natanael Copa <nc...@alpinelinux.org> diff --git a/src/lxc/lxc-clone.in b/src/lxc/lxc-clone.in index 04ef20b..b04f623 100644 --- a/src/lxc/lxc-clone.in +++ b/src/lxc/lxc-clone.in @@ -1,4 +1,4 @@ -#!/bin/bash +#!/bin/sh # # lxc: linux Container library @@ -23,8 +23,10 @@ set -e +program=${0##*/} + usage() { - echo "usage: $(basename $0) -o ORIG_NAME -n NEW_NAME [-s] [-h] [-L FS_SIZE]" >&2 + echo "usage: $program -o ORIG_NAME -n NEW_NAME [-s] [-h] [-L FS_SIZE]" >&2 echo " [-v VG_NAME] [-p LV_PREFIX] [-t FS_TYPE]" >&2 } @@ -44,8 +46,19 @@ help() { echo " only works for non-snapshot LVM)" >&2 } -shortoptions='ho:n:sL:v:p:t:' -longoptions='help,orig:,name:,snapshot,fssize:,vgname:,lvprefix:,fstype:' +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@ snapshot=no @@ -55,95 +68,89 @@ lxc_vg=lxc lxc_lv_prefix="" fstype=ext3 -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 ;; -s|--snapshot) - shift snapshot=yes snapshot_opt="-s" ;; -o|--orig) - shift + optarg_check $opt $1 lxc_orig=$1 shift ;; -L|--fssize) - shift + optarg_check $opt $1 lxc_size=$1 shift ;; -v|--vgname) - shift + optarg_check $opt $1 lxc_vg=$1 shift ;; -n|--name) - shift + optarg_check $opt $1 lxc_new=$1 shift ;; -p|--lvprefix) - shift + optarg_check $opt $1 lxc_lv_prefix=$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 + usage_err ;; esac done if [ -z "$lxc_path" ]; then - echo "$(basename $0): no configuration path defined" >&2 + echo "$program: no configuration path defined" >&2 exit 1 fi if [ ! -r $lxc_path ]; then - echo "$(basename $0): configuration path '$lxc_path' not found" >&2 + echo "$program: configuration path '$lxc_path' not found" >&2 exit 1 fi if [ -z "$lxc_orig" ]; then - echo "$(basename $0): no original container name specified" >&2 - usage - exit 1 + usage_err "$program: no original container name specified" fi if [ -z "$lxc_new" ]; then - echo "$(basename $0): no new container name specified" >&2 - usage - exit 1 + usage_err "$program: no new container name specified" fi if [ "$(id -u)" != "0" ]; then - echo "$(basename $0): must be run as root" >&2 + echo "$program: must be run as root" >&2 exit 1 fi if [ ! -d "$lxc_path/$lxc_orig" ]; then - echo "$(basename $0): '$lxc_orig' does not exist" >&2 + echo "$program: '$lxc_orig' does not exist" >&2 exit 1 fi if [ -d "$lxc_path/$lxc_new" ]; then - echo "$(basename $0): '$lxc_new' already exists" >&2 + echo "$program: '$lxc_new' already exists" >&2 exit 1 fi @@ -162,7 +169,7 @@ cleanup() { if [ $frozen -eq 1 ]; then lxc-unfreeze -n $lxc_orig fi - echo "$(basename $0): aborted" >&2 + echo "$program: aborted" >&2 exit 1 } trap cleanup HUP INT TERM @@ -191,8 +198,8 @@ lxc-info -s -n $lxc_orig|grep RUNNING >/dev/null 2>&1 || container_running=False sed -i '/lxc.rootfs/d' $lxc_path/$lxc_new/config if [ -b $oldroot ]; then - type vgscan || { echo "$(basename $0): lvm is not installed" >&2; false; } - lvdisplay $oldroot > /dev/null 2>&1 || { echo "$(basename $0): non-lvm blockdev cloning is not supported" >&2; false; } + type vgscan || { echo "$program: lvm is not installed" >&2; false; } + lvdisplay $oldroot > /dev/null 2>&1 || { echo "$program: non-lvm blockdev cloning is not supported" >&2; false; } lvm=TRUE # ok, create a snapshot of the lvm device if [ $container_running = "True" ]; then @@ -216,16 +223,16 @@ if [ -b $oldroot ]; then if [ $snapshot = "no" ]; then #mount snapshot mkdir -p ${rootfs}_snapshot - mount /dev/$lxc_vg/${lxc_lv_prefix}${lxc_new}_snapshot ${rootfs}_snapshot || { echo "$(basename $0): failed to mount new rootfs_snapshot" >&2; false; } + mount /dev/$lxc_vg/${lxc_lv_prefix}${lxc_new}_snapshot ${rootfs}_snapshot || { echo "$program: failed to mount new rootfs_snapshot" >&2; false; } #create a new lv lvcreate -L $lxc_size $lxc_vg -n ${lxc_lv_prefix}$lxc_new echo "lxc.rootfs = /dev/$lxc_vg/${lxc_lv_prefix}$lxc_new" >> $lxc_path/$lxc_new/config # and mount it so we can tweak it mkdir -p $rootfs mkfs -t $fstype /dev/$lxc_vg/${lxc_lv_prefix}$lxc_new - mount /dev/$lxc_vg/${lxc_lv_prefix}$lxc_new $rootfs || { echo "$(basename $0): failed to mount new rootfs" >&2; false; } + mount /dev/$lxc_vg/${lxc_lv_prefix}$lxc_new $rootfs || { echo "$program: failed to mount new rootfs" >&2; false; } mounted=1 - rsync -ax ${rootfs}_snapshot/ ${rootfs}/ || { echo "$(basename $0): copying data to new lv failed" >&2; false; } + rsync -ax ${rootfs}_snapshot/ ${rootfs}/ || { echo "$program: copying data to new lv failed" >&2; false; } umount ${rootfs}_snapshot rmdir ${rootfs}_snapshot lvremove -f $lxc_vg/${lxc_lv_prefix}${lxc_new}_snapshot @@ -234,17 +241,17 @@ if [ -b $oldroot ]; then echo "lxc.rootfs = /dev/$lxc_vg/${lxc_lv_prefix}$lxc_new" >> $lxc_path/$lxc_new/config # and mount it so we can tweak it mkdir -p $rootfs - mount /dev/$lxc_vg/${lxc_lv_prefix}$lxc_new $rootfs || { echo "$(basename $0): failed to mount new rootfs" >&2; false; } + mount /dev/$lxc_vg/${lxc_lv_prefix}$lxc_new $rootfs || { echo "$program: failed to mount new rootfs" >&2; false; } mounted=1 fi elif which btrfs >/dev/null 2>&1 && btrfs subvolume list $oldroot >/dev/null 2>&1; then # if oldroot is a btrfs subvolume, assume they want a snapshot - btrfs subvolume snapshot "$oldroot" "$rootfs" 2>&1 || { echo "$(basename $0): btrfs snapshot failed" >&2; false; } + btrfs subvolume snapshot "$oldroot" "$rootfs" 2>&1 || { echo "$program: btrfs snapshot failed" >&2; false; } echo "lxc.rootfs = $rootfs" >> "$lxc_path/$lxc_new/config" else if [ $snapshot = "yes" ]; then - echo "$(basename $0): cannot snapshot a directory" >&2 + echo "$program: cannot snapshot a directory" >&2 cleanup fi if [ $container_running = "True" ]; then @@ -272,7 +279,7 @@ c=$lxc_path/$lxc_new/config mv ${c} ${c}.old ( while read line; do - if [ "${line:0:18}" = "lxc.network.hwaddr" ]; then + if echo $line | grep -q -w '^lxc.network.hwaddr'; then echo "lxc.network.hwaddr= 00:16:3e:$(openssl rand -hex 3| sed 's/\(..\)/\1:/g; s/.$//')" else echo "$line" commit d348bb0b89dd7f448197629748363ae13911ccee Author: Natanael Copa <nc...@alpinelinux.org> Date: Wed Nov 14 11:24:26 2012 +0100 lxc-checkconfig: use posix shell instead of bash Signed-off-by: Natanael Copa <nc...@alpinelinux.org> diff --git a/src/lxc/lxc-checkconfig.in b/src/lxc/lxc-checkconfig.in index 8c2b5e5..ce325cd 100644 --- a/src/lxc/lxc-checkconfig.in +++ b/src/lxc/lxc-checkconfig.in @@ -1,4 +1,4 @@ -#!/bin/bash +#!/bin/sh # Allow environment variables to override grep and config : ${CONFIG:=/proc/config.gz} @@ -96,11 +96,11 @@ else KVER_MINOR=$($GREP '^# Linux' $CONFIG | \ sed -r 's/.* [0-9]\.([0-9]{1,3})\.[0-9]{1,3}.*/\1/') fi -echo -n "File capabilities: " && - ( [[ ${KVER_MAJOR} == 2 && ${KVER_MINOR} < 33 ]] && - is_enabled CONFIG_SECURITY_FILE_CAPABILITIES ) || - ( [[ ( ${KVER_MAJOR} == 2 && ${KVER_MINOR} > 32 ) || - ${KVER_MAJOR} > 2 ]] && $SETCOLOR_SUCCESS && +echo -n "File capabilities: " && \ + ( [ "${KVER_MAJOR}" = "2" ] && [ ${KVER_MINOR} -lt 33 ] && \ + is_enabled CONFIG_SECURITY_FILE_CAPABILITIES ) || \ + ( ( [ "${KVER_MAJOR}" = "2" ] && [ ${KVER_MINOR} -gt 32 ] ) || \ + [ ${KVER_MAJOR} -gt 2 ] && $SETCOLOR_SUCCESS && \ echo -e "enabled" && $SETCOLOR_NORMAL ) echo -nc ------------------------------------------------------------------------------ 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@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/lxc-devel