function download_latest_arch_iso() { latest_iso=$( curl -s "$img_download_page" | grep -e '' | head -1 | sed -r -e 's/^.*.*/\1/' ) if [ ! -f "$latest_iso" ]; then echo -e "${GREEN}Downloading the latest Arch Linux ISO image${NORMAL}" rm -f archlinux*.iso curl -o "${latest_iso}" "${img_download_page}/${latest_iso}" ln -sf "$latest_iso" "$isofile" else echo -e "${GREEN}Latest Arch Linux image already downloaded${NORMAL}" fi } function create_disk_image() { # Create a backing COW image # qemu-img create -o backing_file="$imgfile",backing_fmt=raw -f qcow2 img1.cow if [ ! -f "$imgfile" ]; then echo -e "${GREEN}Creating base disk image${NORMAL}" qemu-img create -f raw "$imgfile" $disk_size else echo -e "${RED}The base disk image file $imgfile already exists.${NORMAL}" echo -e "${RED}Remove it and re-run this script if you intend to run a new fresh installation.${NORMAL}" fi } function _get_ssh_key_name() { keyfiles=$(cat < "$logfile" expect <> /mnt/etc/fstab\r" expect \$prompt # chroot to the newly installed system send -- "arch-chroot /mnt\r" expect \$chroot_prompt # Set the timezone and sync the clock send -- "ln -sf /usr/share/zoneinfo/$timezone /etc/localtime\r" expect \$chroot_prompt send -- "hwclock --systohc\r" expect \$chroot_prompt # Configure localization send -- "echo $locale UTF-8 >> /etc/locale.gen\r" expect \$chroot_prompt send -- "locale-gen\r" expect \$chroot_prompt send -- "echo LANG=en_US.UTF-8 > /etc/locale.conf\r" expect \$chroot_prompt # Set the hostname send -- "echo $hostname > /etc/hostname\r" expect \$chroot_prompt # Generate /etc/hosts send -- "echo -e '127.0.0.1 localhost\\n::1 localhost' >> /etc/hosts\r" expect \$chroot_prompt # Generate the initpcio send -- "mkinitcpio -P\r" expect \$chroot_prompt # Update the keyring # This may currently currently take a long time # see https://www.reddit.com/r/archlinux/comments/rbjbcr/pacman_keyring_update_taking_too_long/ send -- "pacman-key --init\r" expect \$chroot_prompt send -- "pacman-key --populate archlinux\r" expect \$chroot_prompt send -- "pacman-key --refresh-keys\r" expect \$chroot_prompt # As a workaround, you can temporarily disable signature check on pacman.conf #send -- "cp /etc/pacman.conf /etc/pacman.conf.orig\r" #expect \$chroot_prompt #send -- "sed -i /etc/pacman.conf -r -e 's/^(SigLevel\\\\s*=\\\\s*).*$/\\\\1 Never/g'\r" #expect \$chroot_prompt # Install extra packages send -- {echo "$packages" | pacman -S --noconfirm -} send -- "\r" expect \$chroot_prompt # Install syslinux send -- "pacman -S --noconfirm syslinux\r" expect \$chroot_prompt send -- "syslinux-install_update -i -a -m\r" #expect \$chroot_prompt #send -- "dd bs=440 count=1 conv=notrunc if=/usr/lib/syslinux/bios/mbr.bin of=/dev/sda\r" expect \$chroot_prompt send -- "sed -i /boot/syslinux/syslinux.cfg -e '1i SERIAL 0 115200'\r" expect \$chroot_prompt send -- "sed -i /boot/syslinux/syslinux.cfg -e 's|APPEND root=/dev/sda3|APPEND console=tty0 console=ttyS0,115200 root=/dev/sda1|g'\r" expect \$chroot_prompt # Restore the original pacman.conf send -- "mv /etc/pacman.conf.orig /etc/pacman.conf\r" expect \$chroot_prompt # Set the root password send -- "passwd\r" expect "New password: " send -- "$root_password\r" expect "Retype new password: " send -- "$root_password\r" expect \$chroot_prompt # Create a non-admin user send -- "useradd -d /home/$username -m $username\r" expect \$chroot_prompt send -- "passwd $username\r" expect "New password: " send -- "$user_password\r" expect "Retype new password: " send -- "$user_password\r" expect \$chroot_prompt # Enable the dhcpcd service send -- "ln -sf /usr/lib/systemd/system/dhcpcd.service /etc/systemd/system/multi-user.target.wants/dhcpcd.service\r" expect \$chroot_prompt # Enable and configure the SSH daemon send -- "ln -sf /usr/lib/systemd/system/sshd.service /etc/systemd/system/multi-user.target.wants/sshd.service\r" expect \$chroot_prompt send -- "cat <<_EOF_ >> /etc/ssh/sshd_config RSAAuthentication yes PubkeyAuthentication yes _EOF_ " expect \$chroot_prompt # Copy the user SSH key send -- "mkdir -p /home/$username/.ssh\r" expect \$chroot_prompt send -- {echo "$ssh_pubkey" >> "/home/$username/.ssh/authorized_keys"} send -- "\r" expect \$chroot_prompt send -- {echo "$ssh_privkey" > "/home/$username/.ssh/$(basename $ssh_keyfile)"} send -- "\r" expect \$chroot_prompt send -- {chmod 0600 "/home/$username/.ssh/$(basename $ssh_keyfile)"} send -- "\r" expect \$chroot_prompt send -- {echo "$ssh_pubkey" > "/home/$username/.ssh/$(basename $ssh_keyfile).pub"} send -- "\r" expect \$chroot_prompt send -- {chown -R $username "/home/$username/.ssh"} send -- "\r" expect \$chroot_prompt # Run any post-install scripts send -- {$post_install} send -- "\r" expect \$chroot_prompt # Clear the pacman cache send -- "rm -rf /var/cache/pacman/pkg/*\r" expect \$chroot_prompt # Exit and shutdown send -- "exit\r" expect \$prompt send -- "umount /mnt\r" expect \$prompt send -- "shutdown -h now\r" expect eof system {echo -e "\n${GREEN}Arch Linux system installed under ${imgfile}${NORMAL}"} EOF echo "--- Log closed at $(date)" >> "$logfile" }