You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

169 lines
3.2 KiB

#!/usr/bin/env bash
set -eu
#set -x
DISK_NAME="DebianBuster-amd64.disk"
FOR_ROOT=0
USE_SUDO=0
UMOUNT=0
synopsis() {
cat << EOF
Usage : `basename $0` [-h] [-r] [-i user] [-s] [-u]
EOF
}
help() {
cat << EOF
`basename $0` exécute VDN dans un environnement proot en utilisant le disque :
$DISK
`synopsis`
-h : affiche cette aide.
-u : démontages seulement.
EOF
}
usage() {
synopsis
exit 1
}
args() {
local opt
while getopts "hrsi:u" opt; do
case $opt in
h) help; exit 0;;
u) UMOUNT=1;;
?) usage;;
esac
done
shift $(($OPTIND - 1))
[ $# -ne 0 ] && usage || :
}
vdnMountProotForRoot() {
key="$(cat $TMPDIR/vdn-key-$USER)"
disp=$(cat $TMPDIR/vdn-display-$USER)
d=$TMPDIR/vdn-chroot-$USER;
if [ $UMOUNT = 0 ]; then
mount | grep -q $d || mount -o loop,offset=$((2048*512)) $DISK $d
for i in /dev /dev/pts /sys /proc /tmp; do
mount | grep -q $d$i || { mount --bind $i $d$i; }
done
mount --bind $VDN_PATH $d/home/test/vdn
#mount | grep $d
chroot $d su - -s /bin/bash -c "export DISPLAY=":"$disp; \
unset XAUTHORITY; rm -rf /home/test/.config/xfce4-session; \
rm -rf /home/test/.config/xfce4/terminal/; \
xauth add \$DISPLAY . $key; \
export NO_AT_BRIDGE=1; \
xfce4-terminal --disable-server # 2> /dev/null;" test
sleep 1
fi
for i in /tmp /proc /sys /dev/pts /dev; do
mount | grep -q $d$i && { umount $d$i || { echo "umount lazy : $d$i"; umount -l $d$i; } ; } || :
done
mount | grep -q $d/home/test/vdn && { umount $d/home/test/vdn || umount -l $d/home/test/vdn; } || :
mount | grep -q $d && { umount $d || umount -l $d; } || :
m="$(mount | grep $d)"
echo $m
}
vdnUmount() {
set +e
for i in $(mount | grep $TMPDIR/vdn-proot-$USER | cut -d ' ' -f 3 | tac); do
fusermount -u $i
done
set -e
}
vdnMount() {
[ ! -e $DISK ] && error "$DISK not found !" || :
if [ ! -w $DISK ]; then
error "$DISK doit être accessible en écriture !"
fi
# get display
if [ -z $DISPLAY ]; then
error "DISPLAY not set !"
fi
(rm -f $TMPDIR/vdn-display-$USER; umask 077 ; touch $TMPDIR/vdn-display-$USER)
DISP=$(echo $DISPLAY | cut -d ':' -f 2 | cut -d '.' -f 1)
echo $DISPLAY | cut -d ':' -f 2 >> $TMPDIR/vdn-display-$USER
# get xauth key
key=$(xauth list| grep $(uname -n) | grep $DISP | tail -n 1 | tr -s ' ' | cut -d ' ' -f 3)
[ -n "$key" ] || error "Xauth key is empty !"
echo $key | egrep -q '^[[:xdigit:]]+$' || error "Bad xauth key ($key) !"
(rm -f $TMPDIR/vdn-key-$USER; umask 077 ; touch $TMPDIR/vdn-key-$USER)
echo $key >> $TMPDIR/vdn-key-$USER
# mount (fuse)
# 1 : offset (2048*512)
d=$TMPDIR/vdn-proot-$USER;
[ ! -d $d/offset ] && mkdir -p $d/offset || :
$VDN_PATH/bin/bbfs $VDN_PATH/files $d/offset
# 2 : mount part
[ ! -d $d/part ] && mkdir -p $d/part1 || :
$VDN_PATH/bin/ext4fuse $d/offset/$DISK_NAME $d/part1
# 3 : proot
# -b /etc/passwd -b /etc/group
proot -b /proc -b /dev -b /sys -b /dev/pts -b /home -b /tmp -b /etc \
-r $d/part1 /bin/bash
echo "Done"
}
# Programme principal
VDN_PATH=$(readlink -f $(dirname $0)/..); . $VDN_PATH/bin/functions.sh
DISK=$VDN_PATH/files/$DISK_NAME
args "$@"
set -x
if [ $UMOUNT = 0 ]; then
vdnUmount
trap vdnUmount 0
vdnMount
else
vdnUmount
fi