-
Notifications
You must be signed in to change notification settings - Fork 1
/
prepare.sh
402 lines (387 loc) · 14 KB
/
prepare.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
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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
#!/usr/bin/env bash
###
# File: prepare.sh
# Author: Leopold Meinel ([email protected])
# -----
# Copyright (c) 2024 Leopold Meinel & contributors
# SPDX ID: GPL-3.0-or-later
# URL: https://www.gnu.org/licenses/gpl-3.0-standalone.html
# -----
###
# Source config
SCRIPT_DIR="$(dirname -- "$(readlink -f -- "$0")")"
source "$SCRIPT_DIR/install.conf"
# Fail on error
set -e
# Define functions
sed_exit() {
echo "ERROR: 'sed' didn't replace, report this @"
echo " https://github.com/leomeinel/arch-install/issues"
exit 1
}
# Unmount everything from /mnt
mountpoint -q /mnt &&
umount -AR /mnt
# Prompt user for RAID
read -rp "Set up RAID? (Type 'yes' in capital letters): " choice
case "$choice" in
YES)
## Detect disks
readarray -t DISKS < <(lsblk -drnpo NAME -I 259,8,254 | tr -d "[:blank:]")
DISKS_LENGTH="${#DISKS[@]}"
for ((i = 0; i < DISKS_LENGTH; i++)); do
udevadm info -q property --property=ID_BUS --value "${DISKS[$i]}" | grep -q "usb" &&
{
unset 'DISKS[$i]'
continue
}
DISKS=("${DISKS[@]}")
done
[[ "${#DISKS[@]}" -lt 2 ]] &&
{
echo "ERROR: There are less than 2 disks attached!"
exit 1
}
[[ "${#DISKS[@]}" -gt 2 ]] &&
{
echo "WARNING: There are more than 2 disks attached!"
lsblk -drnpo SIZE,NAME,MODEL,LABEL -I 259,8,254
### Prompt user to select 2 RAID members
read -rp "Which disk should be the first RAID member? (Type '/dev/sdX' fex.): " choice0
read -rp "Which disk should be the second RAID member? (Type '/dev/sdY' fex.): " choice1
if [[ "$(tr -d "[:space:]" <<<"$choice0")" != "$(tr -d "[:space:]" <<<"$choice1")" ]] && lsblk -drnpo SIZE,NAME,MODEL,LABEL -I 259,8,254 "$choice0" "$choice1"; then
echo "Using $choice0 and $choice1 for installation."
DISKS=("$choice0" "$choice1")
else
echo "ERROR: Drives not suitable for installation!"
exit 1
fi
}
## Set size for partition of larger disk
SIZE1="$(lsblk -drnbo SIZE "${DISKS[0]}" | tr -d "[:space:]")"
SIZE2="$(lsblk -drnbo SIZE "${DISKS[1]}" | tr -d "[:space:]")"
if [[ "$SIZE1" -eq "$SIZE2" ]]; then
DISK1="${DISKS[0]}"
DISK2="${DISKS[1]}"
PART_SIZE=0
else
echo "WARNING: The attached disks don't have the same size!"
echo " The larger disk will have unpartitioned space remaining."
if [[ "$SIZE1" -gt "$SIZE2" ]]; then
DISK1="${DISKS[0]}"
DISK2="${DISKS[1]}"
PART_SIZE="$((-(("$SIZE1" - "$SIZE2") / 1024)))K"
else
DISK1="${DISKS[1]}"
DISK2="${DISKS[0]}"
PART_SIZE="$((-(("$SIZE2" - "$SIZE1") / 1024)))K"
fi
fi
## Prompt user to confirm erasure
read -rp "Erase $DISK1 and $DISK2? (Type 'yes' in capital letters): " choice
case "$choice" in
YES)
echo "Erasing $DISK1 and $DISK2..."
;;
*)
echo "ERROR: User aborted erasing ${DISK1} and ${DISK2}!"
exit 1
;;
esac
## Detect & close old crypt volumes
if lsblk -rno TYPE | grep -q "crypt"; then
OLD_CRYPT_0="$(lsblk -Mrno TYPE,NAME | grep "crypt" | sed 's/crypt//' | sed -n '1p' | tr -d "[:space:]")"
cryptsetup close "$OLD_CRYPT_0"
fi
## Detect & erase old crypt/raid1 volumes
if lsblk -rno TYPE | grep -q "raid1"; then
OLD_DISK1P2="$(lsblk -rnpo TYPE,NAME "$DISK1" | grep "part" | sed 's/part//' | sed -n '2p' | tr -d "[:space:]")"
OLD_DISK2P2="$(lsblk -rnpo TYPE,NAME "$DISK2" | grep "part" | sed 's/part//' | sed -n '2p' | tr -d "[:space:]")"
OLD_RAID_0="$(lsblk -Mrnpo TYPE,NAME | grep "raid1" | sed 's/raid1//' | sed -n '1p' | tr -d "[:space:]")"
if cryptsetup isLuks "$OLD_RAID_0"; then
cryptsetup erase "$OLD_RAID_0"
fi
sgdisk -Z "$OLD_RAID_0"
mdadm --stop "$OLD_RAID_0"
mdadm --zero-superblock "$OLD_DISK1P2"
mdadm --zero-superblock "$OLD_DISK2P2"
fi
;;
*)
## Prompt user for disk
## NOTE: USB will be valid to allow external SSDs
lsblk -drnpo SIZE,NAME,MODEL,LABEL -I 259,8,254
read -rp "Which disk do you want to erase? (Type '/dev/sdX' fex.): " choice
if lsblk -drnpo SIZE,NAME,MODEL,LABEL -I 259,8,254 "$choice"; then
echo "Erasing $choice..."
DISK1="$choice"
else
echo "ERROR: Drive not suitable for installation!"
exit 1
fi
## Deactivate all vgs
vgchange -an
## Detect, close & erase old crypt volumes
if lsblk -rno TYPE "$DISK1" | grep -q "crypt"; then
OLD_CRYPT_0="$(lsblk -Mrno TYPE,NAME "$DISK1" | grep "crypt" | sed 's/crypt//' | sed -n '1p' | tr -d "[:space:]")"
OLD_DISK1P2="$(lsblk -rnpo TYPE,NAME "$DISK1" | grep "part" | sed 's/part//' | sed -n '2p' | tr -d "[:space:]")"
### Close old crypt volumes
cryptsetup close "$OLD_CRYPT_0"
### Erase old crypt volumes
if cryptsetup isLuks "$OLD_DISK1P2"; then
cryptsetup erase "$OLD_DISK1P2"
sgdisk -Z "$OLD_DISK1P2"
else
echo "ERROR: Can't erase old crypt volume!"
exit 1
fi
fi
;;
esac
# Load $KEYMAP & set time
loadkeys "$KEYMAP"
timedatectl set-ntp true
timedatectl set-timezone "$TIMEZONE"
# Erase & partition disks
sgdisk -Z "$DISK1"
sgdisk -n 0:0:+1G -t 1:ef00 "$DISK1"
if [[ -n "$DISK2" ]]; then
sgdisk -n 0:0:"$PART_SIZE" -t 2:fd00 "$DISK1"
sgdisk -Z "$DISK2"
sgdisk -n 0:0:+1G -t 1:ef00 "$DISK2"
sgdisk -n 0:0:0 -t 2:fd00 "$DISK2"
else
sgdisk -n 0:0:0 -t 2:8300 "$DISK1"
fi
# Configure raid and encryption
DISK1P1="$(lsblk -rnpo TYPE,NAME "$DISK1" | grep "part" | sed 's/part//' | sed -n '1p' | tr -d "[:space:]")"
DISK1P2="$(lsblk -rnpo TYPE,NAME "$DISK1" | grep "part" | sed 's/part//' | sed -n '2p' | tr -d "[:space:]")"
if [[ -n "$DISK2" ]]; then
DISK2P1="$(lsblk -rnpo TYPE,NAME "$DISK2" | grep "part" | sed 's/part//' | sed -n '1p' | tr -d "[:space:]")"
DISK2P2="$(lsblk -rnpo TYPE,NAME "$DISK2" | grep "part" | sed 's/part//' | sed -n '2p' | tr -d "[:space:]")"
## Configure raid1
RAID_DEVICE=/dev/md/md0
mdadm --create --verbose --level=1 --metadata=1.2 --raid-devices=2 --homehost=any --name=md0 "$RAID_DEVICE" "$DISK1P2" "$DISK2P2"
## Configure encryption
read -rp "Secure wipe $DISK1 and $DISK2? (Type 'yes' in capital letters): " choice
if [[ "$choice" == "YES" ]]; then
## Don't fail on error
set +e
dd if=/dev/urandom of="$RAID_DEVICE" bs="$(stat -c "%o" "$RAID_DEVICE")" status=progress
## Fail on error
set -e
fi
cryptsetup -y -v luksFormat "$RAID_DEVICE"
cryptsetup open "$RAID_DEVICE" md0_crypt
else
## Configure encryption
read -rp "Secure wipe $DISK1? (Type 'yes' in capital letters): " choice
if [[ "$choice" == "YES" ]]; then
## Don't fail on error
set +e
dd if=/dev/urandom of="$DISK1P2" bs="$(stat -c "%o" "$DISK1P2")" status=progress
## Fail on error
set -e
fi
cryptsetup -y -v luksFormat "$DISK1P2"
cryptsetup open "$DISK1P2" md0_crypt
fi
# Configure lvm
pvcreate /dev/mapper/md0_crypt
vgcreate vg0 /dev/mapper/md0_crypt
lvcreate -l "${DISK_ALLOCATION[0]}" vg0 -n lv0
lvcreate -l "${DISK_ALLOCATION[1]}" vg0 -n lv1
lvcreate -l "${DISK_ALLOCATION[2]}" vg0 -n lv2
lvcreate -l "${DISK_ALLOCATION[3]}" vg0 -n lv3
# Format efi
mkfs.fat -n EFI -F32 "$DISK1P1"
[[ -n "$DISK2" ]] &&
mkfs.fat -n EFI -F32 "$DISK2P1"
# Configure mounts
## Create subvolumes
SUBVOLUMES_LENGTH="${#SUBVOLUMES[@]}"
[[ "$SUBVOLUMES_LENGTH" -ne "${#CONFIGS[@]}" ]] &&
{
echo "ERROR: SUBVOLUMES and CONFIGS aren't the same length!"
exit 1
}
create_subs0() {
mkfs.btrfs -L "$3" "$4"
mount "$4" /mnt
btrfs subvolume create "/mnt/@$2"
btrfs subvolume create "/mnt/@${2}_snapshots"
create_subs1 "$1"
umount /mnt
}
create_subs1() {
for ((a = 0; a < SUBVOLUMES_LENGTH; a++)); do
if [[ "${SUBVOLUMES[$a]}" != "$1" ]] && grep -nq "^$1" <<<"${SUBVOLUMES[$a]}"; then
btrfs subvolume create "/mnt/@${CONFIGS[$a]}"
btrfs subvolume create "/mnt/@${CONFIGS[$a]}_snapshots"
fi
done
}
LV0="/dev/mapper/vg0-lv0"
LV1="/dev/mapper/vg0-lv1"
LV2="/dev/mapper/vg0-lv2"
LV3="/dev/mapper/vg0-lv3"
for ((i = 0; i < SUBVOLUMES_LENGTH; i++)); do
case "${SUBVOLUMES[$i]}" in
"/")
mkfs.btrfs -L ROOT "$LV0"
mount "$LV0" /mnt
btrfs subvolume create /mnt/@
btrfs subvolume create /mnt/@snapshots
umount /mnt
;;
"/usr/")
create_subs0 "${SUBVOLUMES[$i]}" "${CONFIGS[$i]}" "USR" "$LV1"
;;
"/var/")
create_subs0 "${SUBVOLUMES[$i]}" "${CONFIGS[$i]}" "VAR" "$LV2"
;;
"/home/")
create_subs0 "${SUBVOLUMES[$i]}" "${CONFIGS[$i]}" "HOME" "$LV3"
;;
esac
done
## Mount subvolumes
OPTIONS0="noatime,space_cache=v2,compress=zstd,ssd,discard=async,subvol=/@"
OPTIONS1="nodev,noatime,space_cache=v2,compress=zstd,ssd,discard=async,subvol=/@"
OPTIONS2="nodev,nosuid,noatime,space_cache=v2,compress=zstd,ssd,discard=async,subvol=/@"
OPTIONS3="noexec,nodev,nosuid,noatime,space_cache=v2,compress=zstd,ssd,discard=async,subvol=/@"
mount_subs0() {
mount --mkdir -o "$3$2" "$4" "/mnt$1"
mount --mkdir -o "$OPTIONS3${2}_snapshots" "$4" "/mnt$1.snapshots"
mount_subs1 "$1" "$3" "$4"
}
mount_subs1() {
for ((a = 0; a < SUBVOLUMES_LENGTH; a++)); do
if [[ "${SUBVOLUMES[$a]}" != "$1" ]] && grep -nq "^$1" <<<"${SUBVOLUMES[$a]}"; then
if grep -nq "^${1}lib/" <<<"${SUBVOLUMES[$a]}" && ! grep -nq "^${1}lib/flatpak/" <<<"${SUBVOLUMES[$a]}"; then
mount --mkdir -o "$OPTIONS3${CONFIGS[$a]}" "$3" "/mnt${SUBVOLUMES[$a]}"
else
mount --mkdir -o "$2${CONFIGS[$a]}" "$3" "/mnt${SUBVOLUMES[$a]}"
fi
mount --mkdir -o "$OPTIONS3${CONFIGS[$a]}_snapshots" "$3" "/mnt${SUBVOLUMES[$a]}.snapshots"
fi
done
}
for ((i = 0; i < SUBVOLUMES_LENGTH; i++)); do
case "${SUBVOLUMES[$i]}" in
"/")
mount -o "$OPTIONS0" "$LV0" "/mnt${SUBVOLUMES[$i]}"
mount --mkdir -o "${OPTIONS3}snapshots" "$LV0" "/mnt${SUBVOLUMES[$i]}.snapshots"
;;
"/usr/")
mount_subs0 "${SUBVOLUMES[$i]}" "${CONFIGS[$i]}" "$OPTIONS1" "$LV1"
;;
"/var/")
mount_subs0 "${SUBVOLUMES[$i]}" "${CONFIGS[$i]}" "$OPTIONS2" "$LV2"
;;
"/home/")
mount_subs0 "${SUBVOLUMES[$i]}" "${CONFIGS[$i]}" "$OPTIONS2" "$LV3"
;;
esac
done
chmod 775 /mnt/var/games
## /efi
OPTIONS4="noexec,nodev,nosuid,noatime,fmask=0077,dmask=0077"
mount --mkdir -o "$OPTIONS4" "$DISK1P1" /mnt/efi
[[ -n "$DISK2" ]] &&
mount --mkdir -o "$OPTIONS4" "$DISK2P1" /mnt/.efi.bak
## /boot
mkdir -p /mnt/boot
# Set SSD state to "frozen" after sleep
for link in /dev/disk/by-id/*; do
if [[ "$(readlink -f "$link")" = "$DISK1" ]]; then
DISK1ID="$link"
fi
if [[ -n "$DISK2" ]] && [[ "$(readlink -f "$link")" = "$DISK2" ]]; then
DISK2ID="$link"
fi
done
if [[ -n "$DISK1ID" ]]; then
mkdir -p /mnt/usr/lib/systemd/system-sleep
{
echo 'if [[ "$1" = "post" ]]; then'
echo ' sleep 1'
echo ' if hdparm --security-freeze '"$DISK1ID"'; then'
echo ' logger "$0: SSD freeze command executed successfully"'
echo ' else'
echo ' logger "$0: SSD freeze command failed"'
echo ' fi'
} >/mnt/usr/lib/systemd/system-sleep/freeze-ssd.sh
if [[ -n "$DISK2ID" ]]; then
{
echo ' if hdparm --security-freeze '"$DISK2ID"'; then'
echo ' logger "$0: SSD freeze command executed successfully"'
echo ' else'
echo ' logger "$0: SSD freeze command failed"'
echo ' fi'
} >>/mnt/usr/lib/systemd/system-sleep/freeze-ssd.sh
fi
echo 'fi' >>/mnt/usr/lib/systemd/system-sleep/freeze-ssd.sh
chmod 755 /mnt/usr/lib/systemd/system-sleep/freeze-ssd.sh
fi
# Install packages
## START sed
FILE=/etc/pacman.conf
STRING="^#Color"
grep -q "$STRING" "$FILE" || sed_exit
sed -i "s/$STRING/Color/" "$FILE"
STRING="^#ParallelDownloads =.*"
grep -q "$STRING" "$FILE" || sed_exit
sed -i "s/$STRING/ParallelDownloads = 10/" "$FILE"
STRING="^#NoProgressBar"
grep -q "$STRING" "$FILE" || sed_exit
sed -i "s/$STRING/NoProgressBar/" "$FILE"
## END sed
reflector --save /etc/pacman.d/mirrorlist --country "$MIRRORCOUNTRIES" --protocol https --latest 20 --sort rate
pacman -Syy
pacman -S --noprogressbar --noconfirm --needed lshw
[[ -n "$DISK2" ]] &&
echo "mdadm" >>"$SCRIPT_DIR/pkgs-prepare.txt"
[[ -d "/proc/acpi/button/lid" ]] &&
{
echo "tlp"
echo "tlp-rdw"
} >>"$SCRIPT_DIR/pkgs-prepare.txt"
lscpu | grep "Vendor ID:" | grep -q "GenuineIntel" &&
echo "intel-ucode" >>"$SCRIPT_DIR/pkgs-prepare.txt"
lscpu | grep "Vendor ID:" | grep -q "AuthenticAMD" &&
echo "amd-ucode" >>"$SCRIPT_DIR/pkgs-prepare.txt"
lshw -C display | grep "vendor:" | grep -q "Advanced Micro Devices, Inc." &&
{
echo "vulkan-radeon"
echo "xf86-video-amdgpu"
} >>"$SCRIPT_DIR/pkgs-prepare.txt"
lshw -C display | grep "vendor:" | grep -q "Intel Corporation" &&
{
echo "intel-media-driver"
echo "vulkan-intel"
echo "xf86-video-intel"
} >>"$SCRIPT_DIR"/pkgs-prepare.txt
pacstrap -K /mnt - <"$SCRIPT_DIR/pkgs-prepare.txt"
# Configure /mnt/etc/fstab
genfstab -U /mnt >>/mnt/etc/fstab
{
echo '# tmpfs'
echo 'tmpfs /dev/shm tmpfs rw,noexec,nodev,nosuid 0 0'
echo 'tmpfs /tmp tmpfs rw,nodev,nosuid,uid=0,gid=0,mode=1700 0 0'
} >>/mnt/etc/fstab
[[ -n "$DISK2" ]] &&
{
## START sed
FILE=/mnt/etc/fstab
STRING0="\/.efi.bak.*vfat"
grep -q "$STRING0" "$FILE" || sed_exit
STRING1="rw"
grep -q "$STRING1" "$FILE" || sed_exit
sed -i "/$STRING0/s/$STRING1/$STRING1,noauto/" "$FILE"
## END sed
}
# Prepare /mnt/git/arch-install
mkdir -p /mnt/git
mv "$SCRIPT_DIR" /mnt/git/
chmod +x /mnt/git/arch-install/setup.sh