-
Notifications
You must be signed in to change notification settings - Fork 7
/
arch-installer.sh
executable file
·337 lines (284 loc) · 8.85 KB
/
arch-installer.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
#!/bin/bash
#
# Copyright 2014 Kay Sievers
# Copyright 2015 Intel Corporation. All rights reserved.
#
# This script is free software; you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
#
# This script is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with systemd; If not, see <http://www.gnu.org/licenses/>.
#
# Based on Minimal Fedora Installer by Kay Sievers
# - standalone, fully bootable USB stick/mmc
# - text mode
# - UEFI-only
#
# Erase and reformat an entire disk:
# $ ./arch-installer.sh /dev/sdc
#
# QEMU needs a UEFI firmware:
# sudo qemu-system-x86_64 -machine accel=kvm -m 256 -bios /usr/share/qemu/bios-ovmf.bin /dev/sdc
set -e
SCRIPT_DIR=$(dirname $(realpath ${BASH_SOURCE[0]}))
usage() {
echo "
Usage: $(basename $0) [OPTIONS] <disk or image file to re-format>
OPTIONS
-x comma-separated list of hooks to run as last step,
before unmounting directories. The hook can get the
rootfs location by reading the ROOTFS environment
variable. Each item can be a command or a path to a
file or directory. If a command, the directory
arch-installer-hooks is prepended to PATH environment
variable. In case of a directory it will be executed
by sorting the files alphabetically. This option may
be passed multiple times.
-l Force installation, not checking if network is available
using local pacman database and cache. This relies on all
packages being already present in host cache. If they
aren't the installer will fail later.
Examples:
# using built-in hook \"overlay\" to append contents of files under
# path/to/overlay to files under ROOTFS
arch-installer -x \"overlay-append path/to/overlay\" /dev/sdx1
# copying current system's pacman mirrorlist to the fresh new system
arch-installer -x \"cp /etc/pacman.d/mirrorlist \\\$ROOTFS/etc/pacman.d/\" /dev/sdx1
" 1>&$1;
}
OPT_LOCAL=""
hooks=()
execute_hook() {
export ROOTFS
local hook=$1
if [[ -f "$hook" ]]; then
if [[ ! -x "$hook" ]]; then
echo "Ignoring non-executable hook: \"$hook\"" >&2
return
fi
bash "$hook"
else
bash -c "$hook"
fi
}
args=0
while getopts "x:lh" o; do
case "${o}" in
x)
IFS=, read -ra arg_hooks <<<"${OPTARG}"
hooks+=("${arg_hooks[@]}")
args=$[$args + 2] ;;
l)
OPT_LOCAL="-C $SCRIPT_DIR/arch-installer-files/pacman.conf"
args=$[$args + 1] ;;
h) usage 1; exit 0;;
\?) usage 2; exit 1;;
esac
done
shift $args
if [ -z "$1" ]; then
usage 2
exit 1
fi
echo $1
# make sure we have a current package database and working
# network connection
[ -n "$OPT_LOCAL" ] || pacman -Sy || {
echo "Cannot update package database - is the network up and running?" >&2
exit 1
}
trap '
ret=$?;
set +e;
if [[ $ROOT ]]; then
for i in /proc /run /boot /dev /sys ""; do
umount /run/installer-$ROOT/system$i
done
rm -rf /run/installer-$ROOT
fi
[[ $DEV == /dev/loop* ]] && losetup -d $DEV
if [[ $ret -ne 0 ]]; then
echo FAILED TO GENERATE IMAGE >&2
fi
exit $ret;
' EXIT
# clean up after ourselves no matter how we die.
trap 'exit 1;' SIGINT
if [[ "$1" == /dev/* ]] && [[ ! -b "$1" ]]; then
echo "Couldn't find block device $1" >&2
exit 1
fi
if [[ ! -e "$1" ]] || [[ -f "$1" ]]; then
dd if=/dev/null of="$1" bs=1M seek=4096
DEV=$(losetup -f --show "$1")
else
DEV="$1"
fi
if [[ ! -b "$DEV" ]]; then
echo "$1 is not a block device" >&2
exit 1
fi
ROOT=${DEV##*/}
printf "\n### erasing /dev/$ROOT\n"
# get rid of all signatures
wipefs -a /dev/$ROOT
dd if=/dev/zero of=/dev/$ROOT bs=1M count=4
printf "\n### formatting EFI System Partition\n"
parted /dev/$ROOT --script "mklabel gpt" "mkpart ESP fat32 1MiB 511Mib" "set 1 boot on"
BOOT_PART=/dev/${ROOT}1
[[ -b /dev/${ROOT}p1 ]] && BOOT_PART=/dev/${ROOT}p1
wipefs -a $BOOT_PART
mkfs.vfat -n EFI -F32 $BOOT_PART
printf "\n### formatting System Partition\n"
parted /dev/$ROOT --script "mkpart System ext4 512Mib -1Mib"
case "x`uname -m`" in
xx86_64)
ROOT_UUID=4f68bce3-e8cd-4db1-96e7-fbcaf984b709
;;
xi686|xi586|xi486|xi386)
ROOT_UUID=44479540-f297-41b2-9af7-d131d5f0458a
;;
*)
ROOT_UUID=0fc63daf-8483-4772-8e79-3d69d8477de4
;;
esac
echo "t
2
$ROOT_UUID
w
y
q" | gdisk /dev/$ROOT
if test ${PIPESTATUS[1]} -ne 0; then
echo "error paritioning disk" >&2
exit 1
fi
SYSTEM_PART=/dev/${ROOT}2
[[ -b /dev/${ROOT}p2 ]] && SYSTEM_PART=/dev/${ROOT}p2
wipefs -a $SYSTEM_PART
# TODO: add options to select fs (and add proper packages)
mkfs.ext4 -L System $SYSTEM_PART
rm -rf /run/installer-$ROOT
ROOTFS="/run/installer-$ROOT/system"
#############################################################
echo "### installing Archlinux at /dev/$ROOT"
# mount System
mkdir -p $ROOTFS
mount $SYSTEM_PART $ROOTFS
# mount /boot
mkdir $ROOTFS/boot
mount $BOOT_PART $ROOTFS/boot
printf "\n### download and install base packages\n"
pacman -Sg base | cut -d ' ' -f 2 | \
sed -e /^linux\$/d \
-e /^mdadm/d \
-e /^lvm2/d \
-e /^cryptsetup/d \
-e /^device-mapper/d \
-e /^xfsprogs/d \
-e /^reiserfsprogs/d \
-e /^jfsutils/d \
-e /^man-pages/d \
-e /^man-db/d \
-e /^pcmciautils/d \
-e /^inetutils/d \
-e /^dhcpcd/d \
-e /^netctl/d \
-e /^s-nail/d \
| pacstrap -c $OPT_LOCAL $ROOTFS -
# mount kernel filesystems and /boot again
mount --bind /proc $ROOTFS/proc
mount --bind /dev $ROOTFS/dev
mount --bind /sys $ROOTFS/sys
if ! findmnt $ROOTFS/boot >/dev/null; then
mount $BOOT_PART $ROOTFS/boot
fi
mount -t tmpfs none $ROOTFS/run
# install additional packages
pacman --root=$ROOTFS --noconfirm -S \
mkinitcpio \
bash-completion \
openssh
# at bootup mount / read-writable
cat > $ROOTFS/etc/fstab <<EOF
ROOT / auto rw,relatime,data=ordered 0 0
EOF
# default locale to en_US utf-8.
# TODO: add options for locale
echo "en_US.UTF-8 UTF-8" >> $ROOTFS/etc/locale.gen
chroot $ROOTFS locale-gen
printf "\n### install boot loader\n"
chroot $ROOTFS bootctl install --no-variables
mkdir -p $ROOTFS/boot/loader/entries
read MACHINE_ID < $ROOTFS/etc/machine-id
CMDLINE="console=ttyS0,115200 console=tty0 rw quiet"
# TODO: Change Arch to use kernel-install
cat > $ROOTFS/boot/loader/entries/arch.conf <<EOF
title Arch Linux
options $CMDLINE
linux /vmlinuz-linux
initrd /initramfs-linux.img
EOF
cat > $ROOTFS/boot/loader/entries/arch-fallback.conf <<EOF
title Arch Linux Fallback Initrd
options $CMDLINE
linux /vmlinuz-linux
initrd /initramfs-linux-fallback.img
EOF
cat > $ROOTFS/boot/loader/loader.conf <<EOF
timeout 1
# enable below when using custom kernel
# default $MACHINE_ID-*
default arch-fallback
EOF
# configure for custom kernels
mkdir -p $ROOTFS/etc/kernel/install.d
cat > $ROOTFS/etc/kernel/cmdline <<EOF
$CMDLINE
EOF
sed -i 's/^HOOKS.*/HOOKS="systemd autodetect modconf block filesystems fsck"/' \
$ROOTFS/etc/mkinitcpio.conf
printf "\n### install kernel\n"
pacman --root=$ROOTFS --noconfirm -S linux
printf "\n### customizing image\n"
# set default target
systemctl --root=$ROOTFS set-default multi-user.target
# serial
systemctl --root=$ROOTFS enable [email protected]
# ssh
systemctl --root=$ROOTFS enable sshd.socket
# networkd
systemctl --root=$ROOTFS enable systemd-networkd.service
systemctl --root=$ROOTFS enable systemd-resolved.service
ln -sf /run/systemd/resolve/resolv.conf $ROOTFS/etc/resolv.conf
# enable DHCP for all Ethernet interfaces
mkdir -p $ROOTFS/usr/lib/systemd/network
cat > $ROOTFS/usr/lib/systemd/network/ether.network <<EOF
[Match]
Name=en*
[Network]
DHCP=yes
EOF
for hook in "${hooks[@]}"; do
if [ -d "$hook" ]; then
for f in "$hook"/*; do
execute_hook "$f"
done
elif [ -f "$hook" ]; then
execute_hook "$hook"
else
# consider hook a command
PATH=$SCRIPT_DIR/arch-installer-hooks:$PATH execute_hook "$hook"
fi
done
sync
if [ -f "$1" -a -n "$SUDO_UID" ]; then
chown $SUDO_UID:$SUDO_GID $1
fi
printf "\n### finished\n"