install.sh 8.3 KB

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