-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharch.sh
More file actions
4338 lines (2775 loc) · 95.8 KB
/
arch.sh
File metadata and controls
4338 lines (2775 loc) · 95.8 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
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/bash
: << DOCS
To run on the fly:
bash <(curl -sL bit.ly/a-install)
DOCS
### Set error detection ###
error() {
local sourcefile=$1
local lineno=$2
local errornum=$3
local command=$4
echo -e "\e[0;41m\n\nFile: \e[1;2;41m$1\n\e[0;41mError: \e[1;2;41m$3\n\e[0;41mLine:\e[1;2;41m $2\n\e[0;41mCommand:\e[1;2;41m $4\n\e[0;29m\n"
finished=0
while [ $finished -eq 0 ]; do
echo -e "What now?\n\n[e] edit (last position)\n[n] edit (error position)\n[c] continue\n[x] exit\n"
if [[ "$error_bypass" = '1' ]]; then
choice=c
echo -e "\nSkipping error.\n"
else
read -p "Choice: " -n 2 choice
fi
case $choice in
e) $editor $arch_path/$arch_file; exit ;;
n) $editor +$2 $arch_path/$arch_file; exit ;;
c) set +e; break ;;
x) exit ;;
*) finished=0 ;;
esac
done
}
trap 'error "${BASH_SOURCE}" "${LINENO}" "$?" "${BASH_COMMAND}"' ERR
trap 'echo;echo; read -s -r -n 1 -p "<ctrl> + c pressed. Press any key to continue... "; set +e' SIGINT
error_check () {
case $1 in
0) set +e; echo "No error detection!" ;;
1) set -Eeo pipefail ;;
2) set -Eueo pipefail ;;
3) echo "All commands issued will be printed."; set -Eeox pipefail ;;
esac
}
error_check 1
error_bypass=0
# fml, I could swear all developers have 20/20 vision :/
if [ -f /usr/share/kbd/consolefonts/ter-132b.psf.gz ] && [[ ! $(grep -e '^HOOKS|consolefont' /etc/mkinitcpio.conf) = '' ]]; then
setfont ter-132b
else
setfont -d
fi
fstype='btrfs' # btrfs,ext4,bcachefs,f2fs,xfs,jfs,nilfs2
bootOwnPartition='false' # make separate boot partition (true/false)?
[ $fstype = 'bcachefs' ] && bootOwnPartition=true
# Do we want a separate boot partition (which will be ext2)
if [[ $bootOwnPartition = 'true' ]]; then
espPartNum=1
bootPartNum=2
swapPartNum=3
rootPartNum=4
else
bootPartNum=0
espPartNum=1
swapPartNum=2
rootPartNum=3
fi
espPart=$espPartNum
bootPart=$bootPartNum
swapPart=$swapPartNum
rootPart=$rootPartNum
mnt=/mnt
mnt2=/mnt2
mnt3=/mnt3
efi_path=/efi
encrypt='false' # bcachefs only
encryptLuks='false' # ext4 (Not Working!)
startSwap='8192Mib' # 2048,4096,8192,(8192 + 1024 = 9216)
fsPercent='100' # What percentage of space should the root drive take?
checkPartitions='true' # Check that partitions are configured optimally?
subvolPrefix='@' # eg., '/' or '/@' btrfs and bcachefs only
first_snapshot_name='1' # Only for btrfs
snapshot_dir='/.snapshots'
subvols=($snapshot_dir /boot/grub /var/log /var/tmp)
rootMount='/@root' # (ex., @root) Only used for bcachefs
btrfs_mountopts="noatime,discard=async"
bcachefs_mountopts="noatime"
boot_mountopts="noatime"
efi_mountopts="noatime"
fsckBcachefs='true'
backup_install='true' # say 'true' to do snapshots/rysncs during install (only btrfs/bcachefs)
install_backup='true' # make a compressed backup file?
copy_user_dir='false' # should we copy /home/$user ?
if [ $fstype = 'btrfs' ]; then
backup_type='snapper'
else
backup_type='rsync'
fi
initramfs='mkinitcpio' # mkinitcpio, dracut, booster
extra_modules='lz4' # adds to /etc/mkinitcpio modules
extra_hooks='' # adds to /etc/mkinitcpio hooks
kernel_ops="nmi_watchdog=0 nowatchdog modprobe.blacklist=iTCO_wdt loglevel=3 rd.udev.log_level=3 zswap.enabled=1 zswap.compressor=zstd zswap.max_pool_percent=20 scsi_mod.use_blk_mq=1"
enable_fallback='false' # Enable fallback kernel?
user=user
password='123456'
autologin=true
arch_file=$(basename "$0")
arch_path=$(dirname "$0")
editor="vim"
aur_app=none
aur_path=/home/$user/aur
aur_apps_path=/root/pkgs/
aur_apps_root=''
aur_apps_user=''
#aur_apps_kde='brave-bin'
aur_apps_kde=''
base_install="base linux linux-firmware vim parted gptfdisk arch-install-scripts pacman-contrib tar man-db dosfstools"
user_install="sudo git base-devel"
cage_install="cage firefox"
weston_install="brightnessctl wireplumber pipewire pipewire-pulse weston firefox"
phosh_install="phosh phoc phosh-mobile-settings squeekboard firefox"
gnome_install="gnome-shell polkit nautilus gnome-console xdg-user-dirs dconf-editor gnome-browser-connector gnome-shell-extensions gnome-control-center gnome-weather"
kde_install="plasma-desktop plasma-pa plasma-nm kscreen iio-sensor-proxy dolphin konsole ffmpegthumbs bleachbit ncdu kdiskmark brave-bin networkmanager-openvpn openvpn firefox okular"
#kde_install="plasma-desktop plasma-pa maliit-keyboard plasma-nm kscreen iio-sensor-proxy dolphin konsole ffmpegthumbs bleachbit ncdu kdiskmark brave-bin networkmanager-openvpn openvpn vital-synth"
#kde_install="plasma-desktop plasma-pa maliit-keyboard plasma-nm kscreen iio-sensor-proxy dolphin konsole ffmpegthumbs bleachbit ncdu kdiskmark networkmanager-openvpn openvpn firefox brave-bin code gwenview code helix sudo pacman -S nodejs npm"
ucode=intel-ucode
hostname=Arch
timezone=Canada/Eastern
offline=0
reinstall=0
copy_on_host='1' # Copy packages to host? Set to '0' when installing from an arch iso in ram
wlan="wlan0"
wifi_ssid=""
wifi_pass=""
backup_file=/setup.tar.gz
CONFIG_FILES="
/usr/lib/initcpio/init
/usr/lib/initcpio/hooks/btrfs-rollback
/usr/lib/initcpio/install/btrfs-rollback
/usr/lib/initcpio/hooks/bcachefs-rollback
/usr/lib/initcpio/install/bcachefs-rollback
/usr/lib/initcpio/install/liveroot
/usr/lib/initcpio/hooks/liveroot
/etc/overlayroot.conf
/usr/bin/mount.overlayroot
/usr/lib/initcpio/hooks/overlayroot
/usr/lib/initcpio/install/overlayroot
/etc/booster.yaml
/etc/default/grub-btrfs/config
/etc/dracut.conf.d/
/etc/hostname
/etc/hosts
/etc/iwd/main.conf
/etc/locale.conf
/etc/locale.gen
/etc/localtime
/etc/mkinitcpio.d/linux.preset
/etc/NetworkManager/conf.d/
/etc/NetworkManager/system-connections
/var/lib/NetworkManager/timestamps
/etc/pacman.conf
/etc/pacman-offline.conf
/etc/pacman.d/mirrorlist
/etc/security/limits.conf
/etc/sudoers.d/
/etc/sysctl.d/50-coredump.conf
/etc/sysctl.d/99-cache-pressure.conf
/etc/sysctl.d/99-net-keepalive.conf
/etc/sysctl.d/99-net-timeout.conf
/etc/sysctl.d/99-swappiness.conf
/etc/systemd/coredump.conf.d/custom.conf
/etc/systemd/system/getty@tty1.service.d/autologin.conf
/etc/systemd/system/getty@tty1.service.d/wayland.conf
/etc/wpa_supplicant
/etc/updatedb.conf
/root/.mkshrc
/root/.vimrc
/root/pkgs/
/var/lib/dhcpcd
/var/lib/iwd
/var/spool/cron/root
/home/$user/.bash_profile
/home/$user/.bashrc
/home/$user/.cert/
/home/$user/.config/
/home/$user/.enduin
/home/$user/.hushlogin
/home/$user/.local/
/home/$user/.mkshrc
/home/$user/.mozilla
/home/$user/.profile
/home/$user/.vimrc
/home/$user/.vim/
"
check_pkg () {
for package in "$@"; do
if [ ! "$(pacman -Q $package)" ]; then
echo -e "\nInstalling program required to run command: $package...\n"
pacman --noconfirm -S $package
fi
done
}
check_viable_disk () {
if [[ "$disk" == "" ]]; then
echo -e "\nMissing disk parameter. Exiting.\n"
exit
fi
if [[ ! "$(lsblk --output=PATH -d -n | grep $disk)" ]]; then
echo -e "\nNo such disk found ($disk). Exiting.\n"
sleep 2
exit
fi
}
check_on_root () {
if [[ $(mount | grep -G "$disk.*on / type") ]]; then
echo -e "\nDevice mounted on root. Will not run. Exiting.\n"
sleep 2
exit
fi
}
unmount_disk () {
sync_disk
[ "$mnt" = '' ] && return
if [[ $(mount | grep -E $disk$espPart | grep -E "on $mnt$efi_path") ]]; then
echo "Unmounting $mnt$efi_path..."
umount -n -R $mnt$efi_path
sleep .1
fi
if [[ $(mount | grep -E $disk$bootPart | grep -E "on $mnt/boot") ]] && [[ $bootOwnPartition = true ]]; then
echo "Unmounting $mnt/boot..."
umount -n -R $mnt/boot
sleep .1
fi
if [[ $(mount | grep -E $disk$rootPart | grep -E "on $mnt") ]]; then
# Might need to turn error checking off here
echo "Unmounting $mnt..."
# Shouldn't be in directory we're unmounting
[[ "$(pwd | grep $mnt)" ]] && cd /
umount -n -R $mnt
sleep .1
# Time to get rugged and tough!
if [[ "$(mount | grep /mnt)" ]]; then
echo -e "\nCouldn't unmount. Trying alternative method. Please be patient...\n"
mounted=1
while [[ "$mounted" -eq 1 ]]; do
cd /
if [[ "$(mount | grep /mnt)" ]]; then
sleep .1
umount -l $mnt
else
mounted=0
fi
done
echo -e "\nLazy unmount successful.\n"
systemctl daemon-reload
fi
else
echo -e "\nDisk already unmounted!\n"
fi
if [ "$encryptLuks" = 'true' ] && [ "$(mount | grep /dev/mapper/root)" ]; then
echo
#cryptsetup close root
fi
}
choose_disk () {
search_disks=1
host="$(mount | awk '/ on \/ / { print $1}' | sed 's/p*[0-9]$//g')"
while [ $search_disks -eq 1 ]; do
mnt=''
echo -e "\nDrives found (current mount: /):\n"
#rootfs=$(mount | grep ' / ' | sed 's/(.*//; s/\/dev.* type //; s/ //')
rootfs=$(mount | grep ' / ' | sed 's/(.*//; s/^.*type //; s/ //')
#[ "$(mount | grep ' on / type overlay' | awk '{print $5}')" ] && echo -e "\n *** Running in overlay mode! ***\n"
[ "$rootfs" = "overlay" ] && echo -e "\n *** Running in overlay mode! ***\n"
rootMount="$(findmnt -o FSROOT -n / | sed 's#^/##')"
if [ $rootfs = 'btrfs' ]; then
[ "$(snapper list --columns number,read-only | grep '[0-9][-\*].*yes')" ] && echo -e "\n *** Running in read only mode! ***\n"
#rootSub="$(mount | grep ' / ' | sed 's#.*.subvol=/##; s/)//')"
defaultSub="$(btrfs su get-default / | awk '{ print $9 }')"
if [ ! "$rootMount" = "$defaultSub" ]; then
echo -e "\n *** NOT mounted on default subvolume ($defaultSub)! ***\n\n"
fi
fi
lsblk --output=PATH,SIZE,MODEL,TRAN -d | grep -P "/dev/sd|nvme|vd" | sed "s#$host.*#& $rootfs#g" | sed "s#$host.*#& (host) $rootMount#"
[[ $rootfs = 'btrfs' ]] || [[ $rootfs = 'bcachefs' ]] && extra='snapshots '
choices='quit '$extra'backup edit $ # '$(lsblk -dpnoNAME|grep -P "/dev/sd|nvme|vd")' / update script logout reboot suspend hibernate poweroff stats benchmark rollback-script'
echo -e "\nWhich drive?\n"
select disk in $choices
do
case $disk in
quit) sync_disk; exit ;;
edit) $editor $arch_path/$arch_file; exit ;;
$) sudo -u $user bash ;;
\#) bash ;;
backup) backup_config ;;
script) download_script ;;
update) pacman -Syu ;;
rollback-script) $editor /lib/initcpio/hooks/btrfs-rollback; mkinitcpio -P ;;
logout) killall systemd ;;
reboot) reboot ;;
suspend) echo mem > /sys/power/state ;;
hibernate) echo disk > /sys/power/state ;;
poweroff) poweroff ;;
benchmark) benchmark ;;
stats)
if [[ "$rootfs" = 'btrfs' ]]; then
btrfs su list /
fi
echo; free -h; echo
systemd-analyze | sed 's/in .*=/in/;s/graph.*//'
echo $(systemd-analyze blame | wc -l) systemd services
;;
snapshots) snapshots_menu ;;
/) disk=$host; search_disks=0; break ;;
*) if [[ $disk = '' ]]; then
echo -e "\nInvalid option!\n"
else
if [[ "$(mount | awk '/ on \/ / { print $1}' | sed 's/p*[0-9]$//g')" = $disk ]]; then
mnt=''
else
mnt='/mnt'
fi
search_disks=0
break
fi
;;
'') echo -e "\nInvalid option!\n" ;;
esac
done
if [ "$(echo $disk | grep nvme)" ]; then
bootPart="p$bootPartNum"
espPart="p$espPartNum"
swapPart="p$swapPartNum"
rootPart="p$rootPartNum"
else
bootPart="$bootPartNum"
espPart="$espPartNum"
swapPart="$swapPartNum"
rootPart="$rootPartNum"
fi
echo -e "\nDisk chosen: $disk (mounted on $mnt/)"
done
}
delete_partitions () {
check_on_root
unmount_disk
echo -e "\nWiping disk...\n"
wipefs -af $disk
check_pkg gptfdisk
sgdisk -Zo $disk
}
fs_packages () {
case $fstype in
ext4) pkg=e2fsprogs ;;
btrfs) pkg="btrfs-progs grub-btrfs rsync" ;;
bcachefs) pkg="bcachefs-tools rsync" ;;
xfs) pkg=xfsprogs ;;
f2fs) pkg=f2fs-tools ;;
jfs) pkg=jfsutils ;;
nilfs2) pkg="pacstrap_install nilfs-utils" ;;
esac
echo "$pkg"
}
create_partitions () {
check_on_root
delete_partitions
systemctl daemon-reload
check_pkg parted dosfstools
if [ $bootOwnPartition = true ]; then
parted --fix --align optimal -s $disk mklabel gpt \
mkpart ESP fat32 1Mib 512Mib \
mkpart BOOT ext2 512Mib 1024Mib \
mkpart SWAP linux-swap 1024Mib $startSwap \
set $espPartNum esp on \
set $swapPartNum swap on
mkfs.ext2 -F -L BOOT $disk$bootPart
else
parted --fix --align optimal -s $disk mklabel gpt \
mkpart ESP fat32 1Mib 512Mib \
mkpart SWAP linux-swap 1024Mib $startSwap \
set $espPartNum esp on \
set $swapPartNum swap on
fi
if [ $fstype = bcachefs ]; then
# Parted doesn't recognise bcachefs filesystem
parted -s $disk mkpart ROOT ext4 $startSwap $fsPercent%
else
parted -s $disk mkpart ROOT $fstype $startSwap $fsPercent%
fi
parted -s $disk print
# Won't work without a small delay
sync_disk
echo "Sleeping one second..."
sleep .1
mkfs.fat -F 32 -n EFI $disk$espPart
mkswap -L SWAP $disk$swapPart
if [ "$encryptLuks" = 'true' ]; then
cryptsetup -v luksFormat $disk$rootPart
cryptsetup open $disk$rootPart root
fi
pacman -S --needed --noconfirm $(fs_packages)
case $fstype in
btrfs) pacman -S --needed --noconfirm btrfs-progs
mkfs.btrfs -f -n 32k -L ROOT $disk$rootPart ;;
ext4) if [ "$encryptLuks" = 'true' ]; then
mkfs.ext4 /dev/mapper/root
mount /dev/mapper/root $mnt
# Check that mapping works as intended:
umount $mnt
cryptsetup close root
cryptsetup open $disk$rootPart root
mount /dev/mapper/root $mnt
else
mkfs.ext4 -F -q -t ext4 -L ROOT $disk$rootPart
echo "Running tune2fs to create fast commit journal area..."
tune2fs -O fast_commit $disk$rootPart
fi
;;
xfs) pacman -S --needed --noconfirm xfsprogs
mkfs.xfs -f -L ROOT $disk$rootPart ;;
jfs) pacman -S --needed --noconfirm jfsutils
mkfs.jfs -f -L ROOT $disk$rootPart ;;
f2fs) pacman -S --needed --noconfirm f2fs-tools
mkfs.f2fs -f -l ROOT $disk$rootPart ;;
nilfs2) pacman -S --needed --noconfirm nilfs-utils
mkfs.nilfs2 -f -L ROOT $disk$rootPart ;;
bcachefs) pacman -S --needed --noconfirm bcachefs-tools rsync
if [[ $encrypt = true ]]; then
#You MUST add 'bcachefs' module and hook (after 'filesystem')
bcachefs format -f -L ROOT --encrypted $disk$rootPart
bcachefs unlock -k session $disk$rootPart
else
bcachefs format -f -L ROOT $disk$rootPart
fi
;;
esac
if [[ $checkPartitions = true ]]; then
echo -e "\nRunning tests to check partitions\n"
if [[ -f /home/$user/.local/bin/checkpartitionsalignment.sh ]]; then
/home/user/.local/bin/./checkpartitionsalignment.sh $disk
fi
fi
parted -s $disk print
sync_disk
echo -e "\nPausing for 1 second...\n"
sleep .1
cd /
echo -e "\nMounting $mnt..."
if [ "$encryptLuks" = 'true' ]; then
if [ ! "$(mount | grep /dev/mapper/root)" ]; then
cryptsetup open $disk$rootPart root
fi
mount /dev/mapper/root $mnt
else
mount -t $fstype --mkdir $disk$rootPart $mnt
fi
if [ "$fstype" = "btrfs" ]; then
# https://www.ordinatechnic.com/distribution-specific-guides/Arch/an-arch-linux-installation-on-a-btrfs-filesystem-with-snapper-for-system-snapshots-and-rollbacks
#btrfs subvolume create $mnt/@
#btrfs subvolume create $mnt/@$snapshot_dir
#mkdir $mnt/@$snapshot_dir/$first_snapshot_name
#btrfs subvolume create $mnt/@$snapshot_dir/$first_snapshot_name/snapshot
#mkdir $mnt/@/boot
#btrfs subvolume create $mnt/@/boot/grub
#mkdir $mnt/@/var
#btrfs subvolume create $mnt/@/var/log
#btrfs subvolume create $mnt/@/var/tmp
btrfs subvolume create $mnt/$subvolPrefix
for subvol in "${subvols[@]}"; do
mkdir -p "$(dirname $mnt/$subvolPrefix$subvol)"
btrfs su create $mnt/$subvolPrefix$subvol
done
mkdir -p "$(dirname $mnt/$subvolPrefix$snapshot_dir/$first_snapshot_name/snapshot)"
btrfs su create $mnt/$subvolPrefix$snapshot_dir/$first_snapshot_name/snapshot
btrfs subvolume set-default $(btrfs subvolume list $mnt | grep "$subvolPrefix$snapshot_dir/$first_snapshot_name/snapshot" | grep -oP '(?<=ID )[0-9]+') $mnt
chattr +C $mnt/$subvolPrefix/var/log
chattr +C $mnt/$subvolPrefix/var/tmp
echo
btrfs subvolume list $mnt
echo -e "\nDefault subvolume set to:"
btrfs subvolume get-default $mnt
echo
elif [ "$fstype" = "bcachefs" ]; then
# https://www.reddit.com/r/bcachefs/comments/1b3uv59/booting_into_a_subvolume_and_rollback/
# Subvolumes not currently being added to /etc/fstab
for subvol in "${subvols[@]}"; do
echo -e "Creating subvolume: $mnt$subvolPrefix$subvol..."
# Cannot create the subvolume without dirname path (..)
mkdir -p "$(dirname $mnt$subvolPrefix$subvol)"
bcachefs subvolume create "$mnt$subvolPrefix$subvol"
done
fi
unmount_disk
mount_disk
mkdir -p $mnt/{dev,etc,proc,root,run,sys,tmp,var/cache/pacman/pkg,/var/log,/var/tmp,$aur_apps_path}
chmod -R 750 $mnt/root
mkdir -p $mnt/root/.gnupg
chmod -R 700 $mnt/root/.gnupg
chmod -R 1777 $mnt/var/tmp
if [ $fstype = btrfs ]; then
echo
btrfs su list $mnt
echo
# If not created snapper won't recognise it as a snapshot
setdate=$(date +"%Y-%m-%d %H:%M:%S")
cat > $mnt$snapshot_dir/$first_snapshot_name/info.xml << EOF
<?xml version="1.0"?>
<snapshot>
<type>single</type>
<num>1</num>
<date>$setdate</date>
<description>Initial snapshot</description>
<cleanup></cleanup>
</snapshot>
EOF
fi
genfstab -U $mnt
}
mount_disk () {
# No need to mount if we're on the main machine
[ "$mnt" = "" ] && return
check_on_root
if [[ ! $(mount | grep -E $disk$rootPart | grep -E "on $mnt") ]]; then
if [ "$fstype" = "btrfs" ]; then
if [ $(read -t 2 -sn1 -p "Press any key to change root mount." && echo 1) ]; then
mount -m $disk$rootPart -o "$btrfs_mountopts" $mnt
echo;echo
btrfs su list $mnt
umount -R $mnt
echo -e "\nPlease enter a path to mount:\n"
read mntSubvol
fi
if [ "$mntSubvol" ]; then
mount -m $disk$rootPart -o "$btrfs_mountopts,subvol=$mntSubvol" $mnt
else
# btrfs will automatically mount the 'set-default' subvolume
mount -m $disk$rootPart -o "$btrfs_mountopts" $mnt
fi
echo -e "\nDefault subvolume = $(btrfs su get-default $mnt | sed 's/ID.*path //')\n"
echo mount -m $disk$rootPart -o "$btrfs_mountopts,subvol=$(mount | grep "$disk$rootPart on $mnt " | sed 's#/dev/.*subvol=##; s/)//; s#^/##')" $mnt
#mount -m $disk$rootPart -o "$btrfs_mountopts",subvol=@$snapshot_dir $mnt$snapshot_dir
#mount -m $disk$rootPart -o "$btrfs_mountopts",subvol=@/boot/grub $mnt/boot/grub
#mount -m $disk$rootPart -o "$btrfs_mountopts",subvol=@/var/log,nodatacow $mnt/var/log
#mount -m $disk$rootPart -o "$btrfs_mountopts",subvol=@/var/tmp,nodatacow $mnt/var/tmp
for subvol in "${subvols[@]}"; do
echo mount -m $disk$rootPart -o "$btrfs_mountopts",subvol=$subvolPrefix$subvol $mnt$subvol
mount -m $disk$rootPart -o "$btrfs_mountopts",subvol=$subvolPrefix$subvol $mnt$subvol
done
elif [[ $fstype = bcachefs ]]; then
# mount: /dev/sda4: Input/output error
# [ERROR src/commands/mount.rs:395] Mount failed: Input/output error
# Not sure if this goes before or after encrypt for encrypted device
#echo "Running fsck on disk... Please be patient."
if [ $fsckBcachefs = 'true' ]; then
echo -e "\nWould you like to run fsck first? (type 'y' to run)\n"
read -sn1 key
[ "$key" = 'y' ] && bcachefs fsck -p $disk$rootPart
fi
if [[ $encrypt = true ]]; then
keyctl link @u @s
bcachefs unlock -k session $disk$rootPart
fi
mount -t $fstype --mkdir -o "$bcachefs_mountopts" $disk$rootPart $mnt
if [ "$(ls $mnt | grep '@root')" ]; then
echo -e "\nThere is a drive on $rootMount. Type 'y' to mount it.\n"
read -sn1 key
[ "$key" = 'y' ] && mount --bind -o "$bcachefs_mountopts" $mnt$rootMount $mnt
fi
for subvol in "${subvols[@]}"; do
#if [ ! "$subvol" = '.snapshots' ]; then
echo mount --bind -o "$bcachefs_mountopts" $mnt$subvolPrefix$subvol $mnt$subvolPrefix$subvol
mount --bind --mkdir -o "$bcachefs_mountopts" $mnt$subvolPrefix$subvol $mnt$subvolPrefix$subvol
#fi
done
else
if [ "$encryptLuks" = 'true' ]; then
if [ ! "$(mount | grep /dev/mapper/root)" ]; then
cryptsetup open $disk$rootPart root
fi
mount /dev/mapper/root $mnt
else
# For some reason ext4 prefers a plain mount
if [ "$fstype" = "ext4" ]; then
mount $disk$rootPart $mnt
else
mount -t $fstype --mkdir -o $mountops $disk$rootPart $mnt
fi
fi
fi
fi
if [[ ! $(mount | grep -E $disk$bootPart | grep -E "on $mnt/boot") ]] && [[ $bootOwnPartition = true ]]; then
echo mount -m $disk$bootPart -o $boot_mountopts $mnt/boot
mount -m $disk$bootPart -o $boot_mountopts $mnt/boot
fi
if [[ ! $(mount | grep -E $disk$espPart | grep -E "on $mnt$efi_path") ]]; then
echo mount -m $disk$espPart -o $efi_mountopts $mnt$efi_path
mount -m $disk$espPart -o $efi_mountopts $mnt$efi_path
fi
}
install_base () {
mount_disk
echo '[options]
HoldPkg = pacman glibc
Architecture = auto
CheckSpace
ParallelDownloads = 5
Color
[custom]
SigLevel = Optional TrustAll
Server = file:///var/cache/pacman/pkg/
' > /etc/pacman-offline.conf
if [ ! $mnt = '' ]; then
cp /etc/pacman-offline.conf $mnt/etc/pacman-offline.conf
if [ "$offline" -eq 1 ]; then
echo "Copying database files..."
mkdir -p $mnt/var/lib/pacman/sync
cp -r /var/lib/pacman/sync/*.db $mnt/var/lib/pacman/sync/
fi
fi
reset_keys
check_pkg arch-install-scripts
#check_pkg reflector
#echo -e "\nRunning reflector...\n"
#reflector > /etc/pacman.d/mirrorlist
[ "$(cat /etc/pacman.d/mirrorlist)" = "" ] && update_mirrorlist
mkdir -p $mnt/etc/pacman.d
[ ! $mnt = '' ] && cp /etc/pacman.d/mirrorlist $mnt/etc/pacman.d/mirrorlist
pacstrap_install "$(fs_packages) $base_install"
cp $mnt/etc/pacman.conf $mnt/etc/pacman.conf.pacnew
sed -i 's/#Color/Color/' $mnt/etc/pacman.conf
sed -i 's/CheckSpace/#CheckSpace/' $mnt/etc/pacman.conf
sed -i 's/#ParallelDownloads.*$/ParallelDownloads = 5/' $mnt/etc/pacman.conf
#sed -i 's/SigLevel = .*$/SigLevel = TrustAll/' $mnt/etc/pacman.conf
}
auto_login () {
login_user=$1
echo -e "\nCreating auto-login for $login_user.\n"
mkdir -p $mnt/etc/systemd/system/getty@tty1.service.d
cat > $mnt/etc/systemd/system/getty@tty1.service.d/autologin.conf << EOF
[Service]
Type=simple
ExecStart=
ExecStart=-/sbin/agetty --skip-login --nonewline --noissue --autologin $login_user --noclear %I 38400 linux
EOF
}
hypervisor_setup () {
choices=(exit auto kvm vmware oracle microsoft)
select choice in "${choices[@]}"; do
case $choice in
auto) hypervisor=$(systemd-detect-virt)
echo "Detected: $hypervisor" ;;
kvm) pacstrap_install qemu-guest-agent
systemctl --root=$mnt enable qemu-guest-agent ;;
vmware) pacstrap_install open-vm-tools
systemctl --root=$mnt enable vmtoolsd
systemctl --root=$mnt enable vmware-vmblock-fuse ;;
oracle) pacstrap_install virtualbox-guest-utils
systemctl --root=$mnt enable vboxservice ;;
microsoft) pacstrap_install hyperv
systemctl --root=$mnt enable hv_fcopy_daemon
systemctl --root=$mnt enable hv_kvp_daemon
systemctl --root=$mnt enable hv_vss_daemon ;;
exit) break ;;
*) echo -e "\nInvalid option!\n" ;;
'') echo -e "\nInvalid option!\n" ;;
esac
done
}
setup_fstab () {
mount_disk
if [ $fstype = 'btrfs' ]; then
sed -i 's#^Q /var/lib/machines 0700 - - -#\#&#' $mnt/usr/lib/tmpfiles.d/systemd-nspawn.conf
sed -i 's#^Q /var/lib/portables 0700#\#&#' $mnt/usr/lib/tmpfiles.d/portables.conf
[ "$(btrfs su list $mnt/ | grep var/lib/portables)" ] && btrfs su delete $mnt/var/lib/portables
[ "$(btrfs su list $mnt/ | grep var/lib/machines)" ] && btrfs su delete $mnt/var/lib/machines
fi
echo -e "\nCreating new /etc/fstab file...\n"
genfstab -U $mnt/ > $mnt/etc/fstab
grep $disk /proc/mounts > $mnt/etc/fstab.bak
### Tweak the resulting /etc/fstab generated ###