-
Notifications
You must be signed in to change notification settings - Fork 0
/
arch-setup.sh
430 lines (404 loc) · 12.4 KB
/
arch-setup.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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
############################
# #
# Configuration #
# #
############################
# the disk to setup your system
DEVICE= 'sda'
# your region information
REGION= 'Asia'
# your city
CITY= 'Shanghai'
# log file for temporary outputs
LOGFILE= '/tmp/arch-setup.txt'
# the mounting point for your root partition
MOUNTPOINT= '/mnt/disk'
# just the user name
USERNAME= 'y-jiji'
# your password, change it to yours (and i don't use this one for any of my machines)
PASSWORD= 'awesome-archlinux'
# host name, your shell prompt will look like user@hostname when you login
HOSTNAME= 'arch'
# disk partitions, currently there is a root partition and a boot partition
PARTITION= '
/ | primary ext4 1GiB 100% | | mkfs.ext4 -v
/boot | primary fat32 1MiB 1GiB | esp | mkfs.vfat -F32
'
# pacman mirrorlist, packages will be downloaded from the following servers.
MIRRORLIST= '
# US
# Server = http://mirror.cs.vt.edu/archlinux/$repo/os/$arch
# Server = http://www.gtlib.gatech.edu/pub/archlinux/$repo/os/$arch
# CN
# Server = https://mirrors.sjtug.sjtu.edu.cn/archlinux/$repo/os/$arch
# Server = http://mirrors.bfsu.edu.cn/archlinux/$repo/os/$arch
# Server = http://mirrors.aliyun.com/archlinux/$repo/os/$arch
'
# pacman package list, the following packages will be installed by default
PACKAGES= '
sudo vim git code
amd-ucode linux linux-firmware nvidia
gnome networkmanager
ttf-dejavu ttf-liberation
'
# hosts, namely how host names are mapped to ip-addresses
HOSTS= '
# IPv4 Hosts
127.0.0.1 localhost
::1 localhost
127.0.1.1 $1
# IPv6 Hosts
::1 localhost ip6-localhost ip6-loopback
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
'
##########################
# #
# Logger Setup #
# #
##########################
RED=$'\e[031m'
CYAN=$'\e[036m'
BOLD=$'\e[1m'
GREY=$'\e[38;5;7m'
STOPCOLOR=$'\e[0m'
trim() {
echo -e "${@}" | sed -e 's/^[[:space:]]*//' | awk 'NF' -
}
path-fmt() {
echo -e "${@}" | sed "s/\/$//" | sed "s/\/+/\//"
}
STAGE=()
COLOR=()
add-prefix() {
STAGE+=("$1")
COLOR+=("$2$3$4")
tell "{START}"
}
pop-prefix() {
tell "{END}"
unset STAGE[-1]
unset COLOR[-1]
}
with-retry() {
local COUNT=$1
shift
for I in $(seq 1 $COUNT)
do
$@
if [[ $? == 0 ]]; then break; fi
if [[ $I == $COUNT ]]
then
fail "Cannot finish command '$@' (retry $COUNT)"
fi
done
}
tell() {
local PREFIX=""
for I in $(seq 0 $((${#COLOR[@]}-1)))
do
PREFIX="${PREFIX}:: ${COLOR[$I]}${STAGE[$I]}${STOPCOLOR} "
done
trim "$@" \
| sed "s/^/${PREFIX}:: /g" \
| sed "s/\r/\n/g"
}
##############################
# #
# Cleanup Hooks #
# #
##############################
FAILHOOKS=()
hook() {
FAILHOOKS+=("$@")
}
fail() {
add-prefix "ERROR" $RED
tell "$@"
for I in $(seq 1 ${#FAILHOOKS[@]})
do
tell ${FAILHOOKS[-$I]}
${FAILHOOKS[-$I]} >& /dev/null || {
tell "While executing cleanup hook, '${FAILHOOKS[-$I]}' failed. "
}
done
exit 1
}
########################################
# #
# Pre-installation Tool functions #
# #
########################################
# check if device exists
# $1: the device to check
setup-disk-check() {
add-prefix 'CHECK DISK'
local DEVLIST=$(find /dev | sed "s/^\/dev\///")
local EXISTS=0
for D in ${DEVLIST}
do
if [[ $(trim $1) == $D ]]
then
EXISTS=1
fi
done
if [[ $EXISTS == 0 ]]
then
fail "
Device [$(trim $1)] doesn't exist!
Existing device: $(trim ${DEVLIST} | sed 's/ /, /g')
"
else
tell "
Device [$(trim $1)] successfully detected.
Finish disk check.
"
fi
pop-prefix
}
# partition disk
# $1: device
# $2: partition settings
setup-partition() {
add-prefix 'PARTITION'
tell "
Install parted using pacman.
-- Don't you worry.
-- This only happens in ram.
"
pacman -S --noconfirm parted >& $(trim $LOGFILE) || {
fail "
Cannot install 'parted'. Are you superuser?
-- We need 'parted' to partition the disk!
"
}
tell "Generate guid partition table"
parted --script /dev/$(trim "$1") -- mklabel gpt >& $(trim $LOGFILE) || {
fail "
Fail to generate guid partition table (gpt).
Reason: \"$(cat $(trim $LOGFILE))\"
"
}
local CONF=""
readarray CONF < <(trim "$2")
for I in $(seq 0 $((${#CONF[@]} - 1)))
do
local MKPT=$(trim $(echo ${CONF[$I]} | awk '{split($0,a,"|"); print a[2]}'))
local FLAG=$(trim $(echo ${CONF[$I]} | awk '{split($0,a,"|"); print a[3]}'))
tell "Make partition with configuration: $MKPT"
parted --script /dev/$(trim "$1") -- mkpart $MKPT >& $(trim $LOGFILE) || {
fail "
Cannot partition the disk. are you superuser? what about alignment?
Reason: \"$(cat $(trim $LOGFILE))\"
"
}
for F in $FLAG
do
tell "-- Attach flag $F"
parted --script /dev/$(trim "$1") -- set $(($I+1)) $F on >& $(trim $LOGFILE)
done || fail "Cannot set flag on partition $I $(echo)Reason: \"$(cat $(trim $LOGFILE))\""
done
pop-prefix
}
# make file systems on disk
# $1: device name
# $2: partition settings
setup-fs-format() {
add-prefix 'FS FORMAT'
pacman -S --needed --noconfirm dosfstools
local CONF
local PARS
readarray CONF < <(trim "$2")
readarray PARS < <(find /dev | grep -P "/dev/$(trim $1).+")
if [[ ${#CONF[@]} != ${#PARS[@]} ]]
then
fail "The number of partitions is different from the number of configured partitions. "
fi
# file systems on partitions
for I in $(seq 0 $((${#PARS[@]} - 1)))
do
local MKF="$(trim $(echo ${CONF[$I]} | awk '{split($0,a,"|"); print a[4]}'))"
local PAR=$(trim ${PARS[$I]})
tell "Format file system $MKF $PAR. Wait..."
$MKF $PAR || {
fail "Cannot make file system (with $MKF) on $PAR. "
}
done
pop-prefix
}
# mount partitions to mountpoints
# $1: device name
# $2: disk partition plan
# $3: mount point root
setup-mount() {
add-prefix "MOUNT DISK"
local CONF
local PARS
readarray CONF < <(trim "$2")
readarray PARS < <(find /dev | grep -P "/dev/$(trim $1).+")
if [[ ${#CONF[@]} != ${#PARS[@]} ]]
then
fail "The number of partitions is different from the number of configured partitions. "
fi
swapoff -a -v
for I in $(seq 0 $((${#PARS[@]} - 1)))
do
local POS="$(trim $3)$(trim $(echo ${CONF[$I]} | awk '{split($0,a,"|"); print a[1]}'))"
local PAR=$(trim ${PARS[$I]})
tell "$PAR <-> $POS"
if [[ "$POS" == "swap" ]]
then
swapon $PAR >& $(trim $LOGFILE) || {
fail "Reason: \"$(cat $(trim $LOGFILE))\""
}
hook "swapoff $POS"
else
mount --mkdir $PAR $POS >& $(trim $LOGFILE) || {
fail "Reason: \"$(cat $(trim $LOGFILE))\""
}
hook "umount -l $POS"
fi
done
tell "Generate fstab. "
mkdir $(trim "$MOUNTPOINT")/etc >& /dev/null || tell "/etc alread exists"
genfstab -U $(trim "$MOUNTPOINT") > $(trim "$MOUNTPOINT")/etc/fstab
pop-prefix
}
# use pacstrap to bootstrap a system
# $1: the mountpoint to pacstrap into
setup-pacstrap() {
tell "Install base system. "
cat /etc/pacman.conf > $(trim "$1")/etc/pacman.conf
with-retry 3 pacstrap -K $(trim "$1") base base-devel
}
########################################
# #
# Post-installation Tool Functions #
# #
########################################
# configure boot loading behaviour (systemd)
setup-systemd-boot() {
# https://wiki.archlinux.org/title/Systemd-boot
add-prefix 'SYSTEMD BOOT'
bootctl install --path /boot
# query fstab for root guid
local UUID=$(cat /etc/fstab | awk '$2 == "/"' | awk $'{split($1,f,"=")\nprint f[2]}')
local FSTY=$(cat /etc/fstab | awk '$2 == "/"' | awk $'{print $3}')
# detect microcode image (https://wiki.archlinux.org/title/Microcode)
if [[ -f /boot/amd-ucode.img ]]; then
local UCOD='/amd-ucode.img'
elif [[ -f /boot/intel-ucode.img ]]; then
local UCOD='/intel-ucode.img'
else
fail "
you didn't install intel-ucode or amd-ucode.
for fuck sake, what chip are you using?
"
fi
# configure kernel & ramfs image generation (https://wiki.archlinux.org/title/Mkinitcpio)
(cat - > /etc/mkinitcpio.conf) < <(trim "
MODULES=()
BINARIES=()
FILES=()
HOOKS=(base udev autodetect keyboard keymap modconf block filesystems fsck)
")
# generate kernel & ramfs image
mkinitcpio \
-k /boot/vmlinuz-linux \
-g /boot/initramfs-linux.img
# configure boot loader to load linux image and micro code (https://wiki.archlinux.org/title/Systemd-boot)
(cat - > /boot/loader/entries/arch.conf) < <(trim "
title Arch Linux
linux /vmlinuz-linux
initrd $UCOD
initrd /initramfs-linux.img
options root=UUID=$UUID rw rootfstype=$FSTY
")
# configure boot loader option menu
(cat - > /boot/loader/loader.conf) < <(trim "
timeout 5
default arch
")
pop-prefix
}
# configure network
# $1: hostname
# $2: hosts
setup-network() {
# hostname
(cat - > /etc/hostname) < <(trim "$1")
(cat - > /etc/hosts) < <(trim "$2")
}
# configure localization
# $1: region
# $2: city
setup-localize() {
# locale setup
locale-gen
(cat - > /etc/locale.gen) < <(trim "
en_US.UTF-8 UTF-8
")
(cat - > /etc/locale.conf) < <(trim '
LANG="en_US.UTF-8"
LC_COLLATE="C"
')
# timezone setup
ln -sT "/usr/share/zoneinfo/$(trim "$1")/$(trim "$2")" /etc/localtime
hwclock --systohc
}
# initialize a user and add it to sudoers
# $1: username
# $2: password
setup-user() {
add-prefix "USER ADD"
useradd -m $(trim $1)
passwd $(trim $1) < <(trim "
$(trim $2)
$(trim $2)
")
(cat - > /etc/sudoers) < <(trim '
root ALL=(ALL:ALL) ALL
@includedir /etc/sudoers.d
')
mkdir -p /etc/sudoers.d || tell "/etc/sudoers.d already exists"
echo "$(trim $1) ALL=(ALL) ALL" > /etc/sudoers.d/$(trim $1)
pop-prefix
}
# setup pacman mirror and update database
# $1: the mirrorlist file
setup-pacman() {
trim "$1" > /etc/pacman.d/mirrorlist
with-retry 3 pacman -Syy || fail "Cannot update pacman database. "
with-retry 3 pacman-key --init
with-retry 3 pacman-key --populate
with-retry 3 pacman --needed -S --noconfirm $(trim "$2")
}
###########################
# #
# Program Entry Point #
# #
###########################
if [[ "$1" == "post-install" ]]
then
add-prefix "POST-INSTALL"
setup-pacman "$MIRRORLIST" "$PACKAGES"
setup-network "$HOSTNAME" "$HOSTS"
setup-localize "$REGION" "$CITY"
setup-user "$USERNAME" "$PASSWORD"
setup-systemd-boot
pop-prefix
else
add-prefix "PRE-INSTALL"
trim "$MIRRORLIST" > /etc/pacman.d/mirrorlist
setup-disk-check "$DEVICE"
setup-partition "$DEVICE" "$PARTITION"
setup-fs-format "$DEVICE" "$PARTITION"
setup-mount "$DEVICE" "$PARTITION" "$MOUNTPOINT"
setup-pacstrap "$MOUNTPOINT"
pop-prefix
cp $0 "$(trim "$MOUNTPOINT")/setup.sh" >& $(trim $LOGFILE) \
|| fail "cannot copy current script to $(trim "$MOUNTPOINT")/setup.sh"
arch-chroot "$(trim "$MOUNTPOINT")" bash /setup.sh post-install \
|| fail "chroot & execute failed $(trim "$MOUNTPOINT")"
rm "$(trim "$MOUNTPOINT")/setup.sh"
fi