install.sh 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. export hostname=${hostname:-arch}
  23. # export environment variables for arch.sh automation
  24. export enable_hibernation
  25. export root_password
  26. export username
  27. export password
  28. # optionally print all commands
  29. [ -n "$DEBUG" ] && set -x && export DEBUG
  30. # test network connection
  31. ping -c 3 archlinux.org
  32. # synchronize time (print for debug)
  33. timedatectl set-ntp true
  34. timedatectl status
  35. # if more than one disk exists ask which to use:
  36. if [ -z "$disk" ]; then
  37. export disk=$(lsblk -l | grep disk | awk '{if (!$7) print $1;}')
  38. if [ $(echo "$disk" | wc -l) -gt 1 ]; then
  39. echo -e "multiple disks found:\n$disk"
  40. read -p "please enter the name of the disk you want to use: " disk
  41. fi
  42. fi
  43. # make sure the disk exists or terminate
  44. if [ ! -b "/dev/${disk}" ]; then
  45. echo "unable to locate disk: /dev/${disk}"
  46. exit 1
  47. fi
  48. # assume sane default for swap, or intelligently size for hibernation
  49. export swap_size=2048
  50. if [ "$enable_hibernation" = "y" ]; then
  51. export ram_size=$(numfmt --to-unit=Mi --from-unit=K $(cat /proc/meminfo | grep MemTotal | awk '{print $2}'))
  52. if [ $ram_size -le $(numfmt --to-unit=Mi --from=iec 4G) ]; then
  53. export swap_size=$(echo $(($ram_size * 3 / 2)))
  54. else
  55. swap_size=$(printf %.0f $(echo "sqrt($ram_size) + $ram_size" | bc))
  56. fi
  57. fi
  58. # partition the hard drive
  59. parted "/dev/${disk}" -s 'mklabel gpt'
  60. parted "/dev/${disk}" -s 'mkpart primary fat32 1MiB 1024MiB'
  61. parted "/dev/${disk}" -s 'set 1 esp on'
  62. parted "/dev/${disk}" -s "mkpart primary linux-swap 1024MiB $((swap_size + 1024))MiB"
  63. parted "/dev/${disk}" -s "mkpart primary btrfs $((swap_size + 1024))MiB 100%"
  64. # acquire partitions as an array
  65. export partitions=( $(lsblk -l -xNAME "/dev/${disk}" | grep part | awk '{print $1}') )
  66. # format the partitions
  67. mkfs.fat -F32 -nEFI "/dev/${partitions[0]}"
  68. mkswap -Lswap "/dev/${partitions[1]}"
  69. mkfs.btrfs -fLarch "/dev/${partitions[2]}"
  70. # enable and mount partitions with appropriate settings
  71. swapon "/dev/${partitions[1]}"
  72. export resume_uuid="$(lsblk -no UUID "/dev/${partitions[1]}")"
  73. mount -t btrfs -o "noatime,compress=lzo,autodefrag,ssd,space_cache=v2" "/dev/${partitions[2]}" /mnt
  74. mkdir /mnt/boot
  75. mount "/dev/${partitions[0]}" /mnt/boot
  76. # install base and base-devel package sets, and generate the fstab
  77. pacstrap /mnt base
  78. genfstab -Up /mnt > /mnt/etc/fstab
  79. # this assumes that arch.sh and install/ exist and will copy them to continue
  80. # @note: would prefer to copy to `/tmp` but `/mnt/tmp` from iso is a separate tmpfs
  81. mkdir -p /mnt/srv/arch-desktop
  82. cp -r ./ /mnt/srv/arch-desktop
  83. # proceed to automate arch-chroot installation and push output to a log file that gets copied to the new disk
  84. arch-chroot /mnt /srv/arch-desktop/arch.sh | tee arch-install.log
  85. cp arch-install.log /mnt/arch-install.log
  86. # if auto-reboot is enabled then unmount and automatically reboot
  87. if [ -n "$AUTOREBOOT" ]; then
  88. sync
  89. umount -R /mnt
  90. echo "installation complete, rebooting in 5 seconds..."
  91. (sleep 5 && systemctl reboot) &
  92. else
  93. echo "installation complete, run 'systemctl reboot' when ready..."
  94. fi