13 Commits

Author SHA1 Message Date
8e893017be bump version 2024-10-06 16:22:32 +02:00
9fcf0beb9c fix #15: do not try to execute pf if there's no ip address configured 2024-10-06 16:20:11 +02:00
a293128eea fix #26: show correct usage after clone/create 2024-10-06 16:10:28 +02:00
27aada4b8e fix #24 (part II): always respond to -h with usage message 2024-10-06 16:08:19 +02:00
db33a41983 fix #24 (part I): get rid of perl, check for external programs 2024-10-06 16:04:45 +02:00
6fad6cd2f9 Merge branch 'main' of github.com:TLINDEN/jaildk 2024-09-18 10:31:42 +02:00
cafc20e743 implement #20: added -s parameter to base command to instal scripts 2024-09-18 10:30:55 +02:00
cf812919cb fix #19: bootstrap pkg when building a new base 2024-09-18 10:24:23 +02:00
e2aa249464 fix reinstall aboriting with jail -m doesnt exist 2024-09-18 10:21:44 +02:00
T.v.Dein
4dab8e10ea Merge pull request #22 from TLINDEN/develop
Fix ipfw call
2024-09-17 14:02:40 +02:00
ad1333ebb0 fix #21: only execute ipfw stuff if there's an ipfw.conf 2024-09-17 13:55:20 +02:00
T.v.Dein
514d0adeda Merge pull request #18 from Culsu/main
fixed an issue when trying to start a build-chroot with explicit base…
2024-06-26 18:13:06 +02:00
Culsu
22e02b7ce5 fixed an issue when trying to start a build-chroot with explicit base and version, fixed an issue with optargs indices 2024-06-26 12:05:02 +02:00
2 changed files with 152 additions and 120 deletions

View File

@@ -143,7 +143,7 @@ For an overview of the provided commands, here's the usage screen:
Usage: ./jaildk <command> <command-args>
Building Jails:
base -b <name> [-w] - build a new base
base -b <name> [-w] [-s <script>] - build a new base
build <jail> -m <mode> [-b <base>] [-v <version>] - install a build chroot of a jail
create - create a new jail from a template
clone -s <src> -d <dst> [-o <v>] [-n <v>] - clone an existing jail or jail version

View File

@@ -1,6 +1,6 @@
#!/bin/sh
version=2.0.0
version=2.0.3
# this will be completed during build. Don't touch it, just execute
# make and use the resulting script!
@@ -112,7 +112,7 @@ die() {
exit 1
}
load-jail-config() {
load_jail_config() {
local jail=$1
if test -d $j/etc/$jail; then
# everything inside gets global
@@ -210,12 +210,11 @@ jaildk_build() {
jail=$1
mode=start
shift
shift
BASE=''
VERSION=''
while getopts "b:v:m:" arg; do
OPTIND=1; while getopts "b:v:m:" arg; do
case $arg in
b) BASE=${OPTARG};;
v) VERSION=${OPTARG};;
@@ -224,13 +223,13 @@ jaildk_build() {
esac
done
if test -z "$jail"; then
if test -z "$jail" -o "$jail" = "-h"; then
usage_build
fi
die_if_not_exist $jail $VERSION
load-jail-config $jail
load_jail_config $jail
if test -n "$VERSION"; then
# overridden with -v
@@ -240,13 +239,13 @@ jaildk_build() {
if test -n "$BASE"; then
# dito
base=$BASE
fi
if test -n "$buildbase"; then
base="$buildbase"
elif test -z "$base"; then
# nothing configured, use default: latest
base=`ls $j/base | tail -1`
else
if test -n "$buildbase"; then
base="$buildbase"
elif test -z "$base"; then
# nothing configured, use default: latest
base=`ls $j/base | tail -1`
fi
fi
# install the jail to build/
@@ -256,6 +255,7 @@ jaildk_build() {
start)
# make it usable
ex chroot $j/build/$jail /etc/rc.d/ldconfig onestart
ex chroot $j/build/$jail pkg-static bootstrap -f
ex mkdir -p $j/build/$jail/usr/local/db
;;
esac
@@ -338,7 +338,12 @@ rc_pf() {
conf=$j/etc/$jail/pf.conf
ruleset=$j/etc/$jail/pf-ruleset.conf
load-jail-config $jail
load_jail_config $jail
if test -z "$ip" -a -z "$ip6"; then
echo "PF not supported without configured ip address!" >&2
return
fi
# TODO:
# - put this into a separate function
@@ -520,7 +525,7 @@ rc_ports() {
rw=$5
rcscript=ports
load-jail-config $jail
load_jail_config $jail
if test -z "$ports"; then
# ports not configured, abort
@@ -563,7 +568,7 @@ rc_mount() {
rw=$5
rcscript=mount
load-jail-config $jail
load_jail_config $jail
conf=$j/etc/$jail/$rcscript.conf
@@ -698,7 +703,7 @@ jaildk_install() {
base=''
version=''
while getopts "r:b:v:wm:" arg; do
OPTIND=1; while getopts "r:b:v:wm:" arg; do
case $arg in
w) rw=1;;
b) base=${OPTARG};;
@@ -709,7 +714,7 @@ jaildk_install() {
esac
done
if test -z "$jail"; then
if test -z "$jail" -o "$jail" = "-h"; then
usage_install
fi
@@ -781,7 +786,7 @@ jaildk_uninstall() {
base=''
version=''
while getopts "wa" arg; do
OPTIND=1; while getopts "wa" arg; do
case $arg in
w) rw="-w";;
a) all=1; rw="-w";;
@@ -789,7 +794,7 @@ jaildk_uninstall() {
esac
done
if test -z "$jail"; then
if test -z "$jail" -o "$jail" = "-h"; then
usage_uninstall
fi
@@ -831,6 +836,8 @@ Build a base directory from bsd install media. Options:
build stuff. Use this if you want to use the ports
collection.
-f force mode, remove any old dist files.
-s <script> install additional scripts to /usr/bin, separate multiple
scripts with whitespace.
"
}
@@ -840,11 +847,13 @@ jaildk_base() {
base=""
force=""
rw=""
scripts=""
while getopts "b:wf" arg; do
OPTIND=1; while getopts "b:wfs:" arg; do
case $arg in
w) rw=1;;
b) base=${OPTARG};;
s) scripts="${OPTARG}";;
f) force=1;;
*) usage_base;;
esac
@@ -927,61 +936,66 @@ var/tmp"
if test -d "$basedir"; then
echo "base $basedir already exist!"
exit 1
else
ex mkdir -p $basedir
fi
if test -e /usr/freebsd-dist/MANIFEST; then
clean=''
if test -n "$force"; then
clean=1
else
echo "Found old dist files:"
ls -l /usr/freebsd-dist
echo -n "Want to remove them [nY]? "
read yesno
case $yesno in
y|Y) clean=1;;
*) clean='';;
esac
fi
ex mkdir -p $basedir
if test -n "$clean"; then
ex rm -f /usr/freebsd-dist/*
fi
fi
bsdinstall jail $basedir || exit 1
if test -z "$rw"; then
# run base
for file in $removelist; do
ex rm -rf $basedir/$file
done
if test -e /usr/freebsd-dist/MANIFEST; then
clean=''
if test -n "$force"; then
clean=1
else
# build base with ports support
ex mkdir -p $basedir/usr/ports
fi
ex mkdir $basedir/home
ex rm -rf $basedir/var/db
ex ln -s /usr/local/db $basedir/var/db
# add some symlinks from /var to /tmp to make pkg work properly
ex rm -rf $basedir/var/tmp $basedir/var/cache $basedir/var/run
ex ln -s /tmp $basedir/var/tmp
ex ln -s /tmp $basedir/var/cache
ex ln -s /tmp $basedir/var/run
if test -n "$rw"; then
echo "You have choosen to create a build base with ports support"
echo -n "Want to fetch the ports collection now [Yn]? "
echo "Found old dist files:"
ls -l /usr/freebsd-dist
echo -n "Want to remove them [nY]? "
read yesno
case $yesno in
y|Y|yes|YES)
jaildk_fetchports
;;
y|Y) clean=1;;
*) clean='';;
esac
fi
if test -n "$clean"; then
ex rm -f /usr/freebsd-dist/*
fi
fi
bsdinstall jail $basedir || exit 1
if test -z "$rw"; then
# run base
for file in $removelist; do
ex rm -rf $basedir/$file
done
else
# build base with ports support
ex mkdir -p $basedir/usr/ports
fi
ex mkdir $basedir/home
ex rm -rf $basedir/var/db
ex ln -s /usr/local/db $basedir/var/db
# add some symlinks from /var to /tmp to make pkg work properly
ex rm -rf $basedir/var/tmp $basedir/var/cache $basedir/var/run
ex ln -s /tmp $basedir/var/tmp
ex ln -s /tmp $basedir/var/cache
ex ln -s /tmp $basedir/var/run
# any scripts?
for script in $scripts; do
ex install -m 755 $script -o root -g wheel $basedir/usr/bin/$script
done
if test -n "$rw"; then
echo "You have choosen to create a build base with ports support"
echo -n "Want to fetch the ports collection now [Yn]? "
read yesno
case $yesno in
y|Y|yes|YES)
jaildk_fetchports
;;
esac
fi
}
@@ -1027,7 +1041,7 @@ Hints:
jaildk_clone() {
local src new srcversion newversion update cloneto clonefrom fs srcmount dstmount opts size perm
while getopts "s:d:o:n:" arg; do
OPTIND=1; while getopts "s:d:o:n:" arg; do
case $arg in
o) srcversion=${OPTARG};;
n) newversion=${OPTARG};;
@@ -1054,7 +1068,7 @@ jaildk_clone() {
fi
die_if_not_exist $src "Source jail"
load-jail-config $src
load_jail_config $src
if test -z "$srcversion"; then
srcversion=$version
@@ -1122,7 +1136,7 @@ jaildk_clone() {
fi
bold "To mount the build chroot of the new jail, execute:"
echo "jaildk build $new start"
echo "jaildk build $new -m start"
echo
bold "To login into the build chroot"
echo "jaildk blogin $new"
@@ -1158,7 +1172,7 @@ jaildk_create() {
src=.template
if test -z "$jail"; then
if test -z "$jail" -o "$jail" = "-h"; then
usage_create
fi
@@ -1169,8 +1183,7 @@ jaildk_create() {
mkdir -p $j/etc/$jail
jaildk_clone -s $src -d $jail -o $srcversion -n $newversion
# some perl magic to extract the hostname (if any) from /etc/jail.conf - and write it into the jails rc.conf
jailhostname=$(cat /etc/jail.conf | tr -d '\t\r\n ' | perl -ne '$_ =~ /.*'"$newjail"'(\{(?:\{.*\}|[^{])*\})|\w+/; print $1;' | grep -oE 'hostname=[^;]+' | cut -d= -f2)
jailhostname=$(cat /etc/jail.conf | grep -E "^$jail" -A50 | sed '/\}/q' | grep hostname | cut -d\" -f2)
if [ -n "$jailhostname" ]; then
echo "new name: $jailhostname"
echo "in path $j/etc/$jail/local-etc-$newversion/rc.conf"
@@ -1199,14 +1212,14 @@ jaildk_remove() {
shift
version=''
while getopts "v:" arg; do
OPTIND=1; while getopts "v:" arg; do
case $arg in
v) version=${OPTARG};;
*) usage_remove;;
esac
done
if test -z "$jail"; then
if test -z "$jail" -o "$jail" = "-h"; then
usage_remove
fi
@@ -1274,7 +1287,7 @@ jaildk_jail() {
if test "x$mode" = "xstatus"; then
(
if test -z "$jail"; then
if test -z "$jail" -o "$jail" = "-h"; then
bold "Running jails:"
lookup='*'
else
@@ -1291,7 +1304,7 @@ jaildk_jail() {
build='no'
base=''
load-jail-config $jail
load_jail_config $jail
_eip=''
for map in $maps; do
@@ -1303,8 +1316,8 @@ jaildk_jail() {
done
if jls -j $jail > /dev/null 2>&1; then
# jail is running
eval `jls -j $jail -qn | perl -n -e 'chomp; %j = map { ($a,$b) = split /=/; $a=~ s/\.//g; $a => $b } split/ /; foreach (keys %j) {print "$_=$j{$_}\n"}'`
# jail is running, get some data about jail
eval $(jls -j v6 -qn ip4.addr ip6.addr jid)
if test -n "$ip4addr"; then
ip=$ip4addr
else
@@ -1337,7 +1350,7 @@ jaildk_jail() {
if test -n "$jail"; then
jaildk_rc $jail -m status
fi
elif test -z "$jail"; then
elif test -z "$jail" -o "$jail" = "-h"; then
usage_$mode
else
bold "Jail $jail $mode:"
@@ -1384,7 +1397,7 @@ jaildk_rc() {
rcd=''
while getopts "r:m:" arg; do
OPTIND=1; while getopts "r:m:" arg; do
case $arg in
r) rcd=${OPTARG};;
m) mode=${OPTARG};;
@@ -1396,7 +1409,7 @@ jaildk_rc() {
rcd='all'
fi
if test -z "$jail" -o -z "$mode"; then
if test -z "$jail" -o "$jail" = "-h" -o -z "$mode"; then
usage_rc
fi
@@ -1456,7 +1469,7 @@ jaildk_blogin() {
jail=$1
if test -z "$jail"; then
if test -z "$jail" -o "$jail" = "-h"; then
file=`basename $0`
if test "$file" = "jaildk"; then
file="$0 blogin"
@@ -1505,7 +1518,7 @@ jaildk_login() {
me=`id -u`
jexec="jexec"
if test -z "$jail"; then
if test -z "$jail" -o "$jail" = "-h"; then
file=`basename $0`
if test "$file" = "jaildk"; then
file="$0 jlogin"
@@ -1559,7 +1572,7 @@ jaildk_reinstall() {
jail=$1
shift
while getopts "b:v:" arg; do
OPTIND=1; while getopts "b:v:" arg; do
case $arg in
b) NEWBASE=${OPTARG};;
v) NEWVERSION=${OPTARG};;
@@ -1567,7 +1580,7 @@ jaildk_reinstall() {
esac
done
if test -z "$jail"; then
if test -z "$jail" -o "$jail" = "-h"; then
usage_reinstall
fi
@@ -1583,7 +1596,7 @@ jaildk_reinstall() {
sync
if test -n "$NEWBASE" -o -n "$NEWVERSION"; then
load-jail-config $jail
load_jail_config $jail
ts=`date +%Y%m%d%H%M`
change=''
if test $NEWBASE != $base; then
@@ -1607,7 +1620,7 @@ jaildk_reinstall() {
fi
fi
jaildk_install -m $jail start
jaildk_install $jail -m start
jaildk_jail start $jail
sleep 0.2
@@ -1644,7 +1657,11 @@ jaildk_setup() {
version=`date +%Y%m%d`
for subdir in appl/default-$version/db/ports appl/default-$version/etc etc/.template/etc-$version etc/.template/local-etc-$version home/.template/root-$version log/.template-$version; do
for subdir in appl/default-$version/db/ports \
appl/default-$version/etc \
etc/.template/etc-$version \
etc/.template/local-etc-$version \
home/.template/root-$version log/.template-$version; do
ex mkdir -p $j/$subdir
done
@@ -1764,7 +1781,7 @@ jaildk_update() {
repo="https://github.com/TLINDEN/jaildk.git"
mustberoot
while getopts "f" arg; do
OPTIND=1; while getopts "f" arg; do
case $arg in
f) force=1;;
*) usage_update;;
@@ -1804,7 +1821,7 @@ Fetch current portscollection, use <version> or todays timestamp as new version"
jaildk_fetchports() {
local version=`date +%Y%m%d`
while getopts "v:" arg; do
OPTIND=1; while getopts "v:" arg; do
case $arg in
v) version=${OPTARG};;
*) usage_fetchports;;
@@ -1865,7 +1882,7 @@ jaildk_freeze() {
ADDBASE=""
ADDAPPL=""
while getopts "abv:" arg; do
OPTIND=1; while getopts "abv:" arg; do
case $arg in
a) ADDAPPL=1;;
b) ADDBASE=1;;
@@ -1874,7 +1891,7 @@ jaildk_freeze() {
esac
done
if test -z "$jail"; then
if test -z "$jail" -o "$jail" = "-h"; then
usage_freeze
fi
@@ -1891,7 +1908,7 @@ jaildk_freeze() {
esac
fi
load-jail-config $jail
load_jail_config $jail
if test -n "$VERSION"; then
version=$VERSION
@@ -2068,7 +2085,12 @@ jaildk_ipfw() {
jail=$1
while getopts "m:" arg; do
if ! test -f "$j/etc/$jail/ipfw.conf"; then
# dont do anything in non-ipf shells
return
fi
OPTIND=1; while getopts "m:" arg; do
case $arg in
m) mode=${OPTARG};;
*) usage_ipfw;;
@@ -2079,21 +2101,19 @@ jaildk_ipfw() {
usage_ipfw
fi
if test -f "$j/etc/$jail/ipfw.conf"; then
echo
bold "Managing IPFW Rules..."
case $mode in
start)
ipfw_delete $jail "y"
ipfw_add $jail
;;
stop)
ipfw_delete $jail
;;
esac
bold "... done"
echo
fi
echo
bold "Managing IPFW Rules..."
case $mode in
start)
ipfw_delete $jail "y"
ipfw_add $jail
;;
stop)
ipfw_delete $jail
;;
esac
bold "... done"
echo
}
ipfw_add() {
@@ -2102,7 +2122,7 @@ ipfw_add() {
jail=$1
# support jail variables as well
load-jail-config $jail
load_jail_config $jail
if test -z $ip; then
# Getting current jails IP..
@@ -2183,7 +2203,7 @@ jaildk_vnet() {
BRIDGE=''
while getopts "b:i:r:" arg; do
OPTIND=1; while getopts "b:i:r:" arg; do
case $arg in
b) BRIDGE=${OPTARG};;
*) usage_vnet;;
@@ -2196,7 +2216,7 @@ jaildk_vnet() {
die_if_not_exist $jail
load-jail-config $jail
load_jail_config $jail
if test -z "$ip" -a -z "$gw"; then
usage_vnet
@@ -2273,7 +2293,7 @@ delete directories. Be sure to have backups available!
jaildk_prune() {
local BASE APPL JAIL UNUSED
while getopts "baj:u" arg; do
OPTIND=1; while getopts "baj:u" arg; do
case $arg in
b) BASE=1;;
a) APPL=1;;
@@ -2321,7 +2341,7 @@ jaildk_prune() {
elif test -n "$JAIL"; then
die_if_not_exist $JAIL
load-jail-config $JAIL
load_jail_config $JAIL
if test -z "$UNUSED"; then
bold "Current Active jail version for jail $JAIL:" > /dev/stderr
@@ -2358,7 +2378,7 @@ jaildk_bootstrap() {
PORTS=''
IP=''
while getopts "i:b:v:p:a:" arg; do
OPTIND=1; while getopts "i:b:v:p:a:" arg; do
case $arg in
b) BASE=${OPTARG};;
v) VERSION=${OPTARG};;
@@ -2369,7 +2389,7 @@ jaildk_bootstrap() {
esac
done
if test -z "$jail"; then
if test -z "$jail" -o "$jail" = "-h"; then
usage_bootstrap
fi
@@ -2458,6 +2478,16 @@ mustberoot() {
fi
}
sanitycheck() {
# check if certain programs are installed
for program in cpdup; do
if ! command -v $program 2>&1 >/dev/null; then
echo "$program must be installed!" >&2
exit1
fi
done
}
##########################
#
# main()
@@ -2482,6 +2512,8 @@ if test -z "$runner"; then
usage_jaildk
fi
sanitycheck
case $runner in
start|stop|restart)
# running jails