Commit 4785915a by Natanael Copa Committed by Stéphane Graber

lxc-clone: use posix shell instead of bash

- avoid getopt --longoptions - use 'which' instead of 'type' to detect existance of tools - use 'grep -q -w' instead of bash substring variable expansion ${line:0:18} Signed-off-by: 's avatarNatanael Copa <ncopa@alpinelinux.org> Acked-by: 's avatarStéphane Graber <stgraber@ubuntu.com>
parent 2f0e6908
#!/bin/bash #!/bin/sh
# #
# lxc: linux Container library # lxc: linux Container library
...@@ -44,8 +44,16 @@ help() { ...@@ -44,8 +44,16 @@ help() {
echo " only works for non-snapshot LVM)" >&2 echo " only works for non-snapshot LVM)" >&2
} }
shortoptions='ho:n:sL:v:p:t:' usage_err() {
longoptions='help,orig:,name:,snapshot,fssize:,vgname:,lvprefix:,fstype:' [ -n "$1" ] && echo "$1" >&2
usage
exit 1
}
optarg_check() {
[ -n "$2" ] || usage_err "option $1 requires an argument"
}
lxc_path=@LXCPATH@ lxc_path=@LXCPATH@
bindir=@BINDIR@ bindir=@BINDIR@
snapshot=no snapshot=no
...@@ -55,57 +63,55 @@ lxc_vg=lxc ...@@ -55,57 +63,55 @@ lxc_vg=lxc
lxc_lv_prefix="" lxc_lv_prefix=""
fstype=ext3 fstype=ext3
getopt=$(getopt -o $shortoptions --longoptions $longoptions -- "$@") while [ $# -gt 0 ]; do
if [ $? != 0 ]; then opt="$1"
usage shift
exit 1; case "$opt" in
fi
eval set -- "$getopt"
while true; do
case "$1" in
-h|--help) -h|--help)
help help
exit 1 exit 1
;; ;;
-s|--snapshot) -s|--snapshot)
shift
snapshot=yes snapshot=yes
snapshot_opt="-s" snapshot_opt="-s"
;; ;;
-o|--orig) -o|--orig)
shift optarg_check $opt $1
lxc_orig=$1 lxc_orig=$1
shift shift
;; ;;
-L|--fssize) -L|--fssize)
shift optarg_check $opt $1
lxc_size=$1 lxc_size=$1
shift shift
;; ;;
-v|--vgname) -v|--vgname)
shift optarg_check $opt $1
lxc_vg=$1 lxc_vg=$1
shift shift
;; ;;
-n|--name) -n|--name)
shift optarg_check $opt $1
lxc_new=$1 lxc_new=$1
shift shift
;; ;;
-p|--lvprefix) -p|--lvprefix)
shift optarg_check $opt $1
lxc_lv_prefix=$1 lxc_lv_prefix=$1
shift shift
;; ;;
--) --)
shift
break break
;; ;;
-?)
usage_err "Unknown option: '$opt'"
;;
-*)
# split opts -abc into -a -b -c
set -- $(echo "${opt#-}" | sed 's/\(.\)/ -\1/g') "$@"
;;
*) *)
usage usage_err
exit 1
;; ;;
esac esac
done done
...@@ -191,7 +197,7 @@ lxc-info -s -n $lxc_orig|grep RUNNING >/dev/null 2>&1 || container_running=False ...@@ -191,7 +197,7 @@ 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 sed -i '/lxc.rootfs/d' $lxc_path/$lxc_new/config
if [ -b $oldroot ]; then if [ -b $oldroot ]; then
type vgscan || { echo "$(basename $0): lvm is not installed" >&2; false; } which vgscan >/dev/null || { 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; } lvdisplay $oldroot > /dev/null 2>&1 || { echo "$(basename $0): non-lvm blockdev cloning is not supported" >&2; false; }
lvm=TRUE lvm=TRUE
# ok, create a snapshot of the lvm device # ok, create a snapshot of the lvm device
...@@ -204,7 +210,7 @@ if [ -b $oldroot ]; then ...@@ -204,7 +210,7 @@ if [ -b $oldroot ]; then
fi fi
newlv="${lxc_lv_prefix}${lxc_new}_snapshot" newlv="${lxc_lv_prefix}${lxc_new}_snapshot"
lvcreate -s -L $lxc_size -n $newlv $oldroot lvcreate -s -L $lxc_size -n $newlv $oldroot
type xfs_admin > /dev/null 2>&1 && { which xfs_admin > /dev/null 2>&1 && {
# change filesystem UUID if it is an xfs filesystem # change filesystem UUID if it is an xfs filesystem
xfs_admin -u /dev/$lxc_vg/$newlv && xfs_admin -U generate /dev/$lxc_vg/$newlv xfs_admin -u /dev/$lxc_vg/$newlv && xfs_admin -U generate /dev/$lxc_vg/$newlv
} }
...@@ -272,7 +278,7 @@ c=$lxc_path/$lxc_new/config ...@@ -272,7 +278,7 @@ c=$lxc_path/$lxc_new/config
mv ${c} ${c}.old mv ${c} ${c}.old
( (
while read line; do 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/.$//')" echo "lxc.network.hwaddr= 00:16:3e:$(openssl rand -hex 3| sed 's/\(..\)/\1:/g; s/.$//')"
else else
echo "$line" echo "$line"
......
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