install.sh 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. # detect disks (if only one exists we can use it)
  30. # @todo: make sure this ignores optical drives (???)
  31. export disk=$(lsblk -l | grep disk | awk '{print $1}')
  32. # if more than one disk exists ask which to use:
  33. if [ $(echo "$disk" | wc -l) -gt 1 ]; then
  34. echo -e "multiple disks found:\n$disk"
  35. read -p "please enter the name of the disk you want to use: " disk
  36. # make sure the disk exists or terminate
  37. if [ ! -b "/dev/${disk}" ]; then
  38. echo "unable to locate disk: /dev/${disk}"
  39. exit 1
  40. fi
  41. fi
  42. # either use sane default or intelligently size swap for hibernation
  43. export swap_size=2048
  44. if [ "$enable_hibernation" = "y" ]; then
  45. export ram_size=$(numfmt --from-unit=K --from=iec --to-unit=M --to=iec $(cat /proc/meminfo | grep MemTotal | awk '{print $2}'))
  46. if [ $ram_size -le $(numfmt --from-unit=1024 --from=iec 4G) ]; then
  47. export swap_size=$(printf %.0f $(echo "$ram_size * 1.5" | bc))
  48. else
  49. export ram_sqrt=$(echo "sqrt($(numfmt --from-unit=M --to-unit=Gi $ram_size))" | bc)
  50. export swap_size=$(printf %.0f $(echo "$ram_size + $(numfmt --from-unit=Gi --to-unit=Mi $ram_sqrt)" | bc))
  51. fi
  52. fi
  53. # partition the hard drive
  54. parted "/dev/${disk}" -s 'mklabel gpt'
  55. parted "/dev/${disk}" -s 'mkpart primary fat32 1MiB 1024MiB'
  56. parted "/dev/${disk}" -s 'set 1 esp on'
  57. parted "/dev/${disk}" -s "mkpart primary linux-swap 1024MiB ${swap_size}MiB"
  58. parted "/dev/${disk}" -s "mkpart primary btrfs ${swap_size}MiB 100%"
  59. # acquire partitions as an array
  60. export partitions=( $(lsblk -l -xNAME "/dev/${disk}" | grep part | awk '{print $1}') )
  61. # format the partitions
  62. mkfs.fat -F32 -nEFI "/dev/${partitions[0]}"
  63. mkswap -Larch "/dev/${partitions[1]}"
  64. mkfs.btrfs -fLarch "/dev/${partitions[2]}"
  65. # enable and mount partitions with appropriate settings
  66. swapon "/dev/${partitions[1]}"
  67. mount -o "noatime,compress=lzo,space_cache,autodefrag,ssd" "/dev/${partitions[2]}" /mnt
  68. mkdir /mnt/boot
  69. mount "/dev/${partitions[0]}" /mnt/boot
  70. # install base and base-devel package sets, and generate the fstab
  71. pacstrap /mnt base base-devel
  72. genfstab -U -p /mnt >> /mnt/etc/fstab
  73. # this assumes that arch.sh and install/ exists and will copy them to continue
  74. # @note: would prefer to copy to `/tmp` but `/mnt/tmp` from iso is a separate tmpfs
  75. mkdir -p /mnt/srv/arch-desktop
  76. cp -r install /mnt/srv/arch-desktop/
  77. cp arch.sh /mnt/srv/arch-desktop/
  78. # proceed to automate arch-chroot installation then umount and reboot with an async delay
  79. arch-chroot /mnt /srv/arch-desktop/arch.sh
  80. umount -R /mnt
  81. (sleep 5 && systemctl reboot) &