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