various improvements
[automated-distro-installer] / arch-init
1 #!/bin/bash -x
2
3 # see t.org for how to call
4
5 set -eE -o pipefail
6 trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' ERR
7
8 cd $(dirname $(readlink -f "$BASH_SOURCE"))
9
10 ROOTPW="$(cat /root/shadow/standard)"
11 export ROOTPW
12 export hostname="$1"
13 mirror=$2
14 TPPASS="$(cat /root/shadow/traci-simple)"
15 export TPPASS
16 if [[ $hostname == tp ]]; then
17 ROOTPW="$TPPASS"
18 fi
19
20 (( $# >= 1 )) || { echo "$0: error: need 1 or 2 arguments"; exit 1; }
21
22 mv /root/devbyid /usr/bin
23
24 rm -f /etc/pacman.d/mirrorlist
25 # https://wiki.archlinux.org/index.php/Mirrors#Sorting_mirrors
26
27 if [[ $mirror ]]; then
28 echo "Server = $mirror" >> /etc/pacman.d/mirrorlist
29 fi
30 curl -s 'https://www.archlinux.org/mirrorlist/?country=US&protocol=https&ip_version=4&ip_version=6&use_mirror_status=on' |
31 sed -r 's/^[ #]*(Server *=)/\1/' >> /etc/pacman.d/mirrorlist
32
33 # export class vars with CLASS_ in front to avoid name colissions.
34 ifclass() {
35 local var=${1/#/CLASS_}
36 [[ $hostname == $1 || ${!var} ]]
37 }
38 export -f ifclass
39 for x in $(bash 50-host-classes); do
40 export CLASS_$x=true
41 done
42 export LUKS_DIR=/root/luks
43 export HOSTNAME=$hostname
44 export DISTRO=arch
45 chmod +x partition.DEFAULT
46
47 export PARTITION_PROMPT=true
48
49 # to be idempotent if we fail after partitioning
50 already_partitioned=true
51 mount_out=$(mount)
52 for dir in /mnt{,/home,/boot,/q}; do
53 regex=" on $dir "
54 if [[ ! $mount_out =~ $regex ]]; then
55 already_partitioned=false
56 break
57 fi
58 done
59
60 if ! $already_partitioned; then
61 ./partition.DEFAULT
62 fi
63
64 . /tmp/fai/disk_var.sh
65
66
67 # arch doesn't need crypttab entries for initramfs crypt partititions
68 export rootn=1
69 export bootn=3
70 export swapn=2
71 export BOOT_DEVICE
72 export ROOT_PARTITIONS
73 sed -ri "/^crypt_dev_\S+$rootn /d" /tmp/fai/crypttab
74
75 if ! $already_partitioned; then
76 mount -o subvol=root_$DISTRO $ROOT_PARTITION /mnt
77 mkdir -p /mnt/{q,home,boot}
78 mount -o subvol=q $ROOT_PARTITION /mnt/q
79 mount -o subvol=home_$DISTRO $ROOT_PARTITION /mnt/home
80 mount -o subvol=boot_$DISTRO $BOOT_PARTITION /mnt/boot
81 fi
82
83 # https://wiki.archlinux.org/index.php/Dm-crypt/Device_encryption#Keyfiles
84 cp /root/luks/host-$hostname /mnt/crypto_keyfile.bin
85 chmod 600 /mnt/crypto_keyfile.bin
86
87
88 if [[ $mirror ]]; then
89 echo "$0: 404 errors about core.db etc are normal,
90 they will succeed using the secodary mirror"
91 fi
92 pacstrap /mnt base
93 cp /tmp/fai/{fstab,crypttab} /mnt/etc
94 cp /root/encrypt /mnt/usr/lib/initcpio/hooks
95 # not needed anymore
96 #cp /usr/bin/devbyid /mnt/root
97
98 cp -r .ssh /mnt/root
99 cp -r /root/distro-install-common /mnt/root
100 mkdir -p /mnt/etc/ssh
101 cp /etc/ssh/host_* /mnt/etc/ssh
102
103 cp /root/arch-init-chroot /mnt/root
104 # for manual commands, arch-chroot /mnt bash
105 arch-chroot /mnt /root/arch-init-chroot
106
107 # this gets mounted in chroot so we have to do it outside
108 rm -f /mnt/etc/resolv.conf
109 ln -s /run/systemd/resolve/resolv.conf /mnt/etc/resolv.conf
110
111 # not necsesary, but makes reboot go fast.
112 #umount -R /mnt; sleep 1
113
114 # causes 255 exit code, so doing this from the caller script.
115 # reboot now