-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinstall.sh
executable file
·398 lines (337 loc) · 10.1 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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
#!/bin/bash
set -e
qpushd() {
pushd $@ > /dev/null
}
qpopd() {
popd $@ > /dev/null
}
quiet() {
local DUMMY
set +e
DUMMY=$($@ 2>&1 > /dev/null)
if [ $? -ne 0 ]; then
echo "$DUMMY"
set -e
return 1
fi
set -e
}
ultra_quiet() {
local DUMMY
DUMMY=$($@ 2>&1 > /dev/null)
}
### FORCE ROOT ###
[ $(whoami) != "root" ] && echo "Please run as root" && exit 1
### CD TO MY DIR ###
cd "$(dirname "$0")"
### URLs ###
FZF_DOWNLOAD="$(curl -s https://api.github.com/repos/junegunn/fzf/releases/latest | grep linux_amd64 | sed -nE 's/^\s*"browser_download_url":\s*"(.*)"\s*$/\1/p')"
PARTED_DOWNLOAD="https://archlinux.org/packages/extra/x86_64/parted/download"
POST_INSTALL_SCRIPT="https://raw.githubusercontent.com/augustogunsch/install-arch/master/post-install.sh"
KEYBOARD_MAP="https://raw.githubusercontent.com/augustogunsch/install-arch/master/keyboard-map.csv"
### COLORS ###
RED='\033[0;31m'
LGREEN='\033[1;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
BOLD=$(tput bold)
NORM=$(tput sgr0)
readonly RED
readonly LGREEN
readonly YELLOW
readonly NC
readonly BOLD
readonly NORM
### VARS ###
CUR_PHASE=1
MAX_PHASE=4
### INFO ###
AVAILABLE_PLATFORMS='Both BIOS and UEFI systems\nOnly x86_64 systems\nDistros:\nArch\nArtix (OpenRC)\n'
readonly AVAILABLE_PLATFORMS
echo "This script can only be run interactively. Make sure you are in a supported platform and have an Internet connection. Available platforms:"
echo -e "$AVAILABLE_PLATFORMS"
### SYSTEM ###
DISTRO=$(cat /etc/os-release | sed -nE 's/^ID=(.*)/\1/p')
INIT_SYS=$(basename $(readlink /bin/init))
set +e
[ -d /sys/firmware/efi ] && UEFI=1 || UEFI=0
set -e
readonly DISTRO
readonly INIT_SYS
readonly UEFI
right_chroot() {
[ "$DISTRO" = "arch" ] && arch-chroot $@ || artix-chroot $@
}
right_fstabgen() {
[ "$DISTRO" = "arch" ] && genfstab $@ || fstabgen $@
}
right_basestrap() {
[ "$DISTRO" = "arch" ] && pacstrap $@ || basestrap $@
}
print_phase() {
echo -e "${BOLD}${YELLOW}[$CUR_PHASE/$MAX_PHASE] $1 phase${NC}${NORM}"
CUR_PHASE=$((CUR_PHASE+1))
}
download_fzf() {
[ -f /usr/bin/fzf ] && return 0
echo -n "Downloading fzf (for script use only)..."
curl -sL "$FZF_DOWNLOAD" -o fzf.tar.gz
tar -xf fzf.tar.gz
mv ./fzf /usr/bin/fzf
rm fzf.tar.gz
echo "done"
}
download_parted() {
[ -f /usr/bin/parted ] && return 0
echo -n "Downloading parted (for script use only)..."
curl -sL "$PARTED_DOWNLOAD" -o parted.tar.zst
tar -xf parted.tar.zst
cp -r ./usr /
rm -r ./usr
rm parted.tar.zst
echo "done"
}
prompt() {
echo -n "$1 [Y/n] "
[ $NO_CONFIRM ] && echo "y" && return 1
read ans
case $ans in
n|N) return 0 ;;
*) return 1 ;;
esac
}
prompt_drive() {
local DRIVES="$(lsblk -pno NAME,TYPE,MODEL | awk 'BEGIN {count=1} $1 ~ /^\// {
if ($2 == "disk") {printf("%i'")"' %s \"", count, $1); for(i=3;i<NF;i++) printf("%s ", $i); printf("%s\"\n", $NF); count++ } }')"
echo "Available drives:"
printf " %-12s %s\n" "DISK" "IDENTIFIER"
echo -e "$DRIVES"
echo -n "Choose drive to install $DISTRO into (WARNING: it will be repartitioned and wiped out) (default=1): "
local drive
read drive
[ -z "$drive" ] && drive=1
DRIVE_TARGET=$(echo -e "$DRIVES" | awk '$1 ~ /^'$drive'\)$/ { print $2 }')
if [ -z "$DRIVE_TARGET" ]; then
echo "Invalid target"
exit 1
fi
set +e
prompt "Installing to $DRIVE_TARGET. Confirm?"
[ $? -eq 0 ] && exit 0
set -e
}
partition() {
print_phase "Disk partitioning"
set +e
ultra_quiet swapoff -a
set -e
[ -f /bin/parted ] || download_parted
local rootN
echo -n "Partitioning drive..."
if [ $UEFI -eq 0 ]; then
# Legacy
rootN=2
parted --script "$DRIVE_TARGET" \
mklabel msdos \
mkpart primary linux-swap 0% 4GiB \
mkpart primary ext4 4GiB 100%
echo "done"
else
# EFI
rootN=3
parted --script "$DRIVE_TARGET" \
mklabel gpt \
mkpart swap linux-swap 0% 4GiB \
mkpart boot fat32 4GiB 5Gib \
mkpart root ext4 5GiB 100% \
set 2 esp on
echo "done"
echo -n "Configuring BOOT partition..."
quiet mkfs.fat -F 32 "$DRIVE_TARGET"2
fatlabel "$DRIVE_TARGET"2 BOOT
echo "done"
fi
echo -n "Configuring SWAP partition..."
quiet mkswap -L SWAP "$DRIVE_TARGET"1
quiet swapon "$DRIVE_TARGET"1
echo "done"
echo -n "Configuring ROOT partition..."
quiet mkfs.ext4 -L ROOT "$DRIVE_TARGET"$rootN
quiet mount "$DRIVE_TARGET"$rootN /mnt
[ $UEFI -eq 1 ] && mkdir -p /mnt/boot && quiet mount "$DRIVE_TARGET"2 /mnt/boot
echo "done"
}
install_base() {
print_phase "System installation"
echo -n "Installing base system, kernel, bootloader and vi..."
quiet right_basestrap /mnt base base-devel linux linux-firmware grub vi
echo "done"
if [ "$DISTRO" = "artix" ]; then
if [ "$INIT_SYS" = "openrc-init" ]; then
echo -n "Installing openrc..."
quiet right_basestrap /mnt openrc elogind-openrc
echo "done"
else
echo
echo "Error: Unsupported init system \"$INIT_SYS\""
exit 1
fi
elif [ "$DISTRO" != "arch" ]; then
echo
echo "Error: Unsupported distro \"$DISTRO\""
exit 1
fi
echo -n "Generating fstab..."
right_fstabgen -U /mnt >> /mnt/etc/fstab
echo "done"
}
set_timezone() {
ln -sf "/usr/share/zoneinfo/$TIMEZONE" /mnt/etc/localtime
quiet right_chroot /mnt hwclock --systohc
}
set_locale() {
echo -n "Configuring locale..."
sed "s/^#$LOCALE/$LOCALE/" < /mnt/etc/locale.gen > /etc/locale.gen
quiet locale-gen
cp -f /usr/lib/locale/locale-archive /mnt/usr/lib/locale/locale-archive
cp -f /etc/locale.gen /mnt/etc/locale.gen
local short_locale
local encoding
read short_locale encoding <<< "$LOCALE"
echo "export LANG=\"$short_locale\"" > /mnt/etc/locale.conf
echo "done"
echo -n "Setting keyboard layout..."
IFS=, read -r dummy XKBD_LAYOUT XKBD_MODEL XKBD_VARIANT XKBD_OPTIONS <<< "$(grep "^$KBD_LAYOUT," -m1 keyboard-map.csv)"
# systemd and others may read from here
echo "KEYMAP=$KBD_LAYOUT" > /mnt/etc/vconsole.conf
# while openrc and others may read from here
KBD_LAYOUT_FULL_PATH=$(find /usr/share/kbd/keymaps -name "$KBD_LAYOUT.map.gz")
mkdir -p /mnt/etc/conf.d
echo "keymap=\"$KBD_LAYOUT_FULL_PATH\"" > /mnt/etc/conf.d/keymaps
# and X11 will read from here
local XKBD_CONF="/mnt/etc/X11/xorg.conf.d/00-keyboard.conf"
mkdir -p $(dirname $XKBD_CONF)
echo "Section \"InputClass\"" > $XKBD_CONF
echo " Identifier \"system-keyboard\"" >> $XKBD_CONF
echo " MatchIsKeyboard \"on\"" >> $XKBD_CONF
echo " Option \"XkbLayout\" \"$XKBD_LAYOUT\"" >> $XKBD_CONF
echo " Option \"XkbModel\" \"$XKBD_MODEL\"" >> $XKBD_CONF
[ -n "$XKBD_VARIANT" ] && echo " Option \"XkbVariant\" \"$XKBD_VARIANT\"" >> $XKBD_CONF
[ -n "$XKBD_OPTIONS" ] && echo " Option \"XkbOptions\" \"$XKBD_OPTIONS\"" >> $XKBD_CONF
echo "EndSection" >> $XKBD_CONF
[ "$INIT_SYS" = "openrc-init" ] && quiet right_chroot /mnt rc-update add keymaps boot
echo "done"
}
setup_grub() {
echo -n "Configuring boot loader..."
if [ $UEFI -eq 1 ]; then
quiet right_basestrap /mnt efibootmgr
quiet right_chroot /mnt grub-install --target=x86_64-efi --efi-directory=/boot --bootloader-id=GRUB
else
quiet right_chroot /mnt grub-install --target=i386-pc "$DRIVE_TARGET"
fi
quiet right_chroot /mnt grub-mkconfig -o /boot/grub/grub.cfg
echo "done"
}
setup_users() {
echo -n "Configuring users..."
set +e
# There might be a group with the user's name
awk -F: '$1 ~ /^'$PERSONAL_USER'$/ { exit 1 }' /etc/group
[ $? -eq 0 ] && right_chroot /mnt useradd --badnames -m "$PERSONAL_USER" || \
right_chroot /mnt useradd --badnames -m -g "$PERSONAL_USER" "$PERSONAL_USER"
set -e
echo -e "root:$ROOT_PASSWORD\n$PERSONAL_USER:$PERSONAL_PASSWORD" | chpasswd -R /mnt
echo "done"
}
setup_network() {
echo -n "Configuring hostname and network..."
echo "$MACHINE_HOSTNAME" > /mnt/etc/hostname
echo "127.0.0.1 localhost" > /mnt/etc/hosts
echo "::1 localhost" >> /mnt/etc/hosts
echo "127.0.1.1 $MACHINE_HOSTNAME.localdomain $MACHINE_HOSTNAME" >> /mnt/etc/hosts
quiet right_basestrap /mnt dhcpcd wpa_supplicant
if [ "$DISTRO" = "artix" ]; then
if [ "$INIT_SYS" = "openrc-init" ]; then
echo "hostname=\"$MACHINE_HOSTNAME\"" > /mnt/etc/conf.d/hostname
quiet right_basestrap /mnt connman-openrc
quiet right_chroot /mnt rc-update add connmand
fi
else
quiet right_chroot /mnt systemctl enable dhcpcd
fi
echo "done"
}
ask_password() {
echo -n "Type password for $1: "
stty -echo
read USER_PASSWORD
stty echo
echo
echo -n "Confirm password: "
stty -echo
local PASSWORD_CONFIRM
read PASSWORD_CONFIRM
stty echo
echo
if [ "$USER_PASSWORD" != "$PASSWORD_CONFIRM" ]; then
echo "Wrong passwords. Please try again."
ask_password $1
fi
}
prompt_all() {
download_fzf
prompt_drive
echo -n "Choose timezone:"
qpushd /usr/share/zoneinfo
TIMEZONE="$(fzf --layout=reverse --height=20)"
qpopd
echo "Choose timezone: $TIMEZONE"
echo -n "Choose locale:"
LOCALE=$(sed '/^#\s/D' < /etc/locale.gen | sed '/^#$/D' | sed 's/^#//' | fzf --layout=reverse --height=20)
echo "Choose locale: $LOCALE"
[ -f keyboard-map.csv ] || curl -sL "$KEYBOARD_MAP" -o keyboard-map.csv
echo -n "Choose keyboard layout:"
KBD_LAYOUT="$(awk -F, '{print $1}' keyboard-map.csv | fzf --layout=reverse --height=20)"
echo "Choose keyboard layout: $KBD_LAYOUT"
ask_password root
ROOT_PASSWORD="$USER_PASSWORD"
echo -n "Type your personal username: "
read PERSONAL_USER
ask_password "$PERSONAL_USER"
PERSONAL_PASSWORD="$USER_PASSWORD"
echo -n "Type the machine hostname: "
read MACHINE_HOSTNAME
}
post_install() {
curl -sL "$POST_INSTALL_SCRIPT" -o post-install.sh
mv post-install.sh /mnt/root
chmod +x /mnt/root/post-install.sh
print_phase "Post installation"
echo "From now on, the post installation script will be running"
right_chroot /mnt /root/post-install.sh -nu "$PERSONAL_USER"
}
configure() {
print_phase "System configuration"
set_timezone
set_locale
setup_grub
setup_users
setup_network
}
main() {
prompt_all
partition
install_base
configure
post_install
umount -R /mnt
echo "ALSA must be configured manually. You can use 'alsamixer' to configure and then 'alsactl store' to save the changes."
echo "Most likely you just have to unmute the Main channel."
echo "Graphics drivers must also be installed manually. See Arch Wiki: Xorg#Driver_installation."
echo -n "Ready to reboot. Press any key to continue..."
read dummy
reboot
}
main