-
-
Notifications
You must be signed in to change notification settings - Fork 97
/
alpine-make-vm-image
executable file
·744 lines (623 loc) · 26.2 KB
/
alpine-make-vm-image
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
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
#!/bin/sh
# vim: set ts=4 sw=4:
# SPDX-FileCopyrightText: © 2017 Jakub Jirutka <jakub@jirutka.cz>
# SPDX-License-Identifier: MIT
#---help---
# Usage: alpine-make-vm-image [options] [--] <image> [<script> [<script-opts...>]]
#
# This script creates a bootable Alpine Linux disk image for virtual machines.
# If running on Alpine system (detected by file /etc/alpine-release), then it
# also installs needed packages on the host system. On other systems you must
# install them yourself: qemu-img, qemu-nbd, rsync, sfdisk, dosfstools (for UEFI
# mode) and mkfs utility for the chosen ROOTFS. If $APK is not available on the
# host system, then static apk-tools specified by $APK_TOOLS_URI is downloaded
# and used.
#
# Note that in BIOS mode, it does not create any partitions by default (it's
# really not needed), filesystem is created directly on the image. If you must
# use some platform that foolishly requires partitions in images, use
# --partition.
#
# Arguments:
# <image> Path of block device or disk image file to use or create
# if it does not exist.
#
# <script> Path of script to execute after installing base system in
# the mounted image and before umounting it.
#
# <script-opts> Arguments to pass to the script.
#
# Options and Environment Variables:
# -a --arch ARCH Target CPU architecture. If it doesn't match the host's
# architecture, you must register an interpreter (emulator)
# for the target architecture in binfmt, e.g. qemu userspace
# emulator. Options: x86_64, aarch64 (Alpine v3.19+ / edge).
#
# -B --boot-mode BOOT_MODE Either "BIOS" (default on x86/x86_64) or "UEFI" (default
# on others). "BIOS" is supported only on x86/x86_64.
#
# -b --branch ALPINE_BRANCH Alpine branch to install; used only when
# --repositories-file is not specified. Default is
# latest-stable.
#
# -S --fs-skel-dir FS_SKEL_DIR Path of directory which content to recursively copy
# (using rsync) into / in the image.
#
# --fs-skel-chown FS_SKEL_CHOWN Force all files from FS_SKEL_DIR to be owned by the
# specified USER:GROUP.
#
# -P --partition (PARTITION) Create GUID Partition Table (GPT) with a single partition.
# GPT is always created if --boot-mode is UEFI.
#
# -f --image-format IMAGE_FORMAT Format of the disk image (see qemu-img --help). Not
# applicable if the target image is a block device.
#
# -s --image-size IMAGE_SIZE Size of the disk image to create in bytes or with suffix
# (e.g. 1G, 1024M). Default is 2G. Not applicable if the
# target image is a block device.
#
# -i --initfs-features INITFS_FEATURES List of additional mkinitfs features (basically modules)
# to be included in initramfs (see mkinitfs -L). "base" and
# $ROOTFS is always included, don't specify them here.
# Default is "kms scsi virtio".
#
# -k --kernel-flavor KERNEL_FLAVOR The kernel flavour to install; virt (default), lts
# (Alpine >=3.11), or vanilla (Alpine <3.11).
#
# --keys-dir KEYS_DIR Path of directory with Alpine keys to copy into the image.
# Default is /etc/apk/keys if --arch matches the host arch.
# If does not exist, keys for aarch64 and x86_64 embedded in
# this script will be used.
#
# -m --mirror-uri ALPINE_MIRROR URI of the Aports mirror to fetch packages; used only
# when --repositories-file is not specified. Default is
# http://dl-cdn.alpinelinux.org/alpine.
#
# -C --no-cleanup (CLEANUP) Don't umount and disconnect image when done.
#
# -p --packages PACKAGES Additional packages to install into the image.
#
# -r --repositories-file REPOS_FILE Path of the repositories file to copy into the rootfs.
# If not provided, Alpine's main and community repositories
# on --mirror-uri will be used.
#
# --rootfs ROOTFS Filesystem to create on the image. Default is ext4.
#
# -c --script-chroot (SCRIPT_CHROOT) Bind <script>'s directory at /mnt inside image and chroot
# into the image before executing <script>.
#
# -t --serial-console (SERIAL_CONSOLE) Add configuration for a serial console on ttyS0.
#
# -h --help Show this help message and exit.
#
# -V --version Print version and exit.
#
# APK APK command to use. Default is "apk".
#
# APK_OPTS Options to pass into apk on each execution.
# Default is "--no-progress".
#
# APK_TOOLS_URI URL of apk-tools binary to download if $APK is not found
# on the host system. Default is apk.static from
# https://gitlab.alpinelinux.org/alpine/apk-tools/-/packages.
#
# APK_TOOLS_SHA256 SHA-256 checksum of $APK_TOOLS_URI.
#
# Each option can be also provided by environment variable. If both option and
# variable is specified and the option accepts only one argument, then the
# option takes precedence.
#
# https://github.com/alpinelinux/alpine-make-vm-image
#---help---
set -eu
# Some distros (e.g. Arch Linux) does not have /bin or /sbin in PATH.
PATH="$PATH:/usr/sbin:/usr/bin:/sbin:/bin"
readonly PROGNAME='alpine-make-vm-image'
readonly VERSION='0.13.0'
readonly VIRTUAL_PKG=".make-$PROGNAME"
# An opaque string used to detect changes in resolv.conf.
readonly RESOLVCONF_MARK="### created by $PROGNAME ###"
# Alpine APK keys for verification of packages for x86_64 and aarch64.
readonly ALPINE_KEYS='
4a6a0840:MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA1yHJxQgsHQREclQu4Ohe\nqxTxd1tHcNnvnQTu/UrTky8wWvgXT+jpveroeWWnzmsYlDI93eLI2ORakxb3gA2O\nQ0Ry4ws8vhaxLQGC74uQR5+/yYrLuTKydFzuPaS1dK19qJPXB8GMdmFOijnXX4SA\njixuHLe1WW7kZVtjL7nufvpXkWBGjsfrvskdNA/5MfxAeBbqPgaq0QMEfxMAn6/R\nL5kNepi/Vr4S39Xvf2DzWkTLEK8pcnjNkt9/aafhWqFVW7m3HCAII6h/qlQNQKSo\nGuH34Q8GsFG30izUENV9avY7hSLq7nggsvkn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CXtsbplNhW1IF\nHwsGXBTVcfXg44RLyL8Lk/2dQxDHNHzAUslJXzPxaHBLmt++2COa2EI1iWlvtznk\nOk9WP8SOAIj+xdqoiHcC4j72BOVVgiITIJNHrbppZCq6qPR+fgXmXa+sDcGh30m6\n9Wpbr28kLMSHiENCWTdsFij+NQTd5S47H7XTROHnalYDuF1RpS+DpQidT5tUimaT\nJZDr++FjKrnnijbyNF8b98UCAwEAAQ==
'
readonly HOST_ARCH="$(uname -m)"
# SHA256 checksum of $APK_TOOLS_URI for each architecture.
case "$HOST_ARCH" in
aarch64) : ${APK_TOOLS_SHA256:="d49a63b8b6780fc1342d3e7e14862aa006c30bafbf74beec8e1dfe99e6f89471"};;
x86_64) : ${APK_TOOLS_SHA256:="1c65115a425d049590bec7c729c7fd88357fbb090a6fc8c31d834d7b0bc7d6f2"};;
esac
: ${APK_TOOLS_URI:="https://gitlab.alpinelinux.org/api/v4/projects/5/packages/generic/v2.14.0/$HOST_ARCH/apk.static"}
: ${APK:="apk"}
: ${APK_OPTS:="--no-progress"}
# For compatibility with systems that does not have "realpath" command.
if ! command -v realpath >/dev/null; then
alias realpath='readlink -f'
fi
die() {
printf '\033[1;31mERROR:\033[0m %s\n' "$@" >&2 # bold red
exit 1
}
einfo() {
printf '\n\033[1;36m> %s\033[0m\n' "$@" >&2 # bold cyan
}
# Prints help and exists with the specified status.
help() {
sed -En '/^#---help---/,/^#---help---/p' "$0" | sed -E 's/^# ?//; 1d;$d;'
exit ${1:-0}
}
# Cleans the host system. This function is executed before exiting the script.
cleanup() {
set +eu
trap '' EXIT HUP INT TERM # unset trap to avoid loop
cd /
if [ -d "$temp_dir" ]; then
rm -Rf "$temp_dir"
fi
if [ "$mount_dir" ]; then
umount_recursively "$mount_dir" \
|| die "Failed to unmount $mount_dir; unmount it and disconnect $disk_dev manually"
rm -Rf "$mount_dir"
fi
if [ "$disk_dev" ] && ! [ -b "$IMAGE_FILE" ]; then
qemu-nbd --disconnect "$disk_dev" \
|| die "Failed to disconnect $disk_dev; disconnect it manually"
fi
if [ "$INSTALL_HOST_PKGS" = yes ]; then
_apk del $VIRTUAL_PKG
fi
}
_apk() {
"$APK" $APK_OPTS "$@"
}
# Attaches the specified image as a NBD block device and prints its path.
attach_image() {
local image="$1"
local format="${2:-}"
local disk_dev
disk_dev=$(get_available_nbd) || {
modprobe nbd max_part=16
sleep 1
disk_dev=$(get_available_nbd)
} || die 'No available nbd device found!'
qemu-nbd --connect="$disk_dev" --cache=writeback \
${format:+--format=$format} "$image" \
&& echo "$disk_dev"
}
# Prints UUID of filesystem on the specified block device.
blk_uuid() {
local dev="$1"
blkid "$dev" | sed -En 's/.*\bUUID="([^"]+)".*/\1/p'
}
# Creates GPT parition table on the device $1 for boot mode $2 (BIOS or UEFI).
create_gpt() {
local dev="$1"
local mode="$2"
case "$mode" in
BIOS) printf '%s\n' \
'label: gpt' \
'name=system,type=L,bootable,attrs=LegacyBIOSBootable' \
| sfdisk "$dev" ;;
UEFI) printf '%s\n' \
'label: gpt' \
'name=efi,type=U,size=128M,bootable' \
'name=system,type=L' \
| sfdisk "$disk_dev" ;;
*) die "Invalid mode: $mode" ;;
esac
}
# Writes Alpine APK keys embedded in this script into directory $1.
dump_alpine_keys() {
local dest_dir="$1"
local content id line
mkdir -p "$dest_dir"
for line in $ALPINE_KEYS; do
id=${line%%:*}
content=${line#*:}
printf -- "-----BEGIN PUBLIC KEY-----\n$content\n-----END PUBLIC KEY-----\n" \
> "$dest_dir/alpine-devel@lists.alpinelinux.org-$id.rsa.pub"
done
}
# Prints path of available nbdX device, or returns 1 if not any.
get_available_nbd() {
local dev; for dev in $(find /dev -maxdepth 2 -name 'nbd[0-9]*'); do
if [ "$(blockdev --getsize64 "$dev")" -eq 0 ]; then
echo "$dev"; return 0
fi
done
return 1
}
# Prints name of the package needed for creating the specified filesystem.
fs_progs_pkg() {
local fs="$1" # filesystem name
case "$fs" in
ext4) echo 'e2fsprogs';;
btrfs) echo 'btrfs-progs';;
xfs) echo 'xfsprogs';;
esac
}
# Tests if the specified command exists on the system.
has_cmd() {
command -v "$1" >/dev/null
}
# Binds the directory $1 at the mountpoint $2 and sets propagation to private.
mount_bind() {
mkdir -p "$2"
mount --bind "$1" "$2"
mount --make-private "$2"
}
# Prepares chroot at the specified path.
prepare_chroot() {
local dest="$1"
mkdir -p "$dest"/proc
mount -t proc none "$dest"/proc
mount_bind /dev "$dest"/dev
mount_bind /sys "$dest"/sys
install -D -m 644 /etc/resolv.conf "$dest"/etc/resolv.conf
echo "$RESOLVCONF_MARK" >> "$dest"/etc/resolv.conf
}
# Adds specified services to the runlevel. Current working directory must be
# root of the image.
rc_add() {
local runlevel="$1"; shift # runlevel name
local services="$*" # names of services
local svc; for svc in $services; do
mkdir -p etc/runlevels/$runlevel
ln -s /etc/init.d/$svc etc/runlevels/$runlevel/$svc
echo " * service $svc added to runlevel $runlevel"
done
}
# Ensures that the specified device node exists.
settle_dev_node() {
local dev="$1"
[ -e "$dev" ] && return 0
sleep 1 # give hotplug handler some time to kick in
[ -e "$dev" ] && return 0
if has_cmd udevadm; then
udevadm settle --exit-if-exists="$dev"
elif has_cmd mdev; then
mdev -s
fi
[ -e "$dev" ] && return 0
return 1
}
# Installs and configures extlinux.
setup_extlinux() {
local mnt="$1" # path of directory where is root device currently mounted
local root_dev="$2" # root device
local modules="$3" # modules which should be loaded before pivot_root
local kernel_flavor="$4" # name of default kernel to boot
local serial_port="$5" # serial port number for serial console
local default_kernel="$kernel_flavor"
local kernel_opts=''
[ -z "$serial_port" ] || kernel_opts="console=$serial_port"
if [ "$kernel_flavor" = 'virt' ]; then
_apk search --root . --exact --quiet linux-lts | grep -q . \
&& default_kernel='lts' \
|| default_kernel='vanilla'
fi
sed -Ei \
-e "s|^[# ]*(root)=.*|\1=$root_dev|" \
-e "s|^[# ]*(default_kernel_opts)=.*|\1=\"$kernel_opts\"|" \
-e "s|^[# ]*(modules)=.*|\1=\"$modules\"|" \
-e "s|^[# ]*(default)=.*|\1=$default_kernel|" \
-e "s|^[# ]*(serial_port)=.*|\1=$serial_port|" \
"$mnt"/etc/update-extlinux.conf
chroot "$mnt" extlinux --install /boot
chroot "$mnt" update-extlinux --warn-only 2>&1 \
| { grep -Fv 'extlinux: cannot open device /dev' ||:; } >&2
}
# Configures mkinitfs.
setup_mkinitfs() {
local mnt="$1" # path of directory where is root device currently mounted
local features="$2" # list of mkinitfs features
features=$(printf '%s\n' $features | sort | uniq | xargs)
sed -Ei "s|^[# ]*(features)=.*|\1=\"$features\"|" \
"$mnt"/etc/mkinitfs/mkinitfs.conf
}
# Unmounts all filesystem under the specified directory tree.
umount_recursively() {
local mount_point="$1"
test -n "$mount_point" || return 1
cat /proc/mounts \
| cut -d ' ' -f 2 \
| grep "^$mount_point" \
| sort -r \
| xargs umount -rn
}
# Downloads the specified file using wget and checks checksum.
wgets() (
local url="$1"
local sha256="$2"
local dest="${3:-.}"
cd "$dest" \
&& wget -T 10 --no-verbose "$url" \
&& echo "$sha256 ${url##*/}" | sha256sum -c
)
#============================= M a i n ==============================#
opts=$(getopt -n $PROGNAME -o a:b:B:cCf:hi:k:m:p:Pr:s:S:tV \
-l arch:,boot-mode:,branch:,fs-skel-chown:,fs-skel-dir:,image-format:,image-size:,initfs-features:,kernel-flavor:,keys-dir:,mirror-uri:,no-cleanup,packages:,partition,repositories-file:,rootfs:,script-chroot,serial-console,help,version \
-- "$@") || help 1 >&2
eval set -- "$opts"
while [ $# -gt 0 ]; do
n=2
case "$1" in
-a | --arch) ARCH="$2";;
-B | --boot-mode) BOOT_MODE="$2";;
-b | --branch) ALPINE_BRANCH="$2";;
-S | --fs-skel-dir) FS_SKEL_DIR="$(realpath "$2")";;
--fs-skel-chown) FS_SKEL_CHOWN="$2";;
-f | --image-format) IMAGE_FORMAT="$2";;
-s | --image-size) IMAGE_SIZE="$2";;
-i | --initfs-features) INITFS_FEATURES="${INITFS_FEATURES:-} $2";;
-k | --kernel-flavor) KERNEL_FLAVOR="$2";;
--keys-dir) KEYS_DIR="$(realpath "$2")";;
-m | --mirror-uri) ALPINE_MIRROR="$2";;
-C | --no-cleanup) CLEANUP='no'; n=1;;
-p | --packages) PACKAGES="${PACKAGES:-} $2";;
-P | --partition) PARTITION='yes'; n=1;;
-r | --repositories-file) REPOS_FILE="$(realpath "$2")";;
--rootfs) ROOTFS="$2";;
-t | --serial-console) SERIAL_CONSOLE='yes'; n=1;;
-c | --script-chroot) SCRIPT_CHROOT='yes'; n=1;;
-h | --help) help 0;;
-V | --version) echo "$PROGNAME $VERSION"; exit 0;;
--) shift; break;;
esac
shift $n
done
: ${ALPINE_BRANCH:="latest-stable"}
: ${ALPINE_MIRROR:="http://dl-cdn.alpinelinux.org/alpine"}
: ${ARCH:=}
: ${CLEANUP:="yes"}
: ${FS_SKEL_CHOWN:=}
: ${FS_SKEL_DIR:=}
: ${IMAGE_FORMAT:=}
: ${IMAGE_SIZE:="2G"}
# kms includes virtio-gpu drivers used for graphical output. This is needed
# for aarch64 where '-vga std' is not supported, but useful even for x86_64.
: ${INITFS_FEATURES:="kms scsi virtio"}
: ${KERNEL_FLAVOR:="virt"}
: ${PACKAGES:=}
: ${PARTITION:="no"}
: ${REPOS_FILE:=}
: ${ROOTFS:="ext4"}
: ${SCRIPT_CHROOT:="no"}
: ${SERIAL_CONSOLE:="no"}
case "$ALPINE_BRANCH" in
[0-9]*) ALPINE_BRANCH="v$ALPINE_BRANCH";;
esac
if [ -z "$ARCH" ] || [ "$HOST_ARCH" = "$ARCH" ]; then
: ${KEYS_DIR:="/etc/apk/keys"}
else
: ${KEYS_DIR:=}
fi
case "${ARCH:-"$HOST_ARCH"}" in
x86*) : ${BOOT_MODE:="BIOS"};;
*) : ${BOOT_MODE:="UEFI"}
[ "$BOOT_MODE" = 'BIOS' ] && die 'BIOS boot mode is supported only on x86/x86_64';;
esac
case "$BOOT_MODE" in
BIOS | UEFI) ;;
*) die "Invalid boot-mode: $BOOT_MODE";;
esac
if [ -f /etc/alpine-release ]; then
: ${INSTALL_HOST_PKGS:="yes"}
else
: ${INSTALL_HOST_PKGS:="no"}
fi
SERIAL_PORT=
[ "$SERIAL_CONSOLE" = 'no' ] || SERIAL_PORT='ttyS0'
[ $# -ne 0 ] || help 1 >&2
IMAGE_FILE="$1"; shift
SCRIPT=
[ $# -eq 0 ] || { SCRIPT=$(realpath "$1"); shift; }
[ "$CLEANUP" = no ] || trap cleanup EXIT HUP INT TERM
#-----------------------------------------------------------------------
if [ "$INSTALL_HOST_PKGS" = yes ]; then
einfo 'Installing needed packages on host system'
# We need load btrfs module to avoid the error message:
# 'failed to open /dev/btrfs-control'
if ! grep -q -w "$ROOTFS" /proc/filesystems; then
modprobe $ROOTFS
fi
[ "$BOOT_MODE" = 'UEFI' ] && vfatpkg='dosfstools' || vfatpkg=
_apk add -t $VIRTUAL_PKG qemu-img $(fs_progs_pkg "$ROOTFS") $vfatpkg rsync sfdisk
fi
#-----------------------------------------------------------------------
temp_dir=''
if ! command -v "$APK" >/dev/null; then
einfo "$APK not found, downloading static apk-tools"
temp_dir="$(mktemp -d /tmp/$PROGNAME.XXXXXX)"
wgets "$APK_TOOLS_URI" "$APK_TOOLS_SHA256" "$temp_dir"
APK="$temp_dir/apk.static"
chmod +x "$APK"
fi
#-----------------------------------------------------------------------
if [ ! -f "$IMAGE_FILE" ] && [ ! -b "$IMAGE_FILE" ]; then
einfo "Creating $IMAGE_FORMAT image of size $IMAGE_SIZE"
qemu-img create ${IMAGE_FORMAT:+-f $IMAGE_FORMAT} "$IMAGE_FILE" "$IMAGE_SIZE"
fi
#-----------------------------------------------------------------------
if [ -b "$IMAGE_FILE" ]; then
echo "Provided target $IMAGE_FILE is a block device" >&2
disk_dev="$IMAGE_FILE"
else
einfo "Attaching image $IMAGE_FILE as a NBD device"
disk_dev=$(attach_image "$IMAGE_FILE" "$IMAGE_FORMAT")
fi
#-----------------------------------------------------------------------
if [ "$PARTITION" = yes ] || [ "$BOOT_MODE" = 'UEFI' ]; then
einfo 'Creating GPT partition table'
create_gpt "$disk_dev" "$BOOT_MODE"
if [ "$BOOT_MODE" = 'BIOS' ]; then
root_dev="${disk_dev}p1"
else
esp_dev="${disk_dev}p1"
root_dev="${disk_dev}p2"
fi
# This is needed when running in a container.
settle_dev_node "$root_dev" || die "system didn't create $root_dev node"
else
root_dev="$disk_dev"
fi
#-----------------------------------------------------------------------
einfo 'Creating filesystem(s)'
if [ "$BOOT_MODE" = 'UEFI' ]; then
mkfs.fat -F32 -n EFI "$esp_dev"
fi
# syslinux 6.0.3 cannot boot from ext4 w/ 64bit feature enabled.
# -E nodiscard / -K - do not attempt to discard blocks at mkfs time (it's
# useless for NBD image and prints confusing error).
[ "$ROOTFS" = ext4 ] && mkfs_args='-O ^64bit -E nodiscard' || mkfs_args='-K'
mkfs.$ROOTFS -L root $mkfs_args "$root_dev"
root_uuid=$(blk_uuid "$root_dev")
mount_dir=$(mktemp -d /tmp/$PROGNAME.XXXXXX)
#-----------------------------------------------------------------------
einfo "Mounting image at $mount_dir"
mount "$root_dev" "$mount_dir"
if [ "$BOOT_MODE" = 'UEFI' ]; then
install -d -m 000 "$mount_dir"/boot
mount -t vfat "$esp_dev" "$mount_dir"/boot
fi
#-----------------------------------------------------------------------
einfo 'Installing base system'
cd "$mount_dir"
mkdir -p etc/apk/keys
if [ "$REPOS_FILE" ]; then
install -m 644 "$REPOS_FILE" etc/apk/repositories
else
cat > etc/apk/repositories <<-EOF
$ALPINE_MIRROR/$ALPINE_BRANCH/main
$ALPINE_MIRROR/$ALPINE_BRANCH/community
EOF
fi
if [ -d "$KEYS_DIR" ]; then
cp "$KEYS_DIR"/* etc/apk/keys/
else
dump_alpine_keys etc/apk/keys/
fi
# Use APK cache if available.
if [ -L /etc/apk/cache ]; then
ln -s "$(realpath /etc/apk/cache)" etc/apk/cache
fi
_apk add --root . ${ARCH:+--arch "$ARCH"} --update-cache --initdb alpine-base
fsprogs_pkgs=
# btrfs doesn't use fsck, so we don't have to install btrfs-progs.
[ "$ROOTFS" != 'btrfs' ] && fsprogs_pkgs="$(fs_progs_pkg "$ROOTFS")"
[ "$BOOT_MODE" = 'UEFI' ] && fsprogs_pkgs="$fsprogs_pkgs dosfstools"
[ "$fsprogs_pkgs" ] && _apk add --root . $fsprogs_pkgs
prepare_chroot .
#-----------------------------------------------------------------------
einfo "Installing kernel linux-$KERNEL_FLAVOR"
if [ "$KERNEL_FLAVOR" = 'virt' ]; then
_apk add --root . linux-$KERNEL_FLAVOR
else
# Avoid installing *all* linux-firmware-* packages (see #21).
_apk add --root . linux-$KERNEL_FLAVOR linux-firmware-none
fi
#-----------------------------------------------------------------------
einfo "Installing and configuring mkinitfs"
_apk add --root . mkinitfs
setup_mkinitfs . "base $ROOTFS $INITFS_FEATURES"
#-----------------------------------------------------------------------
if [ "$BOOT_MODE" = 'BIOS' ]; then
einfo 'Setting up extlinux bootloader'
_apk add --root . --no-scripts syslinux
setup_extlinux . "UUID=$root_uuid" "$ROOTFS" "$KERNEL_FLAVOR" "$SERIAL_PORT"
if [ "$PARTITION" = yes ]; then
dd bs=440 count=1 conv=notrunc if=usr/share/syslinux/gptmbr.bin of="$disk_dev"
sync
fi
fi
#-----------------------------------------------------------------------
einfo 'Configuring system'
cat > etc/fstab <<-EOF
# <fs> <mountpoint> <type> <opts> <dump/pass>
UUID=$root_uuid / $ROOTFS relatime 0 1
EOF
if [ "$BOOT_MODE" = 'UEFI' ]; then
echo "LABEL=EFI /boot vfat\
rw,relatime,fmask=0133,codepage=437,iocharset=ascii,shortname=mixed,utf8,errors=remount-ro 0 2" \
>> etc/fstab
# This script is interpreted by UEFI.
# NOTE: When console=$SERIAL_PORT is added here, no messages are printed to
# console on QEMU after switch_root. 'console=tty0' must be explicitly set
# for aarch64 (it's the default on x86_64).
echo "vmlinuz-$KERNEL_FLAVOR" \
"initrd=initramfs-$KERNEL_FLAVOR" \
"root=UUID=$root_uuid" \
"rootfstype=$ROOTFS" \
"modules=$(echo $INITFS_FEATURES | tr ' ' ',')" \
'console=tty0' \
> boot/startup.nsh
fi
# We just prepare this config, but don't start the networking service.
cat > etc/network/interfaces <<-EOF
auto eth0
iface eth0 inet dhcp
post-up /etc/network/if-post-up.d/*
post-down /etc/network/if-post-down.d/*
EOF
mkdir -p etc/network/if-post-up.d
mkdir -p etc/network/if-post-down.d
if [ "$SERIAL_PORT" ]; then
echo "$SERIAL_PORT" >> etc/securetty
sed -Ei "s|^[# ]*($SERIAL_PORT:.*)|\1|" etc/inittab
fi
#-----------------------------------------------------------------------
einfo 'Enabling base system services'
rc_add sysinit devfs dmesg mdev hwdrivers
[ -e etc/init.d/cgroups ] && rc_add sysinit cgroups ||: # since v3.8
rc_add boot modules hwclock swap hostname sysctl bootmisc syslog
# urandom was renamed to seedrng in v3.17
[ -e etc/init.d/seedrng ] && rc_add boot seedrng || rc_add boot urandom
rc_add shutdown killprocs savecache mount-ro
#-----------------------------------------------------------------------
if [ "$PACKAGES" ]; then
einfo 'Installing additional packages'
_apk add --root . $PACKAGES
fi
#-----------------------------------------------------------------------
if [ -L /etc/apk/cache ]; then
rm etc/apk/cache >/dev/null 2>&1
fi
#-----------------------------------------------------------------------
if [ "$FS_SKEL_DIR" ]; then
einfo "Copying content of $FS_SKEL_DIR into image"
[ "$FS_SKEL_CHOWN" ] \
&& rsync_opts="--chown $FS_SKEL_CHOWN" \
|| rsync_opts='--numeric-ids'
rsync --archive --info=NAME2 --whole-file $rsync_opts "$FS_SKEL_DIR"/ . >&2
# rsync may modify perms of the rootfs dir itself, so make sure it's correct.
install -d -m 0755 -o root -g root .
fi
#-----------------------------------------------------------------------
if [ "$SCRIPT" ]; then
script_name="${SCRIPT##*/}"
if [ "$SCRIPT_CHROOT" = 'no' ]; then
einfo "Executing script: $script_name $*"
"$SCRIPT" "$@" || die 'Script failed'
else
einfo "Executing script in chroot: $script_name $*"
mount_bind "${SCRIPT%/*}" mnt/
chroot . /bin/sh -c "cd /mnt && ./$script_name \"\$@\"" -- "$@" \
|| die 'Script failed'
fi
fi
#-----------------------------------------------------------------------
if grep -qw "$RESOLVCONF_MARK" etc/resolv.conf 2>/dev/null; then
cat > etc/resolv.conf <<-EOF
# Default nameservers, replace them with your own.
nameserver 1.1.1.1
nameserver 2606:4700:4700::1111
EOF
fi
rm -Rf var/cache/apk/* ||:
einfo 'Completed'
cd - >/dev/null
ls -lh "$IMAGE_FILE"