build-inside-vm.sh 5.56 KB
Newer Older
1
#!/bin/bash
2
# build-inside-vm.sh builds the images (cloud image, vagrant boxes)
Kristian Klausen's avatar
Kristian Klausen committed
3
4
5

# nounset: "Treat unset variables and parameters [...] as an error when performing parameter expansion."
# errexit: "Exit immediately if [...] command exits with a non-zero status."
6
set -o nounset -o errexit
7
shopt -s extglob
8
readonly DEFAULT_DISK_SIZE="2G"
9
readonly IMAGE="image.img"
10
# shellcheck disable=SC2016
11
readonly MIRROR='https://mirror.pkgbuild.com/$repo/os/$arch'
12

13
14
15
16
17
18
19
20
21
function init() {
  readonly ORIG_PWD="${PWD}"
  readonly OUTPUT="${PWD}/output"
  readonly TMPDIR="$(mktemp --dry-run --directory --tmpdir="${PWD}/tmp")"
  mkdir -p "${OUTPUT}" "${TMPDIR}"
  if [ -n "${SUDO_UID:-}" ]; then
    chown "${SUDO_UID}:${SUDO_GID}" "${OUTPUT}" "${TMPDIR}"
  fi
  cd "${TMPDIR}"
22

23
24
25
  readonly MOUNT="${PWD}/mount"
  mkdir "${MOUNT}"
}
26

Kristian Klausen's avatar
Kristian Klausen committed
27
# Do some cleanup when the script exits
28
function cleanup() {
Kristian Klausen's avatar
Kristian Klausen committed
29
  # We want all the commands to run, even if one of them fails.
30
31
32
33
34
35
36
37
38
39
40
41
42
43
  set +o errexit
  if [ -n "${LOOPDEV:-}" ]; then
    losetup -d "${LOOPDEV}"
  fi
  if [ -n "${MOUNT:-}" ] && mountpoint -q "${MOUNT}"; then
    # We do not want risking deleting ex: the package cache
    umount --recursive "${MOUNT}" || exit 1
  fi
  if [ -n "${TMPDIR:-}" ]; then
    rm -rf "${TMPDIR}"
  fi
}
trap cleanup EXIT

Kristian Klausen's avatar
Kristian Klausen committed
44
# Create the disk, partitions it, format the partition and mount the filesystem
45
function setup_disk() {
46
  truncate -s "${DEFAULT_DISK_SIZE}" "${IMAGE}"
47
48
49
50
51
52
  sgdisk --clear \
    --new 1::+1M --typecode=1:ef02 \
    --new 2::-0 --typecode=2:8300 \
    "${IMAGE}"

  LOOPDEV=$(losetup --find --partscan --show "${IMAGE}")
53
  # Partscan is racy
54
  wait_until_settled "${LOOPDEV}"
55
56
57
58
  mkfs.btrfs "${LOOPDEV}p2"
  mount -o compress-force=zstd "${LOOPDEV}p2" "${MOUNT}"
}

Kristian Klausen's avatar
Kristian Klausen committed
59
# Install Arch Linux to the filesystem (bootstrap)
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
function bootstrap() {
  cat <<EOF >pacman.conf
[options]
Architecture = auto

[core]
Include = mirrorlist

[extra]
Include = mirrorlist

[community]
Include = mirrorlist
EOF
  echo "Server = ${MIRROR}" >mirrorlist

  # We use the hosts package cache
Kristian Klausen's avatar
Kristian Klausen committed
77
  pacstrap -c -C pacman.conf -M "${MOUNT}" base linux grub openssh sudo btrfs-progs reflector
78
79
80
  cp mirrorlist "${MOUNT}/etc/pacman.d/"
}

Kristian Klausen's avatar
Kristian Klausen committed
81
# Cleanup the image and trim it
82
83
84
85
function image_cleanup() {
  # Remove pacman key ring for re-initialization
  rm -rf "${MOUNT}/etc/pacman.d/gnupg/"

86
87
88
89
90
91
92
93
94
  # The mkinitcpio autodetect hook removes modules not needed by the
  # running system from the initramfs. This make the image non-bootable
  # on some systems as initramfs lacks the relevant kernel modules.
  # Ex: Some systems need the virtio-scsi kernel module and not the
  # "autodetected" virtio-blk kernel module for disk access.
  #
  # So for the initial install we use the fallback initramfs, and
  # "autodetect" should add the relevant modules to the initramfs when
  # the user updates the kernel.
95
  cp --reflink=always -a "${MOUNT}/boot/"{initramfs-linux-fallback.img,initramfs-linux.img}
96

97
98
99
100
  sync -f "${MOUNT}/etc/os-release"
  fstrim --verbose "${MOUNT}"
}

101
102
103
104
# Helper function: wait until a given loop device has settled
# ${1} - loop device
function wait_until_settled() {
  udevadm settle
Kristian Klausen's avatar
Kristian Klausen committed
105
  blockdev --flushbufs --rereadpt "${1}"
106
  until test -e "${1}p2"; do
Kristian Klausen's avatar
Kristian Klausen committed
107
108
    echo "${1}p2 doesn't exist yet..."
    sleep 1
109
110
111
  done
}

Kristian Klausen's avatar
Kristian Klausen committed
112
# Mount image helper (loop device + mount)
113
114
function mount_image() {
  LOOPDEV=$(losetup --find --partscan --show "${1:-${IMAGE}}")
115
  # Partscan is racy
Kristian Klausen's avatar
Kristian Klausen committed
116
  wait_until_settled "${LOOPDEV}"
117
118
119
120
121
  mount -o compress-force=zstd "${LOOPDEV}p2" "${MOUNT}"
  # Setup bind mount to package cache
  mount --bind "/var/cache/pacman/pkg" "${MOUNT}/var/cache/pacman/pkg"
}

Kristian Klausen's avatar
Kristian Klausen committed
122
# Unmount image helper (umount + detach loop device)
123
124
125
126
127
128
function unmount_image() {
  umount --recursive "${MOUNT}"
  losetup -d "${LOOPDEV}"
  LOOPDEV=""
}

Kristian Klausen's avatar
Kristian Klausen committed
129
# Compute SHA256, adjust owner to $SUDO_UID:$SUDO_UID and move to output/
130
131
132
133
134
135
136
137
function mv_to_output() {
  sha256sum "${1}" >"${1}.SHA256"
  if [ -n "${SUDO_UID:-}" ]; then
    chown "${SUDO_UID}:${SUDO_GID}" "${1}"{,.SHA256}
  fi
  mv "${1}"{,.SHA256} "${OUTPUT}/"
}

Kristian Klausen's avatar
Kristian Klausen committed
138
# Helper function: create a new image from the "base" image
Kristian Klausen's avatar
Kristian Klausen committed
139
140
141
# ${1} - final file
# ${2} - pre
# ${3} - post
142
function create_image() {
Kristian Klausen's avatar
Kristian Klausen committed
143
144
  local tmp_image
  tmp_image="$(basename "$(mktemp -u)")"
145
146
147
148
149
150
151
152
153
154
155
156
157
  cp -a "${IMAGE}" "${tmp_image}"
  if [ -n "${DISK_SIZE}" ]; then
    truncate -s "${DISK_SIZE}" "${tmp_image}"
    sgdisk --delete 2 "${tmp_image}"
    sgdisk --move-second-header \
      --new 2::-0 --typecode=2:8300 \
      "${tmp_image}"
  fi
  mount_image "${tmp_image}"
  if [ -n "${DISK_SIZE}" ]; then
    btrfs filesystem resize max "${MOUNT}"
  fi

158
159
160
161
162
163
  if [ 0 -lt "${#PACKAGES[@]}" ]; then
    arch-chroot "${MOUNT}" /usr/bin/pacman -S --noconfirm "${PACKAGES[@]}"
  fi
  if [ 0 -lt "${#SERVICES[@]}" ]; then
    arch-chroot "${MOUNT}" /usr/bin/systemctl enable "${SERVICES[@]}"
  fi
Kristian Klausen's avatar
Kristian Klausen committed
164
  "${2}"
165
166
  image_cleanup
  unmount_image
Kristian Klausen's avatar
Kristian Klausen committed
167
168
  "${3}" "${tmp_image}" "${1}"
  mv_to_output "${1}"
169
170
}

171
# ${1} - Optional build version. If not set, will generate a default based on date.
172
173
174
175
176
function main() {
  if [ "$(id -u)" -ne 0 ]; then
    echo "root is required"
    exit 1
  fi
Kristian Klausen's avatar
Kristian Klausen committed
177
  init
178

179
180
  setup_disk
  bootstrap
181
  # shellcheck source=images/base.sh
182
  source "${ORIG_PWD}/images/base.sh"
183
  pre
184
185
  unmount_image

186
187
  local build_version
  if [ -z "${1:-}" ]; then
188
    build_version="$(date +%Y%m%d).0"
189
190
191
192
    echo "WARNING: BUILD_VERSION wasn't set!"
    echo "Falling back to $build_version"
  else
    build_version="${1}"
193
  fi
194
195

  # shellcheck source=images/common.sh
196
  source "${ORIG_PWD}/images/common.sh"
197
198
  for image in "${ORIG_PWD}/images/"!(base|common).sh; do
    # shellcheck source=/dev/null
199
    source "${image}"
200
201
    create_image "${IMAGE_NAME}" pre post
  done
202
}
203
main "$@"