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
confirmation() {
local message
message="${1}"
local message
message="${1}"
local choice
local choice
while :; do
read -p "${message}" -n 1 -r 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
while :; do
read -p "${message}" -n 1 -r 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
#
# 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
local red
local green
local blue
local input
input="${1}"
red="${2}"
green="${3}"
blue="${4}"
input="${1}"
red="${2}"
green="${3}"
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() {
echo_rgb "${1}" 135 195 255
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
#
# 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
# 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)]"
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:]')
# Convert the level to uppercase
local level
level=$(echo "${1}" | tr '[:lower:]' '[:upper:]')
local message
message="${2}"
local message
message="${2}"
case "${level}" in
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
;;
# 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
;;
# 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
;;
# 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
;;
# 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
# 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 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
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)"
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
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
printf "%s\n" "${ret_val}"
printf "%s\n" "${ret_val}"
}
list_disks() {
local disks
disks="${1}"
local disks
disks="${1}"
local count
count=0
log "info" "Available disks"
for disk in ${disks}; do
printf "%s\n" "${count}.)" "${disk}"
count=$(( count + 1 ))
done
local count
count=0
log "info" "Available disks"
for disk in ${disks}; do
printf "%s\n" "${count}.)" "${disk}"
count=$((count + 1))
done
}
get_num_of_elems() {
printf "%s" "${#count}"
printf "%s" "${#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 -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
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/"
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
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
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
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
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" "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}")"
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"
mount /dev/mapper/cryptroot /mnt
mkdir /mnt/boot
mount "${install_path}p1" /mnt/boot
local mem_amount_kb
mem_amount_kb="$(grep MemTotal /proc/meminfo | tr -s " " | cut -d " " -f2)"
mem_amount_mb=$((mem_amount_kb / 1024))
mount /dev/mapper/cryptroot /mnt
mkdir /mnt/boot
mount "${install_path}p1" /mnt/boot
local mem_amount_kb
mem_amount_kb="$(grep MemTotal /proc/meminfo | tr -s " " | cut -d " " -f2)"
mem_amount_mb=$(( mem_amount_kb / 1024 ))
# Space to swap is taken from fedora guidelines:
# https://docs.fedoraproject.org/en-US/Fedora/22/html/Installation_Guide/sect-installation-gui-manual-partitioning-recommended.html
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
# Space to swap is taken from fedora guidelines:
# https://docs.fedoraproject.org/en-US/Fedora/22/html/Installation_Guide/sect-installation-gui-manual-partitioning-recommended.html
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"
dd if=/dev/zero of=/mnt/swapfile bs=1M count="${mem_amount_kb}" status=progress
chmod 600 /mnt/swapfile
mkswap /mnt/swapfile
swapon /mnt/swapfile
log "info" "Finished creating swap file"
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
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
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__
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 -e "toor\ntoor\n" | passwd
echo "arch" > /etc/hostname
@ -325,13 +325,12 @@ __EOF__
yes | pacman -S networkmanager
systemctl enable NetworkManager
__END_CHROOT_CMDS__
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" "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")"
}
main() {
install
install
}
main "${@}"