-
Notifications
You must be signed in to change notification settings - Fork 4
/
make_image_1.sh
executable file
·210 lines (183 loc) · 5.75 KB
/
make_image_1.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
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
#!/bin/bash
# edit this:
DISK=/dev/vdb
DEBIAN_VERSION=bookworm
FSFLAGS="compress=zstd:9"
target=/target
root_device=${DISK}2
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
. ${SCRIPT_DIR}/_make_image_lib.sh
notify install required packages
apt-get update -y
DEBIAN_FRONTEND=noninteractive apt-get install -y debootstrap uuid-runtime btrfs-progs dosfstools
if [ ! -f efi-part.uuid ]; then
echo generate uuid for efi partition
uuidgen > efi-part.uuid
fi
if [ ! -f base-image-part.uuid ]; then
echo generate uuid for base image partition
uuidgen > base-image-part.uuid
fi
if [ ! -f top-part.uuid ]; then
echo generate uuid for top partition
uuidgen > top-part.uuid
fi
efi_uuid=$(cat efi-part.uuid)
base_image_uuid=$(cat base-image-part.uuid)
top_uuid=$(cat top-part.uuid)
if [ ! -f partitions_created.txt ]; then
# TODO mark the BaseImage partition as read-only (bit 60 - 0x1000000000000000)
notify create 2 partitions on ${DISK}
sfdisk $DISK <<EOF
label: gpt
unit: sectors
sector-size: 512
${DISK}1: start=2048, size=409600, type=uefi, name="EFI system partition", uuid=${efi_uuid}
${DISK}2: start=411648, size=409600, type=linux, name="BaseImage", uuid=${base_image_uuid}
EOF
notify resize the second partition on ${DISK} to fill available space
echo ", +" | sfdisk -N 2 $DISK
sfdisk -d $DISK > partitions_created.txt
fi
if [ ! -f btrfs_created.txt ]; then
notify create root filesystem on ${root_device}
mkfs.btrfs -f ${root_device} | tee btrfs_created.txt
fi
if [ ! -f vfat_created.txt ]; then
notify create esp filesystem on ${DISK}1
mkfs.vfat ${DISK}1 | tee vfat_created.txt
fi
if grep -qs "/mnt/btrfs1" /proc/mounts ; then
echo top-level subvolume already mounted on /mnt/btrfs1
else
notify mount top-level subvolume on /mnt/btrfs1
mkdir -p /mnt/btrfs1
mount ${root_device} /mnt/btrfs1 -o ${FSFLAGS},subvolid=5
fi
if [ ! -e /mnt/btrfs1/@ ]; then
notify create @, @swap and @home subvolumes on /mnt/btrfs1
btrfs subvolume create /mnt/btrfs1/@
btrfs subvolume create /mnt/btrfs1/@home
btrfs subvolume create /mnt/btrfs1/@swap
fi
if grep -qs "${target}" /proc/mounts ; then
echo root subvolume already mounted on ${target}
else
notify mount root and home subvolume on ${target}
mkdir -p ${target}
mount ${root_device} ${target} -o ${FSFLAGS},subvol=@
mkdir -p ${target}/home
mount ${root_device} ${target}/home -o ${FSFLAGS},subvol=@home
fi
mkdir -p ${target}/var/cache/apt/archives
if grep -qs "${target}/var/cache/apt/archives" /proc/mounts ; then
echo apt cache directory already bind mounted on target
else
notify bind mounting apt cache directory to target
mount /var/cache/apt/archives ${target}/var/cache/apt/archives -o bind
fi
if [ ! -f ${target}/etc/debian_version ]; then
notify install debian on ${target}
debootstrap ${DEBIAN_VERSION} ${target} http://deb.debian.org/debian
fi
if grep -qs "${target}/proc" /proc/mounts ; then
echo bind mounts already set up on ${target}
else
notify bind mount dev, proc, sys, run, var/tmp on ${target}
mount -t proc none ${target}/proc
mount --make-rslave --rbind /sys ${target}/sys
mount --make-rslave --rbind /dev ${target}/dev
mount --make-rslave --rbind /run ${target}/run
mount --make-rslave --rbind /var/tmp ${target}/var/tmp
fi
notify setup sources.list
cat <<EOF > ${target}/etc/apt/sources.list
deb http://deb.debian.org/debian ${DEBIAN_VERSION} main contrib non-free non-free-firmware
deb http://deb.debian.org/debian ${DEBIAN_VERSION}-updates main contrib non-free non-free-firmware
deb http://security.debian.org/debian-security ${DEBIAN_VERSION}-security main contrib non-free non-free-firmware
deb http://deb.debian.org/debian ${DEBIAN_VERSION}-backports main contrib non-free non-free-firmware
EOF
notify enable ${DEBIAN_VERSION}-backports
mkdir -p ${target}/etc/apt/preferences.d
cp "${SCRIPT_DIR}/installer-files/etc/apt/preferences.d/99backports-temp" "${target}/etc/apt/preferences.d/"
notify install required packages on ${target}
cat <<EOF > ${target}/tmp/packages.txt
locales
adduser
passwd
sudo
systemd
btrfs-progs
dosfstools
tasksel
network-manager
firmware-linux
bluez-firmware
dahdi-firmware-nonfree
firmware-amd-graphics
firmware-ath9k-htc
firmware-atheros
firmware-bnx2
firmware-bnx2x
firmware-brcm80211
firmware-cavium
firmware-intel-sound
firmware-iwlwifi
firmware-libertas
firmware-misc-nonfree
firmware-myricom
firmware-netronome
firmware-netxen
firmware-qcom-media
firmware-qcom-soc
firmware-qlogic
firmware-realtek
firmware-samsung
firmware-siano
firmware-ti-connectivity
firmware-tomu
firmware-zd1211
hdmi2usb-fx2-firmware
midisport-firmware
sigrok-firmware-fx2lafw
binutils
console-setup
cryptsetup
dmraid
exim4-daemon-light
kpartx
lvm2
mdadm
pigz
pkg-config
tpm2-tools
tpm-udev
EOF
cat <<EOF > ${target}/tmp/run2.sh
#!/bin/bash
export DEBIAN_FRONTEND=noninteractive
apt-get update
xargs apt-get install -y < /tmp/packages.txt
EOF
chroot ${target}/ bash /tmp/run2.sh
notify running tasksel
chroot ${target}/ tasksel
if grep -qs "${target}/var/cache/apt/archives" /proc/mounts ; then
notify unmounting apt cache directory from target
umount ${target}/var/cache/apt/archives
else
echo apt cache directory not mounted to target
fi
notify downloading remaining .deb files for the installer
chroot ${target}/ apt-get install -y --download-only locales systemd systemd-boot dracut btrfs-progs tasksel network-manager cryptsetup tpm2-tools linux-image-amd64 openssh-server
notify cleaning up
rm -f ${target}/etc/machine-id
rm -f ${target}/etc/crypttab
rm -f ${target}/var/log/*log
rm -f ${target}/var/log/apt/*log
shrink_btrfs_filesystem ${target}
echo umounting all filesystems
read -p "Enter to continue"
umount -R ${target}
umount -R /mnt/btrfs1
echo "NOW REBOOT AND CONTINUE WITH PART 2"