-
Notifications
You must be signed in to change notification settings - Fork 0
/
liveinstall.sh
executable file
Β·285 lines (241 loc) Β· 7.14 KB
/
liveinstall.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
#!/usr/bin/env bash
set -euo pipefail
SUPPORTED_KERNELS=(linux linux-lts linux-zen)
SUPPORTED_FILESYSTEMS=(ext4 btrfs)
declare -A FS_UTILS_PACKAGES=(
[ext4]="e2fsprogs"
[btrfs]="btrfs-progs"
)
# shellcheck disable=SC2034
COLOR_INFO="#89dceb"
# shellcheck disable=SC2034
COLOR_WARNING="#f9e2af"
# shellcheck disable=SC2034
COLOR_ERROR="#f38ba8"
TARGET_DISK=${TARGET_DISK:-${1:-}}
TARGET_HOSTNAME=${TARGET_HOSTNAME:-}
TARGET_USER=${TARGET_USER:-}
TARGET_KERNEL=${TARGET_KERNEL:-}
TARGET_FILESYSTEM=${TARGET_FILESYSTEM:-}
FORCE=${FORCE:-0}
DRY_RUN=${DRY_RUN:-0}
DEBUG=${DEBUG:-0}
EDITOR_PACKAGE=${EDITOR_PACKAGE:-neovim}
SHELL_PACKAGE=${SHELL_PACKAGE:-fish}
FS_UTILS_PACKAGE=
ADDITIONAL_PACKAGES=${ADDITIONAL_PACKAGES:-}
feedback() {
level=$1 && shift
if ! command -v gum &>/dev/null; then
echo "$level: " "$@"
return
fi
var="COLOR_${level^^}"
color=${!var:-}
echo "$(gum style --foreground="$color" "$level:")" "$@"
} >&2
die() {
feedback ERROR "$@"
if [ "$DEBUG" = true ] || [ "$DEBUG" = "1" ]; then
echo
echo "Debug information:"
echo "=================="
echo
echo "FUNCNAME: ${FUNCNAME[*]}"
echo "BASH_LINENO: ${BASH_LINENO[*]}"
else
echo
echo "Run with DEBUG=1 for more information."
fi
exit 1
} >&2
contains() {
declare word
declare -n array="$1"
for word in "${array[@]}"; do
[ "$word" = "$2" ] && return 0
done
return 1
}
invariant_one() {
declare result=
while read -r line; do
[ -n "$result" ] && die "Invariant violated: multiple results found."
result=$line
done
[ -z "$result" ] && die "Invariant violated: no results found."
echo "$result"
}
confirm() {
gum confirm "$@"
}
input() {
prompt=$1 && shift
gum input --prompt="$prompt" "$@"
}
input_password() {
gum input --prompt="$prompt" --password "$@"
}
choose() {
header=$1 && shift
gum choose --header="$header" "$@"
}
spin() {
title=$1 && shift
if ! command -v gum &>/dev/null; then
echo -n "$title"
"$@" &>/dev/null && echo
return
fi
gum spin --title="$title" -- "$@"
}
ensure_gum_installed() {
command -v gum &>/dev/null || pacman -Sy --noconfirm --needed gum
}
if [ "$DRY_RUN" = true ] || [ "$DRY_RUN" = "1" ]; then
feedback INFO "Dry run mode enabled."
fi
if [ "$(id -u)" -ne 0 ]; then
feedback ERROR "This script must be run as root."
exit 1
fi
if ! [ -d /sys/firmware/efi/efivars ]; then
feedback ERROR "UEFI is not enabled. This script is only for UEFI systems."
exit 1
fi
if [ -z "$TARGET_DISK" ]; then
feedback ERROR "No target disk specified. Specify a block device as an argument to this script (or with TARGET_DISK)."
exit 1
elif ! [ -b "$TARGET_DISK" ]; then
feedback ERROR "'$TARGET_DISK' is not a block device. Try listing block devices with \`lsblk\`."
exit 1
fi
ensure_gum_installed
if ! contains SUPPORTED_KERNELS "$TARGET_KERNEL"; then
TARGET_KERNEL=$(choose "Choose kernel" "${SUPPORTED_KERNELS[@]}")
fi
KERNEL_HEADERS="$TARGET_KERNEL-headers"
echo "Using kernel: $TARGET_KERNEL"
if ! contains SUPPORTED_FILESYSTEMS "$TARGET_FILESYSTEM"; then
TARGET_FILESYSTEM=$(choose "Choose filesystem" "${SUPPORTED_FILESYSTEMS[@]}")
fi
FS_UTILS_PACKAGE="${FS_UTILS_PACKAGES[$TARGET_FILESYSTEM]}"
echo "Using filesystem: $TARGET_FILESYSTEM"
while [ -z "$TARGET_HOSTNAME" ]; do
TARGET_HOSTNAME=$(input "Hostname: ")
[ -z "$TARGET_HOSTNAME" ] && {
feedback ERROR "Hostname cannot be empty."
}
done
echo "Using hostname: $TARGET_HOSTNAME"
while [ -z "$TARGET_USER" ]; do
TARGET_USER=$(input "Username: " --value=loqusion)
[ -z "$TARGET_USER" ] && {
feedback ERROR "Username cannot be empty."
}
done
echo "Using username: $TARGET_USER"
if [ "$DRY_RUN" = true ] || [ "$DRY_RUN" = "1" ]; then
echo
echo "TARGET_DISK: $TARGET_DISK"
echo "TARGET_HOSTNAME: $TARGET_HOSTNAME"
echo "TARGET_USER: $TARGET_USER"
echo "TARGET_KERNEL: $TARGET_KERNEL"
echo "TARGET_FILESYSTEM: $TARGET_FILESYSTEM"
echo "---"
echo "EDITOR_PACKAGE: $EDITOR_PACKAGE"
echo "SHELL_PACKAGE: $SHELL_PACKAGE"
echo "FS_UTILS_PACKAGE: $FS_UTILS_PACKAGE"
echo "ADDITIONAL_PACKAGES: $ADDITIONAL_PACKAGES"
echo
echo "Dry run mode enabled. Exiting."
exit 0
fi
if [ "$FORCE" != true ] && [ "$FORCE" != "1" ]; then
confirm "WARNING: This script will destroy all data on $TARGET_DISK. Continue?" || exit 1
fi
echo
sgdisk "$TARGET_DISK" -og
sgdisk "$TARGET_DISK" -n=1:0:+512M -t=1:ef00 -c=1:"EFI system partition"
sgdisk "$TARGET_DISK" -n=2:0:+4096M -t=2:8200 -c=2:"Linux swap"
sgdisk "$TARGET_DISK" -n=3:0:0 -t=3:830 -c=3:"Linux filesystem"
sgdisk "$TARGET_DISK" -p
BOOT_PARTITION=$(blkid -t PARTLABEL="EFI system partition" -o device | grep -F "$TARGET_DISK" | invariant_one)
SWAP_PARTITION=$(blkid -t PARTLABEL="Linux swap" -o device | grep -F "$TARGET_DISK" | invariant_one)
ROOT_PARTITION=$(blkid -t PARTLABEL="Linux filesystem" -o device | grep -F "$TARGET_DISK" | invariant_one)
mkfs.fat -F 32 -n EFI "$BOOT_PARTITION"
mkswap -L swap "$SWAP_PARTITION"
case "$TARGET_FILESYSTEM" in
btrfs)
mkfs.btrfs -f -L arch_os "$ROOT_PARTITION"
mount "$ROOT_PARTITION" /mnt
btrfs subvolume create /mnt/@
btrfs subvolume set-default /mnt/@
btrfs subvolume create /mnt/@home
btrfs subvolume create /mnt/@var_log
umount /mnt
mount -o subvol=@ "$ROOT_PARTITION" /mnt
mount -m -o subvol=@home "$ROOT_PARTITION" /mnt/home
mount -m -o subvol=@var_log "$ROOT_PARTITION" /mnt/var/log
mount -m "$BOOT_PARTITION" /mnt/boot
swapon "$SWAP_PARTITION"
;;
ext4)
mkfs.ext4 -L arch_os "$ROOT_PARTITION"
mount "$ROOT_PARTITION" /mnt
mount -m "$BOOT_PARTITION" /mnt/boot
swapon "$SWAP_PARTITION"
;;
*)
echo "ERROR: Unsupported filesystem: $TARGET_FILESYSTEM"
echo "Supported filesystems: ${SUPPORTED_FILESYSTEMS[*]}"
exit 1
;;
esac
systemctl restart reflector.service
# shellcheck disable=SC2086
pacstrap -K /mnt \
base base-devel alsa-utils \
"$TARGET_KERNEL" "$KERNEL_HEADERS" linux-firmware intel-ucode \
iwd dhcpcd openssh \
man-db man-pages texinfo \
git github-cli \
"$SHELL_PACKAGE" \
"$EDITOR_PACKAGE" \
"$FS_UTILS_PACKAGE" \
$ADDITIONAL_PACKAGES
genfstab -U /mnt >>/mnt/etc/fstab
arch-chroot /mnt ln -sf /usr/share/zoneinfo/America/Chicago /etc/localtime
arch-chroot /mnt hwclock --systohc
sed -i 's/#en_US.UTF-8 UTF-8/en_US.UTF-8 UTF-8/' /mnt/etc/locale.gen
arch-chroot /mnt locale-gen
echo "LANG=en_US.UTF-8" >/mnt/etc/locale.conf
echo "$TARGET_HOSTNAME" >/mnt/etc/hostname
arch-chroot /mnt mkinitcpio -P
arch-chroot /mnt bootctl install
cat >/mnt/boot/loader/loader.conf <<EOF
default arch.conf
timeout 0
console-mode max
editor yes
EOF
cat >/mnt/boot/loader/entries/arch.conf <<EOF
title Arch Linux
linux /vmlinuz-$TARGET_KERNEL
initrd /intel-ucode.img
initrd /initramfs-$TARGET_KERNEL.img
options root=LABEL=arch_os rw
EOF
cat >/mnt/boot/loader/entries/arch-fallback.conf <<EOF
title Arch Linux (fallback initramfs)
linux /vmlinuz-$TARGET_KERNEL
initrd /intel-ucode.img
initrd /initramfs-$TARGET_KERNEL-fallback.img
options root=LABEL=arch_os rw
EOF
arch-chroot /mnt useradd -m -G wheel -s "/usr/bin/$SHELL_PACKAGE" "$TARGET_USER"
arch-chroot /mnt passwd "$TARGET_USER"
sed -i 's/# %wheel ALL=(ALL:ALL) ALL/%wheel ALL=(ALL:ALL) ALL/' /mnt/etc/sudoers
umount -R /mnt
swapoff "$SWAP_PARTITION"
echo "Done! You can now reboot into your new Arch Linux installation."