Bash Scripts: Unterschied zwischen den Versionen
aus Metalab Wiki, dem offenen Zentrum für meta-disziplinäre Magier und technisch-kreative Enthusiasten.
Zur Navigation springenZur Suche springenAmir (Diskussion | Beiträge) (Die Seite wurde neu angelegt: „==chrtsetup== set up mounts and enter a chroot chrtsetup configuration file to set up a unionfs of a disk image and a filesystem tree: <pre> #!bin/bash # only…“) |
Amir (Diskussion | Beiträge) |
||
Zeile 119: | Zeile 119: | ||
[ $CLEAN ] && clean | [ $CLEAN ] && clean | ||
</pre> | </pre> | ||
+ | |||
+ | [[Kategorie:CLI]] |
Version vom 5. August 2010, 19:20 Uhr
chrtsetup
set up mounts and enter a chroot
chrtsetup configuration file to set up a unionfs of a disk image and a filesystem tree:
#!bin/bash # only $CHROOT and $MOUNTS are relevant to chrtsetup ROOT=/home/user/ubuntu_unionfs IMG=$ROOT/ext3_1GB.dd IMG_MNT=$ROOT/diskimage SHARED=$ROOT/shared FS=$ROOT/chroot CHROOT=$ROOT/chroot #from to fstype mount-options do-chroot MOUNTS=`echo -e "\ /dev $CHROOT/dev none bind false \n\ $ROOT/shared $CHROOT/root/shared none bind false \n\ $IMG $IMG_MNT ext3 loop false \n\ unionfs $CHROOT unionfs dirs=$IMAGE_MNT=rw:$FS=ro false \n\ none /proc proc defaults true \n\ none /sys sysfs defaults true \n\ none /dev/pts devpts defaults true \n\
the script:
#!/bin/bash PREPARE=; ENTER=; CLEAN=; VERBOSE=; RUN=/bin/bash CONFIG= function verbose { [ $VERBOSE ] && echo $@; $@ 1> /dev/null; } function warn { [ $VERBOSE ] && echo "warn: $1" >&2; } function error { echo "error: $1" >&2; exit 1; } function usage { cat <<EOF Usage: chrtsetup [OPTION] [CHRTCONF] -v, --verbose print actions -p, --prepare perform mount sequence only -c, --clean perform umount sequence only -e, --enter enter chroot only -r, --run=COMMAND run specified COMMAND after entering the chroot CHRTCONF is the name of the chroot configuration stored in /etc/chrtsetup/. If neither -p, -c nor -e is set all actions are enabled. Examples: chrtsetup ubuntu # executed setup defined in /etc/chrtsetup/ubuntu, enter chroot and clean up after chrtsetup -c ubuntu # undo mounts performed to setup the chroot chrtsetup -pe ubuntu # mount, enter but do not clean up after EOF exit 2 } function chrtify { cmd=$1 doChroot=$2 if [ "$doChroot" == "true" ]; then echo "chroot -- $CHROOT $cmd" else echo $cmd fi } function doUmount { from=$1 to=$2 fstype=$3 opts=$4 doChroot=$5 umount=`chrtify 'umount' $doChroot` verbose $umount -t $fstype $to || error "umount failed: $@" } function doMount { from=$1 to=$2 fstype=$3 opts=$4 doChroot=$5 mount=`chrtify 'mount' $doChroot` verbose $mount -t $fstype -o $opts $from $to || error "mount failed: $@" } function isMounted { from=$1 to=$2 fstype=$3 opts=$4 doChroot=$5 grepMounts=`chrtify "grep -qF /etc/mtab -e" $doChroot` $grepMounts "$from $to $fstype" } function clean { echo "$MOUNTS" | tac | while read mnt; do isMounted $mnt && doUmount $mnt \ || warn "$mnt not mounted" >&2 done } function prepare { echo "$MOUNTS" | while read mnt; do isMounted $mnt || doMount $mnt \ && warn "$mnt already mounted" >&2 done } [ $# -eq 0 ] && usage eval set -- "`getopt -o vpecr --long verbose,prepare,enter,clean,run -n 'chrtsetup' -- \"$@\"`" while true ; do case "$1" in -v|--verbose) VERBOSE=0 ; shift ;; -p|--prepare) PREPARE=0 ; shift ;; -e|--enter) ENTER=0 ; shift ;; -c|--clean) CLEAN=0 ; shift ;; -r|--run) RUN="$2" ; shift 2;; --) shift ; break ;; esac done CONFIG=$1 [ -z $CONFIG ] && usage [ ! "$CLEAN" -a ! "$PREPARE" -a ! "$ENTER" ] && CLEAN=set PREPARE=set ENTER=set source "/etc/chrtsetup/$CONFIG" [ $PREPARE ] && prepare [ $ENTER ] && chroot "$CHROOT" "$RUN" [ $CLEAN ] && clean