mknet.sh.in 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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 <kernelpkg> Use <kernelpkg> instead of 'linux' to build the image.
  56. -k <keymap> Console keymap to set (us if unset)
  57. -l <locale> Locale to set (en_US.UTF-8 if unset)
  58. -C "cmdline args" Add additional kernel command line arguments.
  59. -T "title" Modify the bootloader title.
  60. -S "splash image" Set a custom splash image for the bootloader
  61. The $PROGNAME script generates a network-bootable tarball of Void Linux
  62. _EOF
  63. exit 1
  64. }
  65. # ########################################
  66. # SCRIPT EXECUTION STARTS HERE
  67. # ########################################
  68. while getopts "r:c:C:T:K:i:o:k:l:h" opt; do
  69. case $opt in
  70. r) XBPS_REPOSITORY="--repository=$OPTARG $XBPS_REPOSITORY";;
  71. c) XBPS_CACHEDIR="--cachedir=$OPTARG";;
  72. i) INITRAMFS_COMPRESSION="$OPTARG";;
  73. K) KERNELPKG="$OPTARG";;
  74. o) OUTPUT_FILE="$OPTARG";;
  75. k) KEYMAP="$OPTARG";;
  76. l) LOCALE="$OPTARG";;
  77. C) BOOT_CMDLINE="$OPTARG";;
  78. T) BOOT_TITLE="$OPTARG";;
  79. S) SPLASH_IMAGE="OPTARG";;
  80. h) usage;;
  81. esac
  82. done
  83. shift $((OPTIND - 1))
  84. BASE_TARBALL="$1"
  85. # We need to infer the target architecture from the filename. All
  86. # other scripts are able to get this from the platforms map because a
  87. # platform is manually specified. Since the netboot tarballs target
  88. # only architectures, its necessary to pull this information from the
  89. # filename.
  90. XBPS_TARGET_ARCH=${BASE_TARBALL%%-ROOTFS*}
  91. XBPS_TARGET_ARCH=${XBPS_TARGET_ARCH##void-}
  92. # Knowing the target arch, we can set the cache up if it hasn't
  93. # already been set
  94. set_cachedir
  95. # This is an aweful hack since the script isn't using privesc
  96. # mechanisms selectively. This is a TODO item.
  97. if [ "$(id -u)" -ne 0 ]; then
  98. die "need root perms to continue, exiting."
  99. fi
  100. # Before going any further, check that the tools that are needed are
  101. # present. If we delayed this we could check for the QEMU binary, but
  102. # its a reasonable tradeoff to just bail out now.
  103. check_tools
  104. # We need to operate on a tempdir, if this fails to create, it is
  105. # absolutely crucial to bail out so that we don't hose the system that
  106. # is running the script.
  107. ROOTFS=$(mktemp -d) || die "failed to create ROOTFS tempdir, exiting..."
  108. BOOT_DIR=$(mktemp -d) || die "failed to create BOOT_DIR tempdir, exiting..."
  109. PXELINUX_DIR="$BOOT_DIR/pxelinux.cfg"
  110. # Now that we have a directory for the ROOTFS, we can expand the
  111. # existing base filesystem into the directory
  112. info_msg "Expanding base tarball $BASE_TARBALL into $ROOTFS for $PLATFORM build."
  113. tar xf "$BASE_TARBALL" -C "$ROOTFS"
  114. info_msg "Install additional dracut modules"
  115. # This section sets up the dracut modules that need to be present on
  116. # the ROOTFS to build the PXE tarball. This includes the netmenu
  117. # module and the autoinstaller
  118. mkdir -p "$ROOTFS/usr/lib/dracut/modules.d/05netmenu"
  119. cp dracut/netmenu/* "$ROOTFS/usr/lib/dracut/modules.d/05netmenu/"
  120. # The netmenu can directly launch the manual installer from the
  121. # initrd. This is the same installer that's on the live media with
  122. # all its quirks, oddities, and wierdness. It's included here for
  123. # places where you might have a lab network and need to run manual
  124. # installs from the network.
  125. cp installer.sh "$ROOTFS/usr/lib/dracut/modules.d/05netmenu/"
  126. # Of course with a PXE environment unattended installs are the norm.
  127. # The autoinstaller is loaded as a very high priority dracut module
  128. # and will fail the build if it can't be installed.
  129. mkdir -p "$ROOTFS/usr/lib/dracut/modules.d/01autoinstaller"
  130. cp dracut/autoinstaller/* "$ROOTFS/usr/lib/dracut/modules.d/01autoinstaller/"
  131. info_msg "Install kernel and additional required netboot packages"
  132. # The rootfs has no kernel in it, so it needs to have at the very
  133. # least dracut, syslinux, and linux installed. binutils provides
  134. # /usr/bin/strip which lets us shrink down the size of the initrd
  135. # dracut-network provides the in-initrd network stack dialog is needed
  136. # by the install environment. ${INITRAMFS_COMPRESSION} is the name of
  137. # the compressor we want to use (lz4 by default).
  138. if [ -z "${XBPS_TARGET_ARCH##*86*}" ] ; then
  139. # This platform is x86 or compatible, we should use
  140. # syslinux/pxelinux to boot the system.
  141. info_msg "Selecting syslinux bootloader"
  142. bootloader_pkg=syslinux
  143. else
  144. # This is likely an arm platform of some kind. In general these
  145. # either have u-boot or a u-boot compatible loader, so we'll use
  146. # that to produce a uImage and a uInitrd
  147. info_msg "Selecting u-boot bootloader"
  148. bootloader_pkg=uboot-mkimage
  149. fi
  150. run_cmd_target "xbps-install $XBPS_CONFFILE $XBPS_CACHEDIR $XBPS_REPOSITORY -r $ROOTFS -Sy ${KERNELPKG-linux} dracut binutils dracut-network dialog ${INITRAMFS_COMPRESSION-xz} ${bootloader_pkg}"
  151. run_cmd_chroot "$ROOTFS" "xbps-reconfigure -a"
  152. # Dracut needs to know the kernel version that will be using this
  153. # initrd so that it can install the kernel drivers in it. Normally
  154. # this check is quite complex, but since this is a clean rootfs and we
  155. # just installed exactly one kernel, this check can get by with a
  156. # really naive command to figure out the kernel version
  157. KERNELVERSION=$(ls "$ROOTFS/usr/lib/modules/")
  158. # Now that things are setup, we can call dracut and build the initrd.
  159. # This will pretty much step through the normal process to build
  160. # initrd with the exception that the autoinstaller and netmenu are
  161. # force added since no module depends on them.
  162. info_msg "Building initrd for kernel version $KERNELVERSION"
  163. run_cmd_chroot "$ROOTFS" "env -i /usr/bin/dracut \
  164. -N \
  165. --${INITRAMFS_COMPRESSION-xz} \
  166. --add-drivers ahci \
  167. --force-add 'autoinstaller netmenu' \
  168. --omit systemd \
  169. /boot/initrd \
  170. $KERNELVERSION"
  171. [ $? -ne 0 ] && die "Failed to generate the initramfs"
  172. info_msg "Collect netboot components"
  173. if [ ${bootloader_pkg} = "syslinux" ] ; then
  174. # The whole point of this endeavor is to get the files needed for PXE.
  175. # Now that they have been generated, we copy them out of the doomed
  176. # ROOTFS and into the $BOOT_DIR where we're staging the rest of the
  177. # tarball
  178. mv -v "$ROOTFS/boot/initrd" "$BOOT_DIR"
  179. cp -v "$ROOTFS/boot/vmlinuz-$KERNELVERSION" "$BOOT_DIR/vmlinuz"
  180. # The initrd has *very* restrictive permissions by default. To
  181. # prevent some SysAdmin down the road having a very frustrating time
  182. # debugging this, we just fix this here and now.
  183. chmod 0644 "$BOOT_DIR/initrd"
  184. # Now we need to grab the rest of the files that go in the tarball.
  185. # Some of these are always required, some of these are canonical, and
  186. # some of this list is from trial and error. Either way, this is the
  187. # minimum needed to get Void up and booting on metal from the network.
  188. for prog in pxelinux.0 ldlinux.c32 libcom32.c32 vesamenu.c32 libutil.c32 chain.c32 ; do
  189. cp -v "$ROOTFS/usr/lib/syslinux/$prog" "$BOOT_DIR"
  190. done
  191. # Lastly we need the default pxelinux config and the splash image.
  192. # This is user configurable, but if that isn't set then we'll use the
  193. # one from data/splash.png instead
  194. mkdir -p "$PXELINUX_DIR"
  195. cp -f pxelinux.cfg/pxelinux.cfg.in "$PXELINUX_DIR/default"
  196. cp -f "${SPLASH_IMAGE-data/splash.png}" "$BOOT_DIR"
  197. # This sets all the variables in the default config file
  198. info_msg "Configuring pxelinux.0 default boot menu"
  199. sed -i -e "s|@@SPLASHIMAGE@@|$(basename "${SPLASH_IMAGE-splash.png}")|" \
  200. -e "s|@@KERNVER@@|${KERNELVERSION}|" \
  201. -e "s|@@KEYMAP@@|${KEYMAP-us}|" \
  202. -e "s|@@ARCH@@|$XBPS_TARGET_ARCH|" \
  203. -e "s|@@LOCALE@@|${LOCALE-en_US.UTF-8}|" \
  204. -e "s|@@BOOT_TITLE@@|${BOOT_TITLE-Void Linux}|" \
  205. -e "s|@@BOOT_CMDLINE@@|${BOOT_CMDLINE}|" \
  206. "$PXELINUX_DIR/default"
  207. else
  208. # u-boot has far far fewer components, but u-boot artifacts do
  209. # require some pre-processing
  210. if [ ! -f "$ROOTFS/boot/uImage" ] ; then
  211. # Build the uImage, this is really just the kernel with a wrapper
  212. # to make u-boot happy. It also sets the load and entry
  213. # addresses, though in general these are overriden by the u-boot
  214. # configuration.
  215. run_cmd_chroot "$ROOTFS" "env -i /usr/bin/mkimage -A arm -O linux -T kernel -C none -a 0x00000000 -e 0x00000000 -n 'Void Kernel' -d /boot/zImage /boot/uImage"
  216. # Build the uInitrd which is similarly just a copy of the real
  217. # initrd in a format that u-boot is willing to ingest.
  218. run_cmd_chroot "$ROOTFS" "env -i /usr/bin/mkimage -A arm -O linux -T ramdisk -C none -a 0 -e 0 -n 'Void Installer Initrd' -d /boot/initrd /boot/uInitrd"
  219. # Copy out the artifacts that are worth keeping
  220. cp "$ROOTFS/boot/uImage" "$BOOT_DIR"
  221. cp "$ROOTFS/boot/uInitrd" "$BOOT_DIR"
  222. cp -r "$ROOTFS/boot/dtbs" "$BOOT_DIR"
  223. else
  224. # Copy the existing uImage out
  225. cp "$ROOTFS/boot/uImage" "$BOOT_DIR"
  226. fi
  227. fi
  228. # Compress the artifacts for distribution
  229. OUTPUT_FILE="void-${XBPS_TARGET_ARCH}-NETBOOT-$(date +%Y%m%d).tar.gz"
  230. info_msg "Compressing results to $OUTPUT_FILE"
  231. cd "$BOOT_DIR" || die "Could not enter image dir"
  232. tar -zcvf "$CURDIR/$OUTPUT_FILE" .
  233. cd "$CURDIR" || die "Could not return to working directory"
  234. # As a final cleanup step, remove the ROOTFS and the expanded BOOT_DIR
  235. info_msg "Cleaning up and removing build directories"
  236. cleanup_chroot
  237. [ -d "$ROOTFS" ] && rm -rf "$ROOTFS"
  238. [ -d "$BOOT_DIR" ] && rm -rf "$BOOT_DIR"