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