better error output
[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 (( $# >= 2 )) || { echo "error: need 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 CLASS_TWO_DISK=true
43 export LUKS_DIR=/root/luks
44 export HOSTNAME=$hostname
45 export DISTRO=arch
46 chmod +x partition.DEFAULT
47
48 export PARTITION_PROMPT=true
49
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 sed -ri "/^crypt_dev_\S+$rootn /d" /tmp/fai/crypttab
73
74 if ! $already_partitioned; then
75 mount -o subvol=root_$DISTRO $ROOT_PARTITION /mnt
76 mkdir -p /mnt/{q,home,boot}
77 mount -o subvol=q $ROOT_PARTITION /mnt/q
78 mount -o subvol=home_$DISTRO $ROOT_PARTITION /mnt/home
79 mount -o subvol=boot_$DISTRO $BOOT_PARTITION /mnt/boot
80 fi
81
82 # https://wiki.archlinux.org/index.php/Dm-crypt/Device_encryption#Keyfiles
83 cp /root/luks/host-$hostname /mnt/crypto_keyfile.bin
84 chmod 600 /mnt/crypto_keyfile.bin
85
86
87 shopt -s extglob
88 case $hostname in
89 # todo: these hosts are broken, not updated to new fai hyrbrid scripts.
90 frodo)
91
92 # for this system, no separate /boot, to keep partitions simple,
93 # since we want simpler backup recovery.
94 mount -U $rootid /mnt
95 ;;&
96 treetowl)
97 mount /dev/mapper/vg_treetowl00-lv02 /mnt
98 mount -U $bootid /mnt/boot
99 ;;&
100 frodo|treetowl)
101 rm -rf /mnt/!(a|i|q|f|boot) /mnt/boot/*
102 ;;
103 esac
104
105
106
107 if [[ $mirror ]]; then
108 echo "$0: 404 errors about core.db etc are normal,
109 they will succeed using the secodary mirror"
110 fi
111 pacstrap /mnt base
112 cp /tmp/fai/{fstab,crypttab} /mnt/etc
113 cp /usr/bin/devbyid /mnt/root
114 case $hostname in
115 frodo)
116 # the root .ssh needs to be like this,
117 # because it\'s used to get the key to mount an encrypted filesystem
118 # on top of itself.
119 d=/mnt/q/root/.ssh
120 rm -rf $d # for idempotency
121 mkdir -p $d
122 scp -oStrictHostKeyChecking=no ian@treetowl:/a/c/machine_specific/frodo/subdir_files/.ssh/* $d
123 cp .ssh/* $d
124 ln -s /q/root/.ssh /mnt/root
125 # background: errors=remount-ro is a debian installer thing. seems like
126 # not a bad idea. man mount says: The default is set in the filesystem
127 # superblock, and can be changed using tune2fs(8)
128
129 cat > /mnt/etc/fstab <<'EOF'
130 UUID=e9ce7b46-9a21-4e79-b7f7-0b18acb57587 / ext4 noatime,errors=remount-ro 0 1
131 UUID=dd67766f-93c5-4ce3-9877-a1d9841dd4a4 none swap sw 0 0
132 /dev/sr0 /media/cdrom0 udf,iso9660 user,noauto 0 0
133 /dev/mapper/crypta7 /mnt/btrfs_root btrfs subvolid=0,noatime,noauto 0 2
134 /dev/mapper/crypta7 /a btrfs subvol=a,noatime,noauto 0 2
135 EOF
136 ;;
137 *)
138 cp -r .ssh /mnt/root
139 cp -r /root/distro-install-common /mnt/root
140 ;;&
141 treetowl)
142 echo "UUID=a9e83bb7-d23d-4de6-ba9f-d88b887f7206 /a ext4 noatime 0 2" >> /mnt/etc/fstab
143 ;;
144 esac
145
146 cp /root/arch-init-chroot /mnt/root
147 # for manual commands, arch-chroot /mnt bash
148 arch-chroot /mnt /root/arch-init-chroot
149
150 # this gets mounted in chroot so we have to do it outside
151 rm -f /mnt/etc/resolv.conf
152 ln -s /run/systemd/resolve/resolv.conf /mnt/etc/resolv.conf
153
154 # not necsesary, but makes reboot go fast.
155 umount -R /mnt; sleep 1
156
157 # causes 255 exit code, so doing this from the caller script.
158 # reboot now