Do an arch Linux install script in bash
- User
Do an arch Linux install script in bash
Assistant
Do an arch Linux install script in bash
Okay, here is a basic Bash script for installing Arch Linux.
WARNINGS:
/home, or complex setups.base system, linux kernel, linux-firmware, base-devel, nano, vim, sudo, networkmanager, and GRUB for UEFI. It does not install a desktop environment, display manager, or any other applications.set -e), but it might fail in unexpected ways depending on your hardware and specific situation.#!/bin/bash
# Arch Linux Installation Script - BASIC UEFI setup
# --- Configuration Variables (Modify if needed, or let script prompt) ---
TARGET_DISK="" # e.g., /dev/sda, /dev/nvme0n1 - Will be prompted
COUNTRY="United_States" # Your country for mirrorlist
ROOT_PASSWORD_SET=false # Flag to track if root password was set
NEW_USER="" # New non-root username - Will be prompted
NEW_USER_PASSWORD_SET=false # Flag to track if user password was set
HOSTNAME="myarch" # Your desired hostname
TIMEZONE="UTC" # e.g., "America/New_York" - Set during chroot
LOCALE="en_US.UTF-8" # e.g., "en_US.UTF-8" - Set during chroot
EFI_PARTITION="" # e.g., /dev/sda1 - Will be prompted after partitioning
ROOT_PARTITION="" # e.g., /dev/sda2 - Will be prompted after partitioning
SWAP_PARTITION="" # e.g., /dev/sda3 - Optional, will be prompted
# --- Script Setup ---
set -e # Exit immediately if a command exits with a non-zero status
trap 'echo "An error occurred. Exiting..."; exit 1' ERR # Catch errors
# --- Functions ---
# Function to display messages
print_step() {
echo ""
echo "--- $1 ---"
echo ""
}
# Function to get user confirmation
confirm() {
read -r -p "$1 [y/N] " response
case "$response" in
[yY][eE][sS]|[yY])
true
;;
*)
false
;;
esac
}
# Function to check if running as root
check_root() {
if [ "$EUID" -ne 0 ]; then
echo "Error: This script must be run as root."
exit 1
fi
}
# Function to check internet connection
check_internet() {
print_step "Checking Internet Connection"
if ping -c 1 archlinux.org &> /dev/null; then
echo "Internet connection successful."
else
echo "Error: Could not connect to the internet. Please check your network connection."
exit 1
fi
}
# Function to select mirrors
select_mirrors() {
print_step "Selecting Pacman Mirrors"
echo "Using '$COUNTRY' for mirror selection."
# Ensure reflector is installed in the live environment
pacman -Sy --noconfirm reflector || echo "Warning: reflector not found in live environment, skipping mirror update."
if command -v reflector &> /dev/null; then
reflector --country "$COUNTRY" --age 24 --protocol https --sort rate --save /etc/pacman.d/mirrorlist
echo "Mirrorlist updated based on country: $COUNTRY"
else
echo "Skipping automated mirror update. Using default mirrorlist."
fi
}
# Function to partition the disk (basic UEFI layout)
partition_disk() {
print_step "Partitioning Disk"
echo "Available disks:"
lsblk -dno NAME,SIZE,MODEL
echo ""
read -p "Enter the target disk (e.g., sda, nvme0n1): " DISK_NAME
TARGET_DISK="/dev/$DISK_NAME"
if [ ! -b "$TARGET_DISK" ]; then
echo "Error: Disk $TARGET_DISK not found."
exit 1
fi
echo ""
echo "WARNING: This will ERASE ALL DATA on $TARGET_DISK."
echo "It will create the following partitions (GPT, UEFI):"
echo " 1. EFI System Partition (512MiB)"
echo " 2. Root Partition (Remaining space)"
echo " 3. Swap Partition (Optional, e.g., 2GiB - prompt later)"
echo ""
if ! confirm "Are you absolutely sure you want to partition $TARGET_DISK?"; then
echo "Partitioning cancelled."
exit 1
fi
# Use sgdisk for GPT partitioning
echo "Creating GPT partition table on $TARGET_DISK..."
sgdisk -Z "$TARGET_DISK" # Zap all data
sgdisk -g "$TARGET_DISK" # Create new GPT table
echo "Creating partitions..."
# Partition 1: EFI System (512 MiB), type EF00
sgdisk -n 1::+512M -t 1:ef00 "$TARGET_DISK"
# Partition 2: Root (remaining space), type 8300 (Linux filesystem)
sgdisk -n 2:: -t 2:8300 "$TARGET_DISK"
# Probe for partitions after creation
partprobe "$TARGET_DISK" || echo "Warning: partprobe failed, you might need to reboot or manually rescan."
sleep 2 # Give kernel time to recognize partitions
# Determine partition names (handles sdaN vs nvme0n1pN)
if [[ "$DISK_NAME" =~ ^nvme ]]; then
EFI_PARTITION="${TARGET_DISK}p1"
ROOT_PARTITION="${TARGET_DISK}p2"
else
EFI_PARTITION="${TARGET_DISK}1"
ROOT_PARTITION="${TARGET_DISK}2"
fi
echo ""
echo "Disk $TARGET_DISK partitioned."
echo "EFI Partition: $EFI_PARTITION"
echo "Root Partition: $ROOT_PARTITION"
echo ""
if confirm "Do you want to create a Swap partition? (Recommended)"; then
read -p "Enter Swap size (e.g., 2G, 4G, 8G - recommended 1-2x RAM): " SWAP_SIZE
if [ -z "$SWAP_SIZE" ]; then
echo "Invalid size, skipping swap partition."
else
print_step "Creating Swap Partition"
# Partition 3: Swap, type 8200
sgdisk -n 3::+"$SWAP_SIZE" -t 3:8200 "$TARGET_DISK"
partprobe "$TARGET_DISK"
sleep 2
if [[ "$DISK_NAME" =~ ^nvme ]]; then
SWAP_PARTITION="${TARGET_DISK}p3"
else
SWAP_PARTITION="${TARGET_DISK}3"
fi
echo "Swap Partition: $SWAP_PARTITION"
fi
fi
echo "Partitions created. You may verify with 'lsblk'."
confirm "Continue with formatting and installation?" || exit 1
}
# Function to format partitions
format_partitions() {
print_step "Formatting Partitions"
echo "Formatting EFI partition ($EFI_PARTITION) as FAT32..."
mkfs.fat -F 32 "$EFI_PARTITION"
echo "Formatting Root partition ($ROOT_PARTITION) as ext4..."
mkfs.ext4 "$ROOT_PARTITION"
if [ -n "$SWAP_PARTITION" ]; then
echo "Formatting Swap partition ($SWAP_PARTITION)..."
mkswap "$SWAP_PARTITION"
swapon "$SWAP_PARTITION"
echo "Swap enabled."
fi
}
# Function to mount partitions
mount_partitions() {
print_step "Mounting Partitions"
echo "Mounting Root partition ($ROOT_PARTITION) to /mnt..."
mount "$ROOT_PARTITION" /mnt
echo "Creating /mnt/boot and mounting EFI partition ($EFI_PARTITION)..."
mkdir -p /mnt/boot
mount "$EFI_PARTITION" /mnt/boot
}
# Function to install base system
install_base() {
print_step "Installing Base System and Essential Packages"
pacstrap /mnt base base-devel linux linux-firmware nano vim sudo networkmanager grub efibootmgr
}
# Function to generate fstab
generate_fstab() {
print_step "Generating fstab"
genfstab -U /mnt >> /mnt/etc/fstab
echo "fstab generated."
}
# Function for chroot configuration
chroot_setup() {
print_step "Entering chroot to perform system configuration"
arch-chroot /mnt <<CHROOT
# --- Inside Chroot ---
# Set Timezone
echo "Setting timezone..."
timedatectl set-ntp true
ln -sf /usr/share/zoneinfo/$TIMEZONE /etc/localtime
hwclock --systohc
# Set Locale
echo "Setting locale ($LOCALE)..."
echo "$LOCALE UTF-8" >> /etc/locale.gen
locale-gen
echo "LANG=$LOCALE" > /etc/locale.conf
# Set Hostname
echo "Setting hostname ($HOSTNAME)..."
echo "$HOSTNAME" > /etc/hostname
# Configure Hosts file
echo "Configuring /etc/hosts..."
echo "127.0.0.1 localhost" > /etc/hosts
echo "::1 localhost" >> /etc/hosts
echo "127.0.1.1 $HOSTNAME.localdomain $HOSTNAME" >> /etc/hosts
# Set Root Password
echo "Setting root password (you will be prompted)..."
passwd
ROOT_PASSWORD_SET=true # This won't propagate outside chroot, just for logic flow within
# Install & Configure Bootloader (GRUB for UEFI)
echo "Installing and configuring GRUB bootloader..."
grub-install --target=x86_64-efi --efi-directory=/boot --bootloader-id=GRUB --recheck
grub-mkconfig -o /boot/grub/grub.cfg
# Create New User
echo "Creating a new user (you will be prompted for username and password)..."
read -p "Enter desired username: " NEW_USER
if [ -z "\$NEW_USER" ]; then
echo "No username entered. Skipping user creation."
else
useradd -m -G wheel \$NEW_USER
echo "Setting password for user \$NEW_USER..."
passwd \$NEW_USER
NEW_USER_PASSWORD_SET=true # Won't propagate outside
echo "User \$NEW_USER created and password set."
# Configure sudo
echo "Configuring sudo for wheel group..."
# This uses visudo non-interactively to uncomment the wheel group line
# Make sure you have nano installed (included in pacstrap above)
EDITOR=nano visudo -c # Check syntax first
sed -i 's/^# %wheel ALL=(ALL:ALL) ALL/%wheel ALL=(ALL:ALL) ALL/' /etc/sudoers
echo "Sudo configured for wheel group."
fi
# Enable essential services
echo "Enabling NetworkManager service..."
systemctl enable NetworkManager
echo "Chroot setup complete."
CHROOT
# Update flags outside chroot (manual sync)
ROOT_PASSWORD_SET=true # Assume success if chroot completed
if [ -n "$NEW_USER" ]; then # If user creation was attempted
NEW_USER_PASSWORD_SET=true # Assume success
fi
}
# Function to unmount partitions
unmount_partitions() {
print_step "Unmounting Partitions"
umount -R /mnt
if [ $? -eq 0 ]; then
echo "Successfully unmounted /mnt."
else
echo "Warning: Could not fully unmount /mnt. You may need to unmount manually."
fi
if [ -n "$SWAP_PARTITION" ]; then
swapoff "$SWAP_PARTITION"
echo "Swap disabled."
fi
}
# --- Main Installation Workflow ---
check_root
check_internet
timedatectl set-ntp true # Ensure time is correct
select_mirrors
# --- Partitioning and Formatting ---
# Note: We ask for the disk and partition *names* after the user manually partitions
# This is safer than blindly automating partitioning in a general script.
# OR uncomment the partition_disk call to automate (DANGEROUS)
# partition_disk
# Safer method: Assume user has partitioned and prompt for partitions
print_step "Manual Partitioning Required"
echo "Please partition your disk(s) now in another terminal using fdisk, cfdisk, parted, etc."
echo "You need at least:"
echo " 1. An EFI System Partition (>= 300MiB, FAT32)"
echo " 2. A Root Partition (ext4 recommended)"
echo " (Optional) A Swap Partition"
echo ""
confirm "Press Enter when you have finished partitioning and are ready to proceed." || exit 1
lsblk # Show partitions for user reference
read -p "Enter the path to the EFI System Partition (e.g., /dev/sda1): " EFI_PARTITION
read -p "Enter the path to the Root Partition (e.g., /dev/sda2): " ROOT_PARTITION
read -p "Enter the path to the Swap Partition (Optional, press Enter to skip): " SWAP_PARTITION
if [ -z "$EFI_PARTITION" ] || [ -z "$ROOT_PARTITION" ]; then
echo "Error: Both EFI and Root partitions must be specified."
exit 1
fi
if [ ! -b "$EFI_PARTITION" ] || [ ! -b "$ROOT_PARTITION" ]; then
echo "Error: One or more specified partitions do not exist."
exit 1
fi
if [ -n "$SWAP_PARTITION" ] && [ ! -b "$SWAP_PARTITION" ]; then
echo "Error: Specified Swap partition does not exist."
exit 1
fi
format_partitions
mount_partitions
install_base
generate_fstab
chroot_setup
# --- Final Steps ---
print_step "Installation Complete!"
# Check if passwords were set (basic check)
if [ "$ROOT_PASSWORD_SET" = false ]; then
echo "Warning: Root password may not have been set. Please set it manually after reboot."
fi
# Check if user was created and password set (basic check)
# This check is tricky because the variables are inside chroot, relying on the flag sync above.
# A more robust script would pass variables or check files afterwards.
if [ -z "$NEW_USER" ] || [ "$NEW_USER_PASSWORD_SET" = false ]; then
echo "Warning: New user or user password may not have been set."
echo "You can create a new user and set passwords after reboot using 'useradd' and 'passwd'."
echo "Remember to add the user to the 'wheel' group and configure sudo if needed."
fi
echo "You can now reboot into your new Arch Linux system."
echo "Before rebooting, you may want to unmount partitions: 'umount -R /mnt'"
# Optional: Offer to unmount before exiting
if confirm "Unmount partitions before exiting?"; then
unmount_partitions
fi
echo "Script finished. Type 'reboot' to restart your system."
How to Use:
iwctl).install.sh):
curl -o install.sh https://pastebin.com/raw/ABCDEFG # Replace ABCDEFG with a link to the script
# OR
nano install.sh # Paste the script content here, save and exit
chmod +x install.sh./install.shsda, nvme0n1).fdisk, cfdisk, or parted in a separate terminal. Create your EFI, Root, and optional Swap partitions./dev/sda1, /dev/sda2).reboot to restart into your newly installed Arch Linux system.This script provides a basic framework. For a production system or more complex setup, you would need a much more sophisticated script or perform the installation manually, referring to the excellent Arch Linux Installation Guide on the Arch Wiki.