-
Notifications
You must be signed in to change notification settings - Fork 45
/
buildroot.sh
executable file
·110 lines (88 loc) · 2.56 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
98
99
100
101
102
103
104
105
106
107
108
109
110
#!/usr/bin/env bash
# shellcheck disable=SC1090
# shellcheck disable=SC1091
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
# Use 'sudo' for commands which require root privileges
use_sudo=0
# If a configuration file exists, import its settings
if [ -r buildroot.conf ]; then
source <(tr -d "\015" < buildroot.conf)
fi
if [ "$use_sudo" = "1" ]; then
SUDO=sudo
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}"
rm -rf "${build_dir:-build_dir}/mnt/"
# Create image
dd if=/dev/zero of="$image" bs=1M count=128
${SUDO} 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
${SUDO} kpartx -as "${image}"
${SUDO} mkfs.vfat /dev/mapper/loop0p1
mkdir ${build_dir}/mnt
${SUDO} mount /dev/mapper/loop0p1 ${build_dir}/mnt
${SUDO} cp -r ${build_dir}/bootfs/* ${build_dir}/mnt
${SUDO} umount ${build_dir}/mnt
${SUDO} kpartx -d "${image}" || true
rmdir ${build_dir}/mnt
else
${SUDO} losetup --find --partscan "${image}"
LOOP_DEV="$(losetup --associated "${image}" | cut -f1 -d':')"
${SUDO} mkfs.vfat "${LOOP_DEV}p1"
mkdir ${build_dir}/mnt
${SUDO} mount "${LOOP_DEV}p1" ${build_dir}/mnt
${SUDO} cp -r ${build_dir}/bootfs/* ${build_dir}/mnt
${SUDO} umount ${build_dir}/mnt
${SUDO} losetup --detach "${LOOP_DEV}"
rmdir ${build_dir}/mnt
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