install.sh 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 -U -p /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. echo "PermitRootLogin yes" >> /mnt/etc/ssh/sshd_config
  80. fi
  81. sleep 10m
  82. umount -R /mnt
  83. (sleep 5 && systemctl reboot) &