install.sh 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #!/usr/bin/bash -e
  2. #
  3. # Author: Casey DeLorme
  4. #
  5. # Description: A fully automated installation script wired for testing with
  6. # packer, but fully functional for a standard installation.
  7. #
  8. # Make sure you have a network connection, install git, clone this repository,
  9. # and finally run this script to automate the entire installation.
  10. #
  11. # pacman -Sy git
  12. # git clone https://git.caseydelorme.com/cdelorme/arch-desktop.git
  13. # cd arch-desktop
  14. # ./setup/install.sh
  15. # check that efivars is mounted or notify the user that it could create a problem
  16. [ ! -d /sys/firmware/efi/efivars ] && echo "efivars not mounted; this install may leave you with a derelict system..." && sleep 30s
  17. # verify required variables
  18. while [[ ! "$enable_hibernation" =~ ^[yn]$ ]]; do read -p "would you like to enable hibernation (yn)? " -n1 enable_hibernation && echo ""; done
  19. while [ -z "$root_password" ]; do read -p "please enter a root password: " -s root_password && echo ""; done
  20. while [ -z "$username" ]; do read -p "please enter your username: " username; done
  21. while [ -z "$password" ]; do read -p "please enter your password: " -s password && echo ""; done
  22. # optionally print all commands
  23. [ -n "$DEBUG" ] && set -x
  24. # test network connection
  25. ping -c 3 archlinux.org
  26. # synchronize time (print for debug)
  27. timedatectl set-ntp true
  28. timedatectl status
  29. # if more than one disk exists ask which to use:
  30. export disk=$(lsblk -l | grep disk | awk '{print $1}')
  31. if [ $(echo "$disk" | wc -l) -gt 1 ]; then
  32. echo -e "multiple disks found:\n$disk"
  33. read -p "please enter the name of the disk you want to use: " disk
  34. # make sure the disk exists or terminate
  35. if [ ! -b "/dev/${disk}" ]; then
  36. echo "unable to locate disk: /dev/${disk}"
  37. exit 1
  38. fi
  39. fi
  40. # assume sane default for swap, or intelligently size for hibernation
  41. export swap_size=2048
  42. if [ "$enable_hibernation" = "y" ]; then
  43. export ram_size=$(numfmt --to-unit=Mi --from-unit=K $(cat /proc/meminfo | grep MemTotal | awk '{print $2}'))
  44. if [ $ram_size -le $(numfmt --to-unit=Mi --from=iec 4G) ]; then
  45. export swap_size=$(echo $(($ram_size * 3 / 2)))
  46. else
  47. swap_size=$(printf %.0f $(echo "sqrt($ram_size) + $ram_size" | bc))
  48. fi
  49. fi
  50. # partition the hard drive
  51. parted "/dev/${disk}" -s 'mklabel gpt'
  52. parted "/dev/${disk}" -s 'mkpart primary fat32 1MiB 1024MiB'
  53. parted "/dev/${disk}" -s 'set 1 esp on'
  54. parted "/dev/${disk}" -s "mkpart primary linux-swap 1024MiB $((swap_size + 1024))MiB"
  55. parted "/dev/${disk}" -s "mkpart primary btrfs $((swap_size + 1024))MiB 100%"
  56. # acquire partitions as an array
  57. export partitions=( $(lsblk -l -xNAME "/dev/${disk}" | grep part | awk '{print $1}') )
  58. # format the partitions
  59. mkfs.fat -F32 -nEFI "/dev/${partitions[0]}"
  60. mkswap -Larch "/dev/${partitions[1]}"
  61. mkfs.btrfs -fLarch "/dev/${partitions[2]}"
  62. # enable and mount partitions with appropriate settings
  63. swapon "/dev/${partitions[1]}"
  64. mount -o "noatime,compress=lzo,space_cache,autodefrag,ssd" "/dev/${partitions[2]}" /mnt
  65. mkdir /mnt/boot
  66. mount "/dev/${partitions[0]}" /mnt/boot
  67. # install base and base-devel package sets, and generate the fstab
  68. pacstrap /mnt base base-devel
  69. genfstab -Up /mnt > /mnt/etc/fstab
  70. # this assumes that arch.sh and install/ exist and will copy them to continue
  71. # @note: would prefer to copy to `/tmp` but `/mnt/tmp` from iso is a separate tmpfs
  72. mkdir -p /mnt/srv/arch-desktop
  73. cp -r install /mnt/srv/arch-desktop/
  74. cp arch.sh /mnt/srv/arch-desktop/
  75. # proceed to automate arch-chroot installation then umount and reboot with an async delay
  76. arch-chroot /mnt /srv/arch-desktop/arch.sh
  77. if [ -n "$PACKER" ]; then
  78. sed -i "/^#\?PermitRootLogin/d" /mnt/etc/ssh/sshd_config
  79. sed -i "/^#\?PasswordAuthentication/d" /mnt/etc/ssh/sshd_config
  80. echo "PasswordAuthentication yes" >> /mnt/etc/ssh/sshd_config
  81. echo "PermitRootLogin yes" >> /mnt/etc/ssh/sshd_config
  82. fi
  83. umount -R /mnt
  84. (sleep 5 && systemctl reboot) &