mirror of
https://github.com/ChrisTitusTech/linutil.git
synced 2024-12-27 05:49:45 +00:00
191 lines
6.0 KiB
Bash
191 lines
6.0 KiB
Bash
|
#!/bin/sh -e
|
||
|
|
||
|
. ../common-script.sh
|
||
|
|
||
|
# Function to display usage instructions
|
||
|
usage() {
|
||
|
printf "%b\n" "${RED} Usage: $0 ${RC}"
|
||
|
echo "No arguments needed. The script will prompt for ISO path and USB device."
|
||
|
exit 1
|
||
|
}
|
||
|
|
||
|
# Function to display all available block devices
|
||
|
list_devices() {
|
||
|
printf "%b\n" "${YELLOW} Available devices and partitions: ${RC}"
|
||
|
echo ""
|
||
|
$ESCALATION_TOOL lsblk -o NAME,FSTYPE,SIZE,MOUNTPOINT,LABEL
|
||
|
echo ""
|
||
|
}
|
||
|
|
||
|
# Function to fetch the latest Arch Linux ISO
|
||
|
fetch_arch_latest_iso() {
|
||
|
ARCH_BASE_URL="https://archive.archlinux.org/iso/"
|
||
|
ARCH_LATEST=$(curl -s "$ARCH_BASE_URL" | grep -oP '(?<=href=")[0-9]{4}\.[0-9]{2}\.[0-9]{2}(?=/)' | sort -V | tail -1)
|
||
|
ARCH_URL="${ARCH_BASE_URL}${ARCH_LATEST}/archlinux-${ARCH_LATEST}-x86_64.iso"
|
||
|
printf "%b\n" "${GREEN} Selected Arch Linux (latest) ISO URL: ${RC} $ARCH_URL"
|
||
|
}
|
||
|
|
||
|
# Function to fetch older Arch Linux ISOs and display in a table format
|
||
|
fetch_arch_older_isos() {
|
||
|
ARCH_BASE_URL="https://archive.archlinux.org/iso/"
|
||
|
ARCH_VERSIONS=$(curl -s "$ARCH_BASE_URL" | grep -oP '(?<=href=")[0-9]{4}\.[0-9]{2}\.[0-9]{2}(?=/)' | sort -V)
|
||
|
|
||
|
# Filter versions to include only those from 2017-04-01 and later
|
||
|
MIN_DATE="2017.04.01"
|
||
|
ARCH_VERSIONS=$(echo "$ARCH_VERSIONS" | awk -v min_date="$MIN_DATE" '$0 >= min_date')
|
||
|
|
||
|
if [ -z "$ARCH_VERSIONS" ]; then
|
||
|
printf "%b\n" "${RED}No Arch Linux versions found from ${MIN_DATE} onwards.${RC}"
|
||
|
exit 1
|
||
|
fi
|
||
|
|
||
|
printf "%b\n" "${YELLOW}Available Arch Linux versions from ${MIN_DATE} onwards:${RC}"
|
||
|
|
||
|
COUNTER=1
|
||
|
ROW_ITEMS=6 # Number of versions to show per row
|
||
|
for VERSION in $ARCH_VERSIONS; do
|
||
|
printf "%-5s${YELLOW}%-15s ${RC}" "$COUNTER)" "$VERSION"
|
||
|
|
||
|
if [ $(( COUNTER % ROW_ITEMS )) -eq 0 ]; then
|
||
|
echo "" # New line after every 6 versions
|
||
|
fi
|
||
|
|
||
|
COUNTER=$((COUNTER + 1))
|
||
|
done
|
||
|
echo "" # New line after the last row
|
||
|
|
||
|
read -p "Select an Arch Linux version (1-$((COUNTER - 1))): " ARCH_OPTION
|
||
|
ARCH_DIR=$(echo "$ARCH_VERSIONS" | sed -n "${ARCH_OPTION}p")
|
||
|
ARCH_URL="${ARCH_BASE_URL}${ARCH_DIR}/archlinux-${ARCH_DIR}-x86_64.iso"
|
||
|
printf "%b\n" "${GREEN}Selected Arch Linux (older) ISO URL: $ARCH_URL${RC}"
|
||
|
}
|
||
|
|
||
|
|
||
|
# Function to fetch the latest Debian Linux ISO
|
||
|
fetch_debian_latest_iso() {
|
||
|
DEBIAN_URL=$(curl -s https://www.debian.org/distrib/netinst | grep -oP '(?<=href=")[^"]+debian-[0-9.]+-amd64-netinst.iso(?=")' | head -1)
|
||
|
printf "%b\n" "${GREEN} Selected Debian Linux (latest) ISO URL: ${RC} $DEBIAN_URL"
|
||
|
}
|
||
|
|
||
|
# Function to ask whether to use local or online ISO
|
||
|
choose_iso_source() {
|
||
|
printf "%b\n" "${YELLOW} Do you want to use a local ISO or download online? ${RC}"
|
||
|
echo "1) Download online"
|
||
|
echo "2) Use local ISO"
|
||
|
echo ""
|
||
|
read -p "Select option (1-2): " ISO_SOURCE_OPTION
|
||
|
|
||
|
case $ISO_SOURCE_OPTION in
|
||
|
1)
|
||
|
fetch_iso_urls # Call the function to fetch online ISO URLs
|
||
|
;;
|
||
|
2)
|
||
|
read -p "Enter the path to the already downloaded ISO file: " ISO_PATH
|
||
|
if [ ! -f "$ISO_PATH" ]; then
|
||
|
printf "%b\n" "${RED} ISO file not found: $ISO_PATH ${RC}"
|
||
|
exit 1
|
||
|
fi
|
||
|
;;
|
||
|
*)
|
||
|
printf "%b\n" "${RED}Invalid option selected. ${RC}"
|
||
|
exit 1
|
||
|
;;
|
||
|
esac
|
||
|
}
|
||
|
|
||
|
# Function to fetch ISO URLs
|
||
|
fetch_iso_urls() {
|
||
|
clear
|
||
|
printf "%b\n" "${YELLOW}Available ISOs for download:${RC}"
|
||
|
echo "1) Arch Linux (latest)"
|
||
|
echo "2) Arch Linux (older versions)"
|
||
|
echo "3) Debian Linux (latest)"
|
||
|
echo ""
|
||
|
read -p "Select the ISO you want to download (1-3): " ISO_OPTION
|
||
|
|
||
|
case $ISO_OPTION in
|
||
|
1)
|
||
|
fetch_arch_latest_iso
|
||
|
ISO_URL=$ARCH_URL
|
||
|
;;
|
||
|
2)
|
||
|
fetch_arch_older_isos
|
||
|
ISO_URL=$ARCH_URL
|
||
|
;;
|
||
|
3)
|
||
|
fetch_debian_latest_iso
|
||
|
ISO_URL=$DEBIAN_URL
|
||
|
;;
|
||
|
*)
|
||
|
printf "%b\n" "${RED}Invalid option selected.${RC}"
|
||
|
exit 1
|
||
|
;;
|
||
|
esac
|
||
|
|
||
|
ISO_PATH=$(basename "$ISO_URL")
|
||
|
printf "%b\n" "${YELLOW}Downloading ISO...${RC}"
|
||
|
curl -L -o "$ISO_PATH" "$ISO_URL"
|
||
|
if [ $? -ne 0 ]; then
|
||
|
printf "%b\n" "${RED}Failed to download the ISO file.${RC}"
|
||
|
exit 1
|
||
|
fi
|
||
|
}
|
||
|
|
||
|
write_iso(){
|
||
|
clear
|
||
|
|
||
|
# Ask whether to use a local or online ISO
|
||
|
choose_iso_source
|
||
|
|
||
|
clear
|
||
|
# Display all available devices
|
||
|
list_devices
|
||
|
|
||
|
# Prompt user for USB device
|
||
|
read -p "Enter the USB device (e.g., /dev/sdX): " USB_DEVICE
|
||
|
|
||
|
# Verify that the USB device exists
|
||
|
if [ ! -b "$USB_DEVICE" ]; then
|
||
|
printf "%b\n" "${RED}USB device not found: $USB_DEVICE${RC}"
|
||
|
exit 1
|
||
|
fi
|
||
|
|
||
|
# Confirm the device selection with the user
|
||
|
printf "%b\n" "${RED}WARNING: This will erase all data on ${USB_DEVICE}. Are you sure you want to continue? (yes/no)${RC}"
|
||
|
read -r CONFIRMATION
|
||
|
|
||
|
if [ "$CONFIRMATION" != "yes" ]; then
|
||
|
printf "%b\n" "${YELLOW}Operation cancelled.${RC}"
|
||
|
exit 1
|
||
|
fi
|
||
|
|
||
|
# Display progress and create the bootable USB drive
|
||
|
printf "%b\n" "${YELLOW}Creating bootable USB drive...${RC}"
|
||
|
if ! $ESCALATION_TOOL dd if="$ISO_PATH" of="$USB_DEVICE" bs=4M status=progress oflag=sync; then
|
||
|
printf "%b\n" "${RED}Failed to create bootable USB drive${RC}"
|
||
|
exit 1
|
||
|
fi
|
||
|
|
||
|
# Sync to ensure all data is written
|
||
|
if ! $ESCALATION_TOOL sync; then
|
||
|
printf "%b\n" "${RED}Failed to sync data${RC}"
|
||
|
exit 1
|
||
|
fi
|
||
|
|
||
|
printf "%b\n" "${GREEN}Bootable USB drive created successfully!${RC}"
|
||
|
|
||
|
# Eject the USB device
|
||
|
printf "%b\n" "${YELLOW}Ejecting ${USB_DEVICE}...${RC}"
|
||
|
if ! $ESCALATION_TOOL umount "${USB_DEVICE}"* 2>/dev/null; then
|
||
|
printf "%b\n" "${RED}Failed to unmount ${USB_DEVICE}${RC}"
|
||
|
fi
|
||
|
if ! $ESCALATION_TOOL eject "$USB_DEVICE"; then
|
||
|
printf "%b\n" "${RED}Failed to eject ${USB_DEVICE}${RC}"
|
||
|
fi
|
||
|
|
||
|
printf "%b\n" "${GREEN}You can safely remove your USB drive. Reinsert the drive to be detected.${RC}"
|
||
|
}
|
||
|
|
||
|
checkEnv
|
||
|
checkEscalationTool
|
||
|
write_iso
|