-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathbuild-rootfs-img.sh
More file actions
executable file
·60 lines (51 loc) · 1.97 KB
/
build-rootfs-img.sh
File metadata and controls
executable file
·60 lines (51 loc) · 1.97 KB
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
#!/bin/bash
: "${LINUX_OUT:=prebuilt/linux}"
: "${IMG_OUT:=out}"
: "${IMGSIZE:=4GiB}"}
set -e
if [ -c /dev/kvm -a -w /dev/kvm ]; then
# Have virtualization support, can use fakemachine (default, fast, safe)
DEBOS="debos -c $(nproc) -m 6Gb"
elif [ -f /.dockerenv ]; then
# Running in a container without access to virtualization, fall back to the slow method
DEBOS="debos -b qemu -c $(nproc) -m 6Gb"
elif [ `id -u` -eq 0 ]; then
# Running as root, can use the host mode without fakemachine (fast, less safe)
DEBOS="debos"
else
DEBOS="sudo debos --disable-fakemachine"
fi
mkdir -p "$IMG_OUT"
if [ ! -f "$IMG_OUT"/debian-ospack.tar.gz -o "$UPDATE_OSPACK" ]; then
./build-ospack.sh
fi
rm -rf "$IMG_OUT"/linux_tmp
mkdir -p "$IMG_OUT"/linux_tmp
cp -r "$LINUX_OUT"/* "$IMG_OUT"/linux_tmp
echo "Creating the root FS image"
$DEBOS --artifactdir="$IMG_OUT" -t imagesize:"$IMGSIZE" -t kerneldir:"$IMG_OUT"/linux_tmp debian-rk3576-img.yaml
sync "$IMG_OUT"/debian-nobootloader.img
owner=$(stat -c %u "$IMG_OUT"/debian-nobootloader.img)
whoami=$(id -u)
if [ "$owner" -ne "$whoami" ]; then
sudo chown "$whoami" "$IMG_OUT"/debian-nobootloader.img
fi
read START COUNT < <(
parted -s -m "$IMG_OUT"/debian-nobootloader.img unit s print \
| awk -F: -v p="2" '$1==p { gsub(/s/,"",$2); gsub(/s/,"",$4); print $2, $4 }'
)
start_bytes=$((START * 512))
count_bytes=$((COUNT * 512))
end_bytes=$((start_bytes + count_bytes))
img_size_bytes=$(stat -c %s "$IMG_OUT"/debian-nobootloader.img)
tail_bytes=$((img_size_bytes - end_bytes))
if [ "$tail_bytes" -gt 0 ]; then
truncate -c -s "$end_bytes" "$IMG_OUT"/debian-nobootloader.img
fi
if [ "$start_bytes" -gt 0 ]; then
fallocate --collapse-range -o 0 -l "$start_bytes" "$IMG_OUT"/debian-nobootloader.img
fi
sync "$IMG_OUT"/debian-nobootloader.img
bmaptool create -o "$IMG_OUT"/debian-rootfs.img.bmap "$IMG_OUT"/debian-nobootloader.img
zeekstd -f -o "$IMG_OUT"/debian-rootfs.img.zst "$IMG_OUT"/debian-nobootloader.img
rm -rf "$IMG_OUT"/linux_tmp "$IMG_OUT"/debian-nobootloader.img