-
Notifications
You must be signed in to change notification settings - Fork 0
/
installer-vm
359 lines (289 loc) · 9.32 KB
/
installer-vm
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
#!/bin/sh
#
# NO COPYRIGHT.
#
# The person who associated a work with this deed has dedicated the work
# to the public domain by waiving all of his or her rights to the work
# worldwide under copyright law, including all related and neighboring
# rights, to the extent allowed by law.
#
# You can copy, modify, distribute and perform the work, even for
# commercial purposes, all without asking permission.
#
# See https://creativecommons.org/publicdomain/zero/1.0/ for additional
# information.
#
###########################################################################
# Only set this when you're happy with all the variables below.
# Running with this set to '1' is a destructive operation.
CONFIGURED=0
###########################################################################
# Names.
USER="me" # Initial non-root user XXX make, chown
HOST="mybox" # hostname
DOMAIN="my.domain" # The remaining bits of FQDN to follow hostname
# General parameters for the install.
SWAPSIZE=4096 # Size of swap in megabytes
# Block device
DA=/dev/vda # First drive, virtio.
#DA=/dev/sda # First drive, scsi/sata.
# Distribution-specific variables.
RELEASE="bullseye"
REPOSITORY="http://deb.debian.org/debian"
SETS="main contrib non-free"
SECURITY_REPOSITORY="http://security.debian.org/debian-security"
# Local cache.
CACHE="http://cache.${DOMAIN}:3142"
export http_proxy="${CACHE}";
# GPT labels, 0 is for the first disk, 1 for the second if mirroring
BIOSBOOT0=grub0 # biosboot
BOOT0=boot0 # /boot
ROOT0=root0 # main vg
###########################################################################
###########################################################################
# Nothing below here should normally need to be modified. #
###########################################################################
###########################################################################
#
# Last ditch sanity check.
#
if [ ${CONFIGURED} -ne 1 ]; then
echo "Please carefully examine and set configuration" \
"variables before proceeding."
exit 1
fi
#
# Confirm each step before we run it.
#
confirm_step()
{
STEP=$(expr $STEP + 1)
echo
read -p "Run step ${STEP}: $*? (y/N) " RESPONSE
if [ "X${RESPONSE}" = "X" ]; then
RESPONSE="N"
fi
if [ "${RESPONSE}" = "Y" -o "${RESPONSE}" = "y" ]; then
return 0
else
return 1
fi
}
#
# Simplistic try/catch handler.
#
newtry()
{
ERRORS=0
}
try()
{
"${@}"
ERRORS=$((${ERRORS} + ${?}))
}
catch()
{
if [ ${ERRORS} -gt 0 ]; then
echo "Error: ${1}"
exit 1
fi
}
STEP=0
###########################################################################
# If we're invoked as the thing inside the chroot...
# XXX maybe break this into steps as well
if [ "X${1}" = "Xchroot" ]; then
export TERM=linux
#
# Free software is about choice!
#
mkdir -p /mnt/etc/apt/preferences.d
cat <<END > /mnt/etc/apt/preferences.d/no-systemd
Package: systemd
Pin: release *
Pin-Priority: -1
Package: systemd-sysv
Pin: release *
Pin-Priority: -1
Package: libnss-systemd
Pin: release *
Pin-Priority: -1
END
cat > /etc/apt/apt.conf.d/02proxy <<END
Acquire::http::Proxy "${CACHE}";
END
apt update
apt install linux-image-amd64
apt-cache dumpavail | perl -00 -ne '/^Package: (.*)/m && print "$1\n"
if (/^Priority: (?:required|important|standard)/m
and ! /^Package: .*systemd/m)' \
| xargs apt --yes --allow-downgrades install
apt install --yes firmware-linux-nonfree
apt install --yes curl bsd-mailx locales man
apt install --yes vim-nox net-tools ifupdown bash-completion patch
locale-gen en_US.UTF-8
dpkg-reconfigure locales
dpkg-reconfigure tzdata
tasksel --task-packages standard | xargs apt install --yes
# XXX install from outside - all of this stuff that doesn't require
# user interaction
apt install --yes mdadm openssh-server rsync console-setup-linux
mdadm --detail --scan > /etc/mdadm/mdadm.conf
ln -s /proc/mounts /etc/mtab
echo "Setting password for root account:"
passwd root
echo "RESUME=none" >> /etc/initramfs-tools/initramfs.conf
update-initramfs -c -k all
apt install --yes grub-pc
grub-install ${DA}
update-grub
adduser --add_extra_groups ${USER}
chown -R ${USER}:${USER} /home/${USER}
exit
fi
###########################################################################
if confirm_step "install prerequisites"; then
newtry
echo > /etc/apt/apt.conf.d/02proxy <<END
Acquire::http::Proxy "${CACHE}";
END
# Flesh out the install environment repositories - kernel n-1 can
# be found here sometimes, example being linux-image-5.10.0-15-amd64
echo "deb ${REPOSITORY} ${RELEASE} ${SETS}" \
> /etc/apt/sources.list
echo "deb ${REPOSITORY} ${RELEASE}-updates ${SETS}" \
>> /etc/apt/sources.list
if [ -n "${SECURITY_REPOSITORY}" ]; then
echo "deb ${SECURITY_REPOSITORY} ${RELEASE}-security ${SETS}" \
>> /etc/apt/sources.list
fi
try apt update
try apt install --yes gdisk debootstrap rsync mdadm parted
catch "prerequisites install failed"
fi
###########################################################################
if confirm_step "clear disk(s) and create partitions"; then
sgdisk --zap-all ${DA}
sgdisk --clear ${DA}
# BIOSBOOT
sgdisk -n 1:$(sgdisk -F ${DA}):+128K -t 1:ef02 ${DA}
# /boot
sgdisk -n 2:$(sgdisk -F ${DA}):+512M -t 2:8300 ${DA}
# root
sgdisk -n 3:$(sgdisk -F ${DA}):0 -t 3:8300 ${DA}
sgdisk --change-name=2:${BOOT0} --change-name=3:${ROOT0} ${DA}
pvcreate /dev/disk/by-partlabel/${ROOT0}
vgcreate rootvg /dev/disk/by-partlabel/${ROOT0}
lvcreate -L ${SWAPSIZE}m -n swap rootvg
lvcreate -l 100%FREE -n root rootvg
fi
###########################################################################
if confirm_step "format /boot, swap, and root"; then
#
# Format boot partition.
#
newtry
try mkfs.ext4 /dev/disk/by-partlabel/${BOOT0}
catch "boot format failed"
#
# Format swap partition.
#
newtry
try mkswap /dev/rootvg/swap
catch "swap format failed"
#
# Format root partition.
#
newtry
try mkfs.ext4 /dev/rootvg/root
catch "root format failed"
fi
###########################################################################
if confirm_step "mount datasets into /mnt"; then
newtry
try mount /dev/rootvg/root /mnt
catch mount root failed
newtry
try mkdir -p /mnt/boot
try mount /dev/disk/by-partlabel/${BOOT0} /mnt/boot
catch mount /boot failed
mkdir -p /mnt/tmp
chmod 1777 /mnt/tmp
mkdir -p /mnt/var/tmp
chmod 1777 /mnt/var/tmp
fi
###########################################################################
if confirm_step "run debootstrap"; then
# Free software is about choice!
mkdir -p /mnt/etc/apt/preferences.d
cat <<END > /mnt/etc/apt/preferences.d/no-systemd
Package: systemd
Pin: release *
Pin-Priority: -1
Package: systemd:i386
Pin: release *
Pin-Priority: -1
Package: systemd-sysv
Pin: release *
Pin-Priority: -1
Package: libnss-systemd
Pin: release *
Pin-Priority: -1
END
newtry
try debootstrap --no-merged-usr --arch=amd64 \
--include=sysvinit-core,libelogind0,lvm2 --variant=minbase \
--exclude=systemd,systemd-sysv,libnss-systemd ${RELEASE} /mnt
catch "debootstrap failed"
fi
###########################################################################
if confirm_step "populate /mnt/etc/fstab"; then
cat <<END > /mnt/etc/fstab
/dev/rootvg/root / ext4 defaults 0 0
/dev/disk/by-partlabel/boot0 /boot ext4 defaults 0 0
/dev/rootvg/swap none swap sw 0 0
END
fi
###########################################################################
if confirm_step "populate /etc/hostname, /etc/default/locale"; then
echo ${HOST} > /mnt/etc/hostname
cat >> /mnt/etc/hosts <<END
127.0.1.1 ${HOST}.${DOMAIN} ${HOST}
END
# Set a default locale.
# XXX let the user set this, above
echo 'LANG="en_US.UTF-8"' > /mnt/etc/default/locale
fi
###########################################################################
if confirm_step "populate apt sources"; then
echo "deb ${REPOSITORY} ${RELEASE} ${SETS}" \
> /mnt/etc/apt/sources.list
echo "deb ${REPOSITORY} ${RELEASE}-updates ${SETS}" \
>> /mnt/etc/apt/sources.list
if [ -n "${SECURITY_REPOSITORY}" ]; then
echo "deb ${SECURITY_REPOSITORY} ${RELEASE}-security ${SETS}" \
>> /mnt/etc/apt/sources.list
fi
fi
###########################################################################
if confirm_step "establish bind mounts from /dev, /proc, /sys"; then
#
# Bind mounts in preparation for chroot.
#
newtry
try mount --rbind /dev /mnt/dev
try mount --rbind /proc /mnt/proc
try mount --rbind /sys /mnt/sys
catch "bind mount failed"
fi
###########################################################################
if confirm_step "copy installer into chroot and execute"; then
cp /etc/resolv.conf /mnt/etc/resolv.conf
cp ${0} /mnt/root/chrooted.sh
chmod 0755 /mnt/root/chrooted.sh
chroot /mnt /root/chrooted.sh chroot
rm /mnt/root/chrooted.sh
fi
###########################################################################
echo
echo "We've gotten to the end! Enjoy!"