install.sh 8.1 KB

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