ns-prepare (1842B)
1 #!/bin/sh 2 3 doforce=0 4 doforceonboot=0 5 [ "$1" = "-f" ] && doforce=1 && shift 1 6 [ "$1" = "-b" ] && doforceonboot=1 && shift 1 7 8 if [ $# -lt 1 ]; 9 then 10 printf "usage: %s [-f] ns\n" "$(basename $0)" >&2 11 exit 1 12 fi 13 14 nsroot="$(ns-root "$1")" 15 [ $? -gt 0 ] && exit $? 16 17 shift 1 18 19 [ -e "$nsroot/.ns/prepared" \ 20 -a $doforce -eq 0 \ 21 -a $doforceonboot -eq 0 ] && exit 0 22 23 if [ $(id -u) -ne 0 ]; 24 then 25 printf "ns-prepare needs to be run with root rights.\n" 2>&1 26 exit 1 27 fi 28 29 MOUNTFS="" 30 UMOUNTFS="" 31 TOUCHFILES="" 32 COPYFILES="" 33 LINKFILES="" 34 ONBOOT=0 35 36 [ -e "$nsroot/.ns/rc.conf" ] && . "$nsroot/.ns/rc.conf" 37 [ $ONBOOT -eq 0 -a $doforceonboot -gt 0 ] && exit 0 38 39 if [ ! -d "$nsroot" ]; 40 then 41 printf "$nsroot does not seem to be a valid directory.\n" >&2 42 exit 1 43 fi 44 45 if [ ! -d "$nsroot/.ns" ]; 46 then 47 mkdir -p "$nsroot/.ns" 48 cat > "$nsroot/.ns/rc.conf" <<!__EOF__ 49 MOUNTFS="" 50 UMOUNTFS="" 51 TOUCHFILES="" 52 COPYFILES="" 53 LINKFILES="" 54 !__EOF__ 55 fi 56 57 filesystems="$MOUNTFS proc sys tmp dev dev/pts mnt home run devel nix usr/share/terminfo" 58 for m in $filesystems; 59 do 60 mkdir -p "$nsroot/$m" 61 if test -d "/$m"; then 62 mountpoint -q "$nsroot/$m" || mount -o rbind "/$m" "$nsroot/$m" 63 fi 64 done 65 66 #[ -d "$nsroot/mnt/root" ] || mkdir -p "$nsroot/mnt/root" 67 #mountpoint -q "$nsroot/mnt/root" || mount -o rbind / "$nsroot/mnt/root" 68 69 tfs="etc/mtab $TOUCHFILES"; 70 for f in $tfs; 71 do 72 touch "$nsroot/$f" 73 done 74 75 cfs="bin/nssh bin/capchroot $COPYFILES" 76 for f in $cfs; 77 do 78 [ -e "/$f" ] && cp -f "/$f" "$nsroot/$f" 79 done 80 81 lnfs="etc/resolv.conf etc/hostname etc/localtime etc/capchroot.allow etc/hosts etc/locale.gen $LINKFILES" 82 for f in $lnfs; 83 do 84 [ -e "/$f" ] && ln -f "/$f" "$nsroot/$f" 85 done 86 87 [ -x /bin/setcap ] && setcap cap_sys_chroot=ep "$nsroot/bin/capchroot" 88 89 touch "$nsroot/.ns/prepared" 90 [ $ONBOOT -gt 0 ] && touch "$nsroot/.ns/preparedonboot" 91 printf "$nsroot has been prepared.\n" 92 93 exit 0 94