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