From 361058999baf2b533d143dc34f48890a41ac8377 Mon Sep 17 00:00:00 2001 From: Thomas von Dein Date: Sun, 29 Nov 2020 16:22:36 +0100 Subject: [PATCH] switched half of the functions to getopt() work in progress!!! --- jaildk | 213 +++++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 139 insertions(+), 74 deletions(-) diff --git a/jaildk b/jaildk index 971bedf..72f66a6 100644 --- a/jaildk +++ b/jaildk @@ -11,14 +11,14 @@ This is jaildk version $version, a jail management toolkit. Usage: $0 ${beg}Building Jails:${end} -base - build a new base -build - install a build chroot of a jail -create - create a new jail from a template -clone - clone an existing jail or jail version -fetch - fetch current port collection +base -b [-w] - build a new base +build [-b ] [-v ] - install a build chroot of a jail +create - create a new jail from a template +clone -s -d [-o ] [-n ] - clone an existing jail or jail version +fetch - fetch current port collection ${beg}Installing Jails:${end} -install - install a jail (prepare mounts, devfs etc) +install [-r function] - install a jail (prepare mounts, devfs etc) uninstall - uninstall a jail remove - remove a jail or a jail version reinstall - stop, remove, install and start a jail @@ -31,12 +31,12 @@ status - display a jail's status rc - execute an rc-script inside a jail ${beg}Managing Jails:${end} -login - login into a jail (also available as separate command) -blogin - chroot into a build jail (dito) +login - login into a jail (also available as separate command) +blogin - chroot into a build jail (dito) ${beg}Transferring Jails:${end} -freeze - freeze (build an image of) a jail -thaw - thaw (install) an image of a jail somewhere else +freeze [-a -b -v ] - freeze (build an image of) a jail +thaw - thaw (install) an image of a jail Run the without arguments to get usage help about the command. @@ -101,28 +101,44 @@ die_if_not_exist() { fi } +usage_build() { + die "Usage: $0 build [-b ] [-v ]" +} + jaildk_build() { jail=$1 mode=$2 - base=$3 - version=$4 + shift + shift + + base='' + version='' - load-jail-config $jail + while getopts "b:v:" arg; do + case $arg in + b) base=1;; + v) version=${OPTARG};; + *) usage_build;; + esac + done if test -z "$mode"; then - echo "Usage: $0 build [] []" - exit 1 + usage_build fi + die_if_not_exist $jail $version + + load-jail-config $jail + if test -n "$buildbase"; then base="$buildbase" elif test -z "$base"; then - # not configured, use default: latest + # nothing configured, use default: latest base=`ls $j/base | tail -1` fi # install the jail to build/ - jaildk_install $jail all $mode rw $base $version + jaildk_install $jail $mode -r all -w -b $base -v $version case $mode in start) @@ -342,46 +358,55 @@ jaildk_rc_mount() { done } -jaildk_install_usage() { - err "Usage: $0 install [[] ]" - err "If is 'all' every script will be executed in rc-order." - err "If is not specified, just execute all scripts with ." - err "Available rc.d-scripts: $RCSCRIPTS_START" - exit 1 +usage_install() { + die "Usage: $0 install [] [-r rc-function] +Install according to its config. Options: + Mode can either be start, stop or status. default: start +-r Only execute function with parameter. default: all. + +Available rc.d-scripts: $RCSCRIPTS_START" } jaildk_install() { jail=$1 - rcd=$2 - mode=$3 + mode=$2 + shift + shift + rcd='' - # used by jaildk_build() only - rw=$4 - base=$5 - version=$6 + # options -b -w -v are undocumented, used by jaildk_build() only + rw='' + base='' + version='' + + while getopts "r:wb:v:" arg; do + case $arg in + w) rw=1;; + b) base=${OPTARG};; + v) version=${OPTARG};; + r) rcd=${OPTARG};; + *) freeze_usage;; + esac + done if test -z "$jail"; then - jaildk_install_usage - fi - - if test -z "$rcd"; then - # default just install all - mode=start - rcd=all - else - case $rcd in - start|stop|restart|status) - # shift args - mode=$rcd - rcd=all - ;; - esac + usage_install fi if test -z "$mode"; then - jaildk_install_usage + mode=start fi + if test -z "$rcd"; then + # default just install everything + rcd=all + fi + + case $mode in + start|stop|restart|status) :;; + *) usage_install;; + esac + die_if_not_exist $jail if test "$rcd" = "all"; then @@ -413,7 +438,7 @@ jaildk_install() { jaildk_uninstall() { # wrapper around _install jail=$1 - rw=$2 + rw=$2 # FIXME !!!!!!!!!! getopt !!!!!!!!!! if test -z "$jail"; then err "Usage: $0 uninstall []" @@ -426,18 +451,33 @@ jaildk_uninstall() { die "Jail $jail($version) is still running, stop it before removing!" fi - jaildk_install $jail all stop $rw + jaildk_install $jail stop -r all -w +} + + +usage_base() { + die "Usage: $0 base -b [-w] +-b can be the name of a base (e.g. 12.2-RELEASE) + or a directory where it shall be created +-w Create a writable base, including compiler and other + build stuff. Use this if you want to use the ports + collection." } jaildk_base() { - base=$1 - rw=$2 + base="" + rw="" + + while getopts "b:w" arg; do + case $arg in + w) rw=1;; + b) base=${OPTARG};; + *) usage_base;; + esac + done if test -z "$base"; then - err "Usage: $0 base []" - err "If the has been set, the base will not cleaned up" - err "and will contain compilers and other build stuff. Use" - err "this if you want to use the ports collection." + usage_base fi removelist="tests @@ -565,21 +605,40 @@ clone() { fi } +usage_clone() { + die "Usage: $0 clone -s -d [-o ] [-n ] +-s Source jail to clone from +-d Destionation jail to create from source +-o Old version +-n New version + +Hints: +- if no source version has been given, tha latest version will be used. +- if no new version has been given, source version will be used. +- if source and new jail are the same, both versions must be given + and a new version of the same jail will be created (update)" +} + jaildk_clone() { - src=$1 - new=$2 - srcversion=$3 - newversion=$4 - update="" + src='' + new='' + srcversion='' + newversion='' + update='' + + while getopts "s:d:o:n:" arg; do + case $arg in + o) srcversion=${OPTARG};; + n) newversion=${OPTARG};; + s) src=${OPTARG};; + d) new=${OPTARG};; + *) usage_clone;; + esac + done + if test -z "$new"; then - echo "Usage: $0 clone [ []]" - echo "Hints:" - echo "- if no source version has been given, tha latest version will be used." - echo "- if no new version has been given, source version will be used." - echo "- if source and new jail are the same, both versions must be given" - echo " and a new version of the same jail will be created (update)" - exit 1 + usage_clone fi if test "$src" = "$new"; then @@ -661,12 +720,16 @@ jaildk_clone() { fi } +usage_create() { + die "Usage: $0 create " +} + jaildk_create() { jail=$1 src=.template if test -z "$jail"; then - die "Usage: $0 create " + usage_create fi . $j/etc/$src/jail.conf @@ -675,7 +738,7 @@ jaildk_create() { mkdir -p $j/etc/$jail - jaildk_clone $src $jail $srcversion $newversion + jaildk_clone -s $src -d $jail -o $srcversion -n $newversion } remove() { @@ -1063,7 +1126,7 @@ endif read yesno case $yesno in y|Y|yes|YES) - jaildk_base $j/base/$os + jaildk_base -b $j/base/$os ;; esac } @@ -1092,7 +1155,7 @@ jaildk_fetch_ports() { ex rm -rf $j/ports/tmp/ports* } -freeze_usage() { +usage_freeze() { echo "Usage: $0 freeze [options] Options: -v freeze of @@ -1123,12 +1186,12 @@ jaildk_freeze() { a) ADDAPPL=1;; b) ADDBASE=1;; v) VERSION=${OPTARG};; - *) freeze_usage;; + *) usage_freeze;; esac done if test -z "$jail"; then - freeze_usage + usage_freeze fi die_if_not_exist $jail "Jail to freeze" $VERSION @@ -1231,9 +1294,12 @@ thaw_tarball() { ex rm -f $srcdir/$tarball } +usage_thaw() { + die "Usage: $0 thaw " +} + jaildk_thaw() { image=$1 - J=$2 # just for testing if test -n "$J"; then j=$J @@ -1243,8 +1309,7 @@ jaildk_thaw() { version=$(echo $image | cut -d\- -f3 | cut -d\. -f1) if ! test -n "$version" -o -n "$jail"; then - err "Usage: $0 thaw " - die "Image filename pattern: --.tgz" + usage_thaw fi if test -d $j/etc/$jail/etc-$version; then