install.sh 8.4 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. if [ "${keymap}" != "us" ] ; then
  110. sed -i "s:\"es\":\"${keymap}\":" "${target}/etc/rc.conf"
  111. sed -i "s:#KEYMAP:KEYMAP:" "${target}/etc/rc.conf"
  112. fi
  113. ln -s "/usr/share/zoneinfo/${timezone}" "${target}/etc/localtime"
  114. }
  115. VAI_add_user() {
  116. chroot "${target}" useradd -m -s /bin/bash -U -G wheel,users,audio,video,cdrom,input "${username}"
  117. if [ -z "${password}" ] ; then
  118. chroot "${target}" passwd "${username}"
  119. else
  120. # For reasons that remain unclear, this does not work in musl
  121. echo "${username}:${password}" | chpasswd -c SHA512 -R "${target}"
  122. fi
  123. }
  124. VAI_configure_grub() {
  125. # Set hostonly
  126. echo "hostonly=yes" > "${target}/etc/dracut.conf.d/hostonly.conf"
  127. # Choose the newest kernel
  128. kernel_version="$(xbps-uhelper -r ${target} version linux | sed 's/_.*//')"
  129. # Install grub
  130. chroot "${target}" grub-install "${disk}"
  131. chroot "${target}" xbps-reconfigure -f "linux${kernel_version}"
  132. # Correct the grub install
  133. chroot "${target}" update-grub
  134. }
  135. VAI_configure_fstab() {
  136. # Grab UUIDs
  137. uuid1="$(blkid -s UUID -o value "${disk}1")"
  138. if [ -d /sys/firmware/efi ] ; then
  139. uuid2="$(blkid -s UUID -o value "${disk}2")"
  140. echo "UUID=$uuid1 /boot/efi vfat defaults 0 0" >> "${target}/etc/fstab"
  141. echo "UUID=$uuid2 / ext4 defaults,errors=remount-ro 0 1" >> "${target}/etc/fstab"
  142. else
  143. echo "UUID=$uuid1 / ext4 defaults,errors=remount-ro 0 1" >> "${target}/etc/fstab"
  144. fi
  145. }
  146. VAI_configure_locale() {
  147. # Set the libc-locale iff glibc
  148. case "${XBPS_ARCH}" in
  149. *-musl)
  150. VAI_info_msg "Glibc locales are not supported on musl"
  151. ;;
  152. *)
  153. sed -i "/${libclocale}/s/#//" "${target}/etc/default/libc-locales"
  154. chroot "${target}" xbps-reconfigure -f glibc-locales
  155. ;;
  156. esac
  157. }
  158. VAI_end_action() {
  159. case $end_action in
  160. reboot)
  161. VAI_info_msg "Rebooting the system"
  162. sync
  163. umount -R "${target}"
  164. reboot -f
  165. ;;
  166. shutdown)
  167. VAI_info_msg "Shutting down the system"
  168. sync
  169. umount -R "${target}"
  170. poweroff -f
  171. ;;
  172. script)
  173. VAI_info_msg "Running user provided script"
  174. xbps-uhelper fetch "${end_script}>/script"
  175. chmod +x /script
  176. target=${target} xbpsrepository=${xbpsrepository} /script
  177. ;;
  178. func)
  179. VAI_info_msg "Running user provided function"
  180. end_function
  181. ;;
  182. esac
  183. }
  184. VAI_configure_autoinstall() {
  185. # -------------------------- Setup defaults ---------------------------
  186. disk_expr=".blockdevices[0].name"
  187. disk="/dev/$(lsblk --json | jq -r "$disk_expr")"
  188. hostname_expr='[.[]|select(.operstate=="UP").addr_info.[]|select(.scope=="global").local].[0]'
  189. hostname="$(ip --json -r a | jq -r "$hostname_expr")"
  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