install.sh 7.9 KB

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