golang.org/x/build@v0.0.0-20240506185731-218518f32b70/env/freebsd-amd64/make.bash (about)

     1  #!/bin/bash
     2  # Copyright 2015 The Go Authors. All rights reserved.
     3  # Use of this source code is governed by a BSD-style
     4  # license that can be found in the LICENSE file.
     5  
     6  # Builds FreeBSD image based on raw disk images provided by FreeBSD.org
     7  # This script boots the image once, side-loads GCE Go builder configuration via
     8  # an ISO mounted as the CD-ROM, and customizes the system before powering down.
     9  # SSH is enabled, and a user gopher, password gopher, is created.
    10  
    11  # Only tested on Ubuntu 20.04.
    12  # Requires packages: qemu-system-x86 qemu-img expect genisoimage
    13  
    14  set -e
    15  
    16  function download_image() {
    17    local img_dir=releases
    18    [ ${IS_SNAPSHOT:-0} -eq 1 ] && img_dir=snapshots
    19    local url=ftp://ftp.freebsd.org/pub/FreeBSD/${img_dir}/VM-IMAGES/${VERSION:?}/amd64/Latest
    20    local img_filename=FreeBSD-${VERSION:?}-amd64${VERSION_TRAILER}.raw.xz
    21    curl -O ${url}/${img_filename}
    22    echo "${SHA256}  ${img_filename}" | sha256sum -c -
    23    xz -d FreeBSD-${VERSION:?}-amd64${VERSION_TRAILER}.raw.xz
    24  }
    25  
    26  case $1 in
    27  9.3)
    28    readonly VERSION=9.3-RELEASE
    29    readonly VERSION_TRAILER="-20140711-r268512"
    30    readonly SHA256=4737218995ae056207c68f3105c0fbe655c32e8b76d2160ebfb1bba56dd5196f
    31  ;;
    32  
    33  10.3)
    34    readonly VERSION=10.3-RELEASE
    35    readonly VERSION_TRAILER=
    36    readonly SHA256=1d710ba643bf6a8ce5bff5a9d69b1657ccff83dd1f2df711d9b4e02f9aab7d06
    37  ;;
    38  10.4)
    39    readonly VERSION=10.4-RELEASE
    40    readonly VERSION_TRAILER=
    41    readonly SHA256=8d1ff92e74a70f1ec039a465467f19abd7892331403ef1d4952d271adddab625
    42  ;;
    43  11.0)
    44    readonly VERSION=11.0-RELEASE
    45    readonly VERSION_TRAILER=
    46    readonly SHA256=f9f7fcac1acfe210979a72e0642a70fcf9c9381cc1884e966eac8381c724158c
    47    ;;
    48  11.1)
    49    readonly VERSION=11.1
    50    readonly VERSION_TRAILER=
    51    readonly SHA256=233c6b269a29c1ce38bb4eb861251d1c74643846c1de937b8e31cc0316632bc0
    52  ;;
    53  11.2)
    54    readonly VERSION=11.2-RELEASE
    55    readonly VERSION_TRAILER=
    56    readonly SHA256=d8638aecbb13bdc891e17187f3932fe477f5655846bdaad8fecd60614de9312c
    57  ;;
    58  11.3)
    59    readonly VERSION=11.3-RELEASE
    60    readonly VERSION_TRAILER=
    61    readonly SHA256=e5f7fb12b828f0af7edf9464a08e51effef05ca9eb5fb52dba6d23a3c7a39223
    62  ;;
    63  11.4)
    64    readonly VERSION=11.4-RELEASE
    65    readonly VERSION_TRAILER=
    66    readonly SHA256=53a9db4dfd9c964d487d9f928754c964e2c3610c579c7f3558c745a75fa430f0
    67  ;;
    68  12.0)
    69    readonly VERSION=12.0-RELEASE
    70    readonly VERSION_TRAILER=
    71    readonly SHA256=9eb70a552f5395819904ed452a02e5805743459dbb1912ebafe4c9ae5de5eb53
    72  ;;
    73  12.1)
    74    readonly VERSION=12.1-RELEASE
    75    readonly VERSION_TRAILER=
    76    readonly SHA256=3750767f042ebf47a1e8e122b67d9cd48ec3cd2a4a60b724e64c4ff6ba33653a
    77  ;;
    78  12.2)
    79    readonly VERSION=12.2-RELEASE
    80    readonly VERSION_TRAILER=
    81    readonly SHA256=0f8593382b6833658c6f6be532d4ffbedde7b75504452e27d912a0183f72ab56
    82  ;;
    83  12.3-SNAPSHOT)
    84    readonly IS_SNAPSHOT=1
    85    readonly VERSION=12.3-STABLE
    86    readonly VERSION_TRAILER=
    87    readonly SHA256=e52ddb31b8f2ff7f8d03571b65e472cde3f92ef6b73ddaa5e7f5d4668ce4394c # https://lists.freebsd.org/archives/freebsd-snapshots/2021-December/000037.html
    88  ;;
    89  13.0)
    90    readonly VERSION=13.0-RELEASE
    91    readonly VERSION_TRAILER=
    92    readonly SHA256=48288a693215a88b26ec81b2648de7433acec7db491aaeaed49c0ffd1612d345
    93    readonly BLIND_SWITCH_CONSOLE=1
    94  ;;
    95  13.0-SNAPSHOT)
    96    readonly IS_SNAPSHOT=1
    97    readonly VERSION=13.0-STABLE
    98    readonly VERSION_TRAILER=
    99    readonly SHA256=7b8fcc2330c8d9f66dd012c5859167d56c227ece39188c8f55b2bddbf688875f # https://lists.freebsd.org/archives/freebsd-snapshots/2021-December/000036.html
   100    readonly BLIND_SWITCH_CONSOLE=1
   101  ;;
   102  *)
   103    echo "Usage: $0 <version>"
   104    echo " version - FreeBSD version to build. Valid choices: 9.3 10.3 10.4 11.0 11.1 11.2 11.3 11.4 12.0 12.1 12.2 13.0 13.0-SNAPSHOT"
   105    exit 1
   106  esac
   107  
   108  function cleanup() {
   109  	rm -rf iso \
   110  		*.iso \
   111  		*.raw \
   112  		*.qcow2
   113  }
   114  
   115  trap cleanup EXIT
   116  
   117  if ! [ -e FreeBSD-${VERSION:?}-amd64.raw ]; then
   118    download_image
   119  fi
   120  
   121  qemu-img create -f qcow2 -b FreeBSD-${VERSION:?}-amd64${VERSION_TRAILER}.raw -F raw disk.qcow2 16G
   122  
   123  mkdir -p iso/boot iso/etc iso/usr/local/etc/rc.d
   124  cp loader.conf iso/boot
   125  cp rc.conf iso/etc
   126  cp sysctl.conf iso/etc
   127  cp buildlet iso/usr/local/etc/rc.d
   128  
   129  cat >iso/install.sh <<'EOF'
   130  #!/bin/sh
   131  set -x
   132  
   133  mkdir -p /usr/local/etc/rc.d/
   134  cp /mnt/usr/local/etc/rc.d/buildlet /usr/local/etc/rc.d/buildlet
   135  chmod +x /usr/local/etc/rc.d/buildlet
   136  cp /mnt/boot/loader.conf /boot/loader.conf
   137  cp /mnt/etc/rc.conf /etc/rc.conf
   138  cat /mnt/etc/sysctl.conf >> /etc/sysctl.conf
   139  adduser -f - <<ADDUSEREOF
   140  gopher::::::Gopher Gopherson::/bin/sh:gopher
   141  ADDUSEREOF
   142  pw user mod gopher -G wheel
   143  
   144  # Enable serial console early in boot process.
   145  echo '-h' > /boot.conf
   146  EOF
   147  
   148  genisoimage -r -o config.iso iso/
   149  # TODO(wathiede): remove sleep
   150  sleep 2
   151  
   152  env DOWNLOAD_UPDATES=$((1-IS_SNAPSHOT)) BLIND_SWITCH_CONSOLE=${BLIND_SWITCH_CONSOLE:=0} expect <<'EOF'
   153  set prompt "root@.*:~ #[ ]"
   154  set timeout -1
   155  set send_human {.1 .3 1 .05 2}
   156  
   157  spawn qemu-system-x86_64 -machine graphics=off -display none -serial stdio \
   158   -fw_cfg name=opt/etc/sercon-port,string=0x3F8 \
   159   -m 1G -drive if=virtio,file=disk.qcow2,format=qcow2,cache=none -cdrom config.iso -net nic,model=virtio -net user
   160  set qemu_pid $spawn_id
   161  
   162  if {$::env(BLIND_SWITCH_CONSOLE)} {
   163      # simulate a user interacting with the boot menu:
   164      # hit "5" (Cons: menu) to switch from Video (Video -> Dual (Serial primary) -> Dual (Video primary) -> Serial),
   165      # to toggle the serial console, then hit "return" to boot
   166      expect -ex "Booting from Hard Disk..."
   167      expect -ex "/"
   168      expect -ex "\n"
   169      send -h " " ;# stop the autoboot timer
   170      send -h 5
   171      sleep 1
   172      send -h "\r"
   173      expect -ex "---<<BOOT>>---"
   174  } else {
   175      # boot with serial console enabled
   176      expect -ex "Welcome to FreeBSD"
   177      expect -re "Autoboot in \[0-9\]\+ seconds"
   178      sleep 1
   179      send -h "3" ;# escape to bootloader prompt
   180      expect -ex "Type '?' for a list of commands, 'help' for more detailed help."
   181      expect -ex "OK "
   182      send -h "set console=\"comconsole\"\n"
   183      expect -ex "OK "
   184      send -h "boot\n"
   185  }
   186  
   187  # wait for login prompt
   188  set timeout 180
   189  expect {
   190      "\nlogin: " {
   191          send "root\n"
   192          sleep 1
   193      }
   194      timeout     { exit 2 }
   195  }
   196  
   197  expect -re $prompt
   198  sleep 1
   199  send "mount_cd9660 /dev/cd0 /mnt\nsh /mnt/install.sh\n"
   200  
   201  expect -re $prompt
   202  sleep 1
   203  
   204  # generate SSH keys
   205  send "service sshd keygen\n"
   206  expect -re "Generating .+ host key."
   207  sleep 1
   208  
   209  expect -re $prompt
   210  sleep 1
   211  set timeout -1
   212  # download updates
   213  if {$::env(DOWNLOAD_UPDATES)} {
   214      send "env PAGER=cat freebsd-update fetch --not-running-from-cron\n"
   215  
   216      expect {
   217          "The following files will be updated as part of updating to" {
   218              sleep 2
   219              expect -re $prompt
   220              send "freebsd-update install\n"
   221              expect "Installing updates..."
   222              expect "done."
   223              sleep 1
   224              send "\n"
   225          }
   226  
   227          "No updates needed to update system to" {
   228              sleep 1
   229              send "\n"
   230          }
   231  
   232          "No mirrors remaining, giving up." { exit 3 }
   233      }
   234  } else {
   235      puts "skipping updates"
   236      send "\n"
   237  }
   238  
   239  expect -re $prompt
   240  sleep 1
   241  send "pkg install -y bash curl git gdb\n"
   242  
   243  expect -re $prompt
   244  send "sync\n"
   245  
   246  expect -re $prompt
   247  send "poweroff\n"
   248  expect "All buffers synced."
   249  
   250  wait -i $qemu_pid
   251  EOF
   252  
   253  # Create Compute Engine disk image.
   254  IMAGE=freebsd-amd64-${VERSION/-RELEASE/}.tar.gz
   255  readonly IMAGE=${IMAGE/\./}
   256  echo "Archiving disk.raw as ${IMAGE:?}... (this may take a while)"
   257  qemu-img convert -f qcow2 -O raw -t none -T none disk.qcow2 disk.raw
   258  tar -Szcf ${IMAGE:?} disk.raw
   259  
   260  echo "Done. GCE image is ${IMAGE:?}"