| 1 | #cloud-config |
| 2 | # vim:syntax=yaml |
| 3 | |
| 4 | growpart: |
| 5 | mode: auto |
| 6 | devices: ['/'] |
| 7 | |
| 8 | {{- if eq .OS "FreeBSD" }} |
| 9 | packages: |
| 10 | # boot.sh depends on sudo. |
| 11 | # TODO: consider replacing sudo with doas. |
| 12 | # FIXME: The hostagent script depends on sudo too. |
| 13 | # https://github.com/lima-vm/lima/issues/4594 |
| 14 | - sudo |
| 15 | {{- end }} |
| 16 | |
| 17 | {{- if .UpgradePackages }} |
| 18 | package_update: true |
| 19 | package_upgrade: true |
| 20 | package_reboot_if_required: true |
| 21 | {{- end }} |
| 22 | |
| 23 | {{- if or .RosettaEnabled (and .Mounts (or (eq .MountType "9p") (eq .MountType "virtiofs"))) }} |
| 24 | mounts: |
| 25 | {{- if .RosettaEnabled }}{{/* Mount the rosetta volume before systemd-binfmt.service(8) starts */}} |
| 26 | - [vz-rosetta, /mnt/lima-rosetta, virtiofs, defaults, "0", "0"] |
| 27 | {{- end }} |
| 28 | {{- if and .Mounts (or (eq .MountType "9p") (eq .MountType "virtiofs")) }} |
| 29 | {{- range $m := $.Mounts}} |
| 30 | - [{{$m.Tag}}, {{$m.MountPoint}}, {{$m.Type}}, "{{$m.Options}}", "0", "0"] |
| 31 | {{- end }} |
| 32 | {{- end }} |
| 33 | {{- end }} |
| 34 | |
| 35 | {{- if .TimeZone }} |
| 36 | timezone: {{.TimeZone}} |
| 37 | {{- end }} |
| 38 | |
| 39 | users: |
| 40 | - name: "{{.User}}" |
| 41 | {{- if eq .OS "FreeBSD" }} |
| 42 | # nuageinit does not support specifying the UID. |
| 43 | # The UID is fixed up in boot.essential.FreeBSD/00-freebsd-user-group.sh |
| 44 | {{- else }} |
| 45 | uid: "{{.UID}}" |
| 46 | {{- end }} |
| 47 | {{- if .Comment }} |
| 48 | gecos: {{ printf "%q" .Comment }} |
| 49 | {{- end }} |
| 50 | homedir: "{{.Home}}" |
| 51 | shell: {{.Shell}} |
| 52 | {{- if eq .OS "Darwin" }} |
| 53 | {{/* On macOS, the password is not locked so as to allow GUI login. */}} |
| 54 | {{/* Since the user can run sudo with their own password, basically we don't need to set up passwordless sudo. */}} |
| 55 | {{/* However, it is still configured to allow `/sbin/shutdown -h now` without password, as it is invoked by `limactl stop` for graceful shutdown. */}} |
| 56 | {{/* (Why doesn't macOS VM support graceful shutdown?) */}} |
| 57 | sudo: ALL=(ALL) NOPASSWD:/sbin/shutdown -h now |
| 58 | {{- else }} |
| 59 | {{- if .PasswordlessSudo }} |
| 60 | sudo: ALL=(ALL) NOPASSWD:ALL |
| 61 | {{- if eq .OS "FreeBSD" }} |
| 62 | groups: |
| 63 | - wheel |
| 64 | doas: permit nopass :wheel |
| 65 | {{- end}} |
| 66 | lock_passwd: true |
| 67 | {{- else }} |
| 68 | sudo: ALL=(ALL) ALL |
| 69 | {{- if eq .OS "FreeBSD" }} |
| 70 | groups: |
| 71 | - wheel |
| 72 | doas: permit :wheel |
| 73 | {{- end}} |
| 74 | lock_passwd: false |
| 75 | {{- end }} |
| 76 | {{- end }} |
| 77 | {{- if eq .OS "FreeBSD" }} |
| 78 | ssh_authorized_keys: |
| 79 | {{- else }} |
| 80 | ssh-authorized-keys: |
| 81 | {{- end }} |
| 82 | {{- range $val := .SSHPubKeys }} |
| 83 | - {{ printf "%q" $val }} |
| 84 | {{- end }} |
| 85 | |
| 86 | {{- if .BootScripts }} |
| 87 | write_files: |
| 88 | - content: | |
| 89 | #!/bin/sh |
| 90 | set -eux |
| 91 | LIMA_CIDATA_MNT="/mnt/lima-cidata" |
| 92 | UNAME="$(uname -s)" |
| 93 | if [ "${UNAME}" = "Darwin" ]; then |
| 94 | LIMA_CIDATA_MNT="/Volumes/cidata" |
| 95 | # Should have been mounted automatically |
| 96 | elif [ "${UNAME}" = "FreeBSD" ]; then |
| 97 | LIMA_CIDATA_DEV="/dev/iso9660/cidata" |
| 98 | if [ ! -e "${LIMA_CIDATA_DEV}" ]; then |
| 99 | # When the iso is created with `hdiutil` on macOS, |
| 100 | # apparently the volume name becomes "CIDATA" not "cidata" |
| 101 | LIMA_CIDATA_DEV="/dev/iso9660/CIDATA" |
| 102 | fi |
| 103 | mkdir -p -m 700 "${LIMA_CIDATA_MNT}" |
| 104 | mount_cd9660 -G wheel -U root -m 0700 -o ro,exec "${LIMA_CIDATA_DEV}" "${LIMA_CIDATA_MNT}" |
| 105 | elif [ "${UNAME}" = "Linux" ]; then |
| 106 | LIMA_CIDATA_DEV="/dev/disk/by-label/cidata" |
| 107 | mkdir -p -m 700 "${LIMA_CIDATA_MNT}" |
| 108 | mount -o ro,mode=0700,dmode=0700,overriderockperm,exec,uid=0 "${LIMA_CIDATA_DEV}" "${LIMA_CIDATA_MNT}" |
| 109 | else |
| 110 | echo "Unsupported OS: ${UNAME}" >&2 |
| 111 | exit 1 |
| 112 | fi |
| 113 | export LIMA_CIDATA_MNT |
| 114 | exec "${LIMA_CIDATA_MNT}"/boot.sh |
| 115 | {{- if or (eq .OS "Darwin") (eq .OS "FreeBSD") }} |
| 116 | owner: root:wheel |
| 117 | {{- else }} |
| 118 | owner: root:root |
| 119 | {{- end }} |
| 120 | {{- if eq .OS "FreeBSD" }} |
| 121 | # nuageinit requires the path to be under an existing directory |
| 122 | path: /usr/sbin/lima-freebsd-init.sh |
| 123 | {{- else }} |
| 124 | path: /var/lib/cloud/scripts/per-boot/00-lima.boot.sh |
| 125 | {{- end }} |
| 126 | permissions: '0755' |
| 127 | {{- if eq .OS "FreeBSD" }} |
| 128 | # nuageinit does not run /var/lib/cloud/scripts/per-boot/* scripts |
| 129 | - content: | |
| 130 | #!/bin/sh |
| 131 | |
| 132 | # PROVIDE: lima_freebsd_init |
| 133 | # REQUIRE: DAEMON |
| 134 | # BEFORE: LOGIN |
| 135 | |
| 136 | . /etc/rc.subr |
| 137 | |
| 138 | name="lima_freebsd_init" |
| 139 | rcvar="lima_freebsd_init_enable" |
| 140 | command="/usr/sbin/lima-freebsd-init.sh" |
| 141 | |
| 142 | load_rc_config "$name" |
| 143 | run_rc_command "$1" |
| 144 | owner: root:wheel |
| 145 | path: /etc/rc.d/lima_freebsd_init |
| 146 | permissions: '0755' |
| 147 | - content: | |
| 148 | lima_freebsd_init_enable="YES" |
| 149 | owner: root:wheel |
| 150 | path: /etc/rc.conf.d/lima_freebsd_init |
| 151 | permissions: '0644' |
| 152 | {{- end }} |
| 153 | {{- end }} |
| 154 | |
| 155 | {{- if .DNSAddresses }} |
| 156 | # This has no effect on systems using systemd-resolved, but is used |
| 157 | # on e.g. Alpine to set up /etc/resolv.conf on first boot. |
| 158 | |
| 159 | manage_resolv_conf: true |
| 160 | |
| 161 | resolv_conf: |
| 162 | nameservers: |
| 163 | {{- range $ns := $.DNSAddresses }} |
| 164 | - {{$ns}} |
| 165 | {{- end }} |
| 166 | {{- end }} |
| 167 | |
| 168 | {{- if or .CACerts.RemoveDefaults .CACerts.Trusted }} |
| 169 | {{ with .CACerts }} |
| 170 | ca_certs: |
| 171 | {{- if .RemoveDefaults }} |
| 172 | remove_defaults: {{ .RemoveDefaults }} |
| 173 | {{- end }} |
| 174 | {{- if .Trusted}} |
| 175 | trusted: |
| 176 | {{- range $cert := .Trusted }} |
| 177 | - | |
| 178 | {{- range $line := $cert.Lines }} |
| 179 | {{ $line }} |
| 180 | {{- end }} |
| 181 | {{- end }} |
| 182 | {{- end }} |
| 183 | {{- end }} |
| 184 | {{- end }} |
| 185 | |
| 186 | {{- $nicHeal := and (eq .OS "Linux") .Networks }} |
| 187 | {{- if or .BootCmds $nicHeal }} |
| 188 | bootcmd: |
| 189 | {{- if $nicHeal }} |
| 190 | - | |
| 191 | # Work around the NIC rename race on Ubuntu 26.04 first boot (LP: #2136392): |
| 192 | # cloud-init cannot rename an already-up NIC, leaving it unconfigured. |
| 193 | # Re-attempt the rename (requires link down), then wait for the slirp NIC |
| 194 | # to be configured. No-op when interfaces already have their expected names. |
| 195 | for pair in{{range $nw := .Networks}} {{$nw.MACAddress}}={{$nw.Interface}}{{end}}; do |
| 196 | mac="${pair%=*}"; want="${pair#*=}" |
| 197 | [ -e "/sys/class/net/$want" ] && continue |
| 198 | cur=$(grep -lix "$mac" /sys/class/net/*/address 2>/dev/null | awk -F/ 'NR==1{print $(NF-1)}') |
| 199 | [ -n "$cur" ] || continue |
| 200 | for i in 1 2 3; do |
| 201 | ip link set "$cur" down && ip link set "$cur" name "$want" && ip link set "$want" up && break |
| 202 | sleep 1 |
| 203 | done |
| 204 | done |
| 205 | i=0 |
| 206 | until ip -4 addr show dev {{.SlirpNICName}} 2>/dev/null | grep -q inet || [ $((i += 1)) -gt 30 ]; do |
| 207 | sleep 1 |
| 208 | done |
| 209 | {{- end }} |
| 210 | {{- range $cmd := $.BootCmds }} |
| 211 | - | |
| 212 | # We need to embed the params.env as a here-doc because /mnt/lima-cidata is not yet mounted |
| 213 | while read -r line; do [ -n "$line" ] && export "$line"; done <<'EOF' |
| 214 | {{- range $key, $val := $.Param }} |
| 215 | PARAM_{{ $key }}={{ $val }} |
| 216 | {{- end }} |
| 217 | EOF |
| 218 | {{- range $line := $cmd.Lines }} |
| 219 | {{ $line }} |
| 220 | {{- end }} |
| 221 | {{- end }} |
| 222 | {{- end }} |