This repository has been archived on 2024-09-01. You can view files and clone it, but cannot push or open issues or pull requests.
al-installer/src/lib/utils.sh
2019-03-16 21:21:48 -07:00

245 lines
5.7 KiB
Bash

#!/usr/bin/bash
# vim:ft=sh:fdm=marker:fmr={,}
# archlabs installer library script file
# this file is not meant to be run directly
# sourcing this file in a non bash shell is not advised
# shellcheck disable=2154,2034
chrun()
{
# run a shell command in the chroot dir $MNT
arch-chroot $MNT /bin/bash -c "$1"
}
json()
{
# get a value from http://api.ipstack.com in json format using my API key
# this includes: ip, geolocation, country name
curl -s "http://api.ipstack.com/$2" |
python3 -c "import sys, json; print(json.load(sys.stdin)['$1'])"
}
src()
{
# source a file ($1), if it fails we die with an error message
. "$1" 2>/dev/null || { printf "\nFailed to source file %s\n" "$1"; die 1; }
}
ssd()
{
# returns 0 (true) when the device passed ($1) is NOT a rotational device
local dev=$1
dev=${dev#/dev/}
if [[ $dev =~ nvme ]]; then
dev=${dev%p[0-9]*}
else
dev=${dev%[0-9]*}
fi
local i
i=$(cat /sys/block/$dev/queue/rotational 2>/dev/null)
[[ $i && $i -eq 0 ]] || return 1
}
die()
{
if (( $# >= 1 )); then
local exitcode=$1
else
local exitcode=0
fi
# reset SIGINT
trap - INT
tput cnorm
if [[ -d $MNT ]] && command cd /; then
umount_dir $MNT
if (( exitcode == 127 )); then
umount -l /run/archiso/bootmnt
systemctl -i reboot
fi
fi
rm -fv /tmp/.ai_*
exit $exitcode
}
sigint()
{
# used to trap SIGINT and cleanly exit the program
printf "\nCTRL-C caught\nCleaning up...\n"
die 1
}
print4()
{
# takes an arbitrary number of input fields and prints them out in fourths on separate lines
local str="$*"
if [[ ${#str} -gt $(( ${COLUMNS:-$(tput cols)} / 2 )) ]]; then
q=$(awk '{print int(NF / 4)}' <<< "$str")
str="$(awk '{
pkgs1=pkgs2=pkgs3=pkgs4=""
for (i=1;i<'"$q"';i++) {
if (i == 1) {
pkgs1=$i
} else {
pkgs1=pkgs1" "$i
}
}
for (i='"$q"';i<'"$((q * 2))"';i++) {
pkgs2=pkgs2" "$i
}
for (i='"$((q * 2))"';i<'"$((q * 3))"';i++) {
pkgs3=pkgs3" "$i
}
for (i='"$((q * 3))"';i<NF;i++) {
pkgs4=pkgs4" "$i
}
printf "%s\n %s\n %s\n %s", pkgs1, pkgs2, pkgs3, pkgs4
}' <<< "$str")"
fi
printf "%s\n" "$str"
}
oneshot()
{
[[ -e /tmp/.ai_$1 || ! $(type $1) ]] && return 0
$1 || return 1
touch "/tmp/.ai_$1"
return 0
}
system_devices()
{
IGNORE_DEV="$(lsblk -lno NAME,MOUNTPOINT | awk '/\/run\/archiso\/bootmnt/ {sub(/[1-9]/, ""); print $1}')"
if [[ $IGNORE_DEV ]]; then
SYS_DEVS="$(lsblk -lno NAME,SIZE,TYPE | awk '/disk/ && !'"/$IGNORE_DEV/"' {print "/dev/" $1 " " $2}')"
else
SYS_DEVS="$(lsblk -lno NAME,SIZE,TYPE | awk '/disk/ {print "/dev/" $1 " " $2}')"
fi
DEV_COUNT="$(wc -l <<< "$SYS_DEVS")"
declare -grx SYS_DEVS IGNORE_DEV DEV_COUNT
}
system_identify()
{
local efidir="/sys/firmware/efi"
if grep -q 'GenuineIntel' /proc/cpuinfo; then
declare -gx UCODE="intel-ucode"
elif grep -q 'AuthenticAMD' /proc/cpuinfo; then
declare -gx UCODE="amd-ucode"
fi
if grep -qi 'apple' /sys/class/dmi/id/sys_vendor; then
modprobe -r -q efivars
else
modprobe -q efivarfs
fi
if [[ -d $efidir ]]; then
SYS="UEFI"
if ! grep -q $efidir/efivars <<< "$(mount)"; then
mount -t efivarfs efivarfs $efidir/efivars
fi
else
SYS="BIOS"
fi
declare -g BT="$DIST Installer - $SYS (x86_64) - Version $VER"
}
system_checks()
{
if [[ $(whoami) != "root" ]]; then
infobox "$_ErrTitle" "$_NotRoot\n$_Exit"
die 1
elif ! grep -qw 'lm' /proc/cpuinfo; then
infobox "$_ErrTitle" "$_Not64Bit\n$_Exit"
die 1
fi
if grep -qw 'BCM4352' <<< "$(lspci -vnn -d 14e4:)"; then
rmmod bcma >/dev/null 2>&1
rmmod wl >/dev/null 2>&1
modprobe wl >/dev/null 2>&1
BROADCOM_WL=true
printf "\nLoading wifi kernel modules please wait...\n"
sleep 2
fi
if ! curl -s --head 'https://www.archlinux.org/mirrorlist/all/' | sed '1q' | grep -qw '200'; then
if [[ $(systemctl is-active NetworkManager) == "active" ]] && hash nmtui >/dev/null 2>&1; then
tput civis
nmtui-connect
if ! curl -s --head 'https://www.archlinux.org/mirrorlist/all/' | sed '1q' | grep -qw '200'; then
infobox "$_ErrTitle" "$_NoNetwork" 3
die 1
fi
fi
fi
return 0
}
preinstall_checks()
{
if ! [[ $(lsblk -o MOUNTPOINT) =~ $MNT ]]; then
msgbox "$_ErrTitle" "$_ErrNoMount"
SELECTED=4
return 1
elif [[ $# -eq 1 && $CONFIG_DONE != true ]]; then
msgbox "$_ErrTitle" "$_ErrNoConfig"
SELECTED=5
return 1
fi
return 0
}
echeck()
{
local last_exit_code=$?
if (( last_exit_code == 0 )); then
return 0
fi
local err
err="$(sed 's/[^[:print:]]//g; s/\[[0-9\;:]*\?m//g; s/==> //g; s/] ERROR:/]\nERROR:/g' "$ERR")"
if [[ $err != "" ]]; then
msgbox "$_ErrTitle" "\nThe command exited abnormally: $1\n\nWith the following error message:\n\n$err"
else
msgbox "$_ErrTitle" "\nThe command exited abnormally: $1\n\nWith the no error message.\n"
fi
if [[ -e $DBG && $TERM == 'linux' ]]; then
more $DBG
fi
die 1
}
debug()
{
set -x
exec 3>| $DBG
BASH_XTRACEFD=3
DEBUG=true
}
umount_dir()
{
swapoff -a
if [[ -d $1 ]]; then
umount -R $1 >/dev/null 2>&1
fi
return 0
}