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