mkplatformfs.sh.in 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. #!/bin/sh
  2. #-
  3. # Copyright (c) 2017 Google
  4. # All rights reserved.
  5. #
  6. # Redistribution and use in source and binary forms, with or without
  7. # modification, are permitted provided that the following conditions
  8. # are met:
  9. # 1. Redistributions of source code must retain the above copyright
  10. # notice, this list of conditions and the following disclaimer.
  11. # 2. Redistributions in binary form must reproduce the above copyright
  12. # notice, this list of conditions and the following disclaimer in the
  13. # documentation and/or other materials provided with the distribution.
  14. #
  15. # THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  16. # IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  17. # OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  18. # IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  19. # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  20. # NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  21. # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  22. # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  23. # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  24. # THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. #-
  26. readonly PROGNAME=$(basename "$0")
  27. readonly ARCH=$(uname -m)
  28. readonly REQTOOLS="xbps-install xbps-reconfigure tar xz"
  29. # This source pulls in all the functions from lib.sh. This set of
  30. # functions makes it much easier to work with chroots and abstracts
  31. # away all the problems with running binaries with QEMU.
  32. # shellcheck source=./lib.sh
  33. . ./lib.sh
  34. # Die is a function provided in lib.sh which handles the cleanup of
  35. # the mounts and removal of temporary directories if the running
  36. # program exists unexpectedly.
  37. trap 'die "Interrupted! exiting..."' INT TERM HUP
  38. # Even though we only support really one target for most of these
  39. # architectures this lets us refer to these quickly and easily by
  40. # XBPS_ARCH. This makes it a lot more obvious what is happening later
  41. # in the script, and it makes it easier to consume the contents of
  42. # these down the road in later scripts.
  43. usage() {
  44. cat <<_EOF
  45. Usage: $PROGNAME [options] <platform> <base-tarball>
  46. Supported platforms: i686, x86_64, GCP,
  47. dockstar, bananapi, beaglebone, cubieboard2, cubietruck,
  48. odroid-c2, odroid-u2, rpi, rpi2 (armv7), rpi3 (aarch64),
  49. usbarmory, ci20, pogoplugv4
  50. Options
  51. -b <syspkg> Set an alternative base-system package (defaults to base-system)
  52. -p <pkgs> Additional packages to install into the rootfs (separated by blanks)
  53. -k <cmd> Call "cmd <ROOTFSPATH>" after building the rootfs
  54. -c <dir> Set XBPS cache directory (defaults to \$PWD/xbps-cachedir-<arch>)
  55. -C <file> Full path to the XBPS configuration file
  56. -r <repo> Set XBPS repository (may be set multiple times)
  57. -x <num> Use <num> threads to compress the image (dynamic if unset)
  58. -h Show this help
  59. -V Show version
  60. _EOF
  61. }
  62. # ########################################
  63. # SCRIPT EXECUTION STARTS HERE
  64. # ########################################
  65. BASEPKG=base-system
  66. while getopts "b:p:k:c:C:r:x:hV" opt; do
  67. case $opt in
  68. b) BASEPKG="$OPTARG" ;;
  69. p) EXTRA_PKGS="$OPTARG" ;;
  70. k) POST_CMD="$OPTARG" ;;
  71. c) XBPS_CACHEDIR="--cachedir=$OPTARG" ;;
  72. C) XBPS_CONFFILE="-C $OPTARG" ;;
  73. r) XBPS_REPOSITORY="$XBPS_REPOSITORY --repository=$OPTARG" ;;
  74. x) COMPRESSOR_THREADS="$OPTARG" ;;
  75. h) usage; exit 0 ;;
  76. V) echo "$PROGNAME @@MKLIVE_VERSION@@"; exit 0 ;;
  77. esac
  78. done
  79. shift $((OPTIND - 1))
  80. PLATFORM="$1"
  81. BASE_TARBALL="$2"
  82. # This is an aweful hack since the script isn't using privesc
  83. # mechanisms selectively. This is a TODO item.
  84. if [ "$(id -u)" -ne 0 ]; then
  85. die "need root perms to continue, exiting."
  86. fi
  87. # Before going any further, check that the tools that are needed are
  88. # present. If we delayed this we could check for the QEMU binary, but
  89. # its a reasonable tradeoff to just bail out now.
  90. check_tools
  91. # Most platforms have a base system package that includes specific
  92. # packages for bringing up the hardware. In the case of the cloud
  93. # platforms the base package includes the components needed to inject
  94. # SSH keys and user accounts. The base platform packages are always
  95. # noarch though, so we strip off the -musl extention if it was
  96. # provided.
  97. case "$PLATFORM" in
  98. bananapi*) PKGS="$BASEPKG ${PLATFORM%-*}-base" ;;
  99. beaglebone*) PKGS="$BASEPKG ${PLATFORM%-*}-base" ;;
  100. cubieboard2*|cubietruck*) PKGS="$BASEPKG ${PLATFORM%-*}-base" ;;
  101. dockstar*) PKGS="$BASEPKG ${PLATFORM%-*}-base" ;;
  102. odroid-u2*) PKGS="$BASEPKG ${PLATFORM%-*}-base" ;;
  103. odroid-c2*) PKGS="$BASEPKG ${PLATFORM%-musl}-base" ;;
  104. rpi3*) PKGS="$BASEPKG rpi-base" ;;
  105. rpi2*) PKGS="$BASEPKG rpi-base" ;;
  106. rpi*) PKGS="$BASEPKG ${PLATFORM%-*}-base" ;;
  107. pogo*) PKGS="$BASEPKG ${PLATFORM%-*}-base" ;;
  108. usbarmory*) PKGS="$BASEPKG ${PLATFORM%-*}-base" ;;
  109. ci20*) PKGS="$BASEPKG ${PLATFORM%-*}-base" ;;
  110. i686*) PKGS="$BASEPKG" ;;
  111. x86_64*) PKGS="$BASEPKG" ;;
  112. GCP*) PKGS="$BASEPKG ${PLATFORM%-*}-base" ;;
  113. *) die "$PROGNAME: invalid platform!";;
  114. esac
  115. # Derive the target architecture using the static map
  116. set_target_arch_from_platform
  117. # And likewise set the cache
  118. set_cachedir
  119. # Append any additional packages if they were requested
  120. if [ -z "$EXTRA_PKGS" ] ; then
  121. PKGS="$PKGS $EXTRA_PKGS"
  122. fi
  123. # We need to operate on a tempdir, if this fails to create, it is
  124. # absolutely crucial to bail out so that we don't hose the system that
  125. # is running the script.
  126. ROOTFS=$(mktemp -d) || die "failed to create tempdir, exiting..."
  127. # Now that we have a directory for the ROOTFS, we can expand the
  128. # existing base filesystem into the directory
  129. info_msg "Expanding base tarball $BASE_TARBALL into $ROOTFS for $PLATFORM build."
  130. tar xf "$BASE_TARBALL" -C "$ROOTFS"
  131. # This will install, but not configure, the packages specified by
  132. # $PKGS. After this step we will do an xbps-reconfigure -f $PKGS
  133. # under the correct architecture to ensure the system is setup
  134. # correctly.
  135. run_cmd_target "xbps-install -S $XBPS_CONFFILE $XBPS_CACHEDIR $XBPS_REPOSITORY -r $ROOTFS -y $PKGS"
  136. # Now that the packages are installed, we need to chroot in and
  137. # reconfigure. This needs to be done as the right architecture.
  138. # Since this is the only thing we're doing in the chroot, we clean up
  139. # right after.
  140. run_cmd_chroot "$ROOTFS" "xbps-reconfigure -a"
  141. # Before final cleanup the ROOTFS needs to be checked to make sure it
  142. # contains an initrd and if its a platform with arch 'arm*' it needs
  143. # to also have a uInitrd. For this to work the system needs to have
  144. # the uboot-mkimage package installed. Base system packages that do
  145. # not provide this must provide the uInitrd pre-prepared if they are
  146. # arm based. x86 images will have this built using native dracut
  147. # using post unpacking steps for platforms that consume the x86
  148. # tarballs. This check is very specific and ensures that applicable
  149. # tooling is present before proceeding.
  150. if [ ! -f "$ROOTFS/boot/uInitrd" ] ||
  151. [ ! -f "$ROOTFS/boot/initrd" ] &&
  152. [ -z "${XBPS_TARGET_ARCH##*arm*}" ] &&
  153. [ -x "$ROOTFS/usr/bin/dracut" ] &&
  154. [ -x "$ROOTFS/usr/bin/mkimage" ]; then
  155. # Dracut needs to know the kernel version that will be using this
  156. # initrd so that it can install the kernel drivers in it. Normally
  157. # this check is quite complex, but since this is a clean rootfs and we
  158. # just installed exactly one kernel, this check can get by with a
  159. # really niave command to figure out the kernel version
  160. KERNELVERSION=$(ls "$ROOTFS/usr/lib/modules/")
  161. # Some platforms also have special arguments that need to be set
  162. # for dracut. This allows us to kludge around issues that may
  163. # exist on certain specific platforms we build for.
  164. set_dracut_args_from_platform
  165. # Now that things are setup, we can call dracut and build the initrd.
  166. # This will pretty much step through the normal process to build
  167. # initrd with the exception that the autoinstaller and netmenu are
  168. # force added since no module depends on them.
  169. info_msg "Building initrd for kernel version $KERNELVERSION"
  170. run_cmd_chroot "$ROOTFS" "env -i /usr/bin/dracut $dracut_args /boot/initrd $KERNELVERSION"
  171. [ $? -ne 0 ] && die "Failed to generate the initramfs"
  172. run_cmd_chroot "$ROOTFS" "env -i /usr/bin/mkimage -A arm -O linux -T ramdisk -C gzip -a 0 -e 0 -n 'Void Linux' -d /boot/initrd /boot/uInitrd"
  173. fi
  174. cleanup_chroot
  175. # The cache isn't that useful since by the time the ROOTFS will be
  176. # used it is likely to be out of date. Rather than shipping it around
  177. # only for it to be out of date, we remove it now.
  178. rm -rf "$ROOTFS/var/cache/*" 2>/dev/null
  179. # Finally we can compress the tarball, the name will include the
  180. # platform and the date on which the tarball was built.
  181. tarball=void-${PLATFORM}-PLATFORMFS-$(date '+%Y%m%d').tar.xz
  182. run_cmd "tar -cp --posix --xattrs -C $ROOTFS . | xz -T${COMPRESSOR_THREADS:-0} -9 > $tarball "
  183. # Now that we have the tarball we don't need the rootfs anymore, so we
  184. # can get rid of it.
  185. rm -rf "$ROOTFS"
  186. # Last thing to do before closing out is to let the user know that
  187. # this succeeded. This also ensures that there's something visible
  188. # that the user can look for at the end of the script, which can make
  189. # it easier to see what's going on if something above failed.
  190. info_msg "Successfully created $tarball ($PLATFORM)"