| 1 | function jz
|
| 2 | # see https://git.io/JfHOu
|
| 3 | set local
|
| 4 | set MACH (sysctl -n hw.machine)
|
| 5 | set ARCH (sysctl -n hw.machine_arch)
|
| 6 | set DOMAIN skunkwerks.at
|
| 7 | set RELEASE 14.3-RELEASE
|
| 8 | set RELDATE 1403000
|
| 9 | set FLAVOUR $RELEASE-$MACH-$ARCH
|
| 10 | # grab the name
|
| 11 | set JAIL $argv[1]
|
| 12 | # the container
|
| 13 | set TEMPLATE /jails/templates/$FLAVOUR
|
| 14 | set INSTANCE /jails/instances/$FLAVOUR/$JAIL
|
| 15 |
|
| 16 | # find the zpool for jails and jailed datasets
|
| 17 | set ZPOOL (zfs list -Ho name /jails | cut -f 1 -d /)
|
| 18 |
|
| 19 | # create a /jailed dataset to contain optional per-jail jailable datasets
|
| 20 | # this can be located on an alternative zpool but must not be mountable
|
| 21 | zfs list -Ho name -d1 |grep /jailed\$ > /dev/null 2>&1 \
|
| 22 | || doas zfs create -o mountpoint=none -o canmount=off $ZPOOL/jailed
|
| 23 | # re-fetch after possible creation
|
| 24 | set JAILED (zfs list -Ho name -d1 | grep /jailed\$)
|
| 25 |
|
| 26 | # create mountpoint if missing
|
| 27 | zfs list -Ho name $ZPOOL/jails > /dev/null 2>&1 \
|
| 28 | || doas zfs create -o mountpoint=/jails -p $ZPOOL/jails
|
| 29 |
|
| 30 | # clone a template if missing
|
| 31 | test -d $INSTANCE \
|
| 32 | || doas zfs clone $ZPOOL$TEMPLATE@ready $ZPOOL$INSTANCE \
|
| 33 | && doas zfs snapshot $ZPOOL$INSTANCE@ready
|
| 34 |
|
| 35 | # add a softlink if missing
|
| 36 | ln -sf $INSTANCE /jails/tags/$JAIL
|
| 37 |
|
| 38 | # derive a hopefully non-clashing IP address
|
| 39 | set IPA (echo -n $JAIL | sha512 | cut -c 1,2)
|
| 40 | set IPB (echo -n $JAIL | sha512 | cut -c 3,4)
|
| 41 | set IP6 $IPA$IPB
|
| 42 | set IP (printf "%d.%d" 0x$IPA 0x$IPB)
|
| 43 | # get the zt 6plane
|
| 44 | set IFACE (ifconfig | sort -r | /usr/bin/egrep -o '^zt[a-z0-9]+' | head -1)
|
| 45 | set SUBNET (ifconfig $IFACE inet6 | rg -Po 'fc\S+::' | uniq)
|
| 46 | set ALIAS "$IFACE|$SUBNET$IP6"
|
| 47 | # display config and try to start it
|
| 48 | echo name=$JAIL ip=$IP ip6=$IP6 iface=$IFACE subnet=$SUBNET alias=$ALIAS
|
| 49 | jls -j $JAIL > /dev/null 2>&1
|
| 50 | if test $status = 0
|
| 51 | doas jexec $JAIL tmux -u new-session -DAs default
|
| 52 | else
|
| 53 | doas jail -vcmr \
|
| 54 | name=$JAIL \
|
| 55 | path=$INSTANCE \
|
| 56 | exec.created="zfs jail $JAIL $JAILED/$JAIL || true" \
|
| 57 | exec.created="test -c $INSTANCE/dev/null || mount -t devfs devfs $INSTANCE/dev" \
|
| 58 | exec.created="test -c $INSTANCE/tmp || mount -t tmpfs tmpfs $INSTANCE/tmp" \
|
| 59 | exec.start="/bin/sh /etc/rc" \
|
| 60 | exec.poststop="umount $INSTANCE/tmp" \
|
| 61 | exec.poststop="umount $INSTANCE/dev" \
|
| 62 | exec.poststop="logger -p info -t jz removed $JAIL" \
|
| 63 | host.domainname=$DOMAIN \
|
| 64 | host.hostid=0 \
|
| 65 | host.hostname=$JAIL.$DOMAIN \
|
| 66 | host.hostuuid=(uuidgen) \
|
| 67 | ip4.addr="lo1|100.64.$IP" \
|
| 68 | ip6.addr="$ALIAS" \
|
| 69 | allow.chflags \
|
| 70 | allow.mount \
|
| 71 | allow.mount.devfs \
|
| 72 | allow.mount.fdescfs \
|
| 73 | allow.mount.nolinprocfs \
|
| 74 | allow.mount.nullfs \
|
| 75 | allow.mount.procfs \
|
| 76 | allow.mount.tmpfs \
|
| 77 | allow.mount.zfs \
|
| 78 | allow.nomlock \
|
| 79 | allow.noquotas \
|
| 80 | allow.noread_msgbuf \
|
| 81 | allow.noset_hostname \
|
| 82 | allow.nosocket_af \
|
| 83 | allow.nosysvipc \
|
| 84 | allow.raw_sockets \
|
| 85 | allow.reserved_ports \
|
| 86 | allow.sysvipc=1 \
|
| 87 | children.max=0 \
|
| 88 | devfs_ruleset=4 \
|
| 89 | enforce_statfs=1 \
|
| 90 | osreldate=$RELDATE \
|
| 91 | osrelease=$RELEASE \
|
| 92 | persist \
|
| 93 | securelevel=2 \
|
| 94 | sysvmsg=disable \
|
| 95 | sysvsem=disable \
|
| 96 | command=/bin/sh -c "env -i TERM=$TERM tmux -u new-session -DAs default"
|
| 97 | end
|
| 98 | # command=/bin/sh
|
| 99 | # allow.nounprivileged_proc_debug \
|
| 100 | # allow.novmm \
|
| 101 | end
|