forked from DavidCWGA/makecm4devterm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmakecm3devterm.sh
More file actions
executable file
·268 lines (256 loc) · 7.17 KB
/
Copy pathmakecm3devterm.sh
File metadata and controls
executable file
·268 lines (256 loc) · 7.17 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
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
#!/bin/bash
valid_host () {
if [ "$(uname -m)" != "armv7l" ]; then
echo "This only works on armv7l." >&2
exit 1
fi
if [[ "$(whoami)" != "root" ]]; then
echo "You need to run this script as root." >&2
exit 1
fi
which apt-get >/dev/null
if [[ $? -ne 0 ]]; then
echo "You need to run this on a Debian-like system, like Debian itself or Raspberry Pi OS." >&2
exit 1
fi
}
eval_args () {
if [ -z "$1" ]; then
echo "Run as: $0 image_file working_dir"
exit 1
else
target_image="$1"
fi
if [ -z "$2" ]; then
workdir="$(pwd)/tmp_root"
echo "No working directory specified, use: $workdir"
echo "[y/n]?"
read -r answer
case $answer in
"[yY]*")
pass
;;
"*")
echo "Run again with a working directory specified"
exit 1
;;
esac
else
workdir="$2"
fi
rootpath="$workdir/mount"
if [ -n "$3" ]; then
echo "Backing up original image"
cp "$target_image" "$target_image.orig"
else
echo "Overwriting image file"
fi
if [[ "$target_image" == *.xz ]]; then
echo "Decompressing image"
unxz "$target_image"
target_image=${target_image/.xz/}
fi
printf 'target_image="%s"\nworkdir="%s"\nrootpath="%s"\n' \
"$target_image" \
"$workdir" \
"$rootpath" > /tmp/chroot_helper
}
prev_args () {
if [ -f "/tmp/chroot_helper" ]; then
source /tmp/chroot_helper
else
echo "There doesn't appear to be a chroot for us to work with"
exit 1
fi
}
check_args () {
if [ -f "/tmp/chroot_helper" ]; then
echo "You might already have a chroot environment running, check here:"
echo ""
cat /tmp/chroot_helper
echo ""
echo "To try and clean this, run: $0 remove"
exit 1
fi
}
prepare_chroot () {
mkdir -p "$workdir/mount/boot"
# resize the image to 10GiB to make sure there's room to perform updates
dd if=/dev/zero of="$target_image" bs=1 count=1 seek=$(echo "$((10 * 1073741824))")
img_end=$(parted -ms "$target_image" print | head -2 | cut -d':' -f2 | tail -1)
parted -ms "$target_image" resizepart 2 "$img_end"
losetup -fP "$target_image"
for n in {1..3}; do
if [[ -b /dev/loop0p$n ]]; then
mkdir -p "$workdir/loop0p$n";
mount "/dev/loop0p$n" "$workdir/loop0p$n";
if [ -f "$workdir/loop0p$n/cmdline.txt" ]; then
echo "Found boot partition";
bootp="$workdir/loop0p$n";
fi;
if [ -f "$workdir/loop0p$n/etc/fstab" ]; then
echo "Found root partition";
rootp="$workdir/loop0p$n";
rootd="/dev/loop0p$n";
fi;
fi;
done
mount --bind "$rootp" "$workdir/mount"
mount --bind "$bootp" "$workdir/mount/boot"
mount --bind /dev "$rootpath/dev/"
mount --bind /sys "$rootpath/sys/"
mount --bind /proc "$rootpath/proc/"
mount --bind /dev/pts "$rootpath/dev/pts"
resize2fs "$rootd"
}
# This is a really basic debugging tool.
# Change `main "$@"` to `old_main "$@"` to access it.
enter_chroot () {
chroot "$rootpath" /bin/bash
}
clean_chroot () {
umount "$rootpath/dev/pts"
umount "$rootpath/dev/"
umount "$rootpath/sys/"
umount "$rootpath/proc/"
umount "$workdir/mount/boot"
umount "$workdir/mount"
umount "$workdir/loop0p"*
losetup -D /dev/loop0
}
deploy_devterm () {
echo "Deploying clockworkpi repositories"
# TODO: see if this can be moved out of the legacy keystore easily
wget -nv "https://raw.githubusercontent.com/clockworkpi/apt/main/debian/KEY.gpg" \
-O "$rootpath/etc/apt/trusted.gpg.d/clockworkpi.asc"
echo "deb https://raw.githubusercontent.com/clockworkpi/apt/main/debian/ stable main" \
> "$rootpath/etc/apt/sources.list.d/clockworkpi.list"
# NOTE: these two commands are for my customized image
# echo "Deploying default xfce4-terminal settings"
# cp terminalrc "$rootpath/."
echo "Deploying 32bit libwiringPi"
cp libwiringPi/* "$rootpath/usr/lib/."
# move into chroot and run everything between EOF
chroot "$rootpath" /bin/bash -euo pipefail <<EOF
set -
apt-get -qq clean
apt-get -qq update
apt-get -qq upgrade
apt-get -qq remove linux-image-rpi-v8:arm64
apt-get -qq install \
devterm-thermal-printer \
devterm-thermal-printer-cups \
devterm-kernel-rpi \
devterm-audio-patch
apt-get -qq dist-upgrade
# TODO: see if this can be skipped by removing linux-image items after the
# upgrade
apt-get -qq remove linux-image-6.1.0-rpi8*
apt-get -qq autoremove
EOF
# a bug in chroot somewhere lets the chroot start cupsd =/
pgrep "cupsd" >/dev/null && killall -2 cupsd
}
deploy_screen () {
chroot "$rootpath" /bin/bash -euo pipefail <<"EOF"
set -x
echo "Configuring GNOME screen rotation"
mkdir -p "/etc/skel/.config"
cat <<EEOF >"/etc/skel/.config/monitors.xml"
<monitors version="2">
<configuration>
<logicalmonitor>
<x>0</x>
<y>0</y>
<primary>yes</primary>
<monitor>
<monitorspec>
<connector>DSI-1</connector>
<vendor>unknown</vendor>
<product>unknown</product>
<serial>unknown</serial>
</monitorspec>
<mode>
<width>480</width>
<height>1280</height>
<rate>60.000</rate>
</mode>
</monitor>
<transform>
<rotation>right</rotation>
</transform>
</logicalmonitor>
</configuration>
</monitors>
EEOF
for d in "/home/"* ; do
mkdir -p "$d/.config"
cp "/etc/skel/.config/monitors.xml" "$d/.config/monitors.xml"
owner_id=$(stat -c '%u' "$d")
chown -R $owner_id "$d/.config"
done
echo -n "Configuring X11 screen rotation: "
if [[ -d "/etc/X11" ]]; then
echo "xrandr --output DSI-1 --rotate right" >"/etc/X11/Xsession.d/0custom_xrandr"
echo "OK"
else
echo "Skipped"
fi
echo -n "Configuring LightDM screen rotation: "
if [[ -d "/etc/lightdm" ]]; then
sed -i '/^#greeter-setup-script=/c\greeter-setup-script=/etc/lightdm/setup.sh' "/etc/lightdm/lightdm.conf"
echo "xrandr --output DSI-1 --rotate right" >"/etc/lightdm/setup.sh"
echo "exit 0" >>"/etc/lightdm/setup.sh"
chmod +x "/etc/lightdm/setup.sh"
echo "OK"
else
echo "Skipped"
fi
echo "Configuring console screen rotation"
sed -i '1s/$/ fbcon=rotate:1/' "/boot/cmdline.txt"
# TODO: Figure out how to rotate [phymouth](https://wiki.debian.org/plymouth) as well
EOF
}
old_main () {
valid_host
case "$1" in
"prepare")
check_args
shift
eval_args "$@"
prepare_chroot
;;
"enter")
prev_args
enter_chroot
;;
"build")
prev_args
deploy_devterm
deploy_screen
;;
"remove")
prev_args
clean_chroot
rm -rf /tmp/chroot_helper
rm -rf "$workdir"
;;
*)
check_args
echo "Run as: $0 [prepare image_file working_dir|enter|remove]"
exit 1
;;
esac
}
main () {
valid_host
eval_args "$@"
prepare_chroot
deploy_devterm
deploy_screen
clean_chroot
rm -rf /tmp/chroot_helper
rm -rf "$workdir"
./pishrink.sh "$target_image"
}
main "$@"