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