-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbase.sh
More file actions
executable file
·465 lines (386 loc) · 12.5 KB
/
Copy pathbase.sh
File metadata and controls
executable file
·465 lines (386 loc) · 12.5 KB
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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
#!/usr/bin/env bash
declare HOSTNAME;
declare PASSWORD_USER;
declare PASSWORD_VOLUMEN;
declare VOLUMEN;
declare VOLUMEN_ID;
function main() {
USERCOMMENT="Nicola Strappazzon C."
USERNAME="nicola"
HOSTNAME="strappazzon"
user_password
volumen_password
configure_basic
partitioning
install_base
install_microcode
configure_input
configure_locale
configure_environment
configure_profile
configure_network
configure_user
configure_bootloader
configure_ntp
configure_wakeup
packages
services
finish
}
function user_password() {
local confirm
echo "==> Set password for root and the user account."
while true; do
IFS="" read -r -s -p "--> Enter your password: " PASSWORD_USER </dev/tty
echo
IFS="" read -r -s -p "--> Confirm your password: " confirm </dev/tty
echo
if [ -z "$PASSWORD_USER" ]; then
echo "==> Password cannot be empty."
continue
fi
if [ "$PASSWORD_USER" = "$confirm" ]; then
break
fi
echo "==> Passwords do not match. Please try again."
done
PASSWORD_USER=$(openssl passwd -6 "$confirm")
}
function volumen_password() {
local confirm
echo "==> Set password for the encrypted disk."
while true; do
IFS="" read -r -s -p "--> Enter your password: " PASSWORD_VOLUMEN </dev/tty
echo
IFS="" read -r -s -p "--> Confirm your password: " confirm </dev/tty
echo
if [ -z "$PASSWORD_VOLUMEN" ]; then
echo "==> Password cannot be empty."
continue
fi
if [ "$PASSWORD_VOLUMEN" = "$confirm" ]; then
break
fi
echo "==> Passwords do not match. Please try again."
done
}
function configure_basic() {
echo "==> Basic configure before install."
# Configure time zone and NTP:
timedatectl set-timezone Europe/Madrid
timedatectl set-ntp true
hwclock --systohc
# Configure mirrorlist:
cp /etc/pacman.d/mirrorlist /etc/pacman.d/mirrorlist.backup
reflector \
--protocol https \
--latest 20 \
--number 10 \
--sort score \
--download-timeout 10 \
--save /etc/pacman.d/mirrorlist
# Synchronize database:
pacman -Sy &> /dev/null
# Configure keyboard layaout:
loadkeys us
}
function partitioning() {
readarray -t VOLUMES_LIST < <(lsblk --list --nodeps --ascii --noheadings --output=NAME --filter 'TYPE=="disk" && SIZE > 0' | sort)
echo "==> Available volumes:"
for VOLUMEN_INDEX in "${!VOLUMES_LIST[@]}"; do
name="${VOLUMES_LIST[$VOLUMEN_INDEX]}"
size=$(lsblk --nodeps --noheadings --output=SIZE "/dev/${name}")
model=$(lsblk --nodeps --noheadings --output=MODEL "/dev/${name}")
printf " %d) %s: %s %s\n" "$((VOLUMEN_INDEX+1))" "${name}" "$(ltrim "${size}")" "${model}"
done
VOLUMENS_COUNT=${#VOLUMES_LIST[@]}
until [[ $VOLUMEN_ID =~ ^[1-9][0-9]*$ ]] && (( VOLUMEN_ID <= VOLUMENS_COUNT )); do
IFS="" read -r -p "--> Choice volume number: " VOLUMEN_ID </dev/tty
done
VOLUMEN="/dev/${VOLUMES_LIST[$((VOLUMEN_ID-1))]}"
echo "--> Has chosen this volume: ${VOLUMEN}"
echo "==> Partitioning and format volume."
# Umount partitions:
umount --quiet --recursive /mnt/boot/efi 2>/dev/null || true
umount --quiet --recursive /mnt 2>/dev/null || true
swapoff --all 2>/dev/null || true
cryptsetup close cryptroot 2>/dev/null || true
udevadm settle
# Delete all partitions:
wipefs --all --force --quiet "${VOLUMEN}"
sgdisk --zap-all "${VOLUMEN}" &>/dev/null || true
# Create new partitions:
parted --script "${VOLUMEN}" mklabel gpt
parted --script "${VOLUMEN}" mkpart efi fat32 1MiB 1024MiB
parted --script "${VOLUMEN}" set 1 esp on
parted --script "${VOLUMEN}" mkpart swap linux-swap 1GiB 32GiB
parted --script "${VOLUMEN}" mkpart root ext4 32GiB 100%
# Verify partitions:
partprobe "${VOLUMEN}"
udevadm settle
DISK=$(basename "${VOLUMEN}")
UEFI=$(lsblk --ascii --noheadings --output=PATH --filter "PARTLABEL=='efi' && PKNAME=='${DISK}'")
SWAP=$(lsblk --ascii --noheadings --output=PATH --filter "PARTLABEL=='swap' && PKNAME=='${DISK}'")
ROOT=$(lsblk --ascii --noheadings --output=PATH --filter "PARTLABEL=='root' && PKNAME=='${DISK}'")
OPTS="noatime,compress=zstd,ssd,space_cache=v2"
if [[ -z "${UEFI}" || -z "${SWAP}" || -z "${ROOT}" ]]; then
echo "ERROR: Partition detection failed"
lsblk -o NAME,PARTLABEL,SIZE
exit 1
fi
# Format partitions:
mkfs.fat -F32 -n UEFI "${UEFI}" &> /dev/null
mkswap -L SWAP "${SWAP}" &> /dev/null
# Encrypt disk
printf "%s" "${PASSWORD_VOLUMEN}" | cryptsetup luksFormat --type luks2 --batch-mode --key-file - "${ROOT}"
printf "%s" "${PASSWORD_VOLUMEN}" | cryptsetup open --key-file - "${ROOT}" cryptroot
udevadm settle
mkfs.btrfs -L ROOT "/dev/mapper/cryptroot" &> /dev/null
mount "/dev/mapper/cryptroot" /mnt
# Mount the swap:
swapon "${SWAP}"
# Create subvolumes:
btrfs subvolume create /mnt/@ &> /dev/null
btrfs subvolume create /mnt/@home &> /dev/null
btrfs subvolume create /mnt/@log &> /dev/null
btrfs subvolume create /mnt/@pkg &> /dev/null
btrfs subvolume create /mnt/@snapshots &> /dev/null
# Unmount the root mount point:
umount /mnt
# Mount all subvolumes in their correct locations:
mount -o ${OPTS},subvol=@ "/dev/mapper/cryptroot" /mnt
mkdir -p /mnt/{home,var/log,var/cache/pacman/pkg,.snapshots,boot}
mount -o ${OPTS},subvol=@home "/dev/mapper/cryptroot" /mnt/home
mount -o ${OPTS},subvol=@log "/dev/mapper/cryptroot" /mnt/var/log
mount -o ${OPTS},subvol=@pkg "/dev/mapper/cryptroot" /mnt/var/cache/pacman/pkg
mount -o ${OPTS},subvol=@snapshots "/dev/mapper/cryptroot" /mnt/.snapshots
mount "${UEFI}" /mnt/boot/
# Remove default directories lost+found:
rm -rf /mnt/boot/lost+found
rm -rf /mnt/lost+found
# Generate fstab:
mkdir -p /mnt/etc/
genfstab -pU /mnt >> /mnt/etc/fstab
}
function install_base() {
echo "==> Installing essential packages."
pacstrap /mnt \
base \
base-devel \
btrfs-progs \
efibootmgr \
iwd \
linux \
linux-firmware \
linux-headers \
limine \
man-db \
networkmanager \
openssh \
vim \
&> /dev/null
}
function install_microcode() {
echo "==> Installing CPU microcode."
if grep -q AuthenticAMD /proc/cpuinfo; then
pacstrap /mnt amd-ucode &> /dev/null
elif grep -q GenuineIntel /proc/cpuinfo; then
pacstrap /mnt intel-ucode &> /dev/null
fi
}
function configure_input() {
sed -i 's/#set bell-style none/set bell-style none/g' /mnt/etc/inputrc
}
function configure_locale() {
echo "en_US.UTF-8 UTF-8" > /mnt/etc/locale.gen
echo "LANG=en_US.UTF-8" > /mnt/etc/locale.conf
echo "LANGUAGE=en_US" >> /mnt/etc/locale.conf
echo "LC_ALL=C" >> /mnt/etc/locale.conf
arch-chroot /mnt locale-gen &> /dev/null
}
function configure_environment() {
cat > /mnt/etc/environment << 'EOF'
EDITOR=vim
TERM=xterm
TERMINAL=xterm
EOF
}
function configure_profile() {
cat > /mnt/etc/skel/.bashrc << 'EOF'
[[ $- != *i* ]] && return
if [ -x /etc/profile.d ]; then
for i in /etc/profile.d/*.sh; do
if [ -f "$i" ]; then
. "$i"
fi
done
fi
EOF
cat > /mnt/etc/profile.d/custom.sh << 'EOF'
#!/bin/sh
if [ -x ~/.bashrc.d ]; then
for i in ~/.bashrc.d/*.sh; do
if [ -f "$i" ]; then
. "$i"
fi
done
fi
EOF
cat > /mnt/etc/profile.d/ps.sh << 'EOF'
#!/bin/sh
if [[ ${EUID} == 0 ]] ; then
PS1='\[\033[01;31m\][\h\[\033[01;36m\] \W\[\033[01;31m\]]\$\[\033[00m\] '
else
PS1='\[\033[01;32m\][\u@\h\[\033[01;37m\] \W\[\033[01;32m\]]\$\[\033[00m\] '
fi
EOF
rm -f /mnt/etc/profile.d/perlbin.*
}
function configure_network() {
echo "==> Network configuration."
echo $HOSTNAME > /mnt/etc/hostname
cat << EOF > /mnt/etc/hosts
::1 localhost
127.0.1.1 localhost ${HOSTNAME}.local ${HOSTNAME}.localdomain $HOSTNAME
EOF
}
function configure_user() {
echo "==> Create user."
arch-chroot /mnt useradd --create-home --shell=/bin/bash --gid=users --groups=wheel,uucp,video --password="$PASSWORD_USER" --comment="$USERCOMMENT" "$USERNAME"
arch-chroot /mnt sed -i 's/^# %wheel ALL=(ALL:ALL) ALL/%wheel ALL=(ALL:ALL) ALL/' /etc/sudoers
cp /mnt/etc/skel/.bashrc /mnt/root/.bashrc
chmod 0600 /mnt/root/.bashrc
arch-chroot /mnt usermod --shell /bin/bash root
printf "root:%s" "$PASSWORD_USER" | arch-chroot /mnt chpasswd --encrypted
}
function configure_bootloader() {
echo "==> Install and configure bootloader."
ROOT_UUID=$(blkid -s UUID -o value "$ROOT")
if grep -q AuthenticAMD /proc/cpuinfo; then
MICROCODE="amd-ucode.img"
elif grep -q GenuineIntel /proc/cpuinfo; then
MICROCODE="intel-ucode.img"
fi
arch-chroot /mnt mkdir -p /boot/EFI/BOOT
arch-chroot /mnt cp /usr/share/limine/BOOTX64.EFI /boot/EFI/BOOT/BOOTX64.EFI
arch-chroot /mnt efibootmgr --create \
--disk "${VOLUMEN}" --part 1 \
--loader '\EFI\BOOT\BOOTX64.EFI' \
--label "Arch Linux Limine" --unicode &> /dev/null
cat << EOF | tee /mnt/boot/limine.conf &> /dev/null
timeout: 0
default_entry: 1
/Arch Linux
protocol: linux
kernel_path: boot():/vmlinuz-linux
module_path: boot():/${MICROCODE}
module_path: boot():/initramfs-linux.img
cmdline: cryptdevice=UUID=${ROOT_UUID}:cryptroot root=/dev/mapper/cryptroot rootflags=subvol=@ rw quiet loglevel=3
EOF
sed -i 's/^MODULES=.*/MODULES=(btrfs)/' /mnt/etc/mkinitcpio.conf
sed -i 's/^HOOKS=.*/HOOKS=(base udev autodetect keyboard keymap consolefont modconf block encrypt filesystems)/' /mnt/etc/mkinitcpio.conf
arch-chroot /mnt mkinitcpio -P &> /dev/null
}
function configure_ntp() {
echo "==> Configure time zone and NTP."
arch-chroot /mnt timedatectl set-timezone Europe/Madrid
arch-chroot /mnt timedatectl set-ntp true
arch-chroot /mnt hwclock --systohc
}
function configure_wakeup() {
echo "==> Configure wakeup."
cat << EOF | sudo tee /mnt/usr/local/bin/wakeup-disable.sh &> /dev/null
#!/usr/bin/env sh
# set -eu
/bin/echo XHC0 > /proc/acpi/wakeup
/bin/echo XHC1 > /proc/acpi/wakeup
/bin/echo GPP0 > /proc/acpi/wakeup
EOF
cat << EOF | sudo tee /mnt/etc/systemd/system/wakeup-disable.service &> /dev/null
[Unit]
Description=Fix suspend by disabling XHC0, XHC1 and GPP0 sleepstate thingie
After=systemd-user-sessions.service plymouth-quit-wait.service
After=rc-local.service
Before=getty.target
IgnoreOnIsolate=yes
[Service]
Type=oneshot
ExecStart=/usr/local/bin/wakeup-disable.sh
RemainAfterExit=true
[Install]
WantedBy=basic.target
EOF
arch-chroot /mnt chmod +x /usr/local/bin/wakeup-disable.sh
arch-chroot /mnt systemctl enable wakeup-disable.service &> /dev/null
arch-chroot /mnt systemctl start wakeup-disable.service &> /dev/null
}
function configure_hibernation() {
echo "==> Configure hibernation ."
cat << EOF | sudo tee /mnt/etc/systemd/sleep.conf &> /dev/null
[Sleep]
AllowHibernation=no
AllowSuspendThenHibernate=no
AllowHybridSleep=no
EOF
}
function packages() {
echo "==> Install aditional packages."
PACKAGES=(
bash-completion
bat
bind-tools
btop
ca-certificates
curl
dosfstools
fzf
git
go
helix
htop
jq
less
libusb
links
lsd
neofetch
net-tools
nmap
rsync
testdisk
tmux
traceroute
ufw
unrar
unzip
usbutils
vim
wget
wl-clipboard
xclip
yazi
)
for PACKAGE in "${PACKAGES[@]}"; do
arch-chroot /mnt pacman --sync --noconfirm --needed "${PACKAGE}" &> /dev/null
done
}
function services() {
echo "==> Enable services."
arch-chroot /mnt systemctl enable NetworkManager &> /dev/null
arch-chroot /mnt systemctl enable sshd &> /dev/null
arch-chroot /mnt systemctl enable systemd-timesyncd &> /dev/null
}
function finish() {
echo "==> Install process is finished."
echo
read -n 1 -s -r -p "Please remove the installation medium and press any KEY to reboot or press Ctrl+C to cancel" </dev/tty
(umount --all-targets --quiet --recursive /mnt/) || true
(swapoff --all) || true
clear
reboot
}
function ltrim() {
local s="$*"
printf "%s" "${s#"${s%%[![:space:]]*}"}"
}
main "$@"