1fada6e23SColin Percival#!/bin/sh 2fada6e23SColin Percival 340ff0753SColin Percival# Package which should be installed onto all EC2 AMIs: 4fada6e23SColin Percival# * ebsnvme-id, which is very minimal and provides important EBS-specific 5fada6e23SColin Percival# functionality, 640ff0753SColin Percivalexport VM_EXTRA_PACKAGES="${VM_EXTRA_PACKAGES} ebsnvme-id" 7fada6e23SColin Percival 8fada6e23SColin Percival# Services which should be enabled by default in rc.conf(5). 9fada6e23SColin Percivalexport VM_RC_LIST="dev_aws_disk ntpd" 10fada6e23SColin Percival 112dac89aeSColin Percival# Build with a 7.9 GB partition; the growfs rc.d script will expand 12fada6e23SColin Percival# the partition to fill the root disk after the EC2 instance is launched. 13fada6e23SColin Percival# Note that if this is set to <N>G, we will end up with an <N+1> GB disk 14fada6e23SColin Percival# image since VMSIZE is the size of the filesystem partition, not the disk 15fada6e23SColin Percival# which it resides within. 162dac89aeSColin Percivalexport VMSIZE=8000m 17fada6e23SColin Percival 18fada6e23SColin Percival# No swap space; it doesn't make sense to provision any as part of the disk 19fada6e23SColin Percival# image when we could be launching onto a system with anywhere between 0.5 20fada6e23SColin Percival# and 4096 GB of RAM. 21fada6e23SColin Percivalexport NOSWAP=YES 22fada6e23SColin Percival 23fada6e23SColin Percivalec2_common() { 24fada6e23SColin Percival # Delete the pkg package and the repo database; they will likely be 25fada6e23SColin Percival # long out of date before the EC2 instance is launched. 26fada6e23SColin Percival mount -t devfs devfs ${DESTDIR}/dev 27fada6e23SColin Percival chroot ${DESTDIR} ${EMULATOR} env ASSUME_ALWAYS_YES=yes \ 28fada6e23SColin Percival /usr/sbin/pkg delete -f -y pkg 29fada6e23SColin Percival umount ${DESTDIR}/dev 30fada6e23SColin Percival rm ${DESTDIR}/var/db/pkg/repo-*.sqlite 31fada6e23SColin Percival 32fada6e23SColin Percival # Turn off IPv6 Duplicate Address Detection; the EC2 networking 33fada6e23SColin Percival # configuration makes it unnecessary. 34fada6e23SColin Percival echo 'net.inet6.ip6.dad_count=0' >> ${DESTDIR}/etc/sysctl.conf 35fada6e23SColin Percival 36fada6e23SColin Percival # Booting quickly is more important than giving users a chance to 37fada6e23SColin Percival # access the boot loader via the serial port. 38fada6e23SColin Percival echo 'autoboot_delay="-1"' >> ${DESTDIR}/boot/loader.conf 39fada6e23SColin Percival echo 'beastie_disable="YES"' >> ${DESTDIR}/boot/loader.conf 40fada6e23SColin Percival 41714a6d43SColin Percival # The EFI RNG on Graviton 2 is particularly slow if we ask for the 42714a6d43SColin Percival # default 2048 bytes of entropy; ask for 64 bytes instead. 43714a6d43SColin Percival echo 'entropy_efi_seed_size="64"' >> ${DESTDIR}/boot/loader.conf 44714a6d43SColin Percival 45fada6e23SColin Percival # Tell gptboot not to wait 3 seconds for a keypress which will 46fada6e23SColin Percival # never arrive. 47fada6e23SColin Percival printf -- "-n\n" > ${DESTDIR}/boot.config 48fada6e23SColin Percival 49fada6e23SColin Percival # The emulated keyboard attached to EC2 instances is inaccessible to 50fada6e23SColin Percival # users, and there is no mouse attached at all; disable to keyboard 51fada6e23SColin Percival # and the keyboard controller (to which the mouse would attach, if 52fada6e23SColin Percival # one existed) in order to save time in device probing. 53fada6e23SColin Percival echo 'hint.atkbd.0.disabled=1' >> ${DESTDIR}/boot/loader.conf 54fada6e23SColin Percival echo 'hint.atkbdc.0.disabled=1' >> ${DESTDIR}/boot/loader.conf 55fada6e23SColin Percival 56a0018c65SJames Wright # There is no floppy drive on EC2 instances so disable the driver. 57a0018c65SJames Wright echo 'hint.fd.0.disabled=1' >> ${DESTDIR}/boot/loader.conf 58a0018c65SJames Wright echo 'hint.fdc.0.disabled=1' >> ${DESTDIR}/boot/loader.conf 59a0018c65SJames Wright 60a0018c65SJames Wright # There is no parallel port on EC2 instances so disable driver. 61a0018c65SJames Wright echo 'hint.ppc.0.disabled=1' >> ${DESTDIR}/boot/loader.conf 62a0018c65SJames Wright 63fada6e23SColin Percival # EC2 has two consoles: An emulated serial port ("system log"), 64fada6e23SColin Percival # which has been present since 2006; and a VGA console ("instance 65fada6e23SColin Percival # screenshot") which was introduced in 2016. 66fada6e23SColin Percival echo 'boot_multicons="YES"' >> ${DESTDIR}/boot/loader.conf 67fada6e23SColin Percival 68fada6e23SColin Percival # Some older EC2 hardware used a version of Xen with a bug in its 69fada6e23SColin Percival # emulated serial port. It is not clear if EC2 still has any such 70fada6e23SColin Percival # nodes, but apply the workaround just in case. 71fada6e23SColin Percival echo 'hw.broken_txfifo="1"' >> ${DESTDIR}/boot/loader.conf 72fada6e23SColin Percival 73*2f3f867aSColin Percival # Graviton 1 through Graviton 4 have a bug in their ACPI where they 74*2f3f867aSColin Percival # mark the PL061's pins as needing to be configured in PullUp mode 75*2f3f867aSColin Percival # (in fact the PL061 has no pullup/pulldown resistors). 76*2f3f867aSColin Percival echo 'debug.acpi.quirks="8"' >> ${DESTDIR}/boot/loader.conf 77*2f3f867aSColin Percival 78fada6e23SColin Percival # Load the kernel module for the Amazon "Elastic Network Adapter" 79fada6e23SColin Percival echo 'if_ena_load="YES"' >> ${DESTDIR}/boot/loader.conf 80fada6e23SColin Percival 81fada6e23SColin Percival # Use the "nda" driver for accessing NVMe disks rather than the 82fada6e23SColin Percival # historical "nvd" driver. 83fada6e23SColin Percival echo 'hw.nvme.use_nvd="0"' >> ${DESTDIR}/boot/loader.conf 84fada6e23SColin Percival 85fada6e23SColin Percival # Disable KbdInteractiveAuthentication according to EC2 requirements. 86fada6e23SColin Percival sed -i '' -e \ 87fada6e23SColin Percival 's/^#KbdInteractiveAuthentication yes/KbdInteractiveAuthentication no/' \ 88fada6e23SColin Percival ${DESTDIR}/etc/ssh/sshd_config 89fada6e23SColin Percival 900aabcd75SColin Percival # RSA host keys are obsolete and also very slow to generate 910aabcd75SColin Percival echo 'sshd_rsa_enable="NO"' >> ${DESTDIR}/etc/rc.conf 920aabcd75SColin Percival 93fada6e23SColin Percival # Use FreeBSD Update mirrors hosted in AWS 94fada6e23SColin Percival sed -i '' -e 's/update.FreeBSD.org/aws.update.FreeBSD.org/' \ 95fada6e23SColin Percival ${DESTDIR}/etc/freebsd-update.conf 96fada6e23SColin Percival 97fada6e23SColin Percival # Use the NTP service provided by Amazon 98fada6e23SColin Percival sed -i '' -e 's/^pool/#pool/' \ 99fada6e23SColin Percival -e '1,/^#server/s/^#server.*/server 169.254.169.123 iburst/' \ 100fada6e23SColin Percival ${DESTDIR}/etc/ntp.conf 101fada6e23SColin Percival 102fada6e23SColin Percival # Provide a map for accessing Elastic File System mounts 103fada6e23SColin Percival cat > ${DESTDIR}/etc/autofs/special_efs <<'EOF' 104fada6e23SColin Percival#!/bin/sh 105fada6e23SColin Percival 106fada6e23SColin Percivalif [ $# -eq 0 ]; then 107fada6e23SColin Percival # No way to know which EFS filesystems exist and are 108fada6e23SColin Percival # accessible to this EC2 instance. 109fada6e23SColin Percival exit 0 110fada6e23SColin Percivalfi 111fada6e23SColin Percival 112fada6e23SColin Percival# Provide instructions on how to mount the requested filesystem. 113fada6e23SColin PercivalFS=$1 114fada6e23SColin PercivalREGION=`fetch -qo- http://169.254.169.254/latest/meta-data/placement/availability-zone | sed -e 's/[a-z]$//'` 115fada6e23SColin Percivalecho "-nfsv4,minorversion=1,oneopenown ${FS}.efs.${REGION}.amazonaws.com:/" 116fada6e23SColin PercivalEOF 117fada6e23SColin Percival chmod 755 ${DESTDIR}/etc/autofs/special_efs 118fada6e23SColin Percival 119fada6e23SColin Percival # The first time the AMI boots, run "first boot" scripts. 120fada6e23SColin Percival touch ${DESTDIR}/firstboot 121fada6e23SColin Percival 122fada6e23SColin Percival return 0 123fada6e23SColin Percival} 124f961ddb2SColin Percival 125f961ddb2SColin Percivalec2_base_networking () { 126f961ddb2SColin Percival # EC2 instances use DHCP to get their network configuration. IPv6 127f961ddb2SColin Percival # requires accept_rtadv. 128f961ddb2SColin Percival echo 'ifconfig_DEFAULT="SYNCDHCP accept_rtadv"' >> ${DESTDIR}/etc/rc.conf 129f961ddb2SColin Percival 130f961ddb2SColin Percival # The EC2 DHCP server can be trusted to know whether an IP address is 131f961ddb2SColin Percival # assigned to us; we don't need to ARP to check if anyone else is using 132f961ddb2SColin Percival # the address before we start using it. 133f961ddb2SColin Percival echo 'dhclient_arpwait="NO"' >> ${DESTDIR}/etc/rc.conf 134f961ddb2SColin Percival 135f961ddb2SColin Percival # Enable IPv6 on all interfaces, and spawn DHCPv6 via rtsold 136f961ddb2SColin Percival echo 'ipv6_activate_all_interfaces="YES"' >> ${DESTDIR}/etc/rc.conf 137f961ddb2SColin Percival echo 'rtsold_enable="YES"' >> ${DESTDIR}/etc/rc.conf 138f961ddb2SColin Percival echo 'rtsold_flags="-M /usr/local/libexec/rtsold-M -a"' >> ${DESTDIR}/etc/rc.conf 139f961ddb2SColin Percival 140f961ddb2SColin Percival # Provide a script which rtsold can use to launch DHCPv6 141f961ddb2SColin Percival mkdir -p ${DESTDIR}/usr/local/libexec 142f961ddb2SColin Percival cat > ${DESTDIR}/usr/local/libexec/rtsold-M <<'EOF' 143f961ddb2SColin Percival#!/bin/sh 144f961ddb2SColin Percival 145f961ddb2SColin Percival/usr/local/sbin/dhclient -6 -nw -N -cf /dev/null $1 146f961ddb2SColin PercivalEOF 147f961ddb2SColin Percival chmod 755 ${DESTDIR}/usr/local/libexec/rtsold-M 148f961ddb2SColin Percival 149f961ddb2SColin Percival return 0 150f961ddb2SColin Percival} 151