add host
[automated-distro-installer] / arch-init
1 #!/bin/bash -x
2 # Copyright (C) 2016 Ian Kelling
3
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
8
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
13
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
18 set -eE -o pipefail
19 trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' ERR
20
21 x="$(readlink -f "$BASH_SOURCE")"; cd ${x%/*}
22
23 export HOSTNAME="$1"
24 mirror=$2
25
26 (( $# >= 1 )) || { echo "$0: error: need 1 or 2 arguments"; exit 1; }
27
28 mv /root/devbyid /usr/bin
29
30 rm -f /etc/pacman.d/mirrorlist
31 # https://wiki.archlinux.org/index.php/Mirrors#Sorting_mirrors
32
33 if [[ $mirror ]]; then
34 echo "Server = $mirror" >> /etc/pacman.d/mirrorlist
35 fi
36 curl -s 'https://www.archlinux.org/mirrorlist/?country=US&protocol=https&ip_version=4&ip_version=6&use_mirror_status=on' |
37 sed -r 's/^[ #]*(Server *=)/\1/' >> /etc/pacman.d/mirrorlist
38
39 . /a/bin/fai/fai-wrapper
40 export LUKS_DIR=/root/luks
41 export DISTRO=arch
42 partition_script=/a/bin/fai/fai/config/hooks/partition.DEFAULT
43 chmod +x $partition_script
44
45 export PARTITION_PROMPT=true
46
47 # to be idempotent if we fail after partitioning
48 already_partitioned=true
49 mount_out=$(mount)
50 for dir in /mnt{,/home,/boot,/a}; do
51 regex=" on $dir "
52 if [[ ! $mount_out =~ $regex ]]; then
53 already_partitioned=false
54 break
55 fi
56 done
57
58 if ! $already_partitioned; then
59 /a/bin/fai/fai/config/hooks/partition.DEFAULT
60 fi
61
62 . /tmp/fai/disk_var.sh
63
64
65 # arch doesn't need crypttab entries for initramfs crypt partititions
66 export rootn=1
67 export bootn=3
68 export swapn=2
69 export BOOT_DEVICE
70 export ROOT_PARTITIONS
71 sed -ri --follow-symlinks "/^crypt_dev_\S+$rootn /d" /tmp/fai/crypttab
72
73 if ! $already_partitioned; then
74 mount -o subvol=root_$DISTRO $ROOT_PARTITION /mnt
75 mkdir -p /mnt/boot
76 mount -o subvol=boot_$DISTRO $BOOT_PARTITION /mnt/boot
77 fi
78
79 # https://wiki.archlinux.org/index.php/Dm-crypt/Device_encryption#Keyfiles
80 cp /root/luks/host-$HOSTNAME /mnt/crypto_keyfile.bin
81 chmod 600 /mnt/crypto_keyfile.bin
82
83
84 if [[ $mirror ]]; then
85 echo "$0: 404 errors about core.db etc are normal,
86 they will succeed using the secodary mirror"
87 fi
88 pacstrap /mnt base
89 cp /tmp/fai/{fstab,crypttab} /mnt/etc
90 cp /a/bin/fai/encrypt /mnt/usr/lib/initcpio/hooks
91 # not needed anymore
92 #cp /usr/bin/devbyid /mnt/root
93
94 cp -r /root/.ssh /mnt/root
95
96 bindmount() {
97 local mountpoint=$2
98 local source=$1
99 mkdir -p $mountpoint
100 mount -o bind $source $mountpoint
101 }
102 bindmount /root/shadow /mnt/q/root/shadow
103 bindmount /a /mnt/a
104
105 mkdir -p /mnt/etc/ssh
106 cp /etc/ssh/ssh_host_* /mnt/etc/ssh
107
108 cp /a/bin/fai/arch-init-chroot /mnt/root
109 # for manual commands, arch-chroot /mnt bash
110 arch-chroot /mnt /root/arch-init-chroot
111
112 # this gets mounted in chroot so we have to do it outside
113 rm -f /mnt/etc/resolv.conf
114 ln -s /run/systemd/resolve/resolv.conf /mnt/etc/resolv.conf
115
116 # not necsesary, but makes reboot go fast.
117 umount -R /mnt; sleep 1
118
119 # causes 255 exit code, so doing this from the caller script.
120 # reboot now