2020-05-24 06:49:41 +00:00
|
|
|
#!/bin/sh
|
|
|
|
|
|
|
|
# This script generates FreeBSD images for OpenNebula, being heavily inspired
|
|
|
|
# from srht's FreeBSD build image definition. It assumes running on a FreeBSD host.
|
2022-05-24 21:05:09 +00:00
|
|
|
# ZFS installation as documented by the FreeBSD project
|
|
|
|
# https://wiki.freebsd.org/RootOnZFS/GPTZFSBoot
|
2020-05-24 06:49:41 +00:00
|
|
|
|
|
|
|
set -e
|
|
|
|
set -x
|
|
|
|
|
|
|
|
# XXX: Handle command-line arguments?
|
2024-02-01 05:01:25 +00:00
|
|
|
RELEASE=14.0-RELEASE
|
2020-05-24 06:59:19 +00:00
|
|
|
ARCH=amd64
|
2022-05-25 10:09:19 +00:00
|
|
|
IMAGE_PATH_ZFS="freebsd-zfs-$RELEASE-$(date -I).img.qcow2"
|
|
|
|
IMAGE_PATH_UFS="freebsd-ufs-$RELEASE-$(date -I).img.qcow2"
|
2020-05-24 06:49:41 +00:00
|
|
|
IMAGE_SIZE=10G
|
|
|
|
|
2022-05-29 14:29:43 +00:00
|
|
|
# Comment out to simply use latest version
|
|
|
|
# Hash checking is disabled when specifying this
|
2022-06-11 20:00:10 +00:00
|
|
|
#CLOUDSETUP_VERSION=1.2
|
2022-05-29 14:29:43 +00:00
|
|
|
|
2020-05-24 06:59:19 +00:00
|
|
|
DIST_BASE="https://download.freebsd.org/ftp/releases/$ARCH/$RELEASE"
|
2022-05-24 21:05:09 +00:00
|
|
|
ZPOOL=zroot
|
2022-05-25 08:59:20 +00:00
|
|
|
ZPOOL_TMP="zinstalling"
|
2020-05-24 06:59:19 +00:00
|
|
|
|
2022-05-24 21:05:09 +00:00
|
|
|
ZFSTARGET="$(mktemp -d /var/tmp/zfsbuild.XXXXX)"
|
|
|
|
UFSTARGET="$(mktemp -d /var/tmp/ufsbuild.XXXXX)"
|
|
|
|
|
2022-05-25 10:09:19 +00:00
|
|
|
if zpool list -Ho name "$ZPOOL_TMP" 2>/dev/null; then
|
2022-05-25 08:59:20 +00:00
|
|
|
echo "The pool $ZPOOL_TMP is already imported." >&2
|
2022-05-24 21:05:09 +00:00
|
|
|
exit 1
|
|
|
|
fi
|
2020-05-24 06:49:41 +00:00
|
|
|
|
|
|
|
cleanup() {
|
2022-05-24 21:05:09 +00:00
|
|
|
sync ||:
|
|
|
|
umount "$UFSTARGET/dev" ||:
|
|
|
|
umount "$UFSTARGET/tmp" ||:
|
|
|
|
umount "$UFSTARGET/var/tmp" ||:
|
|
|
|
umount "$UFSTARGET" ||:
|
2022-05-25 10:09:19 +00:00
|
|
|
zpool export "$ZPOOL_TMP" ||:
|
2022-05-24 21:05:09 +00:00
|
|
|
mdconfig -du md0 ||:
|
|
|
|
mdconfig -du md1 ||:
|
2022-05-25 10:04:36 +00:00
|
|
|
rm -rf "$CLOUDSETUP_WORK" ||:
|
|
|
|
rmdir "$ZFSTARGET" ||:
|
|
|
|
rmdir "$UFSTARGET" ||:
|
2020-05-24 06:49:41 +00:00
|
|
|
}
|
|
|
|
trap cleanup EXIT
|
|
|
|
|
|
|
|
if [ "$(whoami)" != 'root' ]; then
|
|
|
|
echo "This script must be run as root." >&2
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2022-05-24 21:05:09 +00:00
|
|
|
if ! command -v rsync >/dev/null
|
|
|
|
then
|
|
|
|
env ASSUME_ALWAYS_YES=YES pkg install -y rsync
|
|
|
|
fi
|
|
|
|
if ! command -v qemu-img >/dev/null
|
|
|
|
then
|
|
|
|
env ASSUME_ALWAYS_YES=YES pkg install -y qemu-tools
|
|
|
|
fi
|
|
|
|
|
2022-05-27 20:13:42 +00:00
|
|
|
portsnap fetch
|
|
|
|
if [ -f /usr/ports/README ]
|
|
|
|
then
|
|
|
|
portsnap update || portsnap extract
|
|
|
|
else
|
|
|
|
portsnap extract
|
|
|
|
fi
|
2022-05-29 14:29:43 +00:00
|
|
|
|
|
|
|
if [ -n "$CLOUDSETUP_VERSION" ]
|
|
|
|
then
|
|
|
|
sed -i .bak -e '/^PORTVERSION=/ s/[0-9]*\.[0-9]*/'"$CLOUDSETUP_VERSION/" /usr/ports/sysutils/firstboot-cloudsetup/Makefile
|
|
|
|
make -C /usr/ports/sysutils/firstboot-cloudsetup makesum
|
|
|
|
fi
|
2022-05-27 20:13:42 +00:00
|
|
|
make -C /usr/ports/sysutils/firstboot-cloudsetup clean package
|
|
|
|
CLOUDSETUP_VERSION="$(fgrep VERSION /usr/ports/sysutils/firstboot-cloudsetup/Makefile | cut -f2- | tr -d \\t)"
|
|
|
|
CLOUDSETUP_PKG="/usr/ports/sysutils/firstboot-cloudsetup/work/pkg/firstboot-cloudsetup-${CLOUDSETUP_VERSION}.pkg"
|
|
|
|
tar -tzf "$CLOUDSETUP_PKG" >/dev/null # check that it's a valid tar, or we crash due to set -e
|
|
|
|
# tar -t lists the contents of a tar file, but does not extract
|
2022-05-24 21:05:09 +00:00
|
|
|
|
2022-05-29 15:17:42 +00:00
|
|
|
make -C /usr/ports/sysutils/firstboot-freebsd-update clean package
|
|
|
|
FBUPDATE_VERSION="$(fgrep VERSION /usr/ports/sysutils/firstboot-freebsd-update/Makefile | cut -f2- | tr -d \\t)"
|
|
|
|
FBUPDATE_PKG="/usr/ports/sysutils/firstboot-freebsd-update/work/pkg/firstboot-freebsd-update-${FBUPDATE_VERSION}.pkg"
|
|
|
|
tar -tzf "$FBUPDATE_PKG" >/dev/null # check that it's a valid tar, or we crash due to set -e
|
|
|
|
|
2022-05-25 10:09:19 +00:00
|
|
|
ufsdisk="$(mktemp /var/tmp/ufsdisk.XXXXX)"
|
|
|
|
truncate -s 6G "$ufsdisk"
|
|
|
|
mdconfig -a -t vnode -f "$ufsdisk" -u md1
|
2022-05-24 21:05:09 +00:00
|
|
|
gpart create -s gpt /dev/md1
|
2022-05-27 20:04:42 +00:00
|
|
|
#gpart add -t efi -l efiboot0 -s 260M md1
|
2022-05-29 14:29:01 +00:00
|
|
|
gpart add -t freebsd-boot -l gptboot -b 40 -s 512K md1
|
2022-05-24 21:05:09 +00:00
|
|
|
gpart bootcode -b /boot/pmbr -p /boot/gptboot -i 1 md1
|
|
|
|
gpart add -t freebsd-ufs -l rootfs -b 1M -s 5G md1
|
|
|
|
newfs -U /dev/md1p2
|
|
|
|
|
|
|
|
# Mount allocated image.
|
|
|
|
mount /dev/md1p2 "$UFSTARGET"
|
2022-06-12 06:37:24 +00:00
|
|
|
|
2020-05-24 06:49:41 +00:00
|
|
|
# Allocate and partition/format disk image.
|
2022-05-24 21:05:09 +00:00
|
|
|
# We use "legacy boot", aka BIOS boot
|
|
|
|
# Preferably, we'd use EFI boot here, check the FreeBSD wiki link in the header
|
|
|
|
# to see how to make that change, but make the EFI partition larger
|
2022-05-25 10:09:19 +00:00
|
|
|
zfsdisk="$(mktemp /var/tmp/zfsdisk.XXXXX)"
|
|
|
|
truncate -s 6G "$zfsdisk"
|
|
|
|
mdconfig -a -t vnode -f "$zfsdisk" -u md0
|
2020-05-24 06:49:41 +00:00
|
|
|
gpart create -s gpt /dev/md0
|
2022-05-27 20:04:42 +00:00
|
|
|
#gpart add -t efi -l efiboot0 -s 260M md1
|
|
|
|
gpart add -t freebsd-boot -l gptboot0 -b 40 -s 512K md0
|
2022-05-24 21:05:09 +00:00
|
|
|
gpart bootcode -b /boot/pmbr -p /boot/gptzfsboot -i 1 md0
|
|
|
|
gpart add -t freebsd-zfs -l zfs0 -b 1M -s 5G md0
|
2022-05-25 10:09:19 +00:00
|
|
|
zpool create -O compression=on -o ashift=12 -o "altroot=$ZFSTARGET" -m none -t "$ZPOOL_TMP" "$ZPOOL" md0p2
|
2022-05-24 21:05:09 +00:00
|
|
|
|
2022-05-25 10:09:19 +00:00
|
|
|
zfs create -o mountpoint=none "$ZPOOL_TMP/ROOT"
|
2022-05-24 21:05:09 +00:00
|
|
|
# We set zstd-19 so our image will become smaller, at the cost of a longer build time.
|
|
|
|
# At the end of the process, we disable zstd-19 again using zfs inherit compression,
|
|
|
|
# but all files already written will remain zstd-19 compressed
|
2022-05-25 10:09:19 +00:00
|
|
|
zfs create -o mountpoint=/ -o canmount=noauto "$ZPOOL_TMP/ROOT/default"
|
|
|
|
mount -t zfs "$ZPOOL_TMP/ROOT/default" "$ZFSTARGET"
|
|
|
|
zpool set "bootfs=$ZPOOL_TMP/ROOT/default" "$ZPOOL_TMP"
|
|
|
|
|
|
|
|
zfs create -o mountpoint=/tmp -o exec=on -o setuid=off "$ZPOOL_TMP/tmp"
|
|
|
|
zfs create -o canmount=off -o mountpoint=/usr "$ZPOOL_TMP/usr"
|
|
|
|
zfs create "$ZPOOL_TMP/usr/home"
|
|
|
|
zfs create -o exec=off -o setuid=off "$ZPOOL_TMP/usr/src"
|
|
|
|
zfs create -o mountpoint=/usr/ports -o setuid=off "$ZPOOL_TMP/usr/ports"
|
|
|
|
zfs create -o canmount=off -o mountpoint=/var "$ZPOOL_TMP/var"
|
|
|
|
zfs create -o exec=off -o setuid=off "$ZPOOL_TMP/var/audit"
|
|
|
|
zfs create -o exec=off -o setuid=off "$ZPOOL_TMP/var/crash"
|
|
|
|
zfs create -o exec=off -o setuid=off "$ZPOOL_TMP/var/log"
|
|
|
|
zfs create -o atime=on -o exec=off -o setuid=off "$ZPOOL_TMP/var/mail"
|
|
|
|
zfs create -o exec=on -o setuid=off "$ZPOOL_TMP/var/tmp"
|
2022-05-24 21:05:09 +00:00
|
|
|
|
|
|
|
ln -s /usr/home "$ZFSTARGET/home"
|
|
|
|
chmod 1777 "$ZFSTARGET/var/tmp"
|
|
|
|
chmod 1777 "$ZFSTARGET/tmp"
|
|
|
|
|
2020-05-24 06:49:41 +00:00
|
|
|
# Download and extract base system.
|
|
|
|
dist_files="kernel.txz base.txz"
|
2020-05-24 06:59:19 +00:00
|
|
|
dist_dir="/usr/freebsd-dist/$ARCH/$RELEASE"
|
2020-05-24 06:49:41 +00:00
|
|
|
|
2022-05-29 15:18:35 +00:00
|
|
|
mkdir -p "$dist_dir"
|
2020-05-24 06:49:41 +00:00
|
|
|
for f in $dist_files
|
|
|
|
do
|
2020-05-24 06:59:19 +00:00
|
|
|
fetch -m -o "$dist_dir/$f" "$DIST_BASE/$f"
|
2022-05-24 21:05:09 +00:00
|
|
|
tar -C "$UFSTARGET" -xJf "$dist_dir/$f"
|
2020-05-24 06:49:41 +00:00
|
|
|
done
|
|
|
|
|
2022-05-29 15:18:35 +00:00
|
|
|
# Mount dev and tmp in chroot
|
|
|
|
mount -t devfs devfs "$UFSTARGET/dev"
|
2022-05-24 21:05:09 +00:00
|
|
|
mount_nullfs /tmp "$UFSTARGET/tmp"
|
|
|
|
mount_nullfs /var/tmp "$UFSTARGET/var/tmp"
|
|
|
|
|
|
|
|
# Install the first-boot script that configures the network and ssh key
|
2022-05-27 20:13:42 +00:00
|
|
|
# We must use --rootdir and not --chroot, because the file is read from within the chroot
|
|
|
|
# --automatic means that the package is considered to be installed "automatically",
|
|
|
|
# aka as a dependency of something, so pkg autoremove will remove it.
|
|
|
|
# We do not run pkg autoremove ourselves, that's up to the administrator.
|
2022-05-29 15:17:42 +00:00
|
|
|
pkg --rootdir "$UFSTARGET" add --automatic "$CLOUDSETUP_PKG" "$FBUPDATE_PKG"
|
2022-05-24 21:05:09 +00:00
|
|
|
|
2020-05-24 06:49:41 +00:00
|
|
|
# Configure new system.
|
2022-05-24 21:05:09 +00:00
|
|
|
touch "$UFSTARGET/firstboot"
|
|
|
|
sysrc -f "$UFSTARGET/boot/loader.conf" \
|
2022-06-08 16:43:24 +00:00
|
|
|
zfs_load="YES" \
|
|
|
|
autoboot_delay="-1" \
|
2020-05-24 06:49:41 +00:00
|
|
|
|
2022-05-24 21:05:09 +00:00
|
|
|
sysrc -f "$UFSTARGET/etc/rc.conf" \
|
2022-06-11 20:00:22 +00:00
|
|
|
zfs_enable="YES" \
|
2022-06-08 16:43:24 +00:00
|
|
|
ntpd_enable="YES" \
|
|
|
|
sshd_enable="YES" \
|
|
|
|
growfs_enable="YES" \
|
|
|
|
hostname="freebsd" \
|
|
|
|
firstboot_cloudsetup_enable="YES" \
|
|
|
|
firstboot_freebsd_update_enable="YES" \
|
2020-05-24 06:49:41 +00:00
|
|
|
|
2022-05-25 09:04:46 +00:00
|
|
|
# The resolv.conf file is written by firstboot_cloudsetup
|
2022-05-24 21:05:09 +00:00
|
|
|
#cp /etc/resolv.conf "$UFSTARGET/etc/resolv.conf"
|
2020-05-24 06:49:41 +00:00
|
|
|
|
2022-05-24 21:05:09 +00:00
|
|
|
tzsetup -s -C "$UFSTARGET" UTC
|
|
|
|
|
2022-06-02 18:55:19 +00:00
|
|
|
# Add PermitRootLogin without-password, unless PermitRootLogin yes was already set
|
|
|
|
sed -i .orig -e '/^#PermitRootLogin[[:blank:]]/a\
|
|
|
|
PermitRootLogin without-password
|
|
|
|
' -e '/^PermitRootLogin[[:blank:]]*no/ s/\([[:blank:]]\).*$/\1without-password/' \
|
|
|
|
"$UFSTARGET/etc/ssh/sshd_config"
|
|
|
|
if ! grep -Eq '^PermitRootLogin (without-password|yes)' "$UFSTARGET/etc/ssh/sshd_config"
|
|
|
|
then
|
|
|
|
cat >>"$UFSTARGET/etc/ssh/sshd_config" <<EOF
|
|
|
|
|
|
|
|
# Added by Ungleich
|
|
|
|
PermitRootLogin without-password
|
2020-05-24 06:49:41 +00:00
|
|
|
EOF
|
2022-06-02 18:55:19 +00:00
|
|
|
fi
|
2020-05-24 06:49:41 +00:00
|
|
|
|
2022-05-24 21:05:09 +00:00
|
|
|
# Set zstd-19 compression, copy all data to the pool, and then set compression to default again
|
|
|
|
# This will make the base image smaller, at the cost of taking longer to generate, as zstd-19 is slow to write
|
|
|
|
# Therefore, afterwards we restore compression to default, so written files stay zstd-19, which is fast to read,
|
|
|
|
# but files written by the user afterwards will be written with the default compression algorihtm.
|
2022-05-25 10:09:19 +00:00
|
|
|
zfs set compression=zstd-19 "$ZPOOL_TMP/ROOT/default"
|
2022-05-24 21:05:09 +00:00
|
|
|
umount "$UFSTARGET/dev" "$UFSTARGET/tmp" "$UFSTARGET/var/tmp"
|
|
|
|
rsync -aH --fileflags --inplace "$UFSTARGET/." "$ZFSTARGET"
|
2022-05-27 20:04:42 +00:00
|
|
|
|
2022-05-24 21:05:09 +00:00
|
|
|
sysrc -f "$UFSTARGET/boot/loader.conf" -x zfs_load
|
2022-06-11 20:00:22 +00:00
|
|
|
sysrc -f "$UFSTARGET/etc/rc.conf" -x zfs_enable
|
2022-05-27 20:04:42 +00:00
|
|
|
printf '# Device\tMountpoint\tFStype\tOptions\t\tDump\tPass#\n' \
|
|
|
|
>"$ZFSTARGET/etc/fstab"
|
|
|
|
printf '# Device\tMountpoint\tFStype\tOptions\t\tDump\tPass#\n%s\t%s\t\t%s\t%s\t%s\t%s\n' \
|
|
|
|
/dev/gpt/rootfs / ufs rw,noatime 1 1 \
|
|
|
|
>"$UFSTARGET/etc/fstab"
|
2022-05-24 21:05:09 +00:00
|
|
|
sync ||:
|
2022-05-25 10:09:19 +00:00
|
|
|
zfs inherit compression "$ZPOOL_TMP/ROOT/default"
|
2020-05-24 06:49:41 +00:00
|
|
|
|
|
|
|
trap : EXIT
|
2022-06-12 06:37:24 +00:00
|
|
|
cleanup
|
2020-05-24 06:49:41 +00:00
|
|
|
|
2020-05-24 06:59:19 +00:00
|
|
|
mkdir -p "$ARCH"
|
2022-05-24 21:05:09 +00:00
|
|
|
qemu-img convert -f raw -O qcow2 "$zfsdisk" "$ARCH/$IMAGE_PATH_ZFS"
|
|
|
|
qemu-img convert -f raw -O qcow2 "$ufsdisk" "$ARCH/$IMAGE_PATH_UFS"
|
|
|
|
rm "$zfsdisk" "$ufsdisk"
|
2020-05-24 06:49:41 +00:00
|
|
|
|
|
|
|
# Filesystem will be enlarged by growfs(7) on next startup
|
2022-05-24 21:05:09 +00:00
|
|
|
qemu-img resize "$ARCH/$IMAGE_PATH_ZFS" "$IMAGE_SIZE"
|
|
|
|
qemu-img resize "$ARCH/$IMAGE_PATH_UFS" "$IMAGE_SIZE"
|