fix arch install with new partition scheme
[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 $?"' ERR
7
8 cd $(dirname $(readlink -f "$BASH_SOURCE"))
9
10 export ROOTPW="$1"
11 export hostname="$2"
12 mirror=$3
13
14 (( $# >= 2 )) || { echo "error: need 2 arguments"; exit 1; }
15
16 case $hostname in
17 x2)
18 export grubdisk=/dev/sda
19 ;;
20 demohost)
21 export grubdisk=/dev/vda
22 ;;
23 treetowl)
24 bootid=64d495ee-c9fe-4174-b20a-6c5e47abcfa1
25 export grubdisk=$(blkid|sed -nr "/$bootid/s/^([^0-9]+).*/\1/p")
26 ;;
27 frodo)
28 rootid=e9ce7b46-9a21-4e79-b7f7-0b18acb57587
29 export grubdisk=$(blkid|sed -nr "/$rootid/s/(^[^0-9]*).*/\1/p")
30 ;;
31 *)
32 echo "unrecognized hostname: $hostname"
33 exit 1
34 esac
35
36
37 rm -f /etc/pacman.d/mirrorlist
38 # https://wiki.archlinux.org/index.php/Mirrors#Sorting_mirrors
39
40 if [[ $mirror ]]; then
41 echo "Server = $mirror" >> /etc/pacman.d/mirrorlist
42 fi
43 curl -s 'https://www.archlinux.org/mirrorlist/?country=US&protocol=https&ip_version=4&ip_version=6&use_mirror_status=on' |
44 sed -r 's/^[ #]*(Server *=)/\1/' >> /etc/pacman.d/mirrorlist
45
46 # export class vars with CLASS_ in front to avoid name colissions.
47 ifclass() {
48 local var=${1/#/CLASS_}
49 [[ $hostname == $1 || ${!var} ]]
50 }
51 export -f ifclass
52 for x in $(bash 50-host-classes); do
53 export CLASS_$x=true
54 done
55 export CLASS_TWO_DISK=true
56 export LUKS_DIR=/root/luks
57 export HOSTNAME=$hostname
58 chmod +x partition.DEFAULT
59 ./partition.DEFAULT
60 # arch doesn't need crypttab entries for initramfs crypt partititions
61 export rootn=1
62 export bootn=3
63 export swapn=2
64 sed -ri '/^crypt_dev/d' /tmp/fai/crypttab
65 mount -o subvol=/root /dev/mapper/crypt_dev_?da$rootn /mnt
66 mkdir -p /mnt/{q,home}
67 mount -o subvol=/q /dev/mapper/crypt_dev_?da$rootn /mnt/q
68 mount -o subvol=/home /dev/mapper/crypt_dev_?da$rootn /mnt/home
69 mkdir -p /mnt/etc
70 cp /tmp/fai/{fstab,crypttab} /mnt/etc
71 mkdir -p /mnt/boot
72 mount /dev/?da$bootn /mnt/boot
73
74 # https://wiki.archlinux.org/index.php/Dm-crypt/Device_encryption#Keyfiles
75 cp /root/luks/host-$hostname /mnt/crypto_keyfile.bin
76 chmod 600 /mnt/crypto_keyfile.bin
77
78
79 shopt -s extglob
80 case $hostname in
81 # these hosts are broken, not updated to new fai hyrbrid scripts.
82 frodo)
83
84 # for this system, no separate /boot, to keep partitions simple,
85 # since we want simpler backup recovery.
86 mount -U $rootid /mnt
87 ;;&
88 treetowl)
89 mount /dev/mapper/vg_treetowl00-lv02 /mnt
90 mount -U $bootid /mnt/boot
91 ;;&
92 frodo|treetowl)
93 rm -rf /mnt/!(a|i|q|f|boot) /mnt/boot/*
94 ;;
95 esac
96
97
98
99 if [[ $mirror ]]; then
100 echo "$0: 404 errors about core.db etc are normal,
101 they will succeed using the secodary mirror"
102 fi
103 pacstrap /mnt base
104 case $hostname in
105 frodo)
106 # the root .ssh needs to be like this,
107 # because it\'s used to get the key to mount an encrypted filesystem
108 # on top of itself.
109 d=/mnt/q/root/.ssh
110 rm -rf $d # for idempotency
111 mkdir -p $d
112 scp -oStrictHostKeyChecking=no ian@treetowl:/a/c/machine_specific/frodo/subdir_files/.ssh/* $d
113 cp .ssh/* $d
114 ln -s /q/root/.ssh /mnt/root
115 # background: errors=remount-ro is a debian installer thing. seems like
116 # not a bad idea. man mount says: The default is set in the filesystem
117 # superblock, and can be changed using tune2fs(8)
118
119 cat > /mnt/etc/fstab <<'EOF'
120 UUID=e9ce7b46-9a21-4e79-b7f7-0b18acb57587 / ext4 noatime,errors=remount-ro 0 1
121 UUID=dd67766f-93c5-4ce3-9877-a1d9841dd4a4 none swap sw 0 0
122 /dev/sr0 /media/cdrom0 udf,iso9660 user,noauto 0 0
123 /dev/mapper/crypta7 /mnt/btrfs_root btrfs subvolid=0,noatime,noauto 0 2
124 /dev/mapper/crypta7 /a btrfs subvol=a,noatime,noauto 0 2
125 EOF
126 ;;
127 *)
128 echo "$0: first fstab:"
129 cat /mnt/etc/fstab
130 genfstab -p /mnt > /mnt/etc/fstab
131 echo "$0: 2nd fstab:"
132 cat /mnt/etc/fstab
133 cp -r .ssh /mnt/root
134 cp -r /root/distro-install-common /mnt/root
135 ;;&
136 treetowl)
137 echo "UUID=a9e83bb7-d23d-4de6-ba9f-d88b887f7206 /a ext4 noatime 0 2" >> /mnt/etc/fstab
138 ;;
139 esac
140
141 cp /root/arch-init-chroot /mnt/root
142 # for manual commands, arch-chroot /mnt bash
143 arch-chroot /mnt /root/arch-init-chroot
144
145 # this gets mounted in chroot so we have to do it outside
146 rm -f /mnt/etc/resolv.conf
147 ln -s /run/systemd/resolve/resolv.conf /mnt/etc/resolv.conf
148
149 # not necsesary, but makes reboot go fast.
150 umount -R /mnt
151
152 # causes 255 exit code, so doing this from the caller script.
153 # reboot now