Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Various fixes, add usage and basic option parsing #1

Merged
merged 1 commit into from Jul 9, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
@@ -1,2 +1,4 @@
# simple-jails
A simple jails manager based on [FreeBSD Jails the hard way](https://clinta.github.io/freebsd-jails-the-hard-way/)

Currently only supports _thin_ jails.
59 changes: 51 additions & 8 deletions simple-jails.sh
Expand Up @@ -32,7 +32,7 @@ EOF
sjail_fetch(){
version="$1"
echo "Setting up base for ${version}"
source /etc/simple-jails.conf
. /etc/simple-jails.conf

if [ -e ${zfs_jail_mount}/templates/base-${version} ]; then
echo "Error: $version already fetched" 1>&2
Expand All @@ -55,21 +55,22 @@ sjail_fetch(){
done

for i in $to_copy; do
cp ${i} "${zfs_jail_mount}/templates/base-${version}/etc/${i}"
cp ${i} "${zfs_jail_mount}/templates/base-${version}${i}"
done
}

sjail_update(){
version="$1"
echo "Updating ${version}"
source /etc/simple-jails.conf
. /etc/simple-jails.conf
env UNAME_r=${version} freebsd-update -b ${zfs_jail_mount}/templates/base-${version} fetch install
env UNAME_r=${version} freebsd-update -b ${zfs_jail_mount}/templates/base-${version} IDS
}

sjail_set_skel(){
version="$1"
echo "Setting up skeleton for ${version}"
source /etc/simple-jails.conf
. /etc/simple-jails.conf
if [ -e ${zfs_jail_mount}/templates/skeleton-${version} ]; then
echo "Warning: skeleton for $version already created" 1>&2
return 0
Expand All @@ -81,10 +82,12 @@ sjail_set_skel(){
mkdir -p "${zfs_jail_mount}/templates/skeleton-${version}/${i}"
done

chflags noschg "${zfs_jail_mount}/templates/base-${version}/var/empty"
to_move_dirs="etc usr/local tmp var root"
for i in $to_move_dirs; do
mv "${zfs_jail_mount}/templates/base-${version}/${i}" "${zfs_jail_mount}/templates/skeleton-${version}/${i}"
done
chflags schg "${zfs_jail_mount}/templates/skeleton-${version}/var/empty"

current_dir=$(pwd)
cd ${zfs_jail_mount}/templates/base-${version}
Expand All @@ -108,7 +111,7 @@ sjail_create_thinjail() {
version="$1"
jail_name="$2"
echo "Setting up jail: $jail_name"
source /etc/simple-jails.conf
. /etc/simple-jails.conf
if [ ! -e ${zfs_jail_mount}/thinjails ]; then
zfs create ${zfs_data_set}/thinjails
fi
Expand All @@ -119,11 +122,51 @@ sjail_create_thinjail() {
fi

zfs clone ${zfs_data_set}/templates/skeleton-${version}@skeleton ${zfs_data_set}/thinjails/${jail_name}
echo hostname=\"${jail_name}\" > ${zfs_jail_mount}thinjails/${jail_name}/etc/rc.conf
echo hostname=\"${jail_name}\" > ${zfs_jail_mount}/thinjails/${jail_name}/etc/rc.conf

mkdir -p ${zfs_jail_mount}/${jail_name}
cat <<EOF > ${zfs_jail_mount}/${jail_name}.fstab
${zfs_jail_mount}/templates/${version}-RELEASE ${zfs_jail_mount}/${jail_name}/ nullfs ro 0 0
${zfs_jail_mount}/thinjails/${jail_name} ${zfs_jail_mount}/${jail_name}/skeleton nullfs rw 0 0
${zfs_jail_mount}/templates/base-${version} ${zfs_jail_mount}/${jail_name}/ nullfs ro 0 0
${zfs_jail_mount}/thinjails/${jail_name} ${zfs_jail_mount}/${jail_name}/skeleton nullfs rw 0 0
EOF
}


usage() {
cat <<EOF
Usage: `basename $0` COMMAND [args]

Commands:
init Install initial config file (/etc/simple-jails.conf)
fetch VERSION Fetch and create base template for version VERSION
(e.g. 11.2-RELEASE)
update VERSION Apply freebsd-update on base template with version
VERSION. Can be done anytime.
skel VERSION Create skeleton from base template version VERSION
create VERSION NAME Create thin jail NAME from base template with version
VERSION. Update /etc/jail.conf manually.
EOF
exit 0
}

command=$1; shift
case $command in
"init")
sjail_init
;;
"fetch")
sjail_fetch "$@"
;;
"update")
sjail_update "$@"
;;
"skel"*)
sjail_set_skel "$@"
;;
"create")
sjail_create_thinjail "$@"
;;
*)
usage
;;
esac