Bash_Scripts/Misc/Arch-LuksCrpy-Install.bash
2021-11-25 05:22:08 -06:00

351 lines
9.6 KiB
Bash
Executable File

#!/bin/bash
confirmation() {
local message
message="${1}"
local choice
while :; do
read -p "${message}" -n 1 -r choice
echo $choice
case "${choice}" in
y | Y)
return 0
;;
n | N)
return 1
;;
*) echo -e "\nInput must be either y, Y, n, or N" ;;
esac
done
}
echo_rgb() {
# Echo a colored string to the terminal based on rgb values
#
# Positional Arguments:
#
# message <type: string> <position: 1> <required: true>
# - The message to be printed to stdout
# red <type: int> <position: 2> <required: true>
# - The red value from 0 to 255
# green <type: int> <position: 3> <required: true>
# - The green value from 0 to 255
# blue <type: int> <position: 4> <required: true>
# - The blue value from 0 to 255
#
# Usage:
# echo_rgb "Yep" 10 8 30
#
# POSIX Compliant:
# N/A
#
local red
local green
local blue
local input
input="${1}"
red="${2}"
green="${3}"
blue="${4}"
printf "\e[0;38;2;%s;%s;%sm%s\e[m\n" "${red}" "${green}" "${blue}" "${input}"
}
important() {
echo_rgb "${1}" 135 195 255
}
log() {
# Print a message and send it to stdout or stderr depending upon log level, also configurable with debug etc.
#
# Arguments:
# level <type: string> <position: 1> <required: true>
# - The log level, defined within a case check in this function
# message <type: string> <position: 2> <required: true>
# - The info message
# line_number <type: int> <position: 3> <required: false>
# - The line number of the calling function (${LINNO})
#
# Usage:
# log "info" "Could not find that directory"
#
# POSIX Compliant:
# Yes
#
# Set debug status depending if a global debug variable has been set to either 1 or 0
local debug
if [ ${DEBUG} ]; then
debug=${DEBUG}
else
debug=0
fi
local FORMAT
FORMAT="[$(echo_rgb "$(date +%Y-%m-%dT%H:%M:%S)" 180 140 255)]"
# Convert the level to uppercase
local level
level=$(echo "${1}" | tr '[:lower:]' '[:upper:]')
local message
message="${2}"
case "${level}" in
INFO)
# Output all info log levels to stdout
printf "${FORMAT}[$(echo_rgb "INFO" 0 140 255)] %s\n" "${message}" >&1
return 0
;;
WARN | WARNING)
# Output all warning log levels to stdout
printf "${FORMAT}[$(echo_rgb "WARNING" 255 255 0)] %s\n" "${message}" >&1
return 0
;;
DEBUG)
# Output all debug log levels to stdout
if [ "${DEBUG}" ]; then
printf "${FORMAT}[$(echo_rgb "DEBUG" 0 160 110)] %s\n" "${message}" >&1
fi
return 0
;;
ERROR)
# Output all error log levels to stderr
printf "${FORMAT}[$(echo_rgb "ERROR" 255 0 0)] %s\n" "${message}" >&2
return 0
;;
# Further log levels can be added by extending this switch statement with more comparisons
*) # Default case, no matches
# Returns non-zero code as an improper log option was passed, this helps with using `set -e`
printf "${FORMAT}[ERROR] %s\n" "Invalid log level passed, received level \"${level}\" with message \"${message}\"" >&2
return 1
;;
esac
}
get_available_disks() {
local available_disks
available_disks=()
local disk_line
while read -r; do
# tr is used to change multiple whitespaces into a single space for processing by cut
disk_line="$(echo "${REPLY}" | tr -s " " | cut -d " " -f6)"
if [[ "${disk_line}" = "disk" ]]; then
# Add the found disk to our array
available_disks+=("$(echo "${REPLY}" | cut -d " " -f1)")
fi
done <<< "$(lsblk)"
local ret_val
# Build the array into a string, yes this is less efficient, but easier to modify in the future if something changes
for disk in "${available_disks[@]}"; do
ret_val="${ret_val} ${disk}"
done
echo "${ret_val}"
}
list_disks() {
local disks
disks="${1}"
local count
count=0
log "info" "Available disks"
for disk in ${disks}; do
echo " "${count}\.\) "${disk}"
count=$(( count + 1 ))
done
}
get_num_of_elems() {
local count
count=0
for elem in ${1}; do
count=$(( count + 1 ))
done
echo "${count}"
}
install() {
if ! host "google.com" > /dev/null; then
log "error" "Unable to reach google, network is down. This script requires network connectivity with active DNS resolution"
exit 1
fi
local DISK_BASE_PATH
DISK_BASE_PATH="/dev/"
local available_disks
log "info" "Querying system for available disks..."
available_disks="$(get_available_disks)"
disk_count="$(get_num_of_elems "${available_disks}")"
disk_count=$(( disk_count - 1))
local disk_selected
local install_disk
local install_path
local count
local valid_disk_selected
valid_disk_selected=1
while [[ "${valid_disk_selected}" != 0 ]]; do
list_disks "${available_disks}"
read -p "Select a disk number to install arch linux to: " disk_selected
if [ ${disk_selected} -eq ${disk_selected} 2> /dev/null ]; then
:
else
log "error" "A number was not passed"
continue
fi
if [[ "${disk_selected}" -gt "${disk_count}" ]]; then
log "error" "Invalid disk number passed, received ${disk_selected}, but the maximum disk number is ${disk_count}"
elif [[ "${disk_selected}" -lt 0 ]]; then
log "error" "Invalid disk number passed, received ${disk_selected} which was less than 0"
else
for disk in ${available_disks}; do
if [[ ${count} -eq ${disk_selected} ]]; then
install_disk="${disk}"
install_path="${DISK_BASE_PATH}/${install_disk}"
valid_disk_selected=0
fi
count=$(( count + 1 ))
done
fi
done
log "info" "Disk selected given as $(important "${install_disk}")"
if [[ ! -e "${install_path}" ]]; then
log "error" "The given disk, $(important "${install_disk}") does not exist at $(important "${install_path}")"
exit 1
fi
if ! confirmation "Begin installation of Arch Linux to $(important "${install_disk}") (y/N)? "; then
log "info" "Confirmation recieved negative, going back to options..."
exit
fi
log "info" "Installing Arch to disk $(important "${install_disk}")"
log "info" "Wiping partitions on disk $(important "${install_disk}")"
sfdisk --delete "${install_path}" >/dev/null
log "info" "Wiped partitions on $(important "${install_disk}")"
log "info" "Securely erasing remaining data on $(important "${install_disk}")"
# echo YES | cryptsetup open --type plain -d /dev/urandom "${install_path}" to_be_wiped > /dev/null || exit
# dd bs=1M if=/dev/zero of=/dev/mapper/to_be_wiped status=progress 2> /dev/null || exit
# cryptsetup close to_be_wiped || exit
log "info" "Writing new partitions to $(important "${install_disk}")"
(echo n
echo
echo
echo +512M
echo ef00
echo n
echo
echo
echo
echo 8300
echo w
echo Y) | gdisk "${install_path}" > /dev/null
log "info" "Wrote partitions"
log "info" "Encrypting $(important "${install_disk}"), if you are prompted type $(important "YES")"
cryptsetup -y -v luksFormat "${install_path}p2" || exit
log "info" "Requesting opening of $(important "${install_disk}p2")"
cryptsetup open "${install_path}p2" cryptroot || exit
log "info" "Creating file systems..."
mkdfs.fat -F32 "${install_path}p1"
mkfs.ext4 "/dev/mapper/cryptroot"
log "info" "Created file systems"
mount /dev/mapper/cryptroot /mnt
mkdir /mnt/boot
mount "${install_path}p1" /mnt/boot
local mem_amount
mem_amount="$(grep MemTotal /proc/meminfo | tr -s " " | cut -d " " -f2)"
mem_amount=$(( mem_amount * 3/2 / 1000 ))
log "info" "Creating swap file with memory amount: $(important "${mem_amount}") mebibytes"
dd if=/dev/zero of=/mnt/swapfile bs=1M count="${mem_amount}" status=progress
chmod 600 /mnt/swapfile
mkswap /mnt/swapfile
swapon /mnt/swapfile
log "info" "Finished creating swap file"
local packages
packages="base base-devel linux linux-headers linux-firmware neovim"
log "info" "Doing primary installation with pacstrap"
log "info" "Installing the following packages:"
for pkg in ${packages}; do
echo " - ${pkg}"
done
pacstrap /mnt base base-devel linux linux-headers linux-firmware neovim
log "info" "Finished installing packages"
log "info" "Generating fstab"
genfstab -U /mnt >> /mnt/etc/fstab
log "info" "Finished generating fstab"
log "info" "Switching to new installation and finishing up install"
arch-chroot /mnt << __END_CHROOT_CMDS__
echo "arch" > /etc/hostname
cat << __EOF__ > "/etc/hosts"
127.0.0.1 localhost
::1 localhost
127.0.1.1 arch.localdomain arch
__EOF__
echo "en_US.UTF-8 UTF-8" >> /etc/locale.gen
locale-gen
echo "LANG=en_US.UTF-8" > /etc/locale.conf
echo "toor" | passwd root --stdin
local ucode
while read -r; do
if [[ "${REPLY}" = "*VendorID*" ]]; then
if [[ "${REPLY}" = "*AMD*" ]]; then
ucode="amd-ucode"
else
ucode="intel-ucode"
fi
fi
done <<< "$(lscpu)"
yes | pacman -S grub efibootmgr "${ucode}"
local disk_uuid
disk_uuid="$(blkid -s UUID -o value "${install_path}p2")"
echo disk uuid: $disk_uuid
while read -r; do
if [[ "${REPLY}" = "*GRUB_CMD_LINE*" ]]; then
echo "GRUB_CMDLINE_LINUX=\"cryptdevice=UUID=${disk_uuid}:cryptroot\""
else
echo "${REPLY}"
fi
done <<< "$(cat /etc/default/grub)"
grub-install --target=x86_64-efi --efi-directory=/boot --bootloader-id=GRUB
grub-mkconfig -o /boot/grub/grub.cfg
yes | pacman -S networkmanager
systemctl enable NetworkManager
__END_CHROOT_CMDS__
log "info" "Finished, root password set to $(important "toor")"
log "info" "Next steps: update localities if incorrect, add your user, update the root password"
}
main() {
install
}
main "$@"