freebsd-src/release/tools/gce.conf
Colin Percival 0b1c5628c7 release: Rework vm_extra_pre_umount
The vm_extra_pre_umount function in vmimage.subr served two purposes:
It removed /etc/resolv.conf and /qemu (if cross-building), and it
provided a function for cloudware to override in order to make cloud
specific changes to the filesystem before constructing a disk image.

This resulted in a number of bugs:
1. When cross-building, the emulator binary was left as /qemu in the
Azure, GCE, Openstack and Vagrant images.
2. The build host's resolv.conf was left as /etc/resolv.conf in the
basic-ci and basic-cloudinit images.
3. When building GCE images, a Google-specific resolv.conf file was
constructed, and then deleted before the disk image was created.

Move the bits needed for running code inside a VM staging directory
from vm_install_base into a new vm_emulation_setup routine, and move
the corresponding cleanup bits from vm_extra_pre_umount to a new
vm_emulation_cleanup routine.

Remove the /qemu and /etc/resolv.conf cleanups from the cloudware
configuration files (where they exist) since we will now be running
vm_emulation_cleanup to remove those even when vm_extra_pre_umount
has been overridden.

Override vm_emulation_cleanup in gce.conf since in that one case (and
*only* that one case) we don't want to clean up resolv.conf (since it
was constructed for the VM image rather than copied from the host).

releng/14.1 candidate.

MFC after:	1 week
Sponsored by:	https://www.patreon.com/cperciva
2024-05-06 13:40:47 -07:00

133 lines
3.6 KiB
Bash

#!/bin/sh
#
#
# The default of 3GB is too small for GCE, so override the size here.
export VMSIZE=20g
# Set to a list of packages to install.
export VM_EXTRA_PACKAGES="${VM_EXTRA_PACKAGES} firstboot-freebsd-update \
firstboot-pkgs google-cloud-sdk panicmail sudo \
sysutils/py-google-compute-engine lang/python \
lang/python3"
# Set to a list of third-party software to enable in rc.conf(5).
export VM_RC_LIST="ntpd sshd growfs \
firstboot_pkgs firstboot_freebsd_update google_startup \
google_accounts_daemon google_clock_skew_daemon \
google_instance_setup google_network_daemon"
vm_extra_install_base() {
echo 'search google.internal' > ${DESTDIR}/etc/resolv.conf
echo 'nameserver 169.254.169.254' >> ${DESTDIR}/etc/resolv.conf
echo 'nameserver 8.8.8.8' >> ${DESTDIR}/etc/resolv.conf
}
vm_extra_pre_umount() {
# Enable growfs on every boot, not only the first, as as instance's disk can
# be enlarged post-creation
sed -i -e '/KEYWORD: firstboot/d' /etc/rc.d/growfs
cat << EOF >> ${DESTDIR}/etc/rc.conf
dumpdev="AUTO"
ifconfig_DEFAULT="SYNCDHCP mtu 1460"
ntpd_sync_on_start="YES"
# need to fill in something here
#firstboot_pkgs_list=""
panicmail_autosubmit="YES"
EOF
cat << EOF >> ${DESTDIR}/boot/loader.conf
autoboot_delay="-1"
beastie_disable="YES"
loader_logo="none"
hw.memtest.tests="0"
console="comconsole,vidconsole"
hw.vtnet.mq_disable=1
kern.timecounter.hardware=ACPI-safe
aesni_load="YES"
nvme_load="YES"
EOF
echo '169.254.169.254 metadata.google.internal metadata' >> \
${DESTDIR}/etc/hosts
# overwrite ntp.conf
cat << EOF > ${DESTDIR}/etc/ntp.conf
server metadata.google.internal iburst
restrict default kod nomodify notrap nopeer noquery
restrict -6 default kod nomodify notrap nopeer noquery
restrict 127.0.0.1
restrict -6 ::1
restrict 127.127.1.0
EOF
cat << EOF >> ${DESTDIR}/etc/syslog.conf
*.err;kern.warning;auth.notice;mail.crit /dev/console
EOF
cat << EOF >> ${DESTDIR}/etc/ssh/sshd_config
KbdInteractiveAuthentication no
X11Forwarding no
AcceptEnv LANG
AllowAgentForwarding no
ClientAliveInterval 420
EOF
cat << EOF >> ${DESTDIR}/etc/crontab
0 3 * * * root /usr/sbin/freebsd-update cron
EOF
cat << EOF >> ${DESTDIR}/etc/sysctl.conf
net.inet.icmp.drop_redirect=1
net.inet.ip.redirect=0
kern.ipc.soacceptqueue=1024
debug.trace_on_panic=1
debug.debugger_on_panic=0
EOF
# To meet GCE marketplace requirements, extract the src.txz and
# ports.txz distributions to the target virtual machine disk image
# and fetch the sources for the third-party software installed on
# the image.
if [ ! -c "${DESTDIR}/dev/null" ]; then
mkdir -p ${DESTDIR}/dev
mount -t devfs devfs ${DESTDIR}/dev
fi
if [ -e "${DESTDIR}/../ftp/src.txz" ]; then
tar fxJ ${DESTDIR}/../ftp/src.txz -C ${DESTDIR}
fi
if [ -e "${DESTDIR}/../ftp/ports.txz" ]; then
tar fxJ ${DESTDIR}/../ftp/ports.txz -C ${DESTDIR}
_INSTALLED_PACKAGES=$(chroot ${DESTDIR} pkg info -o -q -a)
for PACKAGE in ${_INSTALLED_PACKAGES}; do
chroot ${DESTDIR} \
make -C /usr/ports/${PACKAGE} fetch
done
fi
if [ -c "${DESTDIR}/dev/null" ]; then
umount_loop ${DESTDIR}/dev
fi
## XXX: Verify this is needed. I do not see this requirement
## in the docs, and it impairs the ability to boot-test a copy
## of the image prior to packaging for upload to GCE.
#sed -E -i '' 's/^([^#].*[[:space:]])on/\1off/' ${DESTDIR}/etc/ttys
touch ${DESTDIR}/firstboot
return 0
}
# Do everything except deleting resolv.conf since we construct our own
# Googlized resolv.conf file in vm_extra_install_base.
vm_emulation_cleanup() {
if ! [ -z "${QEMUSTATIC}" ]; then
rm -f ${DESTDIR}/${EMULATOR}
fi
umount_loop ${DESTDIR}/dev
return 0
}