Skip to content

Instantly share code, notes, and snippets.

@siegfried
Last active May 28, 2021 14:36
Show Gist options
  • Save siegfried/907904752b1b5db760782f476f44fca4 to your computer and use it in GitHub Desktop.
Save siegfried/907904752b1b5db760782f476f44fca4 to your computer and use it in GitHub Desktop.
A shell script to create chroot jail
#!/bin/sh
set -e
arch=`uname -m`
release=`uname -r`
show_help() {
cat << EOF
Usage: ${0##*/} [-h] [-m ARCH] [-r RELEASE] [-i IMAGE] [-d DIR] PATH
Create a chroot jail at PATH with the file sets downloaded from
/etc/installurl.
-h display this help and exit.
-m ARCH choose the machine hardware name (defaults to
$arch).
-r RELEASE choose the OS release (defaults to $release).
-i IMAGE create jail from an ISO 9660 image containing
file sets.
-d DIR create jail from a directory containing file
sets.
EOF
}
OPTIND=1
while getopts "hm:r:i:d:" opt; do
case $opt in
h)
show_help
exit 0
;;
m)
arch=$OPTARG
;;
r)
release=$OPTARG
;;
i)
image=$OPTARG
(! [ -f "$image" ]) && (show_help >&2) && exit 1
;;
d)
file_dir=$OPTARG
(! [ -d "$file_dir" ]) && (show_help >&2) && exit 1
;;
'?')
show_help >&2
exit 1
;;
:)
show_help >&2
exit 1
;;
esac
done
shift $((OPTIND-1))
short_release=`echo $release | sed "s/\.//g"`
[ -z "$1" ] && (show_help >&2) && exit 1
cleanup_iso() {
umount "$1"
vnconfig -u "$2"
rm -rf "$1"
}
mount_iso() {
vnd_dev="$(/sbin/vnconfig -l | awk -F ':' '/not in use/ { print $1 ; exit }')"
[ -z "$vnd_dev" ] && (echo "No vnd device available" >&2) && exit 1
vnconfig "$vnd_dev" "$1"
echo "Mount $1 onto $2"
mount -t cd9660 "/dev/${vnd_dev}c" "$2"
trap "cleanup_iso $2 $vnd_dev" EXIT
}
download() {
cd "$1"
installurl=`cat /etc/installurl`
home="${installurl}/${release}/${arch}"
ftp "${home}/SHA256.sig"
for fileset in base comp game man xbase xfont xserv xshare
do
filename="${fileset}${short_release}.tgz"
ftp "${home}/${filename}"
signify -Cp "/etc/signify/openbsd-${short_release}-base.pub" -x SHA256.sig "$filename"
done
cd - 2>&1 >/dev/null
}
if [ -f "$image" ]; then
dir=`mktemp -p /tmp -d jail.XXXXXXXXXX` || exit 1
mount_iso "$image" "$dir"
files="${dir}/${release}/${arch}/*${short_release}.tgz"
elif [ -d "$file_dir" ]; then
echo "Use file sets in ${file_dir} directly"
files="${file_dir}/*${short_release}.tgz"
else
file_dir=`mktemp -p /tmp -d jail.XXXXXXXXXX` || exit 1
trap "rm -rf $file_dir" EXIT
echo "Download file sets to ${file_dir}"
download "$file_dir"
files="${file_dir}/*${short_release}.tgz"
fi
jail=$1
mkdir $jail
for file in $files
do
echo "Extract ${file} to ${jail}"
tar -C $jail -zxphf $file
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment