forked from FooDeas/raspberrypi-ua-netinst
-
Notifications
You must be signed in to change notification settings - Fork 0
/
buildroot.sh
executable file
·97 lines (77 loc) · 2.1 KB
/
buildroot.sh
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
#!/usr/bin/env bash
set -e # exit if any command fails
# Set defaults for configurable behavior
# Controls production of a bz2-compressed image
compress_bz2=1
# Controls production of an xz-compressed image
compress_xz=1
# If a configuration file exists, import its settings
if [ -e buildroot.conf ]; then
# shellcheck disable=SC1091
source buildroot.conf
fi
build_dir=build_dir
version_tag="$(git describe --exact-match --tags HEAD 2> /dev/null || true)"
version_commit="$(git rev-parse --short "@{0}" 2> /dev/null || true)"
if [ -n "${version_tag}" ]; then
imagename="raspberrypi-ua-netinst-${version_tag}"
elif [ -n "${version_commit}" ]; then
imagename="raspberrypi-ua-netinst-git-${version_commit}"
else
imagename="raspberrypi-ua-netinst-$(date +%Y%m%d)"
fi
export imagename
image=${build_dir}/${imagename}.img
# Prepare
rm -f "${image}"
# Create image
dd if=/dev/zero of="$image" bs=1M count=128
fdisk "${image}" <<EOF
n
p
1
t
b
w
EOF
if ! losetup --version &> /dev/null; then
losetup_lt_2_22=true
elif [ "$(echo "$(losetup --version | rev|cut -f1 -d' '|rev|cut -d'.' -f-2)"'<'2.22 | bc -l)" -ne 0 ]; then
losetup_lt_2_22=true
else
losetup_lt_2_22=false
fi
if [ "$losetup_lt_2_22" = "true" ]; then
kpartx -as "${image}"
mkfs.vfat /dev/mapper/loop0p1
mount /dev/mapper/loop0p1 /mnt
cp -r ${build_dir}/bootfs/* /mnt/
umount /mnt
kpartx -d "${image}" || true
else
losetup --find --partscan "${image}"
LOOP_DEV="$(losetup --associated "${image}" | cut -f1 -d':')"
mkfs.vfat "${LOOP_DEV}p1"
mount "${LOOP_DEV}p1" /mnt
cp -r ${build_dir}/bootfs/* /mnt/
umount /mnt
losetup --detach "${LOOP_DEV}"
fi
# Create archives
if [ "$compress_xz" = "1" ]; then
rm -f "${image}.xz"
if ! xz -9v --keep "${image}"; then
# This happens e.g. on Raspberry Pi because xz runs out of memory.
echo "WARNING: Could not create '${IMG}.xz' variant." >&2
fi
rm -f "${imagename}.img.xz"
mv "${image}.xz" ./
fi
if [ "$compress_bz2" = "1" ]; then
rm -f "${imagename}.img.bz2"
( bzip2 -9v > "${imagename}.img.bz2" ) < "${image}"
fi
# Cleanup
if [ "$compress_xz" = "1" ] || [ "$compress_bz2" = "1" ]; then
rm -f "${image}"
fi