archInstall.sh 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. #!/bin/bash
  2. function checkVariables {
  3. if [[ -z $DISK ]]; then
  4. printf "System drive (DISK) is not set\n"
  5. exit 99
  6. fi
  7. if [[ -z $HOSTNAME ]]; then
  8. printf "Hostname (HOSTNAME) is not set\n"
  9. exit 99
  10. fi
  11. if [[ -z $USERNAME ]]; then
  12. printf "Username (USERNAME) is not set\n"
  13. exit 99
  14. fi
  15. if [[ -z $PASSWORD ]]; then
  16. printf "Password (PASSWORD) is not set\n"
  17. exit 99
  18. fi
  19. }
  20. function preChrootStep {
  21. if [[ $(whoami) != "root" ]]; then
  22. printf "This script has to be run as root"
  23. exit 1
  24. fi
  25. if ! ls /sys/firmware/efi/efivars > /dev/null 2>&1; then
  26. printf "This script can only be run when booted in UEFI mode\n"
  27. exit 1
  28. fi
  29. if ! ping -c 1 archlinux.org > /dev/null 2>&1; then
  30. printf "Unable to ping archlinux.org, check internet connectivity\n"
  31. exit 1
  32. fi
  33. printf "Arch installation\n"
  34. read -p "System drive: " DISK
  35. read -p "Hostname: " HOSTNAME
  36. read -p "Username: " USERNAME
  37. PASSWORD=$(openssl passwd -6) || exit $?
  38. LOGFILE="archInstall.$(date +%Y%m%d-%H%M%S).log"
  39. printf "Installing arch on ${DISK}\n" | tee -a $LOGFILE
  40. BLOCK_INFO=$(lsblk -n --output PATH,TYPE)
  41. printf "$BLOCK_INFO" | grep -e " disk$" | grep -e "^${DISK} " > /dev/null 2>&1
  42. if (( $? > 0 )); then
  43. printf "${DISK} does not seem to be a disk\n" | tee -a $LOGFILE
  44. exit 2
  45. fi
  46. if (( $(printf "$BLOCK_INFO" | grep -e " part$" | wc -l) > 0 )); then
  47. printf "${DISK} already has several partitions\n" | tee -a $LOGFILE
  48. read -p "Do you really wish to continue and erase all partitions? [N/y] " CONTINUE
  49. if [[ ! $CONTINUE =~ ^[yY]$ ]]; then
  50. printf "Exiting due to user input\n" | tee -a $LOGFILE
  51. exit 0
  52. fi
  53. fi
  54. printf "Loading keymap\n" | tee -a $LOGFILE
  55. loadkeys sv-latin1 || exit $?
  56. printf "Creating partitions\n" | tee -a $LOGFILE
  57. (
  58. echo g # New partition table
  59. echo n # New partition
  60. echo p # Primary partition
  61. echo 1 # Partition number
  62. echo # First sector (use default)
  63. echo +1024M # Last sector
  64. echo n # New partition
  65. echo p # Primary partition
  66. echo 2 # Partition number
  67. echo # First sector (use default)
  68. echo # Last sector (use default)
  69. echo t # Set type
  70. echo 1 # Select partition
  71. echo 1 # Set type EFI
  72. echo t # Select partition
  73. echo 2 # Selection partition
  74. echo 20 # Set type Linux file system
  75. echo w # Write changes
  76. ) | fdisk -W always ${DISK} >> $LOGFILE 2>&1 || exit $?
  77. BOOTPART="${DISK}p1"
  78. ROOTPART="${DISK}p2"
  79. printf "Creating LUKS2 container\n" | tee -a $LOGFILE
  80. cryptsetup luksFormat --type luks2 ${ROOTPART} 2>&1 | tee -a $LOGFILE || exit $?
  81. printf "Decrypt LUKS2 container\n" | tee -a $LOGFILE
  82. cryptsetup open ${ROOTPART} cryptlvm 2>&1 | tee -a $LOGFILE || exit $?
  83. printf "Setup LVM volumes\n" | tee -a $LOGFILE
  84. pvcreate /dev/mapper/cryptlvm >> $LOGFILE 2>&1 || exit $?
  85. vgcreate VolGroup1 /dev/mapper/cryptlvm >> $LOGFILE 2>&1 || exit $?
  86. lvcreate -l 100%FREE VolGroup1 -n root
  87. lvreduce -L -256M VolGroup1/root >> $LOGFILE 2>&1 || exit $?
  88. printf "Creating filesystems\n" | tee -a $LOGFILE
  89. mkfs.fat -F32 ${BOOTPART} >> $LOGFILE 2>&1 || exit $?
  90. mkfs.ext4 /dev/VolGroup1/root >> $LOGFILE 2>&1 || exit $?
  91. printf "Mount filesystems\n" | tee -a $LOGFILE
  92. mount /dev/VolGroup1/root /mnt
  93. mkdir -p /mnt/boot
  94. mount ${BOOTPART} /mnt/boot
  95. printf "Creating swap file\n" | tee -a $LOGFILE
  96. SWAPSIZE=$(free --giga | grep Mem: | awk '{printf "%dG", $2 * 1.5}')
  97. mkswap -U clear --size $SWAPSIZE --file /mnt/swapfile >> $LOGFILE 2>&1 || exit $?
  98. swapon /mnt/swapfile >> $LOGFILE 2>&1 || exit $?
  99. PACSTRAPPKGS="base linux linux-firmware linux-headers networkmanager efibootmgr vim sudo sed git lvm2"
  100. printf "Checking CPU manufacturer\n" | tee -a $LOGFILE
  101. CPU=$(lscpu | grep "^Vendor ID:" | awk '{ print $3 }')
  102. if [[ ! -z $CPU ]]; then
  103. if [[ "$CPU" == "GenuineIntel" ]]; then
  104. PACSTRAPPKGS="${PACSTRAPPKGS} intel-ucode"
  105. elif [[ "$CPU" == "AuthencticAMD" ]]; then
  106. PACSTRAPPKGS="${PACSTRAPPKGS} amd-ucode"
  107. fi
  108. fi
  109. printf "Installing base system\n" | tee -a $LOGFILE
  110. pacstrap -K /mnt ${PACSTRAPPKGS} >> $LOGFILE 2>&1 || exit $?
  111. printf "Generate fstab\n" | tee -a $LOGFILE
  112. genfstab -U /mnt >> /mnt/etc/fstab || exit $?
  113. }
  114. function chrootStep {
  115. checkVariables
  116. printf "Setting up time\n" | tee -a $LOGFILE
  117. ln -sf /usr/share/zoneinfo/Europe/Stockholm /etc/localtime || exit $?
  118. hwclock --systohc || exit $?
  119. printf "Setting up locale\n" | tee -a $LOGFILE
  120. sed -i -e 's/^#\(en_US.UTF-8\)/\1/' /etc/locale.gen || exit $?
  121. sed -i -e 's/^#\(sv_SE.UTF-8\)/\1/' /etc/locale.gen || exit $?
  122. locale-gen >&2 || exit $?
  123. echo "LANG=en_US.UTF-8" > /etc/locale.conf
  124. echo "LC_TIME=sv_SE.UTF-8" >> /etc/locale.conf
  125. echo "KEYMAP=sv-latin1" > /etc/vconsole.conf
  126. printf "Setting hostname to $HOSTNAME\n"
  127. echo "$HOSTNAME" > /etc/hostname
  128. printf "Add wheel to sudoers\n"
  129. echo "%wheel ALL=(ALL) ALL" > /etc/sudoers.d/wheel
  130. sed -i -e 's/^#\(%wheel ALL=(ALL) ALL\)/\1/' /etc/sudoers || exit $?
  131. printf "Creating user\n" | tee -a $LOGFILE
  132. useradd -m $USERNAME -G wheel >&2 || exit $?
  133. echo "${USERNAME}:${PASSWORD}" | chpasswd -e >&2 || exit $?
  134. printf "Setting temporary root password\n" | tee -a $LOGFILE
  135. echo "root:root" | chpasswd >&2 || exit $?
  136. printf "Starting and enabling NetworkManager\n" | tee -a $LOGFILE
  137. systemctl enable NetworkManager >&2 || exit $?
  138. systemctl start NetworkManager >&2 || exit $?
  139. printf "Add decryption kernel options\n" | tee -a $LOGFILE
  140. LUKSUUID=$(blkid | grep "${ROOTPART}" | sed -E 's/^.*\bUUID\b="(\S+)".*$/\1/') || exit $?
  141. mkdir -p /etc/cmdline.d >&2 || exit $?
  142. echo "rd.luks.name=${LUKSUUID}=cryptlvm root=/dev/VolGroup1/root rw rootfstype=ext4 rd.shell=0 rd.emergency=reboot" > /etc/cmdline.d/root.conf
  143. printf "Add LUKS related hooks to mkinitcpio.conf" | tee -a $LOGFILE
  144. for OPT in systemd keyboard sd-vconsole sd-encrypt lvm2; do
  145. sed -i -E '/^HOOKS=.*\b'$OPT'\b/!s/^(HOOKS=.*)\)$/\1 '$OPT'\)/' /etc/mkinitcpio.conf >&2 || exit $?
  146. done
  147. printf "Rebuild kernel\n" | tee -a $LOGFILE
  148. mkinitcpio -p linux 2>&1 || exit $?
  149. printf "Install bootloader (systemd)\n" | tee -a $LOGFILE
  150. bootctl install
  151. printf "Exiting chroot\n" | tee -a $LOGFILE
  152. exit
  153. }
  154. function postChrootStep {
  155. printf "Cleanup\n" | tee -a $LOGFILE
  156. cp $LOGFILE /mnt/$LOGFILE
  157. cd /
  158. swapoff /mnt/swapfile
  159. umount -R /mnt
  160. }
  161. if [[ $1 == "chroot" ]]; then
  162. chrootStep
  163. exit
  164. fi
  165. # Run preChroot operations
  166. preChrootStep
  167. # Run chroot operations
  168. printf "Adding install script to mount directory\n" | tee -a $LOGFILE
  169. cp $0 /mnt/
  170. printf "Chrooting arch\n" | tee -a $LOGFILE
  171. DISK="${DISK}" \
  172. HOSTNAME="${HOSTNAME}" \
  173. USERNAME="${USERNAME}" \
  174. PASSWORD="${PASSWORD}" \
  175. ROOTPART="${ROOTPART}" \
  176. arch-chroot /mnt /$(basename $0) chroot 2>> $LOGFILE | tee -a $LOGFILE
  177. if (( ${PIPESTATUS[0]} > 0 )); then
  178. exit ${PIPESTATUS[0]}
  179. fi
  180. # Run postchroot operations
  181. postChrootStep
  182. printf "Base installation completed, logfile at $LOGFILE.\nReboot and proceed with goodies.\nRemember to change/deactivate root login\n" | tee -a $LOGFILE