mknet.sh.in 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. #!/bin/sh
  2. #
  3. # vim: set ts=4 sw=4 et:
  4. #
  5. #-
  6. # Copyright (c) 2009-2015 Juan Romero Pardines.
  7. # All rights reserved.
  8. #
  9. # Redistribution and use in source and binary forms, with or without
  10. # modification, are permitted provided that the following conditions
  11. # are met:
  12. # 1. Redistributions of source code must retain the above copyright
  13. # notice, this list of conditions and the following disclaimer.
  14. # 2. Redistributions in binary form must reproduce the above copyright
  15. # notice, this list of conditions and the following disclaimer in the
  16. # documentation and/or other materials provided with the distribution.
  17. #
  18. # THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  19. # IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  20. # OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  21. # IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  22. # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  23. # NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  24. # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  25. # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  27. # THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. #-
  29. readonly PROGNAME=$(basename "$0")
  30. readonly REQTOOLS="xbps-install tar"
  31. # This script needs to jump around, so we'll remember where we started
  32. # so that we can get back here
  33. readonly CURDIR="$(pwd)"
  34. # This source pulls in all the functions from lib.sh. This set of
  35. # functions makes it much easier to work with chroots and abstracts
  36. # away all the problems with running binaries with QEMU.
  37. # shellcheck source=./lib.sh
  38. . ./lib.sh
  39. # Die is a function provided in lib.sh which handles the cleanup of
  40. # the mounts and removal of temporary directories if the running
  41. # program exists unexpectedly.
  42. trap 'bailout' INT TERM
  43. bailout() {
  44. [ -d "$BOOT_DIR" ] && rm -rf "$BOOT_DIR"
  45. die "An unchecked exception has occured!"
  46. }
  47. usage() {
  48. cat <<_EOF
  49. Usage: $PROGNAME [options] <rootfs>
  50. Options:
  51. -r <repo-url> Use this XBPS repository (may be specified multiple times).
  52. -c <cachedir> Use this XBPS cache directory.
  53. -i <lz4|gzip|bzip2|xz> Compression type for the initramfs image (xz if unset).
  54. -o <file> Output file name for the netboot tarball (auto if unset).
  55. -k <keymap> Console keymap to set (us if unset)
  56. -l <locale> Locale to set (en_US.UTF-8 if unset)
  57. -C "cmdline args" Add additional kernel command line arguments.
  58. -T "title" Modify the bootloader title.
  59. -S "splash image" Set a custom splash image for the bootloader
  60. The $PROGNAME script generates a network-bootable tarball of Void Linux
  61. _EOF
  62. exit 1
  63. }
  64. # ########################################
  65. # SCRIPT EXECUTION STARTS HERE
  66. # ########################################
  67. while getopts "r:c:C:T:i:o:h" opt; do
  68. case $opt in
  69. r) XBPS_REPOSITORY="--repository=$OPTARG $XBPS_REPOSITORY";;
  70. c) XBPS_CACHEDIR="--cachedir=$OPTARG";;
  71. i) INITRAMFS_COMPRESSION="$OPTARG";;
  72. o) OUTPUT_FILE="$OPTARG";;
  73. k) KEYMAP="$OPTARG";;
  74. l) LOCALE="$OPTARG";;
  75. C) BOOT_CMDLINE="$OPTARG";;
  76. T) BOOT_TITLE="$OPTARG";;
  77. S) SPLASH_IMAGE="OPTARG";;
  78. h) usage;;
  79. esac
  80. done
  81. shift $((OPTIND - 1))
  82. BASE_TARBALL="$1"
  83. # We need to infer the target architecture from the filename. All
  84. # other scripts are able to get this from the platforms map because a
  85. # platform is manually specified. Since the netboot tarballs target
  86. # only architectures, its necessary to pull this information from the
  87. # filename.
  88. XBPS_TARGET_ARCH=${BASE_TARBALL%%-ROOTFS*}
  89. XBPS_TARGET_ARCH=${XBPS_TARGET_ARCH##void-}
  90. # Knowing the target arch, we can set the cache up if it hasn't
  91. # already been set
  92. set_cachedir
  93. # This is an aweful hack since the script isn't using privesc
  94. # mechanisms selectively. This is a TODO item.
  95. if [ "$(id -u)" -ne 0 ]; then
  96. die "need root perms to continue, exiting."
  97. fi
  98. # Before going any further, check that the tools that are needed are
  99. # present. If we delayed this we could check for the QEMU binary, but
  100. # its a reasonable tradeoff to just bail out now.
  101. check_tools
  102. # We need to operate on a tempdir, if this fails to create, it is
  103. # absolutely crucial to bail out so that we don't hose the system that
  104. # is running the script.
  105. ROOTFS=$(mktemp -d) || die "failed to create ROOTFS tempdir, exiting..."
  106. BOOT_DIR=$(mktemp -d) || die "failed to create BOOT_DIR tempdir, exiting..."
  107. PXELINUX_DIR="$BOOT_DIR/pxelinux.cfg"
  108. # Now that we have a directory for the ROOTFS, we can expand the
  109. # existing base filesystem into the directory
  110. info_msg "Expanding base tarball $BASE_TARBALL into $ROOTFS for $PLATFORM build."
  111. tar xf "$BASE_TARBALL" -C "$ROOTFS"
  112. info_msg "Install additional dracut modules"
  113. # This section sets up the dracut modules that need to be present on
  114. # the ROOTFS to build the PXE tarball. This includes the netmenu
  115. # module and the autoinstaller
  116. mkdir -p "$ROOTFS/usr/lib/dracut/modules.d/05netmenu"
  117. cp dracut/netmenu/* "$ROOTFS/usr/lib/dracut/modules.d/05netmenu/"
  118. # The netmenu can directly launch the manual installer from the
  119. # initrd. This is the same installer that's on the live media with
  120. # all its quirks, oddities, and wierdness. It's included here for
  121. # places where you might have a lab network and need to run manual
  122. # installs from the network.
  123. cp installer.sh "$ROOTFS/usr/lib/dracut/modules.d/05netmenu/"
  124. # Of course with a PXE environment unattended installs are the norm.
  125. # The autoinstaller is loaded as a very high priority dracut module
  126. # and will fail the build if it can't be installed.
  127. mkdir -p "$ROOTFS/usr/lib/dracut/modules.d/01autoinstaller"
  128. cp dracut/autoinstaller/* "$ROOTFS/usr/lib/dracut/modules.d/01autoinstaller/"
  129. info_msg "Install kernel and additional required netboot packages"
  130. # The rootfs has no kernel in it, so it needs to have at the very
  131. # least dracut, syslinux, and linux installed. binutils provides
  132. # /usr/bin/strip which lets us shrink down the size of the initrd
  133. # dracut-network provides the in-initrd network stack
  134. # dialog is needed by the install environment
  135. # ${INITRAMFS_COMPRESSION} is the name of the compressor we want to use (lz4 by default)
  136. run_cmd_target "xbps-install $XBPS_CONFFILE $XBPS_CACHEDIR $XBPS_REPOSITORY -r $ROOTFS -Sy linux dracut syslinux binutils dracut-network dialog ${INITRAMFS_COMPRESSION-xz}"
  137. run_cmd_chroot "$ROOTFS" "xbps-reconfigure -a"
  138. # Dracut needs to know the kernel version that will be using this
  139. # initrd so that it can install the kernel drivers in it. Normally
  140. # this check is quite complex, but since this is a clean rootfs and we
  141. # just installed exactly one kernel, this check can get by with a
  142. # really niave command to figure out the kernel version
  143. KERNELVERSION=$(ls "$ROOTFS/usr/lib/modules/")
  144. # Now that things are setup, we can call dracut and build the initrd.
  145. # This will pretty much step through the normal process to build
  146. # initrd with the exception that the autoinstaller and netmenu are
  147. # force added since no module depends on them.
  148. info_msg "Building initrd for kernel version $KERNELVERSION"
  149. run_cmd_chroot "$ROOTFS" "env -i /usr/bin/dracut \
  150. -N \
  151. --${INITRAMFS_COMPRESSION-xz} \
  152. --add-drivers ahci \
  153. --force-add 'autoinstaller netmenu' \
  154. --omit systemd \
  155. /boot/initrd \
  156. $KERNELVERSION"
  157. [ $? -ne 0 ] && die "Failed to generate the initramfs"
  158. info_msg "Collect netboot components"
  159. # The whole point of this endeavor is to get the files needed for PXE.
  160. # Now that they have been generated, we copy them out of the doomed
  161. # ROOTFS and into the $BOOT_DIR where we're staging the rest of the
  162. # tarball
  163. mv -v "$ROOTFS/boot/initrd" "$BOOT_DIR"
  164. cp -v "$ROOTFS/boot/vmlinuz-$KERNELVERSION" "$BOOT_DIR/vmlinuz"
  165. # The initrd has *very* restrictive permissions by default. To
  166. # prevent some SysAdmin down the road having a very frustrating time
  167. # debugging this, we just fix this here and now.
  168. chmod 0644 "$BOOT_DIR/initrd"
  169. # Now we need to grab the rest of the files that go in the tarball.
  170. # Some of these are always required, some of these are canonical, and
  171. # some of this list is from trial and error. Either way, this is the
  172. # minimum needed to get Void up and booting on metal from the network.
  173. for prog in pxelinux.0 ldlinux.c32 libcom32.c32 vesamenu.c32 libutil.c32 chain.c32 ; do
  174. cp -v "$ROOTFS/usr/share/syslinux/$prog" "$BOOT_DIR"
  175. done
  176. # Lastly we need the default pxelinux config and the splash image.
  177. # This is user configurable, but if that isn't set then we'll use the
  178. # one from data/splash.png instead
  179. mkdir -p "$PXELINUX_DIR"
  180. cp -f pxelinux.cfg/pxelinux.cfg.in "$PXELINUX_DIR/default"
  181. cp -f "${SPLASH_IMAGE-data/splash.png}" "$BOOT_DIR"
  182. # This sets all the variables in the default config file
  183. info_msg "Configuring pxelinux.0 default boot menu"
  184. sed -i -e "s|@@SPLASHIMAGE@@|$(basename "${SPLASH_IMAGE-splash.png}")|" \
  185. -e "s|@@KERNVER@@|${KERNELVERSION}|" \
  186. -e "s|@@KEYMAP@@|${KEYMAP-us}|" \
  187. -e "s|@@ARCH@@|$XBPS_TARGET_ARCH|" \
  188. -e "s|@@LOCALE@@|${LOCALE-en_US.UTF-8}|" \
  189. -e "s|@@BOOT_TITLE@@|${BOOT_TITLE-Void Linux}|" \
  190. -e "s|@@BOOT_CMDLINE@@|${BOOT_CMDLINE}|" \
  191. "$PXELINUX_DIR/default"
  192. # Default output file format
  193. OUTPUT_FILE="void-${XBPS_TARGET_ARCH}-NETBOOT-$(date +%Y%m%d).tar.gz"
  194. info_msg "Compressing results to $OUTPUT_FILE"
  195. cd "$BOOT_DIR" || die "Could not enter image dir"
  196. tar -zcvf "$CURDIR/$OUTPUT_FILE" .
  197. cd "$CURDIR" || die "Could not return to working directory"
  198. # As a final cleanup step, remove the ROOTFS and the expanded BOOT_DIR
  199. info_msg "Cleaning up and removing build directories"
  200. cleanup_chroot
  201. [ -d "$ROOTFS" ] && rm -rf "$ROOTFS"
  202. [ -d "$BOOT_DIR" ] && rm -rf "$BOOT_DIR"