main
ec1f561 ยท 3 months ago 21 commits
  1#!/bin/bash
  2
  3LOGFILE="arch_install.log"
  4
  5ensure_connectivity_and_source(){
  6    if ! sudo ping -c 3 archlinux.org >/dev/null 2>&1; then
  7        echo "Network connectivity check failed. Please ensure you are connected to the internet."
  8        exit 1
  9    fi 
 10    
 11    if ! command -v curl >/dev/null 2>&1; then
 12        echo "Install curl and run the script"
 13        exit 1
 14    fi
 15
 16    COMMON_SCRIPT_URL="https://scripts.ansari.wtf/base.sh"
 17    source <(curl -s $COMMON_SCRIPT_URL)
 18}
 19
 20cleanup(){
 21    echo
 22    echo -e "${YELLOW}Setup interrupted. Cleaning up...${NC}"
 23    exit 1
 24}
 25
 26
 27gather_user_info(){
 28    set -e
 29    log_info "Gathering User Configuration Details."
 30    execute_with_output "lsblk" "Listing disk partitions"
 31    prompt_input "Enter the disk to install Arch Linux (eg /dev/sda)" DISK
 32
 33    if [ -z "$DISK" ]; then
 34        log_error "No disk specified. Exiting."
 35        exit 1
 36    fi
 37
 38    prompt_input "Enter the hostname" HOSTNAME
 39    if [ -z "$HOSTNAME" ]; then
 40        log_error "No hostname specified. Exiting."
 41        exit 1
 42    fi
 43
 44    prompt_input "Enter the username" USERNAME
 45    if [ -z "$USERNAME" ]; then
 46        log_error "No username specified. Exiting."
 47        exit 1 
 48    fi
 49
 50    prompt_masked_password "Enter the user password" PASSWORD
 51    if [ -z "$PASSWORD" ]; then
 52        log_error "No password specified. Exiting."
 53        exit 1
 54    fi  
 55
 56    prompt_masked_password "Enter the root password" ROOT_PASSWORD
 57    if [ -z "$ROOT_PASSWORD" ]; then    
 58        log_error "No root password specified. Exiting."
 59        exit 1
 60    fi
 61
 62    prompt_input "Enter the timezone (e.g., Asia/Kolkata)" TIMEZONE
 63    if [ -z "$TIMEZONE" ]; then
 64        log_warning "No timezone specified. Using default 'Asia/Kolkata'."
 65        TIMEZONE="Asia/Kolkata"
 66    fi
 67
 68    clear_screen_from_given_line 8
 69    log_success "User configuration details collected successfully."
 70    log true "${CYAN}\nHostname: $HOSTNAME \nUsername: $USERNAME \nDisk: $DISK \nTimezone: $TIMEZONE ${NC}"
 71}
 72
 73partition_and_format_disk(){
 74    log_warning "About to partition the disk. Ensure you have backups of any important data on $DISK."
 75    log_warning "Will create: 1GB boot partition (EFI), remaining space for root"
 76    ask_yes_no "Do you want to continue with disk partitioning?" || {
 77        log_info "Disk partitioning aborted by user."
 78        exit 0
 79    } 
 80    clear_screen_from_given_line 14
 81    echo
 82    log_info "Partitioning disk $DISK with Btrfs filesystem."
 83    execute_step "parted --script $DISK mklabel gpt" "Creating GPT partition table on $DISK"
 84    execute_step "parted --script $DISK mkpart primary fat32 1MiB 1025MiB" "Creating EFI partition on $DISK"
 85    execute_step "parted --script $DISK set 1 esp on" "Setting ESP flag on partition 1"
 86    execute_step "parted --script $DISK mkpart primary btrfs 1025MiB 100%" "Creating Btrfs partition on $DISK"
 87    execute_with_output "lsblk $DISK" "Listing updated partitions"
 88    execute_step "mkfs.fat -F32 ${DISK}1" "Formatting EFI partition"
 89    execute_step "mkfs.btrfs -f ${DISK}2" "Formatting Btrfs partition"
 90
 91    clear_screen_from_given_line 16
 92    log_success "Disk partitioning and formatting completed successfully."
 93}
 94
 95
 96create_btrfs_subvolumes() {
 97    echo
 98    log_info "Creating BTRFS subvolumes - ${DISK}2"
 99    
100    execute_step "mount ${DISK}2 /mnt" "Mounting Btrfs partition"
101    execute_step "btrfs subvolume create /mnt/@" "Creating root subvolume"
102    execute_step "btrfs subvolume create /mnt/@home" "Creating home subvolume"
103    execute_step "btrfs subvolume create /mnt/@log" "Creating log subvolume"
104    execute_step "btrfs subvolume create /mnt/@pkg" "Creating package subvolume"
105    execute_step "btrfs subvolume create /mnt/@snapshots" "Creating snapshots subvolume"
106    execute_step "umount /mnt" "Unmounting Btrfs partition"
107    
108    clear_screen_from_given_line 19
109
110    log_success "BTRFS subvolumes created successfully."
111}
112
113mount_filesystem(){
114    echo
115    log_info "Mounting BTRFS subvolumes"
116    execute_step "mount -o noatime,ssd,compress=zstd,space_cache=v2,discard=async,subvol=@ ${DISK}2 /mnt" "Mounting root subvolume"
117    execute_step "mkdir -p /mnt/{boot,home,var/log,var/cache/pacman/pkg,.snapshots}" "Creating mount directories"
118    execute_step "mount -o noatime,ssd,compress=zstd,space_cache=v2,discard=async,subvol=@home ${DISK}2 /mnt/home" "Mounting home subvolume"
119    execute_step "mount -o noatime,ssd,compress=zstd,space_cache=v2,discard=async,subvol=@log ${DISK}2 /mnt/var/log" "Mounting log subvolume"
120    execute_step "mount -o noatime,ssd,compress=zstd,space_cache=v2,discard=async,subvol=@pkg ${DISK}2 /mnt/var/cache/pacman/pkg" "Mounting package subvolume"
121    execute_step "mount -o noatime,ssd,compress=zstd,space_cache=v2,discard=async,subvol=@snapshots ${DISK}2 /mnt/.snapshots" "Mounting snapshots subvolume"
122    execute_step "mount ${DISK}1 /mnt/boot" "Mounting EFI partition"
123    clear_screen_from_given_line 22
124    log_success "BTRFS subvolumes mounted successfully."
125}
126
127install_base_system(){
128    echo
129    log_info "Installing base system packages"
130    log_warning "About to install base system packages. This may take a while."
131    execute_step "pacstrap -K /mnt base base-devel linux linux-headers linux-firmware btrfs-progs neovim openssh networkmanager sudo git zsh" "Installing base system packages"
132    clear_screen_from_given_line 25
133    log_success "Base system packages installed successfully."
134}
135
136generate_fstab(){
137    echo
138    log_info "Generating fstab file"
139    execute_step "genfstab -U /mnt >> /mnt/etc/fstab" "Generating fstab"
140    clear_screen_from_given_line 28
141    log_success "fstab file generated successfully."
142}
143
144
145create_chroot_script() {
146    echo
147    log_info "Creating chroot setup script"
148       cat <<CHROOT_EOF > /mnt/setup_chroot.sh
149#!/bin/bash
150
151source <( curl -s $COMMON_SCRIPT_URL )
152LOGFILE="/chroot_setup_arch.log"
153
154configure_system_basics() {
155    log_info "Configuring system basics"
156    execute_step "echo '$HOSTNAME' > /etc/hostname" "Setting up hostname - $HOSTNAME"
157    execute_step "ln -sf /usr/share/zoneinfo/$TIMEZONE /etc/localtime" "Setting timezone to $TIMEZONE"
158    execute_step "hwclock --systohc" "Synchronizing hardware clock"
159    execute_step "sed -i 's/#en_US.UTF-8 UTF-8/en_US.UTF-8 UTF-8/' /etc/locale.gen" "Enabling en_US.UTF-8 locale"
160    execute_step "locale-gen" "Generating locales"
161    execute_step "echo 'LANG=en_US.UTF-8' > /etc/locale.conf" "Setting system locale"
162    log_success "System basics configured successfully."
163}
164
165configure_initramfs() {
166    log_info "Configuring initramfs for Btrfs"
167    execute_step "sed -i 's/^MODULES=()/MODULES=(btrfs)/' /etc/mkinitcpio.conf" "Configuring initramfs for Btrfs"
168    echo "KEYMAP=us" > /etc/vconsole.conf
169    execute_step "mkinitcpio -P" "Generating initramfs"
170    log_success "Initramfs configured successfully."
171}
172
173configure_initramfs_nvidia() {
174    log_info "Configuring initramfs for Btrfs NVIDIA"
175    execute_step "sed -i 's/^MODULES=(btrfs)/MODULES=(btrfs nvidia nvidia_modeset nvidia_uvm nvidia_drm)/' /etc/mkinitcpio.conf" "Configuring initramfs for Btrfs"
176    execute_step "mkinitcpio -P" "Generating initramfs"
177    log_success "Initramfs configured successfully."
178}
179
180setup_users() {
181    log_info "Setting up users"
182    execute_step "echo -e '$ROOT_PASSWORD\n$ROOT_PASSWORD' | passwd root" "Setting root password"   
183    execute_step "useradd -m -G wheel -s /bin/bash $USERNAME" "Creating user $USERNAME"
184    execute_step "echo -e '$PASSWORD\n$PASSWORD' | passwd $USERNAME" "Setting user password for $USERNAME"
185    execute_step "echo '$USERNAME ALL=(ALL) ALL' >> /etc/sudoers.d/$USERNAME" "Adding $USERNAME to sudoers"
186    log_success "Users set up successfully."
187}
188
189setup_bootloader() {
190    log_info "Setting up bootloader"
191
192    execute_step "bootctl install" "Installing bootctl"
193    execute_step "mkdir -p /boot/loader/entries" "Creating boot loader entries"
194
195    PARTUUID=\$(blkid -s PARTUUID -o value ${DISK}2)
196    cat << EOF > /boot/loader/entries/arch.conf
197title   Arch Linux
198linux   /vmlinuz-linux
199initrd  /initramfs-linux.img
200options root=PARTUUID=\$PARTUUID rootflags=subvol=@ rw nvidia_drm.modeset=1 quiet splash
201EOF
202
203    log_success "Bootloader set up successfully."
204}
205
206setup_nvidia_packages() {
207    log_info "Setting up Nvidia packages"
208    execute_step "pacman -S dkms libva-nvidia-driver nvidia-open-dkms --noconfirm" "Installing Nvidia packages (OSS version)"
209    log_success "Nvidia Packages installed successfully."
210}
211
212enable_services() {
213    log_info "Enabling essential services"
214    execute_step "systemctl enable NetworkManager" "Enabling NetworkManager"
215    execute_step "systemctl enable sshd" "Enabling SSH daemon"
216    log_success "Essential services enabled successfully."
217}
218
219log_info "==== Starting Arch Linux Chroot Setup ===="
220
221configure_system_basics
222configure_initramfs
223setup_users
224setup_bootloader
225ask_yes_no "Do you want to install Nvidia Packages?" && {
226    setup_nvidia_packages
227    configure_initramfs_nvidia
228} 
229enable_services
230
231log_success "Chroot setup completed successfully."
232CHROOT_EOF
233
234    chmod +x /mnt/setup_chroot.sh
235    log_success "Chroot setup script created successfully."
236}
237
238append_chroot_log_to_main_log() {
239    log_info "Copying chroot log to main log"
240    if [ -f /mnt/chroot_setup_arch.log ]; then
241        cat /mnt/chroot_setup_arch.log >> $LOGFILE
242        log_info "Chroot setup log copied to $LOGFILE"
243    else
244        log_warning "Chroot setup log not found."
245    fi
246}
247
248main(){
249    clear
250    ensure_connectivity_and_source
251    setup_cleanup_trap cleanup
252    show_box "Arch Installation" 
253    log_info "Starting Arch Linux installation process."
254    gather_user_info
255    partition_and_format_disk
256    create_btrfs_subvolumes
257    mount_filesystem
258    install_base_system
259    generate_fstab
260    create_chroot_script
261    arch-chroot /mnt /setup_chroot.sh
262    append_chroot_log_to_main_log
263    log_success "Arch Linux installation completed successfully."
264}
265
266
267main