-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild-image
executable file
·40 lines (34 loc) · 1.39 KB
/
build-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
#!/usr/bin/env bash
set -euox pipefail
# Args Input (removes trailing slash)
DIR="${1%/}"
UID_GID="$2"
# Output
TARGET="$DIR/../"
IMAGE="$DIR.img"
# Internal (must match run's)
NAME=$(basename "$DIR")
KERNEL="$TARGET/$NAME.kernel"
INITRD="$TARGET/$NAME.initrd"
# cp Kernel & InitFS + chown it
cp "$DIR/boot/vmlinuz-linux" "$KERNEL"
cp "$DIR/boot/initramfs-linux-fallback.img" "$INITRD"
chown "$UID_GID" "$KERNEL" "$INITRD"
# Create Raw Disk Image from DIR
# As we are running as root anyway, there is really no point using `udisksctl loop-setup --no-user-interaction --file "$IMAGE"`
# to avoids needing to be root, because it leaks /dev/loop* devices if script fails; and (more importantly) is async and timing is PITA.
# Neither libguestfs's guestmount, or gnome-disk-image-mounter; both https://askubuntu.com/questions/670701/how-to-mount-an-image-file-without-root.
# NB: We don't need losetup, as in https://github.com/anatol/vmtest/blob/master/docs/prepare_image.md#build-an-arch-linux-rootfs, as directory exists already.
SIZE_APPROXIMATION=$(du -bs "$DIR" | cut -f1)
SIZE=$(( "$SIZE_APPROXIMATION" * 2 ))
rm -f "$IMAGE"
truncate -s "$SIZE" "$IMAGE"
mkfs.ext4 -v -L LinuxRootFS -F "$IMAGE"
MOUNT="$DIR.mnt"
mkdir -p "$MOUNT"
mount "$IMAGE" "$MOUNT"
cp -rpa "$DIR"/* "$MOUNT"
umount "$MOUNT"
# rmdir "$MOUNT" can cause "Device or resource busy"
chown "$UID_GID" "$IMAGE"
# TODO Create Copy-On-Write (COW) Image