build-inside-vm.sh 4.77 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 DISK_SIZE="20G"
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
46
47
48
49
50
51
52
function setup_disk() {
  truncate -s "${DISK_SIZE}" "${IMAGE}"
  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
77
  pacstrap -c -C pacman.conf -M "${MOUNT}" base linux grub openssh sudo haveged 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
86
87
88
89
function image_cleanup() {
  # Remove pacman key ring for re-initialization
  rm -rf "${MOUNT}/etc/pacman.d/gnupg/"

  sync -f "${MOUNT}/etc/os-release"
  fstrim --verbose "${MOUNT}"
}

90
91
92
93
# 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
94
  blockdev --flushbufs --rereadpt "${1}"
95
  until test -e "${1}p2"; do
Kristian Klausen's avatar
Kristian Klausen committed
96
97
    echo "${1}p2 doesn't exist yet..."
    sleep 1
98
99
100
  done
}

Kristian Klausen's avatar
Kristian Klausen committed
101
# Mount image helper (loop device + mount)
102
103
function mount_image() {
  LOOPDEV=$(losetup --find --partscan --show "${1:-${IMAGE}}")
104
  # Partscan is racy
Kristian Klausen's avatar
Kristian Klausen committed
105
  wait_until_settled "${LOOPDEV}"
106
107
108
109
110
  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
111
# Unmount image helper (umount + detach loop device)
112
113
114
115
116
117
function unmount_image() {
  umount --recursive "${MOUNT}"
  losetup -d "${LOOPDEV}"
  LOOPDEV=""
}

Kristian Klausen's avatar
Kristian Klausen committed
118
# Copy image and mount the copied image
119
120
121
122
123
function copy_and_mount_image() {
  cp -a "${IMAGE}" "${1}"
  mount_image "${1}"
}

Kristian Klausen's avatar
Kristian Klausen committed
124
# Compute SHA256, adjust owner to $SUDO_UID:$SUDO_UID and move to output/
125
126
127
128
129
130
131
132
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
133
# Helper function: create a new image from the "base" image
Kristian Klausen's avatar
Kristian Klausen committed
134
135
136
# ${1} - final file
# ${2} - pre
# ${3} - post
137
function create_image() {
Kristian Klausen's avatar
Kristian Klausen committed
138
139
  local tmp_image
  tmp_image="$(basename "$(mktemp -u)")"
Kristian Klausen's avatar
Kristian Klausen committed
140
  copy_and_mount_image "${tmp_image}"
141
142
143
144
145
146
  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
147
  "${2}"
148
149
  image_cleanup
  unmount_image
Kristian Klausen's avatar
Kristian Klausen committed
150
151
  "${3}" "${tmp_image}" "${1}"
  mv_to_output "${1}"
152
153
}

154
# ${1} - Optional build version. If not set, will generate a default based on date.
155
156
157
158
159
function main() {
  if [ "$(id -u)" -ne 0 ]; then
    echo "root is required"
    exit 1
  fi
Kristian Klausen's avatar
Kristian Klausen committed
160
  init
161

162
163
  setup_disk
  bootstrap
164
  # shellcheck source=images/base.sh
165
  source "${ORIG_PWD}/images/base.sh"
166
  pre
167
168
  unmount_image

169
170
  local build_version
  if [ -z "${1:-}" ]; then
171
    build_version="$(date +%Y%m%d).0"
172
173
174
175
    echo "WARNING: BUILD_VERSION wasn't set!"
    echo "Falling back to $build_version"
  else
    build_version="${1}"
176
  fi
177
178

  # shellcheck source=images/common.sh
179
  source "${ORIG_PWD}/images/common.sh"
180
181
  for image in "${ORIG_PWD}/images/"!(base|common).sh; do
    # shellcheck source=/dev/null
182
    source "${image}"
183
184
    create_image "${IMAGE_NAME}" pre post
  done
185
}
186
main "$@"