Bash Scripts: Unterschied zwischen den Versionen
Amir (Diskussion | Beiträge) |
Keine Bearbeitungszusammenfassung |
||
(3 dazwischenliegende Versionen von einem anderen Benutzer werden nicht angezeigt) | |||
Zeile 30: | Zeile 30: | ||
<pre> | <pre> | ||
#!/bin/bash | #!/bin/bash | ||
PREPARE=; ENTER=; CLEAN=; VERBOSE=; | PREPARE=; ENTER=; CLEAN=; VERBOSE=; X=; Y=; | ||
RUN=/bin/bash | RUN="/bin/bash" | ||
CONFIG= | CONFIG= | ||
DISPLAY= | |||
function verbose { [ $VERBOSE ] && echo $@; $@ 1> /dev/null; } | function verbose { [ $VERBOSE ] && echo $@; $@ 1> /dev/null; } | ||
Zeile 38: | Zeile 39: | ||
function error { echo "error: $1" >&2; exit 1; } | function error { echo "error: $1" >&2; exit 1; } | ||
function usage { | function usage { | ||
[ "$1" ] && error "$1" | |||
cat <<EOF | cat <<EOF | ||
Usage: chrtsetup [ | Usage: chrtsetup [OPTIONS] [CHRTCONF] | ||
-v, --verbose print actions | -v, --verbose print actions | ||
-p, --prepare perform mount sequence only | -p, --prepare perform mount sequence only | ||
-c, --clean perform umount sequence only | -c, --clean perform umount sequence only | ||
-e, --enter enter chroot only | -e, --enter enter chroot only | ||
-x DISPLAY configures the xsession defined by DISPLAY | |||
to allow all connections from localhost | |||
(xhost +localhost) and sets the DISPLAY | |||
variable inside the chroot accordingly to | |||
support connecting X11 clients from inside the | |||
chroot to your X11 server. | |||
-y DISPLAY does the same like -x but additionally sets the | |||
window manager name of the xsession to a | |||
non-reparenting window manager (wmname LG3D) to | |||
avoid problems with some jdk versions running | |||
with awesome wm. (shouldn't interfere with | |||
conventional use-cases :) | |||
-r, --run=COMMAND run specified COMMAND after entering the chroot | -r, --run=COMMAND run specified COMMAND after entering the chroot | ||
CHRTCONF is the name of the chroot configuration stored in /etc/chrtsetup/. | CHRTCONF is the name of the chroot configuration stored in /etc/chrtsetup/. | ||
If neither -p, -c nor -e is set | If neither -p, -c nor -e is set -pce is assumed. | ||
Examples: | Examples: | ||
chrtsetup ubuntu | chrtsetup ubuntu # executed setup defined in /etc/chrtsetup/ubuntu, enter chroot and clean up after | ||
chrtsetup -c ubuntu | chrtsetup -c ubuntu # undo mounts performed to setup the chroot | ||
chrtsetup -pe ubuntu | chrtsetup -pe ubuntu # mount, enter but do not clean up after | ||
chrtsetup -x localhost:0.0 --run="xterm" ubuntu # configure the xsession and start xterm inside the chroot | |||
EOF | EOF | ||
Zeile 59: | Zeile 74: | ||
function chrtify { cmd=$1 doChroot=$2 | function chrtify { cmd=$1 doChroot=$2 | ||
[ "$doChroot" == "true" ] | if [ "$doChroot" == "true" ]; then | ||
echo "chroot -- $CHROOT $cmd" | |||
else | |||
echo $cmd | |||
fi | |||
} | } | ||
Zeile 92: | Zeile 109: | ||
done | done | ||
} | } | ||
function setupX { | |||
RUN="DISPLAY=$1 $RUN" | |||
export DISPLAY=$1 | |||
xhost +localhost | |||
[ $Y ] && wmname LG3D | |||
} | |||
### main | |||
[ $# -eq 0 ] && usage | [ $# -eq 0 ] && usage | ||
eval set -- "`getopt -o vpecr --long verbose,prepare,enter,clean,run -n 'chrtsetup' -- \"$@\"`" | eval set -- "`getopt -o vpecr:x:y: --long verbose,prepare,enter,clean,run: -n 'chrtsetup' -- \"$@\"`" | ||
while true ; do | while true ; do | ||
case "$1" in | case "$1" in | ||
Zeile 103: | Zeile 129: | ||
-c|--clean) CLEAN=0 ; shift ;; | -c|--clean) CLEAN=0 ; shift ;; | ||
-r|--run) RUN="$2" ; shift 2;; | -r|--run) RUN="$2" ; shift 2;; | ||
-x|--x) X=0; DISPLAY="$2" ; shift 2;; | |||
-y|--y) Y=0; DISPLAY="$2" ; shift 2;; | |||
--) shift ; break ;; | --) shift ; break ;; | ||
esac | esac | ||
Zeile 108: | Zeile 136: | ||
CONFIG=$1 | CONFIG=$1 | ||
[ -z $CONFIG ] && usage | [ -z $CONFIG ] && usage "CONFIG is mandatory" | ||
[ ! "$CLEAN" -a ! "$PREPARE" -a ! "$ENTER" ] && CLEAN=set PREPARE=set ENTER=set | [ ! "$CLEAN" -a ! "$PREPARE" -a ! "$ENTER" ] && CLEAN=set PREPARE=set ENTER=set | ||
source "/etc/chrtsetup/$CONFIG" | source "/etc/chrtsetup/$CONFIG" | ||
[ "$X" -o "$Y" ] && setupX $DISPLAY | |||
[ $PREPARE ] && prepare | [ $PREPARE ] && prepare | ||
[ $ENTER ] && chroot "$CHROOT" "$RUN" | [ $ENTER ] && chroot "$CHROOT" bash -c "$RUN" | ||
[ $CLEAN ] && clean | [ $CLEAN ] && clean | ||
</pre> | </pre> | ||
Zeile 168: | Zeile 196: | ||
[[Kategorie:CLI]] | [[Kategorie:CLI]] | ||
[[Kategorie:Howto]] |