install.sh 7.8 KB

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