This repository has been archived by the owner on Aug 19, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
base_install.sh
446 lines (374 loc) · 14.2 KB
/
base_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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
#!/bin/bash
# COLORS {{{
Bold=$(tput bold)
Reset=$(tput sgr0)
Red=$(tput setaf 1)
Yellow=$(tput setaf 3)
BRed=${Bold}${Red}
BYellow=${Bold}${Yellow}
#}}}
# PROMPTS {{{
PROMPT_2="Enter n° of options (ex: 1 2 3 or 1-3): "
PROMPT_1="Enter your option: "
#}}}
function print_line() {
printf "%$(tput cols)s\n" | tr ' ' '-'
}
function print_error() {
T_COLS=`tput cols`
echo -e "\n\n${BRed}$1${Reset}\n" | fold -sw $(( $T_COLS - 1 ))
sleep 3
return 1
}
function print_title() {
clear
print_line
echo -e "# ${Bold}$1${Reset}"
print_line
echo ""
}
function pause() {
print_line
read -e -sn 1 -p "Press enter to continue..."
}
function print_info() {
T_COLS=`tput cols`
echo -e "${Bold}$1${Reset}\n" | fold -sw $(( $T_COLS - 18)) | sed 's/^/\t/'
}
function checkbox() {
#display [X] or [ ]
[[ "$1" -eq 1 ]] && echo -e "${BBlue}[${Reset}${Bold}X${BBlue}]${Reset}" || echo -e "${BBlue}[ ${BBlue}]${Reset}";
}
function mainmenu_item() {
#if the task is done make sure we get the state
if [[ $3 != "" ]] && [[ $3 != "/" ]]; then
state="${BGreen}[${Reset}$3${BGreen}]${Reset}"
else
state="${BGreen}[${Reset}Not Set${BGreen}]${Reset}"
fi
echo -e "$(checkbox "$1") ${Bold}$2${Reset} ${state}"
}
function invalid_option() {
print_line
echo "${BRed}Invalid option, Try another one.${Reset}"
pause
}
function read_input_options() {
local line
local packages
if [[ ! $@ ]]; then
read -p "${PROMPT_2}" OPTION
else
OPTION=$@
fi
array=(${OPTION})
for line in ${array[@]/,/ }; do
if [[ ${line/-/} != ${line} ]]; then
for (( i=${line%-*}; i <= ${line#*-}; i++ )); do
packages+={$i};
done
else
packages+=($line)
fi
done
OPTIONS=(${packages[@]})
}
function contains_element() {
for e in in "${@:2}"; do [[ ${e} == ${1} ]] && break; done;
}
function unique_elements() {
RESULT_UNIQUE_ELEMENTS=($(echo $@ | tr ' ' '\n' | sort -u | tr '\n' ' '))
}
function confirm_operation() {
read -p "${BYellow}$1 [y/N]: ${Reset}" OPTION
OPTION=`echo "${OPTION}" | tr '[:upper:]' '[:lower:]'`
}
function set_password() {
while true; do
read -s -p "Password for $1: " password1
echo
read -s -p "Confirm the password: " password2
echo
if [[ ${password1} == ${password2} ]]; then
eval $2=${password1}
break
fi
echo "Please try again"
done
}
function arch_chroot() {
arch-chroot ${MOUNT_POINT} "/bin/bash" -c "${1}"
}
UEFI_BIOS_TEXT="Boot Not Detected"
INSTALL_DEVICE=
MIRRORLIST_COUNTRIES=()
RANK_MIRRORS=0
LOCALE_UTF8="en_US.UTF-8"
ZONE=
SUBZONE=
HOSTNAME="archlinux"
ROOT_PASSWORD=
USER_NAME="arch"
USER_PASSWORD="123456"
MOUNT_POINT="/mnt"
function select_mirrorlist() {
print_title "MIRRORLIST - https://wiki.dex.php/Mirrors"
print_info "This option is a guide to selecting and configuring your mirrors, and a listing of current available mirrors."
local countries_code=("AU" "AT" "BY" "BE" "BR" "BG" "CA" "CL" "CN" "CO" "CZ" "DK" "EE" "FI" "FR" "DE" "GR" "HK" "HU" "ID" "IN" "IR" "IE" "IL" "IT" "JP" "KZ" "KR" "LV" "LU" "MK" "NL" "NC" "NZ" "NO" "PL" "PT" "RO" "RU" "RS" "SG" "SK" "ZA" "ES" "LK" "SE" "CH" "TW" "TR" "UA" "GB" "US" "UZ" "VN")
local countries_name=("Australia" "Austria" "Belarus" "Belgium" "Brazil" "Bulgaria" "Canada" "Chile" "China" "Colombia" "Czech Republic" "Denmark" "Estonia" "Finland" "France" "Germany" "Greece" "Hong Kong" "Hungary" "Indonesia" "India" "Iran" "Ireland" "Israel" "Italy" "Japan" "Kazakhstan" "Korea" "Latvia" "Luxembourg" "Macedonia" "Netherlands" "New Caledonia" "New Zealand" "Norway" "Poland" "Portugal" "Romania" "Russia" "Serbia" "Singapore" "Slovakia" "South Africa" "Spain" "Sri Lanka" "Sweden" "Switzerland" "Taiwan" "Turkey" "Ukraine" "United Kingdom" "United States" "Uzbekistan" "Viet Nam")
PS3=${PROMPT_2}
echo -e "Select your country:\n"
select v in "${countries_name[@]}"; do
read_input_options $REPLY
for OPT in ${OPTIONS[@]}; do
country_code=${countries_code[$(( $OPT - 1 ))]}
if [[ ${country_code} ]]; then
MIRRORLIST_COUNTRIES=( ${country_code} ${MIRRORLIST_COUNTRIES[@]} )
fi
done
unique_elements ${MIRRORLIST_COUNTRIES[@]}
MIRRORLIST_COUNTRIES=( ${RESULT_UNIQUE_ELEMENTS[@]} )
if [[ ${#MIRRORLIST_COUNTRIES} -eq 0 ]]; then
return 1
fi
confirm_operation "DO you want to rank mirrors?"
if [[ ${OPTION} == "y" ]]; then
RANK_MIRRORS=1
else
RANK_MIRRORS=0
fi
break
done
}
function configure_mirrorlist() {
local params=""
for country in ${MIRRORLIST_COUNTRIES[@]}; do
params+="country=${country}&"
done
url="https://www.archlinux.org/mirrorlist/?${params}protocol=http&protocol=https&ip_version=4&ip_version=6"
# Get latest mirror list and save to tmpfile
tmpfile=$(mktemp --suffix=-mirrorlist)
curl -Lo ${tmpfile} ${url}
sed -i 's/^#Server/Server/g' ${tmpfile}
# Backup and replace current mirrorlist file (if new file is non-zero)
if [[ -s ${tmpfile} ]]; then
{ echo " Backing up the original mirrorlist..."
mv -f /etc/pacman.d/mirrorlist /etc/pacman.d/mirrorlist.orig; } &&
{ echo " Rotating the new list into place..."
mv -f ${tmpfile} /etc/pacman.d/mirrorlist; }
else
print_error " Unable to update, could not download list."
fi
pacman -Syyu --noconfirm
if [[ ${RANK_MIRRORS} -eq 1 ]]; then
pacman -S pacman-contrib --noconfirm
cp /etc/pacman.d/mirrorlist /etc/pacman.d/mirrorlist.tmp
print_info "Next, Ranking mirrors will take a so long time, wait..."
rankmirrors /etc/pacman.d/mirrorlist.tmp > /etc/pacman.d/mirrorlist
rm /etc/pacman.d/mirrorlist.tmp
fi
chmod +r /etc/pacman.d/mirrorlist
}
function select_device() {
local devices_list=(`lsblk -d | awk 'NR>1 { print "/dev/" $1 }'`)
PS3=${PROMPT_1}
echo -e "Select device to install Arch Linux:\n"
select device in "${devices_list[@]}"; do
if contains_element ${device} ${devices_list[@]}; then
confirm_operation "Data on ${device} will be damaged"
break
else
invalid_option
fi
done
if [[ ${OPTION} == "y" ]]; then
INSTALL_DEVICE=${device}
return 0
fi
return 1
}
function format_devices() {
# TODO
# Support LVM?
sgdisk --zap-all ${INSTALL_DEVICE}
local boot_partion="${INSTALL_DEVICE}1"
local system_partion="${INSTALL_DEVICE}2"
[[ ${UEFI_BIOS_TEXT} == "Boot Not Detected" ]] && print_error "Boot method isn't be detected!"
[[ ${UEFI_BIOS_TEXT} == "UEFI detected" ]] && printf "n\n1\n\n+512M\nef00\nw\ny\n" | gdisk ${INSTALL_DEVICE} && yes | mkfs.fat -F32 ${boot_partion}
[[ ${UEFI_BIOS_TEXT} == "BIOS detected" ]] && printf "n\n1\n\n+2M\nef02\nw\ny\n" | gdisk ${INSTALL_DEVICE} && yes | mkfs.ext2 ${boot_partion}
printf "n\n2\n\n\n8300\nw\ny\n"| gdisk ${INSTALL_DEVICE}
yes | mkfs.ext4 ${system_partion}
mount ${system_partion} /mnt
[[ ${UEFI_BIOS_TEXT} == "UEFI detected" ]] && mkdir -p /mnt/boot/efi && mount ${boot_partion} /mnt/boot/efi
}
function select_timezone() {
print_title "HARDWARE CLOCK TIME - https://wiki.archlinux.org/index.php/Internationalization"
print_info "This is set in /etc/adjtime. Set the hardware clock mode uniformly between your operating systems on the same machine. Otherwise, they will overwrite the time and cause clock shifts (which can cause time drift correction to be miscalibrated)."
local timezones=(`timedatectl list-timezones | sed 's/\/.*$//' | uniq`)
PS3=${PROMPT_1}
echo -e "Select zone:\n"
select ZONE in ${timezones[@]}; do
if contains_element ${ZONE} ${timezones[@]}; then
local _subzones=(`timedatectl list-timezones | grep ${ZONE} | sed 's/^.*\///'`)
PS3="${PROMPT_1}"
echo "Select subzone:"
select SUBZONE in "${_subzones[@]}"; do
if contains_element "$SUBZONE" "${_subzones[@]}"; then
break
else
invalid_option
fi
done
break
else
invalid_option
fi
done
}
function configure_timezone() {
print_title "TIMEZONE - https://wiki.archlinux.org/index.php/Timezone"
print_info "In an operating system the time (clock) is determined by four parts: Time value, Time standard, Time Zone, and DST (Daylight Saving Time if applicable)."
arch_chroot "ln -sf /usr/share/zoneinfo/${ZONE}/${SUBZONE} /etc/localtime"
arch_chroot "sed -i '/#NTP=/d' /etc/systemd/timesyncd.conf"
arch_chroot "sed -i 's/#Fallback//' /etc/systemd/timesyncd.conf"
arch_chroot "echo \"FallbackNTP=0.pool.ntp.org 1.pool.ntp.org 0.fr.pool.ntp.org\" >> /etc/systemd/timesyncd.conf"
arch_chroot "systemctl enable systemd-timesyncd.service"
arch_chroot "hwclock --systohc --localtime"
}
function configure_locale() {
arch_chroot "sed -i 's/#\(${LOCALE_UTF8}\)/\1/' /etc/locale.gen"
echo "LANG=${LOCALE_UTF8}" > "${MOUNT_POINT}/etc/locale.conf"
arch_chroot "locale-gen"
}
function set_hostname() {
local result
read -p "Input your Hostname[ex: ${HOSTNAME}}]: " result
if [[ ! -z ${result} ]]; then
HOSTNAME=${result}
fi
}
function configure_hostname() {
if [[ -e "${MOUNT_POINT}/etc/hostname" ]]; then
mv -f "${MOUNT_POINT}/etc/hostname" "${MOUNT_POINT}/etc/hostname.orig"
fi
if [[ -e "${MOUNT_POINT}/etc/hosts" ]];
then mv -f "${MOUNT_POINT}/etc/hosts" "${MOUNT_POINT}/etc/hosts.orig"
fi
echo "${HOSTNAME}" > "${MOUNT_POINT}/etc/hostname"
arch_chroot "echo '127.0.0.1 localhost ${HOSTNAME} ${HOSTNAME}.localdomain' >> /etc/hosts"
arch_chroot "echo '::1 localhost ${HOSTNAME} ${HOSTNAME}.localdomain' >> /etc/hosts"
}
function set_root_password() {
set_password root ROOT_PASSWORD
if [[ ! ${ROOT_PASSWORD} ]]; then
return 1
fi
}
function configure_user() {
arch_chroot "echo 'root:${ROOT_PASSWORD}' | chpasswd"
arch_chroot "useradd -m -s $(which zsh) -G wheel ${USER_NAME} && echo '${USER_NAME}:${USER_PASSWORD}' | chpasswd"
}
function set_login_user() {
local result
read -p "Input login user name[ex: ${USER_NAME}]: " result
if [[ ! -z ${result} ]]; then
USER_NAME=${result}
fi
set_password ${USER_NAME} USER_PASSWORD ${USER_PASSWORD}
}
function uefi_bios_detect() {
if [[ "$(cat /sys/class/dmi/id/sys_vendor)" == 'Apple Inc.' ]] || [[ "$(cat /sys/class/dmi/id/sys_vendor)" == 'Apple Computer, Inc.' ]]; then
modprobe -r -q efivars || true # if MAC
else
modprobe -q efivarfs # all others
fi
if [[ -d "/sys/firmware/efi/" ]]; then
## Mount efivarfs if it is not already mounted
if [[ -z $(mount | grep /sys/firmware/efi/efivars) ]]; then
mount -t efivarfs efivarfs /sys/firmware/efi/efivars
fi
UEFI_BIOS_TEXT="UEFI detected"
else
UEFI_BIOS_TEXT="BIOS detected"
fi
}
function bootloader_uefi() {
arch_chroot "pacman -S efibootmgr --noconfirm"
arch_chroot "grub-install --target=x86_64-efi --bootloader-id=GRUB --efi-directory=/boot/efi"
arch_chroot "mkdir -p /boot/efi/EFI/BOOT"
arch_chroot "cp /boot/efi/EFI/GRUB/grubx64.efi /boot/efi/EFI/BOOT/BOOTX64.EFI"
arch_chroot "echo 'bcf boot add 1 fs0:\EFI\grubx64.efi \"My GRUB bootloader\" && exit' > /boot/efi/startup.sh"
arch_chroot "grub-mkconfig -o /boot/grub/grub.cfg"
}
function bootloader_bios() {
arch_chroot "grub-install ${INSTALL_DEVICE}"
arch_chroot "grub-mkconfig -o /boot/grub/grub.cfg"
}
function bootloader_install() {
case ${UEFI_BIOS_TEXT} in
"UEFI detected") bootloader_uefi;;
"BIOS detected") bootloader_bios;;
*) print_error "Bootloader isn't detected.";;
esac
}
function system_install() {
format_devices
configure_mirrorlist
# Install system-base
yes '' | pacstrap -i /mnt base linux linux-firmware grub os-prober openssh git zsh
yes '' | genfstab -U /mnt >> /mnt/etc/fstab
configure_timezone
configure_hostname
configure_locale
configure_user
bootloader_install
arch_chroot "systemctl enable dhcpcd sshd"
}
function install() {
confirm_operation "Operation is irreversible, Are you sure?"
if [[ ${OPTION} = "y" ]]; then
system_install
print_line
confirm_operation "Do you want to reboot system?"
if [[ ${OPTION} == "y" ]]; then
reboot
fi
exit 0
else
return
fi
}
print_title "https://wiki.archlinux.org/index.php/Arch_Install_Scripts"
print_info "The Arch Install Scripts are a set of Bash scripts that simplify Arch installation."
uefi_bios_detect
pause
checklist=( 0 0 0 0 0 0 0 )
while true; do
print_title "ARCHLINUX ULTIMATE INSTALL - https://github.com/tinyRatP/archlinux_install"
echo " ${UEFI_BIOS_TEXT}"
echo ""
echo " 1) $(mainmenu_item "${checklist[1]}" "Select Mirrors" "${MIRRORLIST_COUNTRIES[*]}" )"
echo " 2) $(mainmenu_item "${checklist[2]}" "Select Device" "${INSTALL_DEVICE}" )"
echo " 3) $(mainmenu_item "${checklist[3]}" "Select Timezone" "${ZONE}/${SUBZONE}" )"
echo " 5) $(mainmenu_item "${checklist[4]}" "Set Hostname" "${HOSTNAME}" )"
echo " 6) $(mainmenu_item "${checklist[5]}" "Set Root Password" "${ROOT_PASSWORD}" )"
echo " 7) $(mainmenu_item "${checklist[6]}" "Set Login User" "${USER_NAME}/${USER_PASSWORD}" )"
echo ""
echo " i) install"
echo " q) quit"
echo ""
read_input_options
for OPT in ${OPTIONS[@]}; do
case ${OPT} in
1) select_mirrorlist && checklist[1]=1;;
2) select_device && checklist[2]=1;;
3) select_timezone && checklist[3]=1;;
4) set_hostname && checklist[5]=1;;
5) set_root_password && checklist[6]=1;;
6) set_login_user && checklist[7]=1;;
"i") install;;
"q") exit 0;;
*) invalid_option;;
esac
done
done