-
Notifications
You must be signed in to change notification settings - Fork 194
/
Copy pathinstall.sh
executable file
·333 lines (273 loc) · 8.58 KB
/
install.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
#!/bin/sh
type getargbool >/dev/null 2>&1 || . /lib/dracut-lib.sh
# These functions pulled from void's excellent mklive.sh
VAI_info_msg() {
printf "\033[1m%s\n\033[m" "$@"
}
VAI_print_step() {
CURRENT_STEP=$((CURRENT_STEP+1))
VAI_info_msg "[${CURRENT_STEP}/${STEP_COUNT}] $*"
}
# ----------------------- Install Functions ------------------------
VAI_welcome() {
clear
printf "=============================================================\n"
printf "================ Void Linux Auto-Installer ==================\n"
printf "=============================================================\n"
}
VAI_udev_settle() {
/usr/bin/udevd --daemon
/usr/bin/udevadm trigger --action=add --type=subsystems
/usr/bin/udevadm trigger --action=add --type=devices
/usr/bin/udevadm settle
}
VAI_get_address() {
mkdir -p /var/lib/dhclient
# This will fork, but it means that over a slow link the DHCP
# lease will still be maintained. It also doesn't have a
# hard-coded privsep user in it like dhcpcd.
dhclient
}
VAI_partition_disk() {
# Paritition Disk
if [ -d /sys/firmware/efi ] ; then
VAI_info_msg "Partitioning Disk for UEFI Boot"
sfdisk "${disk}" <<EOF
label: gpt
,100M,U,
,,L,
EOF
else
VAI_info_msg "Partitioning Disk for BIOS Boot"
sfdisk "${disk}" <<EOF
label: dos
,,L,*
EOF
fi
}
VAI_format_disk() {
# Make Filesystems
if [ -d /sys/firmware/efi ] ; then
mkfs.vfat -F32 "${disk}1"
mkfs.ext4 -F "${disk}2"
else
mkfs.ext4 -F "${disk}1"
fi
}
VAI_mount_target() {
# Mount targetfs
mkdir -p "${target}"
if [ -d /sys/firmware/efi ] ; then
mount "${disk}2" "${target}"
mkdir -p "${target}/boot/efi"
mount "${disk}1" "${target}/boot/efi"
else
mount "${disk}1" "${target}"
fi
}
VAI_install_xbps_keys() {
mkdir -p "${target}/var/db/xbps/keys"
cp /var/db/xbps/keys/* "${target}/var/db/xbps/keys"
}
VAI_install_base_system() {
# Install a base system
_grub="grub"
if [ -d /sys/firmware/efi ] ; then
_grub="${_grub} grub-x86_64-efi"
fi
XBPS_ARCH="${XBPS_ARCH}" xbps-install -Sy -R "${xbpsrepository}" -r /mnt base-system ${_grub}
# Install additional packages
if [ -n "${pkgs}" ] ; then
# shellcheck disable=SC2086
XBPS_ARCH="${XBPS_ARCH}" xbps-install -Sy -R "${xbpsrepository}" -r /mnt ${pkgs}
fi
}
VAI_prepare_chroot() {
# Mount efivars if this is an EFI system
if [ -d /sys/firmware/efi ] ; then
mount -t efivarfs none /sys/firmware/efi/efivars
fi
# Mount dev, bind, proc, etc into chroot
mount -t proc proc "${target}/proc"
mount --rbind /sys "${target}/sys"
mount --rbind /dev "${target}/dev"
}
VAI_configure_sudo() {
# Give wheel sudo
echo "%wheel ALL=(ALL:ALL) ALL" > "${target}/etc/sudoers.d/00-wheel"
chmod 0440 "${target}/etc/sudoers.d/00-wheel"
}
VAI_correct_root_permissions() {
chroot "${target}" chown root:root /
chroot "${target}" chmod 755 /
}
VAI_configure_hostname() {
# Set the hostname
echo "${hostname}" > "${target}/etc/hostname"
}
VAI_configure_rc_conf() {
# Set the value of various tokens
sed -i "s:Europe/Madrid:${timezone}:" "${target}/etc/rc.conf"
sed -i "s:\"es\":\"${keymap}\":" "${target}/etc/rc.conf"
# Activate various tokens
sed -i "s:#HARDWARECLOCK:HARDWARECLOCK:" "${target}/etc/rc.conf"
sed -i "s:#TIMEZONE:TIMEZONE:" "${target}/etc/rc.conf"
sed -i "s:#KEYMAP:KEYMAP:" "${target}/etc/rc.conf"
}
VAI_add_user() {
chroot "${target}" useradd -m -s /bin/bash -U -G wheel,users,audio,video,cdrom,input "${username}"
if [ -z "${password}" ] ; then
chroot "${target}" passwd "${username}"
else
# For reasons that remain unclear, this does not work in musl
echo "${username}:${password}" | chpasswd -c SHA512 -R "${target}"
fi
}
VAI_configure_grub() {
# Set hostonly
echo "hostonly=yes" > "${target}/etc/dracut.conf.d/hostonly.conf"
# Choose the newest kernel
kernel_version="$(xbps-uhelper -r ${target} version linux | sed 's/_.*//')"
# Install grub
chroot "${target}" grub-install "${disk}"
chroot "${target}" xbps-reconfigure -f "linux${kernel_version}"
# Correct the grub install
chroot "${target}" update-grub
}
VAI_configure_fstab() {
# Grab UUIDs
uuid1="$(blkid -s UUID -o value "${disk}1")"
if [ -d /sys/firmware/efi ] ; then
uuid2="$(blkid -s UUID -o value "${disk}2")"
echo "UUID=$uuid1 /boot/efi vfat defaults 0 0" >> "${target}/etc/fstab"
echo "UUID=$uuid2 / ext4 defaults,errors=remount-ro 0 1" >> "${target}/etc/fstab"
else
echo "UUID=$uuid1 / ext4 defaults,errors=remount-ro 0 1" >> "${target}/etc/fstab"
fi
}
VAI_configure_locale() {
# Set the libc-locale iff glibc
case "${XBPS_ARCH}" in
*-musl)
VAI_info_msg "Glibc locales are not supported on musl"
;;
*)
sed -i "/${libclocale}/s/#//" "${target}/etc/default/libc-locales"
chroot "${target}" xbps-reconfigure -f glibc-locales
;;
esac
}
VAI_end_action() {
case $end_action in
reboot)
VAI_info_msg "Rebooting the system"
sync
umount -R "${target}"
reboot -f
;;
shutdown)
VAI_info_msg "Shutting down the system"
sync
umount -R "${target}"
poweroff -f
;;
script)
VAI_info_msg "Running user provided script"
xbps-uhelper fetch "${end_script}>/script"
chmod +x /script
target=${target} xbpsrepository=${xbpsrepository} /script
;;
func)
VAI_info_msg "Running user provided function"
end_function
;;
esac
}
VAI_configure_autoinstall() {
# -------------------------- Setup defaults ---------------------------
disk_expr=".blockdevices[0].name"
disk="/dev/$(lsblk --json | jq -r "$disk_expr")"
hostname_expr='[.[]|select(.operstate=="UP").addr_info.[]|select(.scope=="global").local].[0]'
hostname="$(ip --json -r a | jq -r "$hostname_expr")"
target="/mnt"
timezone="America/Chicago"
keymap="us"
libclocale="en_US.UTF-8"
username="voidlinux"
end_action="shutdown"
end_script="/bin/true"
XBPS_ARCH="$(xbps-uhelper arch)"
case $XBPS_ARCH in
*-musl)
xbpsrepository="https://repo-default.voidlinux.org/current/musl"
;;
*)
xbpsrepository="https://repo-default.voidlinux.org/current"
;;
esac
# --------------- Pull config URL out of kernel cmdline -------------------------
set +e
if getargbool 0 autourl ; then
set -e
xbps-uhelper fetch "$(getarg autourl)>/etc/autoinstall.cfg"
else
set -e
mv /etc/autoinstall.default /etc/autoinstall.cfg
fi
# Read in the resulting config file which we got via some method
if [ -f /etc/autoinstall.cfg ] ; then
VAI_info_msg "Reading configuration file"
. ./etc/autoinstall.cfg
fi
# Bail out if we didn't get a usable disk
if [ -z "$disk" ] ; then
die "No valid disk!"
fi
}
VAI_main() {
CURRENT_STEP=0
STEP_COUNT=17
VAI_welcome
VAI_print_step "Wait on hardware"
VAI_udev_settle
VAI_print_step "Bring up the network"
VAI_get_address
VAI_print_step "Configuring installer"
VAI_configure_autoinstall
VAI_print_step "Configuring disk"
VAI_partition_disk
VAI_format_disk
VAI_print_step "Mounting the target filesystems"
VAI_mount_target
VAI_print_step "Installing XBPS keys"
VAI_install_xbps_keys
VAI_print_step "Installing the base system"
VAI_install_base_system
VAI_print_step "Granting sudo to default user"
VAI_configure_sudo
VAI_print_step "Setting hostname"
VAI_configure_hostname
VAI_print_step "Configure rc.conf"
VAI_configure_rc_conf
VAI_print_step "Preparing the chroot"
VAI_prepare_chroot
VAI_print_step "Fix ownership of /"
VAI_correct_root_permissions
VAI_print_step "Adding default user"
VAI_add_user
VAI_print_step "Configuring GRUB"
VAI_configure_grub
VAI_print_step "Configuring /etc/fstab"
VAI_configure_fstab
VAI_print_step "Configuring libc-locales"
VAI_configure_locale
VAI_print_step "Performing end-action"
VAI_end_action
}
# If we are using the autoinstaller, launch it
if getargbool 0 auto ; then
set -e
VAI_main
# Very important to release this before returning to dracut code
set +e
fi