Formatting

This commit is contained in:
Price Hiller 2022-01-18 12:47:43 -06:00
parent 895acd1694
commit ab01803bc5

View File

@ -1,306 +1,306 @@
#!/bin/bash #!/bin/bash
confirmation() { confirmation() {
local message local message
message="${1}" message="${1}"
local choice local choice
while :; do while :; do
read -p "${message}" -n 1 -r choice read -p "${message}" -n 1 -r choice
case "${choice}" in case "${choice}" in
y | Y) y | Y)
return 0 return 0
;; ;;
n | N) n | N)
return 1 return 1
;; ;;
*) echo -e "\nInput must be either y, Y, n, or N" ;; *) echo -e "\nInput must be either y, Y, n, or N" ;;
esac esac
done done
} }
echo_rgb() { echo_rgb() {
# Echo a colored string to the terminal based on rgb values # Echo a colored string to the terminal based on rgb values
# #
# Positional Arguments: # Positional Arguments:
# #
# message <type: string> <position: 1> <required: true> # message <type: string> <position: 1> <required: true>
# - The message to be printed to stdout # - The message to be printed to stdout
# red <type: int> <position: 2> <required: true> # red <type: int> <position: 2> <required: true>
# - The red value from 0 to 255 # - The red value from 0 to 255
# green <type: int> <position: 3> <required: true> # green <type: int> <position: 3> <required: true>
# - The green value from 0 to 255 # - The green value from 0 to 255
# blue <type: int> <position: 4> <required: true> # blue <type: int> <position: 4> <required: true>
# - The blue value from 0 to 255 # - The blue value from 0 to 255
# #
# Usage: # Usage:
# echo_rgb "Yep" 10 8 30 # echo_rgb "Yep" 10 8 30
# #
# POSIX Compliant: # POSIX Compliant:
# N/A # N/A
# #
local red local red
local green local green
local blue local blue
local input local input
input="${1}" input="${1}"
red="${2}" red="${2}"
green="${3}" green="${3}"
blue="${4}" blue="${4}"
printf "\e[0;38;2;%s;%s;%sm%s\e[m\n" "${red}" "${green}" "${blue}" "${input}" printf "\e[0;38;2;%s;%s;%sm%s\e[m\n" "${red}" "${green}" "${blue}" "${input}"
} }
important() { important() {
echo_rgb "${1}" 135 195 255 echo_rgb "${1}" 135 195 255
} }
log() { log() {
# Print a message and send it to stdout or stderr depending upon log level, also configurable with debug etc. # Print a message and send it to stdout or stderr depending upon log level, also configurable with debug etc.
# #
# Arguments: # Arguments:
# level <type: string> <position: 1> <required: true> # level <type: string> <position: 1> <required: true>
# - The log level, defined within a case check in this function # - The log level, defined within a case check in this function
# message <type: string> <position: 2> <required: true> # message <type: string> <position: 2> <required: true>
# - The info message # - The info message
# line_number <type: int> <position: 3> <required: false> # line_number <type: int> <position: 3> <required: false>
# - The line number of the calling function (${LINNO}) # - The line number of the calling function (${LINNO})
# #
# Usage: # Usage:
# log "info" "Could not find that directory" # log "info" "Could not find that directory"
# #
# POSIX Compliant: # POSIX Compliant:
# Yes # Yes
# #
# Set debug status depending if a global debug variable has been set to either 1 or 0 # Set debug status depending if a global debug variable has been set to either 1 or 0
local debug local debug
if [ ${DEBUG} ]; then if [ ${DEBUG} ]; then
debug=${DEBUG} debug=${DEBUG}
else else
debug=0 debug=0
fi fi
local FORMAT local FORMAT
FORMAT="[$(echo_rgb "$(date +%Y-%m-%dT%H:%M:%S)" 180 140 255)]" FORMAT="[$(echo_rgb "$(date +%Y-%m-%dT%H:%M:%S)" 180 140 255)]"
# Convert the level to uppercase # Convert the level to uppercase
local level local level
level=$(echo "${1}" | tr '[:lower:]' '[:upper:]') level=$(echo "${1}" | tr '[:lower:]' '[:upper:]')
local message local message
message="${2}" message="${2}"
case "${level}" in case "${level}" in
INFO) INFO)
# Output all info log levels to stdout # Output all info log levels to stdout
printf "${FORMAT}[$(echo_rgb "INFO" 0 140 255)] %s\n" "${message}" >&1 printf "${FORMAT}[$(echo_rgb "INFO" 0 140 255)] %s\n" "${message}" >&1
return 0 return 0
;; ;;
WARN | WARNING) WARN | WARNING)
# Output all warning log levels to stdout # Output all warning log levels to stdout
printf "${FORMAT}[$(echo_rgb "WARNING" 255 255 0)] %s\n" "${message}" >&1 printf "${FORMAT}[$(echo_rgb "WARNING" 255 255 0)] %s\n" "${message}" >&1
return 0 return 0
;; ;;
DEBUG) DEBUG)
# Output all debug log levels to stdout # Output all debug log levels to stdout
if [ "${DEBUG}" ]; then if [ "${DEBUG}" ]; then
printf "${FORMAT}[$(echo_rgb "DEBUG" 0 160 110)] %s\n" "${message}" >&1 printf "${FORMAT}[$(echo_rgb "DEBUG" 0 160 110)] %s\n" "${message}" >&1
fi fi
return 0 return 0
;; ;;
ERROR) ERROR)
# Output all error log levels to stderr # Output all error log levels to stderr
printf "${FORMAT}[$(echo_rgb "ERROR" 255 0 0)] %s\n" "${message}" >&2 printf "${FORMAT}[$(echo_rgb "ERROR" 255 0 0)] %s\n" "${message}" >&2
return 0 return 0
;; ;;
# Further log levels can be added by extending this switch statement with more comparisons # Further log levels can be added by extending this switch statement with more comparisons
*) # Default case, no matches *) # Default case, no matches
# Returns non-zero code as an improper log option was passed, this helps with using `set -e` # 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 printf "${FORMAT}[ERROR] %s\n" "Invalid log level passed, received level \"${level}\" with message \"${message}\"" >&2
return 1 return 1
;; ;;
esac esac
} }
get_available_disks() { get_available_disks() {
local available_disks local available_disks
available_disks=() available_disks=()
local disk_line local disk_line
while read -r; do while read -r; do
# tr is used to change multiple whitespaces into a single space for processing by cut # tr is used to change multiple whitespaces into a single space for processing by cut
disk_line="$(echo "${REPLY}" | tr -s " " | cut -d " " -f6)" disk_line="$(echo "${REPLY}" | tr -s " " | cut -d " " -f6)"
if [[ "${disk_line}" = "disk" ]]; then if [[ "${disk_line}" = "disk" ]]; then
# Add the found disk to our array # Add the found disk to our array
available_disks+=("$(echo "${REPLY}" | cut -d " " -f1)") available_disks+=("$(echo "${REPLY}" | cut -d " " -f1)")
fi fi
done <<< "$(lsblk)" done <<<"$(lsblk)"
local ret_val local ret_val
# Build the array into a string, yes this is less efficient, but easier to modify in the future if something changes # 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 for disk in "${available_disks[@]}"; do
ret_val="${ret_val} ${disk}" ret_val="${ret_val} ${disk}"
done done
printf "%s\n" "${ret_val}" printf "%s\n" "${ret_val}"
} }
list_disks() { list_disks() {
local disks local disks
disks="${1}" disks="${1}"
local count local count
count=0 count=0
log "info" "Available disks" log "info" "Available disks"
for disk in ${disks}; do for disk in ${disks}; do
printf "%s\n" "${count}.)" "${disk}" printf "%s\n" "${count}.)" "${disk}"
count=$(( count + 1 )) count=$((count + 1))
done done
} }
get_num_of_elems() { get_num_of_elems() {
printf "%s" "${#count}" printf "%s" "${#count}"
} }
install() { install() {
if ! host "google.com" > /dev/null; then 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" log "error" "Unable to reach google, network is down. This script requires network connectivity with active DNS resolution"
exit 1 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 -rp "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 fi
local DISK_BASE_PATH
DISK_BASE_PATH="/dev/"
if [[ "${disk_selected}" -gt "${disk_count}" ]]; then local available_disks
log "error" "Invalid disk number passed, received ${disk_selected}, but the maximum disk number is ${disk_count}"
elif [[ "${disk_selected}" -lt 0 ]]; then log "info" "Querying system for available disks..."
log "error" "Invalid disk number passed, received ${disk_selected} which was less than 0" available_disks="$(get_available_disks)"
else disk_count="$(get_num_of_elems "${available_disks}")"
for disk in ${available_disks}; do disk_count=$((disk_count - 1))
if [[ ${count} -eq ${disk_selected} ]]; then
install_disk="${disk}" local disk_selected
install_path="${DISK_BASE_PATH}/${install_disk}" local install_disk
valid_disk_selected=0 local install_path
local count
local valid_disk_selected
valid_disk_selected=1
while [[ "${valid_disk_selected}" != 0 ]]; do
list_disks "${available_disks}"
read -rp "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 fi
count=$(( count + 1 ))
done 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 fi
done
log "info" "Disk selected given as $(important "${install_disk}")"
if [[ ! -e "${install_path}" ]]; then if ! confirmation "Begin installation of Arch Linux to $(important "${install_disk}") (y/N)? "; then
log "error" "The given disk, $(important "${install_disk}") does not exist at $(important "${install_path}")" log "info" "Confirmation recieved negative, going back to options..."
exit 1 exit
fi fi
if ! confirmation "Begin installation of Arch Linux to $(important "${install_disk}") (y/N)? "; then log "info" "Installing Arch to disk $(important "${install_disk}")"
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
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..."
mkfs.fat -F32 "${install_path}p1"
mkfs.ext4 "/dev/mapper/cryptroot"
log "info" "Created file systems"
log "info" "Wiping partitions on disk $(important "${install_disk}")" mount /dev/mapper/cryptroot /mnt
sfdisk --delete "${install_path}" >/dev/null mkdir /mnt/boot
log "info" "Wiped partitions on $(important "${install_disk}")" mount "${install_path}p1" /mnt/boot
log "info" "Securely erasing remaining data on $(important "${install_disk}")" local mem_amount_kb
echo YES | cryptsetup open --type plain -d /dev/urandom "${install_path}" to_be_wiped > /dev/null || exit mem_amount_kb="$(grep MemTotal /proc/meminfo | tr -s " " | cut -d " " -f2)"
dd bs=1M if=/dev/zero of=/dev/mapper/to_be_wiped status=progress mem_amount_mb=$((mem_amount_kb / 1024))
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..."
mkfs.fat -F32 "${install_path}p1"
mkfs.ext4 "/dev/mapper/cryptroot"
log "info" "Created file systems"
mount /dev/mapper/cryptroot /mnt # Space to swap is taken from fedora guidelines:
mkdir /mnt/boot # https://docs.fedoraproject.org/en-US/Fedora/22/html/Installation_Guide/sect-installation-gui-manual-partitioning-recommended.html
mount "${install_path}p1" /mnt/boot if ((mem_amount_mb < 2048)); then
local mem_amount_kb mem_amount_kb=$((mem_amount_kb * 3 / 1024))
mem_amount_kb="$(grep MemTotal /proc/meminfo | tr -s " " | cut -d " " -f2)" elif ((mem_amount_mb < 8192)); then
mem_amount_mb=$(( mem_amount_kb / 1024 )) mem_amount_kb=$((mem_amount_kb * 2 / 1024))
elif ((mem_amount_mb < 65536)); then
# Space to swap is taken from fedora guidelines: mem_amount_kb=$((mem_amount_kb * 3 / 2 / 1024))
# https://docs.fedoraproject.org/en-US/Fedora/22/html/Installation_Guide/sect-installation-gui-manual-partitioning-recommended.html fi
if (( mem_amount_mb < 2048 )); then
mem_amount_kb=$(( mem_amount_kb * 3 / 1024 ))
elif (( mem_amount_mb < 8192 )); then
mem_amount_kb=$(( mem_amount_kb * 2 / 1024 ))
elif (( mem_amount_mb < 65536 )); then
mem_amount_kb=$((mem_amount_kb * 3/2 / 1024 ))
fi
log "info" "Creating swap file with memory amount: $(important "${mem_amount_kb}") mebibytes" log "info" "Creating swap file with memory amount: $(important "${mem_amount_kb}") mebibytes"
dd if=/dev/zero of=/mnt/swapfile bs=1M count="${mem_amount_kb}" status=progress dd if=/dev/zero of=/mnt/swapfile bs=1M count="${mem_amount_kb}" status=progress
chmod 600 /mnt/swapfile chmod 600 /mnt/swapfile
mkswap /mnt/swapfile mkswap /mnt/swapfile
swapon /mnt/swapfile swapon /mnt/swapfile
log "info" "Finished creating swap file" log "info" "Finished creating swap file"
local packages local packages
packages="base base-devel linux linux-headers linux-firmware neovim" packages="base base-devel linux linux-headers linux-firmware neovim"
log "info" "Doing primary installation with pacstrap" log "info" "Doing primary installation with pacstrap"
log "info" "Installing the following packages:" log "info" "Installing the following packages:"
for pkg in ${packages}; do for pkg in ${packages}; do
echo " - ${pkg}" echo " - ${pkg}"
done done
pacstrap /mnt base base-devel linux linux-headers linux-firmware neovim pacstrap /mnt base base-devel linux linux-headers linux-firmware neovim
log "info" "Finished installing packages" log "info" "Finished installing packages"
log "info" "Generating fstab" log "info" "Generating fstab"
genfstab -U /mnt >> /mnt/etc/fstab genfstab -U /mnt >>/mnt/etc/fstab
log "info" "Finished generating fstab" log "info" "Finished generating fstab"
log "info" "Switching to new installation and finishing up install" log "info" "Switching to new installation and finishing up install"
arch-chroot /mnt << __END_CHROOT_CMDS__ arch-chroot /mnt <<__END_CHROOT_CMDS__
echo -e "toor\ntoor\n" | passwd echo -e "toor\ntoor\n" | passwd
echo "arch" > /etc/hostname echo "arch" > /etc/hostname
@ -325,13 +325,12 @@ __EOF__
yes | pacman -S networkmanager yes | pacman -S networkmanager
systemctl enable NetworkManager systemctl enable NetworkManager
__END_CHROOT_CMDS__ __END_CHROOT_CMDS__
log "info" "Next steps: update localities if incorrect, add your user, update the root password" log "info" "Next steps: update localities if incorrect, add your user, update the root password"
log "info" "Notice default login is the following, username: $(important "root") password: $(important "toor")" log "info" "Notice default login is the following, username: $(important "root") password: $(important "toor")"
} }
main() { main() {
install install
} }
main "${@}" main "${@}"