1#!/bin/sh
2
3# Packages which should be installed onto all EC2 AMIs:
4# * ebsnvme-id, which is very minimal and provides important EBS-specific
5# functionality,
6# * amazon-ssm-agent (not enabled by default, but some users need to use
7# it on systems not connected to the internet).
8export VM_EXTRA_PACKAGES="${VM_EXTRA_PACKAGES} ebsnvme-id amazon-ssm-agent"
9
10# Services which should be enabled by default in rc.conf(5).
11export VM_RC_LIST="dev_aws_disk ntpd"
12
13# Build with a 5.9 GB partition; the growfs rc.d script will expand
14# the partition to fill the root disk after the EC2 instance is launched.
15# Note that if this is set to <N>G, we will end up with an <N+1> GB disk
16# image since VMSIZE is the size of the filesystem partition, not the disk
17# which it resides within.
18export VMSIZE=6000m
19
20# No swap space; it doesn't make sense to provision any as part of the disk
21# image when we could be launching onto a system with anywhere between 0.5
22# and 4096 GB of RAM.
23export NOSWAP=YES
24
25ec2_common() {
26	# Delete the pkg package and the repo database; they will likely be
27	# long out of date before the EC2 instance is launched.
28	mount -t devfs devfs ${DESTDIR}/dev
29	chroot ${DESTDIR} ${EMULATOR} env ASSUME_ALWAYS_YES=yes \
30		/usr/sbin/pkg delete -f -y pkg
31	umount ${DESTDIR}/dev
32	rm ${DESTDIR}/var/db/pkg/repo-*.sqlite
33
34	# Turn off IPv6 Duplicate Address Detection; the EC2 networking
35	# configuration makes it unnecessary.
36	echo 'net.inet6.ip6.dad_count=0' >> ${DESTDIR}/etc/sysctl.conf
37
38	# Booting quickly is more important than giving users a chance to
39	# access the boot loader via the serial port.
40	echo 'autoboot_delay="-1"' >> ${DESTDIR}/boot/loader.conf
41	echo 'beastie_disable="YES"' >> ${DESTDIR}/boot/loader.conf
42
43	# Tell gptboot not to wait 3 seconds for a keypress which will
44	# never arrive.
45	printf -- "-n\n" > ${DESTDIR}/boot.config
46
47	# The emulated keyboard attached to EC2 instances is inaccessible to
48	# users, and there is no mouse attached at all; disable to keyboard
49	# and the keyboard controller (to which the mouse would attach, if
50	# one existed) in order to save time in device probing.
51	echo 'hint.atkbd.0.disabled=1' >> ${DESTDIR}/boot/loader.conf
52	echo 'hint.atkbdc.0.disabled=1' >> ${DESTDIR}/boot/loader.conf
53
54	# EC2 has two consoles: An emulated serial port ("system log"),
55	# which has been present since 2006; and a VGA console ("instance
56	# screenshot") which was introduced in 2016.
57	echo 'boot_multicons="YES"' >> ${DESTDIR}/boot/loader.conf
58
59	# Some older EC2 hardware used a version of Xen with a bug in its
60	# emulated serial port.  It is not clear if EC2 still has any such
61	# nodes, but apply the workaround just in case.
62	echo 'hw.broken_txfifo="1"' >> ${DESTDIR}/boot/loader.conf
63
64	# Load the kernel module for the Amazon "Elastic Network Adapter"
65	echo 'if_ena_load="YES"' >> ${DESTDIR}/boot/loader.conf
66
67	# Use the "nda" driver for accessing NVMe disks rather than the
68	# historical "nvd" driver.
69	echo 'hw.nvme.use_nvd="0"' >> ${DESTDIR}/boot/loader.conf
70
71	# Disable KbdInteractiveAuthentication according to EC2 requirements.
72	sed -i '' -e \
73		's/^#KbdInteractiveAuthentication yes/KbdInteractiveAuthentication no/' \
74		${DESTDIR}/etc/ssh/sshd_config
75
76	# Use FreeBSD Update mirrors hosted in AWS
77	sed -i '' -e 's/update.FreeBSD.org/aws.update.FreeBSD.org/' \
78		${DESTDIR}/etc/freebsd-update.conf
79
80	# Use the NTP service provided by Amazon
81	sed -i '' -e 's/^pool/#pool/' \
82		-e '1,/^#server/s/^#server.*/server 169.254.169.123 iburst/' \
83		${DESTDIR}/etc/ntp.conf
84
85	# Provide a map for accessing Elastic File System mounts
86	cat > ${DESTDIR}/etc/autofs/special_efs <<'EOF'
87#!/bin/sh
88
89if [ $# -eq 0 ]; then
90        # No way to know which EFS filesystems exist and are
91        # accessible to this EC2 instance.
92        exit 0
93fi
94
95# Provide instructions on how to mount the requested filesystem.
96FS=$1
97REGION=`fetch -qo- http://169.254.169.254/latest/meta-data/placement/availability-zone | sed -e 's/[a-z]$//'`
98echo "-nfsv4,minorversion=1,oneopenown ${FS}.efs.${REGION}.amazonaws.com:/"
99EOF
100	chmod 755 ${DESTDIR}/etc/autofs/special_efs
101
102	# The first time the AMI boots, run "first boot" scripts.
103	touch ${DESTDIR}/firstboot
104
105	return 0
106}
107