Bash_Scripts/Misc/Arch-LuksCrpy-Install.bash

334 lines
9.4 KiB
Bash
Raw Normal View History

2021-11-25 01:25:19 -06:00
#!/bin/bash
2021-11-25 03:58:27 -06:00
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
}
2021-11-25 01:25:19 -06:00
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)"
2021-11-25 01:46:21 -06:00
local ret_val
2021-11-25 01:25:19 -06:00
# 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
2021-11-25 01:46:21 -06:00
echo "${ret_val}"
2021-11-25 01:36:39 -06:00
}
2021-11-25 03:58:27 -06:00
list_disks() {
local disks
disks="${1}"
2021-11-25 04:03:30 -06:00
2021-11-25 03:58:27 -06:00
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}"
}
2021-11-25 01:25:19 -06:00
install() {
2021-11-25 03:58:27 -06:00
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/"
2021-11-25 01:25:19 -06:00
local available_disks
log "info" "Querying system for available disks..."
available_disks="$(get_available_disks)"
2021-11-25 03:58:27 -06:00
disk_count="$(get_num_of_elems "${available_disks}")"
disk_count=$(( disk_count - 1))
2021-11-25 04:03:30 -06:00
2021-11-25 03:58:27 -06:00
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
2021-11-25 04:03:30 -06:00
2021-12-19 15:25:16 -06:00
if ${disk_selected} -eq ${disk_selected} 2> /dev/null; then
2021-11-25 03:58:27 -06:00
:
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}")"
2021-11-25 04:03:30 -06:00
2021-11-25 03:58:27 -06:00
if [[ ! -e "${install_path}" ]]; then
log "error" "The given disk, $(important "${install_disk}") does not exist at $(important "${install_path}")"
exit 1
fi
2021-11-25 04:03:30 -06:00
2021-11-25 03:58:27 -06:00
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
2021-11-25 01:29:43 -06:00
2021-11-25 03:58:27 -06:00
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}")"
2021-11-25 16:47:30 -06:00
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
2021-11-25 16:47:30 -06:00
cryptsetup close to_be_wiped || exit
2021-11-25 03:58:27 -06:00
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
2021-11-25 03:58:27 -06:00
log "info" "Wrote partitions"
2021-11-25 04:29:24 -06:00
log "info" "Encrypting $(important "${install_disk}"), if you are prompted type $(important "YES")"
2021-11-25 04:55:22 -06:00
cryptsetup -y -v luksFormat "${install_path}p2" || exit
2021-11-25 05:12:04 -06:00
log "info" "Requesting opening of $(important "${install_disk}p2")"
cryptsetup open "${install_path}p2" cryptroot || exit
2021-11-25 03:58:27 -06:00
log "info" "Creating file systems..."
2021-11-25 16:45:48 -06:00
mkfs.fat -F32 "${install_path}p1"
2021-11-25 03:58:27 -06:00
mkfs.ext4 "/dev/mapper/cryptroot"
log "info" "Created file systems"
2021-11-25 04:03:30 -06:00
2021-11-25 03:58:27 -06:00
mount /dev/mapper/cryptroot /mnt
mkdir /mnt/boot
2021-11-25 04:55:22 -06:00
mount "${install_path}p1" /mnt/boot
2021-11-25 03:58:27 -06:00
local mem_amount
mem_amount="$(grep MemTotal /proc/meminfo | tr -s " " | cut -d " " -f2)"
2021-11-25 04:27:39 -06:00
mem_amount=$(( mem_amount * 3/2 / 1000 ))
2021-11-25 03:58:27 -06:00
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"
2021-11-25 04:03:30 -06:00
2021-11-25 03:58:27 -06:00
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}"
2021-11-25 01:29:43 -06:00
done
2021-11-25 01:25:19 -06:00
2021-11-25 17:38:47 -06:00
pacstrap /mnt base base-devel linux linux-headers linux-firmware neovim
2021-11-25 03:58:27 -06:00
log "info" "Finished installing packages"
log "info" "Generating fstab"
genfstab -U /mnt >> /mnt/etc/fstab
log "info" "Finished generating fstab"
2021-11-25 05:22:08 -06:00
log "info" "Switching to new installation and finishing up install"
arch-chroot /mnt << __END_CHROOT_CMDS__
2021-11-26 00:17:36 -06:00
echo -e "toor\ntoor\n" | passwd
2021-11-25 18:30:36 -06:00
2021-11-25 03:58:27 -06:00
echo "arch" > /etc/hostname
cat << __EOF__ > "/etc/hosts"
127.0.0.1 localhost
::1 localhost
127.0.1.1 arch.localdomain arch
__EOF__
2021-11-26 01:37:21 -06:00
sed -i 's/#en_US.UTF-8 UTF-8/en_US.UTF-8 UTF-8/' /etc/locale.gen
2021-11-25 18:30:36 -06:00
locale-gen
echo 'LANG=en_US.UTF-8' > /etc/locale.conf
2021-11-25 03:58:27 -06:00
2021-11-26 01:37:21 -06:00
sed -i 's/HOOKS=.*/HOOKS=(base udev autodetect keyboard modconf block encrypt filesystems fsck)/' /etc/mkinitcpio.conf
2021-11-25 17:56:05 -06:00
mkinitcpio -P
2021-11-25 18:30:36 -06:00
yes | pacman -S grub efibootmgr intel-ucode amd-ucode
2021-11-26 01:37:21 -06:00
sed -i "s/GRUB_CMDLINE_LINUX=.*/GRUB_CMDLINE_LINUX=\"cryptdevice=UUID=$(blkid -s UUID -o value "${install_path}p2"):cryptroot\"/" /etc/default/grub
2021-11-25 03:58:27 -06:00
grub-install --target=x86_64-efi --efi-directory=/boot --bootloader-id=GRUB
grub-mkconfig -o /boot/grub/grub.cfg
2021-11-25 04:03:30 -06:00
2021-11-25 04:31:59 -06:00
yes | pacman -S networkmanager
2021-11-25 03:58:27 -06:00
systemctl enable NetworkManager
__END_CHROOT_CMDS__
2021-11-25 05:22:08 -06:00
log "info" "Next steps: update localities if incorrect, add your user, update the root password"
2021-12-19 15:25:16 -06:00
log "info" "Notice default login is the following, username: $(important "root") password: $(important "toor")
2021-11-25 01:25:19 -06:00
}
main() {
install
}
main "$@"