mirror of
https://git.hardenedbsd.org/hardenedbsd/HardenedBSD.git
synced 2024-11-23 11:51:08 +01:00
1e7c1f1742
There may be some very sharp edges here while refactoring. - Move amd64/mk-vmimage.sh -> scripts/mk-vmimage.sh. - Remove vm-base target from Makefile.vm. - In vm-image target, use getopts flags for argument passing. - Create tools/vmimage.subr, containing default and prototype for the following functions that are used to drive the build, run in this order: vm_install_base() vm_extra_install_base() vm_extra_install_packages() vm_extra_install_ports() vm_extra_enable_services() vm_extra_pre_umount() vm_create_disk() vm_extra_create_disk() - In tools/azure.conf, override: vm_extra_install_base() vm_extra_pre_umount() vm_extra_create_disk() - In tools/openstack.conf, override: vm_extra_install_base() vm_extra_pre_umount() Sponsored by: The FreeBSD Foundation
151 lines
3.1 KiB
Bash
151 lines
3.1 KiB
Bash
#!/bin/sh
|
|
#
|
|
# $FreeBSD$
|
|
#
|
|
#
|
|
# Common functions for virtual machine image build scripts.
|
|
#
|
|
|
|
export PATH="/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/sbin"
|
|
trap "cleanup" INT QUIT TRAP ABRT TERM
|
|
|
|
mkimg_bootcode="/boot/pmbr"
|
|
mkimg_partitions="-p freebsd-boot/bootfs:=/boot/gptboot"
|
|
mkimg_partitions="${mkimg_partitions} -p freebsd-swap/swapfs::1G"
|
|
mkimg_partitions="${mkimg_partitions} freebsd-ufs/rootfs:=${VMBASE}"
|
|
|
|
usage() {
|
|
echo "${0} usage:"
|
|
echo "${@}"
|
|
return 1
|
|
}
|
|
|
|
err() {
|
|
printf "${@}\n"
|
|
cleanup
|
|
return 1
|
|
}
|
|
|
|
cleanup() {
|
|
if [ ! -z "${mddev}" ]; then
|
|
mdconfig -d -u ${mddev}
|
|
fi
|
|
umount ${DESTDIR}/dev
|
|
umount ${DESTDIR}
|
|
|
|
return 0
|
|
}
|
|
|
|
vm_create_base() {
|
|
# Creates the UFS root filesystem for the virtual machine disk,
|
|
# written to the formatted disk image with mkimg(1).
|
|
|
|
mkdir -p ${DESTDIR}
|
|
truncate -s ${VMSIZE} ${VMBASE}
|
|
mddev=$(mdconfig -f ${VMBASE})
|
|
newfs -j /dev/${mddev}
|
|
mount /dev/${mddev} ${DESTDIR}
|
|
|
|
return 0
|
|
}
|
|
|
|
vm_install_base() {
|
|
# Installs the FreeBSD userland/kernel to the virtual machine disk.
|
|
|
|
cd ${WORLDDIR} && \
|
|
make DESTDIR=${DESTDIR} \
|
|
installworld installkernel distribution || \
|
|
err "\n\nCannot install the base system to ${DESTDIR}."
|
|
|
|
echo '# Custom /etc/fstab for FreeBSD VM images' \
|
|
> ${DESTDIR}/etc/fstab
|
|
echo '/dev/gpt/rootfs / ufs rw 1 1' \
|
|
>> ${DESTDIR}/etc/fstab
|
|
echo '/dev/gpt/swapfs none swap sw 0 0' \
|
|
>> ${DESTDIR}/etc/fstab
|
|
|
|
chroot ${DESTDIR} /usr/bin/newaliases
|
|
chroot ${DESTDIR} /etc/rc.d/ldconfig forcestart
|
|
|
|
return 0
|
|
}
|
|
|
|
vm_extra_install_base() {
|
|
# Prototype. When overridden, runs extra post-installworld commands
|
|
# as needed, based on the target virtual machine image or cloud
|
|
# provider image target.
|
|
|
|
return 0
|
|
}
|
|
|
|
vm_extra_enable_services() {
|
|
if [ ! -z "${VM_RC_LIST}" ]; then
|
|
for _rcvar in ${VM_RC_LIST}; do
|
|
echo ${_rcvar}_enable="YES" >> ${DESTDIR}/etc/rc.conf
|
|
done
|
|
fi
|
|
|
|
return 0
|
|
}
|
|
|
|
vm_extra_install_packages() {
|
|
chroot ${DESTDIR} env ASSUME_ALWAYS_YES=yes \
|
|
/usr/sbin/pkg bootstrap -y
|
|
if [ ! -z "${VM_EXTRA_PACKAGES}" ]; then
|
|
chroot ${DESTDIR} env ASSUME_ALWAYS_YES=yes \
|
|
/usr/sbin/pkg install -y ${VM_EXTRA_PACKAGES}
|
|
fi
|
|
|
|
return 0
|
|
}
|
|
|
|
vm_extra_install_ports() {
|
|
# Prototype. When overridden, installs additional ports within the
|
|
# virtual machine environment.
|
|
|
|
return 0
|
|
}
|
|
|
|
vm_umount_base() {
|
|
i=0
|
|
sync
|
|
while ! umount ${DESTDIR}/dev ${DESTDIR}; do
|
|
i=$(( $i + 1 ))
|
|
if [ $i -ge 10 ]; then
|
|
# This should never happen. But, it has happened.
|
|
msg="Cannot umount(8) ${DESTDIR}\n"
|
|
msg="${msg}Something has gone horribly wrong."
|
|
err "${msg}"
|
|
fi
|
|
sleep 1
|
|
done
|
|
|
|
return 0
|
|
}
|
|
|
|
vm_create_disk() {
|
|
if [ -z "${mkimg_paritions}" ]; then
|
|
err "No partition types specified. Skipping."
|
|
return 1
|
|
fi
|
|
echo "Creating image... Please wait."
|
|
echo
|
|
mkimg -f ${mkimg_format} -s ${mkimg_scheme} \
|
|
${mkimg_bootcode} \
|
|
${mkimg_partitions} \
|
|
${mkimg_outfile}
|
|
|
|
mkimg -b /boot/pmbr -p freebsd-boot/bootfs:=/boot/gptboot \
|
|
-p freebsd-swap/swapfs::1G \
|
|
-p freebsd-ufs/rootfs:=${VMBASE} \
|
|
-o ${VMIMAGE}.raw
|
|
|
|
return 0
|
|
}
|
|
|
|
vm_extra_create_disk() {
|
|
|
|
return 0
|
|
}
|
|
|