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