install.sh 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. #!/bin/sh
  2. type getargbool >/dev/null 2>&1 || . /lib/dracut-lib.sh
  3. # These functions pulled from void's excellent mklive.sh
  4. VAI_info_msg() {
  5. printf "\033[1m%s\n\033[m" "$@"
  6. }
  7. VAI_print_step() {
  8. CURRENT_STEP=$((CURRENT_STEP+1))
  9. VAI_info_msg "[${CURRENT_STEP}/${STEP_COUNT}] $*"
  10. }
  11. # ----------------------- Install Functions ------------------------
  12. VAI_welcome() {
  13. clear
  14. printf "=============================================================\n"
  15. printf "================ Void Linux Auto-Installer ==================\n"
  16. printf "=============================================================\n"
  17. }
  18. VAI_udev_settle() {
  19. /usr/bin/udevd --daemon
  20. /usr/bin/udevadm trigger --action=add --type=subsystems
  21. /usr/bin/udevadm trigger --action=add --type=devices
  22. /usr/bin/udevadm settle
  23. }
  24. VAI_get_address() {
  25. mkdir -p /var/lib/dhclient
  26. # This will fork, but it means that over a slow link the DHCP
  27. # lease will still be maintained. It also doesn't have a
  28. # hard-coded privsep user in it like dhcpcd.
  29. dhclient
  30. }
  31. VAI_partition_disk() {
  32. # Paritition Disk
  33. if [ -d /sys/firmware/efi ] ; then
  34. VAI_info_msg "Partitioning Disk for UEFI Boot"
  35. sfdisk "${disk}" <<EOF
  36. label: gpt
  37. ,100M,U,
  38. ,,L,
  39. EOF
  40. else
  41. VAI_info_msg "Partitioning Disk for BIOS Boot"
  42. sfdisk "${disk}" <<EOF
  43. label: dos
  44. ,,L,*
  45. EOF
  46. fi
  47. }
  48. VAI_format_disk() {
  49. # Make Filesystems
  50. if [ -d /sys/firmware/efi ] ; then
  51. mkfs.vfat -F32 "${disk}1"
  52. mkfs.ext4 -F "${disk}2"
  53. else
  54. mkfs.ext4 -F "${disk}1"
  55. fi
  56. }
  57. VAI_mount_target() {
  58. # Mount targetfs
  59. mkdir -p "${target}"
  60. if [ -d /sys/firmware/efi ] ; then
  61. mount "${disk}2" "${target}"
  62. mkdir -p "${target}/boot/efi"
  63. mount "${disk}1" "${target}/boot/efi"
  64. else
  65. mount "${disk}1" "${target}"
  66. fi
  67. }
  68. VAI_install_xbps_keys() {
  69. mkdir -p "${target}/var/db/xbps/keys"
  70. cp /var/db/xbps/keys/* "${target}/var/db/xbps/keys"
  71. }
  72. VAI_install_base_system() {
  73. # Install a base system
  74. _grub="grub"
  75. if [ -d /sys/firmware/efi ] ; then
  76. _grub="${_grub} grub-x86_64-efi"
  77. fi
  78. XBPS_ARCH="${XBPS_ARCH}" xbps-install -Sy -R "${xbpsrepository}" -r /mnt base-system ${_grub}
  79. # Install additional packages
  80. if [ -n "${pkgs}" ] ; then
  81. # shellcheck disable=SC2086
  82. XBPS_ARCH="${XBPS_ARCH}" xbps-install -Sy -R "${xbpsrepository}" -r /mnt ${pkgs}
  83. fi
  84. }
  85. VAI_prepare_chroot() {
  86. # Mount efivars if this is an EFI system
  87. if [ -d /sys/firmware/efi ] ; then
  88. mount -t efivarfs none /sys/firmware/efi/efivars
  89. fi
  90. # Mount dev, bind, proc, etc into chroot
  91. mount -t proc proc "${target}/proc"
  92. mount --rbind /sys "${target}/sys"
  93. mount --rbind /dev "${target}/dev"
  94. }
  95. VAI_configure_sudo() {
  96. # Give wheel sudo
  97. echo "%wheel ALL=(ALL:ALL) ALL" > "${target}/etc/sudoers.d/00-wheel"
  98. chmod 0440 "${target}/etc/sudoers.d/00-wheel"
  99. }
  100. VAI_correct_root_permissions() {
  101. chroot "${target}" chown root:root /
  102. chroot "${target}" chmod 755 /
  103. }
  104. VAI_configure_hostname() {
  105. # Set the hostname
  106. echo "${hostname}" > "${target}/etc/hostname"
  107. }
  108. VAI_configure_rc_conf() {
  109. # Set the value of various tokens
  110. sed -i "s:Europe/Madrid:${timezone}:" "${target}/etc/rc.conf"
  111. sed -i "s:\"es\":\"${keymap}\":" "${target}/etc/rc.conf"
  112. # Activate various tokens
  113. sed -i "s:#HARDWARECLOCK:HARDWARECLOCK:" "${target}/etc/rc.conf"
  114. sed -i "s:#TIMEZONE:TIMEZONE:" "${target}/etc/rc.conf"
  115. sed -i "s:#KEYMAP:KEYMAP:" "${target}/etc/rc.conf"
  116. }
  117. VAI_add_user() {
  118. chroot "${target}" useradd -m -s /bin/bash -U -G wheel,users,audio,video,cdrom,input "${username}"
  119. if [ -z "${password}" ] ; then
  120. chroot "${target}" passwd "${username}"
  121. else
  122. # For reasons that remain unclear, this does not work in musl
  123. echo "${username}:${password}" | chpasswd -c SHA512 -R "${target}"
  124. fi
  125. }
  126. VAI_configure_grub() {
  127. # Set hostonly
  128. echo "hostonly=yes" > "${target}/etc/dracut.conf.d/hostonly.conf"
  129. # Choose the newest kernel
  130. kernel_version="$(chroot "${target}" xbps-query linux | awk -F "[-_]" '/pkgver/ {print $2}')"
  131. # Install grub
  132. chroot "${target}" grub-install "${disk}"
  133. chroot "${target}" xbps-reconfigure -f "linux${kernel_version}"
  134. # Correct the grub install
  135. chroot "${target}" update-grub
  136. }
  137. VAI_configure_fstab() {
  138. # Grab UUIDs
  139. uuid1="$(blkid -s UUID -o value "${disk}1")"
  140. if [ -d /sys/firmware/efi ] ; then
  141. uuid2="$(blkid -s UUID -o value "${disk}2")"
  142. echo "UUID=$uuid1 /boot/efi vfat defaults 0 0" >> "${target}/etc/fstab"
  143. echo "UUID=$uuid2 / ext4 defaults,errors=remount-ro 0 1" >> "${target}/etc/fstab"
  144. else
  145. echo "UUID=$uuid1 / ext4 defaults,errors=remount-ro 0 1" >> "${target}/etc/fstab"
  146. fi
  147. }
  148. VAI_configure_locale() {
  149. # Set the libc-locale iff glibc
  150. case "${XBPS_ARCH}" in
  151. *-musl)
  152. VAI_info_msg "Glibc locales are not supported on musl"
  153. ;;
  154. *)
  155. sed -i "/${libclocale}/s/#//" "${target}/etc/default/libc-locales"
  156. chroot "${target}" xbps-reconfigure -f glibc-locales
  157. ;;
  158. esac
  159. }
  160. VAI_end_action() {
  161. case $end_action in
  162. reboot)
  163. VAI_info_msg "Rebooting the system"
  164. sync
  165. umount -R "${target}"
  166. reboot -f
  167. ;;
  168. shutdown)
  169. VAI_info_msg "Shutting down the system"
  170. sync
  171. umount -R "${target}"
  172. poweroff -f
  173. ;;
  174. script)
  175. VAI_info_msg "Running user provided script"
  176. xbps-uhelper fetch "${end_script}>/script"
  177. chmod +x /script
  178. target=${target} xbpsrepository=${xbpsrepository} /script
  179. ;;
  180. func)
  181. VAI_info_msg "Running user provided function"
  182. end_function
  183. ;;
  184. esac
  185. }
  186. VAI_configure_autoinstall() {
  187. # -------------------------- Setup defaults ---------------------------
  188. disk="$(lsblk -ipo NAME,TYPE,MOUNTPOINT | awk '{if ($2=="disk") {disks[$1]=0; last=$1} if ($3=="/") {disks[last]++}} END {for (a in disks) {if(disks[a] == 0){print a; break}}}')"
  189. hostname="$(ip -4 -o -r a | awk -F'[ ./]' '{x=$7} END {print x}')"
  190. target="/mnt"
  191. timezone="America/Chicago"
  192. keymap="us"
  193. libclocale="en_US.UTF-8"
  194. username="voidlinux"
  195. end_action="shutdown"
  196. end_script="/bin/true"
  197. XBPS_ARCH="$(xbps-uhelper arch)"
  198. case $XBPS_ARCH in
  199. *-musl)
  200. xbpsrepository="https://repo-default.voidlinux.org/current/musl"
  201. ;;
  202. *)
  203. xbpsrepository="https://repo-default.voidlinux.org/current"
  204. ;;
  205. esac
  206. # --------------- Pull config URL out of kernel cmdline -------------------------
  207. set +e
  208. if getargbool 0 autourl ; then
  209. set -e
  210. xbps-uhelper fetch "$(getarg autourl)>/etc/autoinstall.cfg"
  211. else
  212. set -e
  213. mv /etc/autoinstall.default /etc/autoinstall.cfg
  214. fi
  215. # Read in the resulting config file which we got via some method
  216. if [ -f /etc/autoinstall.cfg ] ; then
  217. VAI_info_msg "Reading configuration file"
  218. . ./etc/autoinstall.cfg
  219. fi
  220. # Bail out if we didn't get a usable disk
  221. if [ -z "$disk" ] ; then
  222. die "No valid disk!"
  223. fi
  224. }
  225. VAI_main() {
  226. CURRENT_STEP=0
  227. STEP_COUNT=17
  228. VAI_welcome
  229. VAI_print_step "Wait on hardware"
  230. VAI_udev_settle
  231. VAI_print_step "Bring up the network"
  232. VAI_get_address
  233. VAI_print_step "Configuring installer"
  234. VAI_configure_autoinstall
  235. VAI_print_step "Configuring disk"
  236. VAI_partition_disk
  237. VAI_format_disk
  238. VAI_print_step "Mounting the target filesystems"
  239. VAI_mount_target
  240. VAI_print_step "Installing XBPS keys"
  241. VAI_install_xbps_keys
  242. VAI_print_step "Installing the base system"
  243. VAI_install_base_system
  244. VAI_print_step "Granting sudo to default user"
  245. VAI_configure_sudo
  246. VAI_print_step "Setting hostname"
  247. VAI_configure_hostname
  248. VAI_print_step "Configure rc.conf"
  249. VAI_configure_rc_conf
  250. VAI_print_step "Preparing the chroot"
  251. VAI_prepare_chroot
  252. VAI_print_step "Fix ownership of /"
  253. VAI_correct_root_permissions
  254. VAI_print_step "Adding default user"
  255. VAI_add_user
  256. VAI_print_step "Configuring GRUB"
  257. VAI_configure_grub
  258. VAI_print_step "Configuring /etc/fstab"
  259. VAI_configure_fstab
  260. VAI_print_step "Configuring libc-locales"
  261. VAI_configure_locale
  262. VAI_print_step "Performing end-action"
  263. VAI_end_action
  264. }
  265. # If we are using the autoinstaller, launch it
  266. if getargbool 0 auto ; then
  267. set -e
  268. VAI_main
  269. # Very important to release this before returning to dracut code
  270. set +e
  271. fi