This repository has been archived by the owner on Feb 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 26
/
install-debian
executable file
·367 lines (316 loc) · 9.14 KB
/
install-debian
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
#!/bin/bash
#
# install-debian
#
# Convert a command to simply invoke it and echo
conceal () {
eval "$1 () {
echo \"\$ $1\" \"\$@\"
}"
}
age () {
born_on=`stat --format %Y $1`
now=`date +%s`
echo $(($now - $born_on))
}
build_from_template () {
sed \
-e "s/\${BLKNAME}/`basename ${BLKNAME}`/g" \
< $1.template > $1
}
on_target () {
sudo LANG=C PATH=/usr/sbin:$PATH chroot ${PWD}/sysimage "$@"
}
do_prep () {
printf '\n\n>>>> Preparing to install\n\n'
git submodule update --init
PACKAGES="debootstrap pigz dosfstools"
[ ! -z "$CRYPT" ] && PACKAGES="$PACKAGES cryptsetup"
if [ -x "$(command -v apt-get)" ]
then
sudo apt-get install -y $PACKAGES
else if [ -x "$(command -v pacman)" ]
then
sudo pacman -S --needed --noconfirm $PACKAGES
else
printf '\n\nWARNING: could not find a package manager.\n'
printf 'Make sure the following packages are installed:\n'
printf " $PACKAGES\n\n"
fi
fi
}
#
# Use sfdisk to write a pre-prepared partition table.
#
# Currently the partition table is pre-configured for a 64GB device.
#
do_partition () {
printf '\n\n>>>> Partitioning\n\n'
build_from_template gpt.sfdisk
sudo sfdisk ${BLKDEV} < gpt.sfdisk
}
#
# Write the system firmware to the required partitions.
#
do_bootloader () {
printf '\n\n>>>> Installing Pinebook Pro boot firmware\n\n'
sudo dd if=bootloader/pinebook/filesystem/idbloader.img \
of=${PARTITION}1 bs=4096
sudo dd if=bootloader/pinebook/filesystem/uboot.img \
of=${PARTITION}2 bs=4096
sudo dd if=bootloader/pinebook/filesystem/trust.img \
of=${PARTITION}3 bs=4096
}
#
# Create the three filesystems.
#
# Currently the EFI filesystem is unused but it included here since it
# will be useful in the future when we enable EFI support in u-boot.
#
do_mkfs () {
printf '\n\n>>>> Formatting\n\n'
sudo mkfs.vfat -n EFI -F 32 ${PARTITION}4
sudo mkfs.ext4 -FL ${BLKNAME}-Boot ${PARTITION}5
if [ -z "$CRYPT" ]
then
sudo mkfs.ext4 -FL ${BLKNAME}-RootFS ${PARTITION}6
else
sudo cryptsetup luksFormat ${PARTITION}6
sudo cryptsetup open ${PARTITION}6 ${BLKNAME}-RootFS
sudo mkfs.ext4 -FL ${BLKNAME}-RootFS /dev/mapper/${BLKNAME}-RootFS
fi
}
do_mount () {
printf '\n\n>>>> Mounting target filesystem\n\n'
if [ -z "$CRYPT" ]
then
mkdir -p ${SYSIMAGE}
sudo mount ${PARTITION}6 ${SYSIMAGE}
sudo mkdir -p ${SYSIMAGE}/boot
sudo mount ${PARTITION}5 ${SYSIMAGE}/boot
sudo mkdir -p ${SYSIMAGE}/boot/efi
sudo mount ${PARTITION}4 ${SYSIMAGE}/boot/efi
else
mkdir -p ${SYSIMAGE}
[ ! -e /dev/mapper/${BLKNAME}-RootFS ] \
&& sudo cryptsetup open ${PARTITION}6 ${BLKNAME}-RootFS
sudo mount /dev/mapper/${BLKNAME}-RootFS ${SYSIMAGE}
sudo mkdir -p ${SYSIMAGE}/boot
sudo mount ${PARTITION}5 ${SYSIMAGE}/boot
sudo mkdir -p ${SYSIMAGE}/boot/efi
sudo mount ${PARTITION}4 ${SYSIMAGE}/boot/efi
fi
}
#
# Construct a minimal Debian root image
#
# Strictly speaking we are not fully minimal because we add some packages to
# handle keyboard mappings, networking (including remote login) and kernel
# updates.
#
do_debootstrap () {
printf '\n\n>>>> Installing base system\n\n'
# If there is no cached copy or the copy is older than
# seven days then regenerate from scratch
if [ ! -z ${DEBOOTSTRAP_PACKAGES} ] || \
[ ! -e debootstrap-${RELEASE}-${ARCH}.tar.gz ] || \
[ `age debootstrap-${RELEASE}-${ARCH}.tar.gz` -gt $((7*24*60*60)) ]
then
do_real_debootstrap
fi
sudo tar -C ${SYSIMAGE} -xf debootstrap-${RELEASE}-${ARCH}.tar.gz
printf '\n\n>>>> Creating Swapfile\n\n'
sudo dd if=/dev/zero of=${SYSIMAGE}/swapfile bs=1M count=2048 status=progress
sudo chmod 600 ${SYSIMAGE}/swapfile
sudo mkswap ${SYSIMAGE}/swapfile
build_from_template etc/fstab
sudo install -m644 etc/fstab ${SYSIMAGE}/etc/fstab
sudo install -m644 etc/tmpfiles.d/* ${SYSIMAGE}/etc/tmpfiles.d
sudo install -m644 etc/apt/sources.list.${RELEASE} ${SYSIMAGE}/etc/apt/sources.list
if [ ! -z "$CRYPT" ]
then
build_from_template etc/fstab.crypt
sudo install -m644 etc/fstab.crypt ${SYSIMAGE}/etc/fstab
build_from_template etc/crypttab
sudo install -m644 etc/crypttab ${SYSIMAGE}/etc/crypttab
fi
# Steal network config from the host filesystem. Normally debootstrap
# will do this for us but if we roam networks between generating the
# cached image and consuming it then we must update the file.
sudo install -m644 /etc/resolv.conf ${SYSIMAGE}/etc/resolv.conf
}
# The recipe to generate the tarball.
do_real_debootstrap () {
sudo debootstrap \
--arch=${ARCH} \
--include ca-certificates,console-setup,cryptsetup-initramfs,initramfs-tools,locales,keyboard-configuration,sudo,u-boot-menu,gnupg,plymouth,plymouth-themes${DEBOOTSTRAP_PACKAGES} \
${RELEASE} ${SYSIMAGE}
if [ -z $DRYRUN ]
then
sudo tar -C ${SYSIMAGE} -cf - . | pigz -9c > debootstrap-${RELEASE}-${ARCH}.tar.gz
else
echo "sudo tar -C ${SYSIMAGE} -cf - . | pigz -9c > debootstrap-${RELEASE}-${ARCH}.tar.gz"
fi
}
do_mount2 () {
printf '\n\n>>>> Mount special file systems\n\n'
for i in dev proc sys
do
sudo mkdir -p ${SYSIMAGE}/$i
sudo mount --bind /$i ${SYSIMAGE}/$i
done
}
# To save download time we cache the results of debootstrap... which
# means it may be out of date.
do_update () {
printf '\n\n>>>> Checking for updates\n\n'
on_target apt-get update
on_target apt-get upgrade -y
}
#
# We configure /etc/default/u-boot prior to installing the kernel since
# that means debian will automatically generate an extlinux.conf for us.
#
# Note that we have to create a dummy DT file (rk3399-pinebook-pro.dtb)
# since the u-boot integration will not include this in extlinux.conf
# if it does not exist... and we don't have a real one until we have copied
# it from the kernel image.
#
do_kernel () {
printf '\n\n>>>> Installing kernel\n\n'
cat etc/default/u-boot.append | sudo tee -a ${SYSIMAGE}/etc/default/u-boot > /dev/null
cat etc/initramfs-tools/modules.append | sudo tee -a ${SYSIMAGE}/etc/initramfs-tools/modules > /dev/null
sudo install -m644 etc/initramfs-tools/conf.d/* ${SYSIMAGE}/etc/initramfs-tools/conf.d/
if [ -f etc/apt/sources.list.d/kernel-obs.list.${RELEASE} ]
then
sudo install -m644 etc/apt/sources.list.d/kernel-obs.list.${RELEASE} ${SYSIMAGE}/etc/apt/sources.list.d/kernel-obs.list
else
# Use the bullseye kernel for releases we don't know about.
sudo install -m644 etc/apt/sources.list.d/kernel-obs.list.bullseye ${SYSIMAGE}/etc/apt/sources.list.d/kernel-obs.list
fi
sudo install -m644 etc/apt/trusted.gpg.d/* ${SYSIMAGE}/etc/apt/trusted.gpg.d/
sudo install etc/kernel/postinst.d/* ${SYSIMAGE}/etc/kernel/postinst.d/
sudo mkdir -p ${SYSIMAGE}/var/lib/alsa/
sudo install -m644 var/lib/alsa/asound.state ${SYSIMAGE}/var/lib/alsa/asound.state
on_target dpkg --add-architecture arm64
on_target apt-get update
on_target apt-get install -y linux-image-pinebookpro-arm64
}
do_firmware () {
printf '\n\n>>>> Installed additional firmware files\n\n'
sudo mkdir -p ${SYSIMAGE}/lib/firmware/{brcm,rockchip}
sudo cp -a ap6256-firmware/*.[bcht]* ${SYSIMAGE}/lib/firmware/brcm/
sudo cp -a pinebook-firmware/brcm/* ${SYSIMAGE}/lib/firmware/brcm/
sudo cp -a pinebook-firmware/rockchip/* ${SYSIMAGE}/lib/firmware/rockchip/
}
do_configure () {
printf '\n\n>>>> Basic configuration\n\n'
if [ -z $DRYRUN ]
then
read -p "Choose a hostname (or press Return to keep default): " NEWHOSTNAME
read -p "About to create main user, please enter username: " NEWUSER
else
NEWUSER=dryrunuser
fi
if [ ! -z "${NEWHOSTNAME}" ]
then
on_target bash -c "echo ${NEWHOSTNAME} > /etc/hostname"
fi
on_target adduser --add_extra_groups $NEWUSER
on_target adduser $NEWUSER sudo
on_target dpkg-reconfigure keyboard-configuration
on_target dpkg-reconfigure locales
on_target dpkg-reconfigure tzdata
}
do_tasksel () {
printf '\n\n>>>> Task selection\n\n'
on_target tasksel
}
do_umount () {
printf '\n\n>>>> umount\n\n'
for i in boot/efi boot dev proc sys
do
sudo umount ${SYSIMAGE}/$i && true
done
sudo umount ${SYSIMAGE} && true
if [ ! -z "$CRYPT" ]
then
sudo cryptsetup close /dev/mapper/${BLKNAME}-RootFS && true
fi
}
do_force_umount () {
printf "\n\n>>>> Forceful unmount of all ${BLKDEV} partitions\n\n"
for i in $(mount | grep ${BLKDEV} | cut -f1 -d\ )
do
echo "Unmounting $i"
sudo umount $i
done
}
do_clean () {
rm -f \
debootstrap-*.tar.gz \
gpt.sfdisk \
etc/crypttab \
etc/fstab \
etc/fstab.crypt
}
do_shell () {
trap do_umount EXIT
do_mount
do_mount2
on_target bash
}
do_all () {
do_prep
do_partition
do_bootloader
do_mkfs
trap do_umount EXIT
do_mount
do_debootstrap
do_mount2
do_update
do_kernel
do_firmware
do_configure
do_tasksel
}
ARCH=arm64
RELEASE=bullseye
SYSIMAGE="${PWD}/sysimage"
MMCBLK="$(readlink -f /dev/disk/by-path/platform-fe320000.*mmc)"
# For now we have *very* simple argument processing (similar to make)
unset DO
for i in "$@"
do
case "$i" in
do_*)
DO="$DO $i"
;;
*)
eval "$i"
esac
done
[ -z $DO ] && DO=do_all
[ -z $BLKDEV ] && BLKDEV=${MMCBLK}
[ -z $BLKNAME ] && BLKNAME=`basename ${BLKDEV}`
case "$BLKDEV" in
*[0-9])
PARTITION=${BLKDEV}p
;;
*)
PARTITION=${BLKDEV}
;;
esac
[ ! -z $DEBOOTSTRAP_PACKAGES ] && DEBOOTSTRAP_PACKAGES=,${DEBOOTSTRAP_PACKAGES}
if [ ! -z $DRYRUN ]
then
conceal git
conceal sudo
else
set -e
fi
for i in $DO
do
$i
done