install.sh 4.1 KB

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