summaryrefslogtreecommitdiff
path: root/net/test/build_rootfs.sh
blob: ee79c867c46fda7726c87560908cbb60b9127ebb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
#!/bin/bash
#
# Copyright (C) 2018 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

set -e
set -u

SCRIPT_DIR=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd -P)

usage() {
  echo -n "usage: $0 [-h] [-s bullseye|bullseye-cuttlefish|bullseye-rockpi|bullseye-server] "
  echo -n "[-a i386|amd64|armhf|arm64] -k /path/to/kernel "
  echo -n "-i /path/to/initramfs.gz [-d /path/to/dtb:subdir] "
  echo "[-m http://mirror/debian] [-n rootfs|disk] [-r initrd] [-e] [-g]"
  exit 1
}

mirror=http://ftp.debian.org/debian
embed_kernel_initrd_dtb=0
install_grub=0
suite=bullseye
arch=amd64

dtb_subdir=
initramfs=
kernel=
ramdisk=
disk=
dtb=

while getopts ":hs:a:m:n:r:k:i:d:eg" opt; do
  case "${opt}" in
    h)
      usage
      ;;
    s)
      if [[ "${OPTARG%-*}" != "bullseye" ]]; then
        echo "Invalid suite: ${OPTARG}" >&2
        usage
      fi
      suite="${OPTARG}"
      ;;
    a)
      arch="${OPTARG}"
      ;;
    m)
      mirror="${OPTARG}"
      ;;
    n)
      disk="${OPTARG}"
      ;;
    r)
      ramdisk="${OPTARG}"
      ;;
    k)
      kernel="${OPTARG}"
      ;;
    i)
      initramfs="${OPTARG}"
      ;;
    d)
      dtb="${OPTARG%:*}"
      if [ "${OPTARG#*:}" != "${dtb}" ]; then
        dtb_subdir="${OPTARG#*:}/"
      fi
      ;;
    e)
      embed_kernel_initrd_dtb=1
      ;;
    g)
      install_grub=1
      ;;
    \?)
      echo "Invalid option: ${OPTARG}" >&2
      usage
      ;;
    :)
      echo "Invalid option: ${OPTARG} requires an argument" >&2
      usage
      ;;
  esac
done

# Disable Debian's "persistent" network device renaming
cmdline="net.ifnames=0 rw 8250.nr_uarts=2 PATH=/usr/sbin:/bin:/usr/bin"
cmdline="${cmdline} embed_kernel_initrd_dtb=${embed_kernel_initrd_dtb}"
cmdline="${cmdline} install_grub=${install_grub}"

case "${arch}" in
  i386)
    cmdline="${cmdline} console=ttyS0 exitcode=/dev/ttyS1"
    machine="pc-i440fx-2.8,accel=kvm"
    qemu="qemu-system-i386"
    partguid="8303"
    cpu="max"
    ;;
  amd64)
    cmdline="${cmdline} console=ttyS0 exitcode=/dev/ttyS1"
    machine="pc-i440fx-2.8,accel=kvm"
    qemu="qemu-system-x86_64"
    partguid="8304"
    cpu="max"
    ;;
  armhf)
    cmdline="${cmdline} console=ttyAMA0 exitcode=/dev/ttyS0"
    machine="virt,gic-version=2"
    qemu="qemu-system-arm"
    partguid="8307"
    cpu="cortex-a15"
    ;;
  arm64)
    cmdline="${cmdline} console=ttyAMA0 exitcode=/dev/ttyS0"
    machine="virt,gic-version=2"
    qemu="qemu-system-aarch64"
    partguid="8305"
    cpu="cortex-a53" # "max" is too slow
    ;;
  *)
    echo "Invalid arch: ${OPTARG}" >&2
    usage
    ;;
esac

if [[ -z "${disk}" ]]; then
  if [[ "${install_grub}" = "1" ]]; then
    base_image_name=disk
  else
    base_image_name=rootfs
  fi
  disk="${base_image_name}.${arch}.${suite}.$(date +%Y%m%d)"
fi
disk=$(realpath "${disk}")

if [[ -z "${ramdisk}" ]]; then
  ramdisk="initrd.${arch}.${suite}.$(date +%Y%m%d)"
fi
ramdisk=$(realpath "${ramdisk}")

if [[ -z "${kernel}" ]]; then
  echo "$0: Path to kernel image must be specified (with '-k')"
  usage
elif [[ ! -e "${kernel}" ]]; then
  echo "$0: Kernel image not found at '${kernel}'"
  exit 2
fi

if [[ -z "${initramfs}" ]]; then
  echo "Path to initial ramdisk image must be specified (with '-i')"
  usage
elif [[ ! -e "${initramfs}" ]]; then
  echo "Initial ramdisk image not found at '${initramfs}'"
  exit 3
fi

# Sometimes it isn't obvious when the script fails
failure() {
  echo "Filesystem generation process failed." >&2
  rm -f "${disk}" "${ramdisk}"
}
trap failure ERR

# Import the package list for this release
packages=$(cpp "${SCRIPT_DIR}/rootfs/${suite}.list" | grep -v "^#" | xargs | tr -s ' ' ',')

# For the debootstrap intermediates
tmpdir=$(mktemp -d)
tmpdir_remove() {
  echo "Removing temporary files.." >&2
  sudo rm -rf "${tmpdir}"
}
trap tmpdir_remove EXIT

workdir="${tmpdir}/_"
mkdir "${workdir}"
chmod 0755 "${workdir}"
sudo chown root:root "${workdir}"

# Run the debootstrap first
cd "${workdir}"

retries=5
while ! sudo debootstrap --arch="${arch}" --variant=minbase --include="${packages}" \
        --foreign "${suite%-*}" . "${mirror}"; do
    retries=$((${retries} - 1))
    if [ ${retries} -le 0 ]; then
	failure
	exit 1
    fi
    echo "debootstrap failed - trying again - ${retries} retries left"
done

# Copy some bootstrapping scripts into the rootfs
sudo cp -a "${SCRIPT_DIR}"/rootfs/*.sh root/
sudo cp -a "${SCRIPT_DIR}"/rootfs/net_test.sh sbin/net_test.sh
sudo chown root:root sbin/net_test.sh

# Extract the ramdisk to bootstrap with to /
lz4 -lcd "${initramfs}" | sudo cpio -idum lib/modules/*

# Create /host, for the pivot_root and 9p mount use cases
sudo mkdir host

# debootstrap workaround: Run debootstrap in docker sometimes causes the
# /proc being a symlink in first stage. We need to fix the symlink to an empty
# directory.
if [ -L "${workdir}/proc" ]; then
  echo "/proc in debootstrap 1st stage is a symlink. Fixed!"
  sudo rm -f "${workdir}/proc"
  sudo mkdir "${workdir}/proc"
fi

# Leave the workdir, to build the filesystem
cd -

# For the initial ramdisk, and later for the final rootfs
mount=$(mktemp -d)
mount_remove() {
  rmdir "${mount}"
  tmpdir_remove
}
trap mount_remove EXIT

# The initial ramdisk filesystem must be <=512M, or QEMU's -initrd
# option won't touch it
initrd=$(mktemp)
initrd_remove() {
  rm -f "${initrd}"
  mount_remove
}
trap initrd_remove EXIT
truncate -s 512M "${initrd}"
/sbin/mke2fs -F -t ext4 -L ROOT "${initrd}"

# Mount the new filesystem locally
sudo mount -o loop -t ext4 "${initrd}" "${mount}"
image_unmount() {
  sudo umount "${mount}"
  initrd_remove
}
trap image_unmount EXIT

# Copy the patched debootstrap results into the new filesystem
sudo cp -a "${workdir}"/* "${mount}"
sudo rm -rf "${workdir}"

# Unmount the initial ramdisk
sudo umount "${mount}"
trap initrd_remove EXIT

if [[ "${install_grub}" = 1 ]]; then
  part_num=0
  # $1 partition size
  # $2 gpt partition type
  # $3 partition name
  # $4 bypass alignment checks (use on <1MB partitions only)
  # $5 partition attribute bit to set
  sgdisk() {
    part_num=$((part_num+1))
    [[ -n "${4:-}" ]] && prefix="-a1" || prefix=
    [[ -n "${5:-}" ]] && suffix="-A:${part_num}:set:$5" || suffix=
    /sbin/sgdisk ${prefix} \
      "-n:${part_num}:$1" "-t:${part_num}:$2" "-c:${part_num}:$3" \
      ${suffix} "${disk}" >/dev/null 2>&1
  }
  # If there's a bootloader, we need to make space for the GPT header, GPT
  # footer and EFI system partition (legacy boot is not supported)
  # Keep this simple - modern gdisk reserves 1MB for the GPT header and
  # assumes all partitions are 1MB aligned
  truncate -s "$((1 + 128 + 10 * 1024 + 1))M" "${disk}"
  /sbin/sgdisk --zap-all "${disk}" >/dev/null 2>&1
  # On RockPi devices, steal a bit of space at the start of the disk for
  # some special bootloader partitions. Some of these have to start/end
  # at specific offsets as well
  if [[ "${suite#*-}" = "rockpi" ]]; then
    # See https://opensource.rock-chips.com/wiki_Boot_option
    # Keep in sync with rootfs/*-rockpi.sh
    sgdisk "64:8127"   "8301"        "idbloader" "true"
    sgdisk "8128:+64"  "8301"        "uboot_env" "true"
    sgdisk "8M:+4M"    "8301"        "uboot"
    sgdisk "12M:+4M"   "8301"        "trust"
    sgdisk "16M:+1M"   "8301"        "misc"
    sgdisk "17M:+128M" "ef00"        "esp"       ""     "0"
    sgdisk "145M:0"    "8305"        "rootfs"    ""     "2"
    system_partition="6"
    rootfs_partition="7"
  else
    sgdisk "0:+128M"   "ef00"        "esp"       ""     "0"
    sgdisk "0:0"       "${partguid}" "rootfs"    ""     "2"
    system_partition="1"
    rootfs_partition="2"
  fi

  # Create an empty EFI system partition; it will be initialized later
  system_partition_start=$(partx -g -o START -s -n "${system_partition}" "${disk}" | xargs)
  system_partition_end=$(partx -g -o END -s -n "${system_partition}" "${disk}" | xargs)
  system_partition_num_sectors=$((${system_partition_end} - ${system_partition_start} + 1))
  system_partition_num_vfat_blocks=$((${system_partition_num_sectors} / 2))
  /sbin/mkfs.vfat -n SYSTEM -F 16 --offset=${system_partition_start} "${disk}" ${system_partition_num_vfat_blocks} >/dev/null
  # Copy the rootfs to just after the EFI system partition
  rootfs_partition_start=$(partx -g -o START -s -n "${rootfs_partition}" "${disk}" | xargs)
  rootfs_partition_end=$(partx -g -o END -s -n "${rootfs_partition}" "${disk}" | xargs)
  rootfs_partition_num_sectors=$((${rootfs_partition_end} - ${rootfs_partition_start} + 1))
  rootfs_partition_offset=$((${rootfs_partition_start} * 512))
  rootfs_partition_size=$((${rootfs_partition_num_sectors} * 512))
  dd if="${initrd}" of="${disk}" bs=512 seek="${rootfs_partition_start}" conv=fsync,notrunc 2>/dev/null
  /sbin/e2fsck -p -f "${disk}"?offset=${rootfs_partition_offset} || true
  disksize=$(stat -c %s "${disk}")
  /sbin/resize2fs "${disk}"?offset=${rootfs_partition_offset} ${rootfs_partition_num_sectors}s
  truncate -s "${disksize}" "${disk}"
  /sbin/sgdisk -e "${disk}"
  /sbin/e2fsck -p -f "${disk}"?offset=${rootfs_partition_offset} || true
  /sbin/e2fsck -fy "${disk}"?offset=${rootfs_partition_offset} || true
else
  # If there's no bootloader, the initrd is the disk image
  cp -a "${initrd}" "${disk}"
  truncate -s 10G "${disk}"
  /sbin/e2fsck -p -f "${disk}" || true
  /sbin/resize2fs "${disk}"
  system_partition=
  rootfs_partition="raw"
fi

# Create another fake block device for initrd.img writeout
raw_initrd=$(mktemp)
raw_initrd_remove() {
  rm -f "${raw_initrd}"
  initrd_remove
}
trap raw_initrd_remove EXIT
truncate -s 64M "${raw_initrd}"

# Get number of cores for qemu. Restrict the maximum value to 8.
qemucpucores=$(nproc)
if [[ ${qemucpucores} -gt 8 ]]; then
  qemucpucores=8
fi

# Complete the bootstrap process using QEMU and the specified kernel
${qemu} -machine "${machine}" -cpu "${cpu}" -m 2048 >&2 \
  -kernel "${kernel}" -initrd "${initrd}" -no-user-config -nodefaults \
  -no-reboot -display none -nographic -serial stdio -parallel none \
  -smp "${qemucpucores}",sockets="${qemucpucores}",cores=1,threads=1 \
  -object rng-random,id=objrng0,filename=/dev/urandom \
  -device virtio-rng-pci-non-transitional,rng=objrng0,id=rng0,max-bytes=1024,period=2000 \
  -drive file="${disk}",format=raw,if=none,aio=threads,id=drive-virtio-disk0 \
  -device virtio-blk-pci-non-transitional,scsi=off,drive=drive-virtio-disk0 \
  -drive file="${raw_initrd}",format=raw,if=none,aio=threads,id=drive-virtio-disk1 \
  -device virtio-blk-pci-non-transitional,scsi=off,drive=drive-virtio-disk1 \
  -chardev file,id=exitcode,path=exitcode \
  -device pci-serial,chardev=exitcode \
  -append "root=/dev/ram0 ramdisk_size=524288 init=/root/stage1.sh ${cmdline}"
[[ -s exitcode ]] && exitcode=$(cat exitcode | tr -d '\r') || exitcode=2
rm -f exitcode
if [ "${exitcode}" != "0" ]; then
  echo "Second stage debootstrap failed (err=${exitcode})"
  exit "${exitcode}"
fi

# Fix up any issues from the unclean shutdown
if [[ ${rootfs_partition} = "raw" ]]; then
    sudo e2fsck -p -f "${disk}" || true
else
    rootfs_partition_start=$(partx -g -o START -s -n "${rootfs_partition}" "${disk}" | xargs)
    rootfs_partition_end=$(partx -g -o END -s -n "${rootfs_partition}" "${disk}" | xargs)
    rootfs_partition_num_sectors=$((${rootfs_partition_end} - ${rootfs_partition_start} + 1))
    rootfs_partition_offset=$((${rootfs_partition_start} * 512))
    rootfs_partition_tempfile2=$(mktemp)
    dd if="${disk}" of="${rootfs_partition_tempfile2}" bs=512 skip=${rootfs_partition_start} count=${rootfs_partition_num_sectors}
    e2fsck -p -f "${rootfs_partition_tempfile2}" || true
    dd if="${rootfs_partition_tempfile2}" of="${disk}" bs=512 seek=${rootfs_partition_start} count=${rootfs_partition_num_sectors} conv=fsync,notrunc
    rm -f "${rootfs_partition_tempfile2}"
    e2fsck -fy "${disk}"?offset=${rootfs_partition_offset} || true
fi
if [[ -n "${system_partition}" ]]; then
  system_partition_start=$(partx -g -o START -s -n "${system_partition}" "${disk}" | xargs)
  system_partition_end=$(partx -g -o END -s -n "${system_partition}" "${disk}" | xargs)
  system_partition_num_sectors=$((${system_partition_end} - ${system_partition_start} + 1))
  system_partition_offset=$((${system_partition_start} * 512))
  system_partition_size=$((${system_partition_num_sectors} * 512))
  system_partition_tempfile=$(mktemp)
  dd if="${disk}" of="${system_partition_tempfile}" bs=512 skip=${system_partition_start} count=${system_partition_num_sectors}
  /sbin/fsck.vfat -a "${system_partition_tempfile}" || true
  dd if="${system_partition_tempfile}" of="${disk}" bs=512 seek=${system_partition_start} count=${system_partition_num_sectors} conv=fsync,notrunc
  rm -f "${system_partition_tempfile}"
fi

# New workdir for the initrd extraction
workdir="${tmpdir}/initrd"
mkdir "${workdir}"
chmod 0755 "${workdir}"
sudo chown root:root "${workdir}"

# Change into workdir to repack initramfs
cd "${workdir}"

# Process the initrd to remove kernel-specific metadata
kernel_version=$(basename $(lz4 -lcd "${raw_initrd}" | sudo cpio -idumv 2>&1 | grep usr/lib/modules/ - | head -n1))
lz4 -lcd "${raw_initrd}" | sudo cpio -idumv
sudo rm -rf usr/lib/modules
sudo mkdir -p usr/lib/modules

# Debian symlinks /usr/lib to /lib, but we'd prefer the other way around
# so that it more closely matches what happens in Android initramfs images.
# This enables 'cat ramdiskA.img ramdiskB.img >ramdiskC.img' to "just work".
sudo rm -f lib
sudo mv usr/lib lib
sudo ln -s /lib usr/lib

# Repack the ramdisk to the final output
find * | sudo cpio -H newc -o --quiet | lz4 -lc9 >"${ramdisk}"

# Pack another ramdisk with the combined artifacts, for boot testing
cat "${ramdisk}" "${initramfs}" >"${initrd}"

# Leave workdir to boot-test combined initrd
cd -

rootfs_partition_tempfile=$(mktemp)
# Mount the new filesystem locally
if [[ ${rootfs_partition} = "raw" ]]; then
    sudo mount -o loop -t ext4 "${disk}" "${mount}"
else
    rootfs_partition_start=$(partx -g -o START -s -n "${rootfs_partition}" "${disk}" | xargs)
    rootfs_partition_offset=$((${rootfs_partition_start} * 512))
    rootfs_partition_end=$(partx -g -o END -s -n "${rootfs_partition}" "${disk}" | xargs)
    rootfs_partition_num_sectors=$((${rootfs_partition_end} - ${rootfs_partition_start} + 1))
    dd if="${disk}" of="${rootfs_partition_tempfile}" bs=512 skip=${rootfs_partition_start} count=${rootfs_partition_num_sectors}
fi
image_unmount2() {
  sudo umount "${mount}"
  raw_initrd_remove
}
if [[ ${rootfs_partition} = "raw" ]]; then
    trap image_unmount2 EXIT
fi

# Embed the kernel and dtb images now, if requested
if [[ ${rootfs_partition} = "raw" ]]; then
    if [[ "${embed_kernel_initrd_dtb}" = "1" ]]; then
	if [ -n "${dtb}" ]; then
	    sudo mkdir -p "${mount}/boot/dtb/${dtb_subdir}"
	    sudo cp -a "${dtb}" "${mount}/boot/dtb/${dtb_subdir}"
	    sudo chown -R root:root "${mount}/boot/dtb/${dtb_subdir}"
	fi
	sudo cp -a "${kernel}" "${mount}/boot/vmlinuz-${kernel_version}"
	sudo chown root:root "${mount}/boot/vmlinuz-${kernel_version}"
    fi
else
    if [[ "${embed_kernel_initrd_dtb}" = "1" ]]; then
	if [ -n "${dtb}" ]; then
	    e2mkdir -G 0 -O 0 "${rootfs_partition_tempfile}":"/boot/dtb/${dtb_subdir}"
	    e2cp -G 0 -O 0 "${dtb}" "${rootfs_partition_tempfile}":"/boot/dtb/${dtb_subdir}"
	fi
	e2cp -G 0 -O 0 "${kernel}" "${rootfs_partition_tempfile}":"/boot/vmlinuz-${kernel_version}"
    fi
fi

# Unmount the initial ramdisk
if [[ ${rootfs_partition} = "raw" ]]; then
    sudo umount "${mount}"
else
    rootfs_partition_start=$(partx -g -o START -s -n "${rootfs_partition}" "${disk}" | xargs)
    rootfs_partition_end=$(partx -g -o END -s -n "${rootfs_partition}" "${disk}" | xargs)
    rootfs_partition_num_sectors=$((${rootfs_partition_end} - ${rootfs_partition_start} + 1))
    dd if="${rootfs_partition_tempfile}" of="${disk}" bs=512 seek=${rootfs_partition_start} count=${rootfs_partition_num_sectors} conv=fsync,notrunc
fi
rm -f "${rootfs_partition_tempfile}"
trap raw_initrd_remove EXIT

# Boot test the new system and run stage 3
${qemu} -machine "${machine}" -cpu "${cpu}" -m 2048 >&2 \
  -kernel "${kernel}" -initrd "${initrd}" -no-user-config -nodefaults \
  -no-reboot -display none -nographic -serial stdio -parallel none \
  -smp "${qemucpucores}",sockets="${qemucpucores}",cores=1,threads=1 \
  -object rng-random,id=objrng0,filename=/dev/urandom \
  -device virtio-rng-pci-non-transitional,rng=objrng0,id=rng0,max-bytes=1024,period=2000 \
  -drive file="${disk}",format=raw,if=none,aio=threads,id=drive-virtio-disk0 \
  -device virtio-blk-pci-non-transitional,scsi=off,drive=drive-virtio-disk0 \
  -chardev file,id=exitcode,path=exitcode \
  -device pci-serial,chardev=exitcode \
  -netdev user,id=usernet0,ipv6=off \
  -device virtio-net-pci-non-transitional,netdev=usernet0,id=net0 \
  -append "root=LABEL=ROOT init=/root/${suite}.sh ${cmdline}"
[[ -s exitcode ]] && exitcode=$(cat exitcode | tr -d '\r') || exitcode=2
rm -f exitcode
if [ "${exitcode}" != "0" ]; then
  echo "Root filesystem finalization failed (err=${exitcode})"
  exit "${exitcode}"
fi

# Fix up any issues from the unclean shutdown
if [[ ${rootfs_partition} = "raw" ]]; then
    sudo e2fsck -p -f "${disk}" || true
else
    rootfs_partition_start=$(partx -g -o START -s -n "${rootfs_partition}" "${disk}" | xargs)
    rootfs_partition_end=$(partx -g -o END -s -n "${rootfs_partition}" "${disk}" | xargs)
    rootfs_partition_num_sectors=$((${rootfs_partition_end} - ${rootfs_partition_start} + 1))
    rootfs_partition_offset=$((${rootfs_partition_start} * 512))
    rootfs_partition_tempfile2=$(mktemp)
    dd if="${disk}" of="${rootfs_partition_tempfile2}" bs=512 skip=${rootfs_partition_start} count=${rootfs_partition_num_sectors}
    e2fsck -p -f "${rootfs_partition_tempfile2}" || true
    dd if="${rootfs_partition_tempfile2}" of="${disk}" bs=512 seek=${rootfs_partition_start} count=${rootfs_partition_num_sectors} conv=fsync,notrunc
    rm -f "${rootfs_partition_tempfile2}"
    e2fsck -fy "${disk}"?offset=${rootfs_partition_offset} || true
fi
if [[ -n "${system_partition}" ]]; then
  system_partition_start=$(partx -g -o START -s -n "${system_partition}" "${disk}" | xargs)
  system_partition_end=$(partx -g -o END -s -n "${system_partition}" "${disk}" | xargs)
  system_partition_num_sectors=$((${system_partition_end} - ${system_partition_start} + 1))
  system_partition_offset=$((${system_partition_start} * 512))
  system_partition_size=$((${system_partition_num_sectors} * 512))
  system_partition_tempfile=$(mktemp)
  dd if="${disk}" of="${system_partition_tempfile}" bs=512 skip=${system_partition_start} count=${system_partition_num_sectors}
  /sbin/fsck.vfat -a "${system_partition_tempfile}" || true
  dd if="${system_partition_tempfile}" of="${disk}" bs=512 seek=${system_partition_start} count=${system_partition_num_sectors} conv=fsync,notrunc
  rm -f "${system_partition_tempfile}"
fi

# Mount the final disk image locally
if [[ ${rootfs_partition} = "raw" ]]; then
    sudo mount -o loop -t ext4 "${disk}" "${mount}"
else
    rootfs_partition_start=$(partx -g -o START -s -n "${rootfs_partition}" "${disk}" | xargs)
    rootfs_partition_offset=$((${rootfs_partition_start} * 512))
    sudo mount -o loop,offset=${rootfs_partition_offset} -t ext4 "${disk}" "${mount}"
fi
image_unmount3() {
  sudo umount "${mount}"
  raw_initrd_remove
}
trap image_unmount3 EXIT

# Fill the rest of the space with zeroes, to optimize compression
sudo dd if=/dev/zero of="${mount}/sparse" bs=1M 2>/dev/null || true
sudo rm -f "${mount}/sparse"

echo "Debian ${suite} for ${arch} filesystem generated at '${disk}'."
echo "Initial ramdisk generated at '${ramdisk}'."