archInstall.sh 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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)
  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 o # 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 +300M # 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 w # Write changes
  70. ) | fdisk -W always ${DISK} >> $LOGFILE 2>&1 || exit $?
  71. BOOTPART="${DISK}1"
  72. BTRFSPART="${DISK}2"
  73. printf "Creating filesystems\n" | tee -a $LOGFILE
  74. mkfs.fat ${BOOTPART} >> $LOGFILE 2>&1 || exit $?
  75. mkfs.btrfs ${BTRFSPART} >> $LOGFILE 2>&1 || exit $?
  76. printf "Mounting btrfs partition\n" | tee -a $LOGFILE
  77. mount ${BTRFSPART} /mnt >> $LOGFILE 2>&1 || exit $?
  78. printf "Creating btrfs subvolumes\n" | tee -a $LOGFILE
  79. btrfs subvolume create /mnt/@root >> $LOGFILE 2>&1 || exit $?
  80. btrfs subvolume create /mnt/@home >> $LOGFILE 2>&1 || exit $?
  81. btrfs subvolume create /mnt/@log >> $LOGFILE 2>&1 || exit $?
  82. btrfs subvolume create /mnt/@swap >> $LOGFILE 2>&1 || exit $?
  83. printf "Unmounting btrfs partition\n" | tee -a $LOGFILE
  84. umount /mnt >> $LOGFILE 2>&1
  85. printf "Mounting root\n" | tee -a $LOGFILE
  86. mount -o defaults,relatime,compress=zstd,subvol=@root ${BTRFSPART} /mnt >> $LOGFILE 2>&1 || exit $?
  87. printf "Creating mount directories\n" | tee -a $LOGFILE
  88. mkdir -p /mnt/boot/efi /mnt/home /mnt/var/log /mnt/swap >> $LOGFILE 2>&1 || exit $?
  89. printf "Mounting volumes\n" | tee -a $LOGFILE
  90. mount ${BOOTPART} /mnt/boot/efi >> $LOGFILE 2>&1 || exit $?
  91. mount ${BTRFSPART} -o defaults,relatime,compress=zstd,subvol=@home /mnt/home >> $LOGFILE 2>&1 || exit $?
  92. mount ${BTRFSPART} -o defaults,relatime,compress=zstd,subvol=@log /mnt/var/log >> $LOGFILE 2>&1 || exit $?
  93. mount ${BTRFSPART} -o defaults,relatime,compress=zstd,subvol=@swap /mnt/swap >> $LOGFILE 2>&1 || exit $?
  94. printf "Creating swap file\n" | tee -a $LOGFILE
  95. btrfs filesystem mkswapfile --size 8G /mnt/swap/swapfile >> $LOGFILE 2>&1 || exit $?
  96. swapon /mnt/swap/swapfile
  97. PACSTRAPPKGS="base linux linux-firmware btrfs-progs grub efibootmgr networkmanager sudo sed git ansible"
  98. printf "Checking CPU manufacturer\n" | tee -a $LOGFILE
  99. CPU=$(lscpu | grep "^Vendor ID:" | awk '{ print $3 }')
  100. if [[ ! -z $CPU ]]; then
  101. if [[ "$CPU" == "GenuineIntel" ]]; then
  102. PACSTRAPPKGS="${PACSTRAPPKGS} intel-ucode"
  103. elif [[ "$CPU" == "AuthencticAMD" ]]; then
  104. PACSTRAPPKGS="${PACSTRAPPKGS} amd-ucode"
  105. fi
  106. fi
  107. printf "Installing base system\n" | tee -a $LOGFILE
  108. pacstrap -K /mnt ${PACSTRAPPKGS} >> $LOGFILE 2>&1 || exit $?
  109. printf "Generate fstab\n" | tee -a $LOGFILE
  110. genfstab -U /mnt >> /mnt/etc/fstab || exit $?
  111. }
  112. function chrootStep {
  113. checkVariables
  114. printf "Setting up time\n"
  115. ln -sf /usr/share/zoneinfo/Europe/Stockholm /etc/localtime || exit $?
  116. hwclock --systohc || exit $?
  117. printf "Setting up locale\n"
  118. sed -i -e 's/^#\(en_US.UTF-8\)/\1/' /etc/locale.gen || exit $?
  119. sed -i -e 's/^#\(sv_SE.UTF-8\)/\1/' /etc/locale.gen || exit $?
  120. locale-gen >&2 || exit $?
  121. echo "LANG=en_US.UTF-8" > /etc/locale.conf
  122. echo "LC_TIME=sv_SE.UTF-8" >> /etc/locale.conf
  123. echo "KEYMAP=sv-latin1" > /etc/vconsole.conf
  124. printf "Setting hostname to $HOSTNAME\n"
  125. echo "$HOSTNAME" > /etc/hostname
  126. printf "Add wheel to sudoers\n"
  127. echo "%wheel ALL=(ALL) ALL" > /etc/sudoers.d/wheel
  128. sed -i -e 's/^#\(%wheel ALL=(ALL) ALL\)/\1/' /etc/sudoers || exit $?
  129. printf "Creating user\n" | tee -a $LOGFILE
  130. useradd -m $USERNAME -G wheel >&2 || exit $?
  131. echo "${USERNAME}:${PASSWORD}" | chpasswd -e >&2 || exit $?
  132. printf "Setting temporary root password\n"
  133. echo "root:root" | chpasswd >&2 || exit $?
  134. printf "Starting and enabling NetworkManager\n"
  135. systemctl enable NetworkManager >&2 || exit $?
  136. systemctl start NetworkManager >&2 || exit $?
  137. printf "Installing GRUB\n"
  138. grub-install --target=x86_64-efi --efi-directory=/boot/efi --bootloader-id=GRUB >&2 || exit $?
  139. grub-mkconfig -o /boot/grub/grub.cfg >&2 || exit $?
  140. printf "Exiting chroot\n"
  141. exit
  142. }
  143. function postChrootStep {
  144. printf "Cleanup\n" | tee -a $LOGFILE
  145. cp $LOGFILE /mnt/$LOGFILE
  146. cd /
  147. swapoff /mnt/swap/swapfile
  148. umount -R /mnt
  149. }
  150. if [[ $1 == "chroot" ]]; then
  151. chrootStep
  152. exit
  153. fi
  154. # Run preChroot operations
  155. preChrootStep
  156. # Run chroot operations
  157. printf "Adding install script to mount directory\n" | tee -a $LOGFILE
  158. cp $0 /mnt/
  159. printf "Chrooting arch\n" | tee -a $LOGFILE
  160. DISK="${DISK}" HOSTNAME="${HOSTNAME}" USERNAME="${USERNAME}" PASSWORD="${PASSWORD}" arch-chroot /mnt /$(basename $0) chroot 2>> $LOGFILE | tee -a $LOGFILE
  161. if (( ${PIPESTATUS[0]} > 0 )); then
  162. exit ${PIPESTATUS[0]}
  163. fi
  164. # Run postchroot operations
  165. postChrootStep
  166. printf "Base installation completed, logfile at $LOGFILE.\nReboot and proceed with goodies.\nRemember to change/deactivate root login\n" | tee -a $LOGFILE