-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcustomise_armbian
executable file
·218 lines (187 loc) · 6.91 KB
/
customise_armbian
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
#!/usr/bin/env bash
# exit when any command fails
set -e
# keep track of the last executed command
trap 'last_command=$current_command; current_command=$BASH_COMMAND' DEBUG
# echo an error message before exiting
trap 'echo "\"${last_command}\" command filed with exit code $?."' EXIT
IMAGE="${1}"
SWAP_PARTITION_LABEL="SWAP"
VAR_FS_LABEL="VAR"
RESIZE_FS_TO="2G"
# removed "snapd"
action="set_board, shell, swap_by_label, var_by_label, resize_fs, install_apparmor"
while getopts ":aH:i:s:o:uw:" o; do
case "${o}" in
a)
action="${action}, avahi"
;;
i)
IMAGE=${OPTARG}
;;
H)
action="${action}, set_hostname"
new_hostname="${OPTARG}"
;;
s)
action="${action}, salt_minion"
salt_master="${OPTARG}"
;;
o)
output_file="${OPTARG}"
;;
u)
action="${action}, upgrade"
;;
w)
write_to="${OPTARG}"
;;
*)
usage
;;
esac
done
function delete_lo_device () {
lo_device="${1}"
umount_target
losetup -v -d "${lo_device}"
}
function umount_target () {
#umount -l "/target/dev/pts" 2>/dev/null
if [ -f /target/usr/bin/qemu-arm-static ]; then
rm /target/usr/bin/qemu-arm-static
fi
#umount -l "/target/dev" 2>/dev/null
#umount -l "/target/sys" 2>/dev/null
#umount -l "/target/proc" 2>/dev/null
if [ $(mount |awk 'BEGIN{FS=" on "}{print $2}' | awk 'BEGIN{FS=" type "}{print $1}' | grep -c '/target') -gt 0 ]; then
umount -R "/target"
fi
#sleep 1
#while read mount_point; do
# echo "umount -l ${mount_point}"
#umount -l "${mount_point}"
#sleep 1
#umount -l "${mount_point}"
#done < <(mount |grep '/target' | awk 'BEGIN{FS=" on "}{print $2}' | awk 'BEGIN{FS=" type "}{print $1}')
}
if [[ "${IMAGE}" =~ \.xz$ ]]; then
if [ "${output_file}" != "" ]; then
echo "Extracting compressed image"
xz -d -c "${IMAGE}" >"${output_file}"
IMAGE="${output_file}"
else
echo "Extracting compressed image to temporary location"
extracted_image=$(mktemp /var/tmp/customise_armbian.XXXXXX)
xz -d -c "${IMAGE}" >"${extracted_image}"
IMAGE="${extracted_image}"
fi
else
echo "Copying source image"
if [ "${output_file}" != "" ]; then
cp "${IMAGE}" "${output_file}"
IMAGE="${output_file}"
fi
fi
if [[ "${action}" =~ resize_fs ]]; then
echo "Expanding image and partition to ${RESIZE_FS_TO:-2G}"
qemu-img resize -f raw "${IMAGE}" "${RESIZE_FS_TO:-2G}"
parted "${IMAGE}" resizepart 1 100%
fi
lo_device=$(losetup -j "${IMAGE}" | awk 'BEGIN{FS=": "}{print $1}' | tail -n 1)
while read lo_device; do
echo "Deleting existing loopback device '${lo_device}'"
delete_lo_device "${lo_device}"
lo_device=$(losetup -j "${IMAGE}" | awk 'BEGIN{FS=": "}{print $1}' | tail -n 1)
done < <(losetup -j "${IMAGE}" | awk 'BEGIN{FS=": "}{print $1}' | tail -n 1)
echo "Creating loopback device"
lo_device=$(losetup -f --partscan --show "${IMAGE}")
echo "Created device '${lo_device}'"
if [[ "${action}" =~ resize_fs ]]; then
echo "Expanding filesystem"
e2fsck -f "${lo_device}"p1
resize2fs "${lo_device}"p1
fi
mkdir -p /target
echo "Mounting file system in first partition"
mount "${lo_device}"p1 /target
mount --rbind --make-rslave /dev /target/dev
mount --rbind --make-rslave /dev/pts /target/dev/pts
mount --rbind --make-rslave /sys /target/sys
mount --rbind --make-rslave /proc /target/proc
cp -a /usr/bin/qemu-arm-static /target/usr/bin
if [ -f /target/etc/os-release ]; then
source /target/etc/os-release
fi
if [[ "${action}" =~ set_board ]]; then
echo "Setting board specific settings (DTB, console, verbosity)"
echo -e "board_name=hc1\nusbstoragequirks=0x2537:0x1066:u,0x2537:0x1068:u\n" >/target/boot/armbianEnv.txt
sed -i 's/^setenv console.*$/setenv console "serial"/g' /target/boot/boot.ini
sed -i 's/^setenv verbosity.*$/setenv verbosity "5"/g' /target/boot/boot.ini
fi
if [[ "${action}" =~ set_hostname ]]; then
echo "Setting hostname"
# The following doesn't work because dbus isn't running inside the chroot
#chroot /target /usr/bin/hostnamectl set-hostname "${new_hostname}"
echo "${new_hostname}" >/target/etc/hostname
#TODO: We also need to remove the existing names for the salt minion to get
# the right minion_id
grep -v -e '127.0.1.1' -e '::1' /target/etc/hosts >/target/tmp/hosts.tmp
echo -e "127.0.1.1\t${new_hostname} localhost" >>/target/tmp/hosts.tmp
echo -e "::1\t${new_hostname} localhost ip6-localhost ip6-loopback" >>/target/tmp/hosts.tmp
mv /target/tmp/hosts.tmp /target/etc/hosts
fi
# Add any extra repositories here so we only need to run "apt update" once
if [[ "${action}" =~ salt_minion ]]; then
echo "Adding SaltStack package repository"
# Using the amd64 repository on armhf/aarch64 seems to work fine. There are no actual Arm repos.
echo "deb [arch=amd64] http://repo.saltstack.com/py3/ubuntu/${VERSION_ID}/amd64/latest ${VERSION_CODENAME} main" >/target/etc/apt/sources.list.d/saltstack.list
wget -O - "https://repo.saltstack.com/py3/ubuntu/${VERSION_ID}/amd64/latest/SALTSTACK-GPG-KEY.pub" | chroot /target apt-key add -
fi
if [[ "${action}" =~ swap_by_label ]]; then
echo "/dev/disk/by-partlabel/${SWAP_PARTITION_LABEL} swap swap defaults,nofail 0 0" >>/target/etc/fstab
fi
if [[ "${action}" =~ var_by_label ]]; then
echo "/dev/disk/by-label/${VAR_FS_LABEL} /var ext4 defaults,nofail 0 0" >>/target/etc/fstab
fi
if [[ "${action}" =~ avahi ]] || [[ "${action}" =~ salt_minion ]] || [[ "${action}" =~ upgrade ]] ; then
chroot /target /usr/bin/apt update
fi
if [[ "${action}" =~ snapd ]]; then
chroot /target /usr/bin/apt install -y snapd
fi
if [[ "${action}" =~ install_apparmor ]]; then
chroot /target /usr/bin/apt install -y apparmor
fi
if [[ "${action}" =~ avahi ]]; then
chroot /target /usr/bin/apt install -y avahi-daemon libnss-mdns
# The following stops nss_mdns defaulting to IPv4 names
sed -i /target/etc/nsswitch.conf -e 's/mdns4_minimal/mdns_minimal/g' -e 's/\[NOTFOUND=return\]//g'
fi
if [[ "${action}" =~ salt_minion ]]; then
echo "Installing SaltStack Minion"
chroot /target /usr/bin/apt install -y salt-minion
printf "master: ${salt_master}\nappend_domain: local\nminion_id_lowercase: True\nminion_id_caching: False" >/target/etc/salt/minion.d/local.conf
if [ -f /target/etc/salt/minion_id ]; then
rm /target/etc/salt/minion_id
fi
if [ -d /target/etc/salt/pki/ ]; then
rm -fr /target/etc/salt/pki/
fi
fi
if [[ "${action}" =~ upgrade ]]; then
echo "Installing all available updates"
#chroot /target /usr/bin/apt full-upgrade -y
fi
if [[ "${action}" =~ shell ]]; then
echo "Chroot into target"
chroot /target /usr/bin/bash -i
fi
if [[ "${action}" =~ var_by_label ]]; then
# TODO: Create a first run unit that extracts this .tar.gz into /var and then
# deletes it.
tar -czf /target/var.tgz /target/var
fi
echo "Unmounting target"
delete_lo_device "${lo_device}"
trap - EXIT