mkplatformfs.sh 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. rpi-armv6l, rpi-armv7l, rpi-aarch64,
  48. pinebookpro, pinephone, rock64
  49. Options
  50. -b <syspkg> Set an alternative base-system package (defaults to base-system)
  51. -p <pkgs> Additional packages to install into the rootfs (separated by blanks)
  52. -k <cmd> Call "cmd <ROOTFSPATH>" after building the rootfs
  53. -c <dir> Set XBPS cache directory (defaults to \$PWD/xbps-cachedir-<arch>)
  54. -C <file> Full path to the XBPS configuration file
  55. -r <repo> Set XBPS repository (may be set multiple times)
  56. -x <num> Use <num> threads to compress the image (dynamic if unset)
  57. -o <file> Filename to write the PLATFORMFS archive to
  58. -n Do not compress the image, instead print out the rootfs directory
  59. -h Show this help
  60. -V Show version
  61. _EOF
  62. }
  63. # ########################################
  64. # SCRIPT EXECUTION STARTS HERE
  65. # ########################################
  66. BASEPKG=base-system
  67. COMPRESSION="y"
  68. while getopts "b:p:k:c:C:r:x:o:nhV" opt; do
  69. case $opt in
  70. b) BASEPKG="$OPTARG" ;;
  71. p) EXTRA_PKGS="$OPTARG" ;;
  72. k) POST_CMD="$OPTARG" ;;
  73. c) XBPS_CACHEDIR="--cachedir=$OPTARG" ;;
  74. C) XBPS_CONFFILE="-C $OPTARG" ;;
  75. r) XBPS_REPOSITORY="--repository=$OPTARG $XBPS_REPOSITORY" ;;
  76. x) COMPRESSOR_THREADS="$OPTARG" ;;
  77. o) FILENAME="$OPTARG" ;;
  78. n) COMPRESSION="n" ;;
  79. V) version; exit 0;;
  80. *) usage; exit 0 ;;
  81. esac
  82. done
  83. shift $((OPTIND - 1))
  84. PLATFORM="$1"
  85. BASE_TARBALL="$2"
  86. # This is an aweful hack since the script isn't using privesc
  87. # mechanisms selectively. This is a TODO item.
  88. if [ "$(id -u)" -ne 0 ]; then
  89. die "need root perms to continue, exiting."
  90. fi
  91. # Before going any further, check that the tools that are needed are
  92. # present. If we delayed this we could check for the QEMU binary, but
  93. # its a reasonable tradeoff to just bail out now.
  94. check_tools
  95. # Most platforms have a base system package that includes specific
  96. # packages for bringing up the hardware. In the case of the cloud
  97. # platforms the base package includes the components needed to inject
  98. # SSH keys and user accounts. The base platform packages are always
  99. # noarch though, so we strip off the -musl extention if it was
  100. # provided.
  101. case "$PLATFORM" in
  102. rpi*) PKGS="$BASEPKG rpi-base" ;;
  103. i686*) PKGS="$BASEPKG" ;;
  104. x86_64*) PKGS="$BASEPKG" ;;
  105. GCP*) PKGS="$BASEPKG ${PLATFORM%-*}-base" ;;
  106. pinebookpro*) PKGS="$BASEPKG ${PLATFORM%-*}-base" ;;
  107. pinephone*) PKGS="$BASEPKG ${PLATFORM%-*}-base" ;;
  108. rock64*) PKGS="$BASEPKG ${PLATFORM%-*}-base" ;;
  109. *) die "$PROGNAME: invalid platform!";;
  110. esac
  111. # Derive the target architecture using the static map
  112. set_target_arch_from_platform
  113. # And likewise set the cache
  114. set_cachedir
  115. # Append any additional packages if they were requested
  116. if [ -n "$EXTRA_PKGS" ] ; then
  117. PKGS="$PKGS $EXTRA_PKGS"
  118. fi
  119. # We need to operate on a tempdir, if this fails to create, it is
  120. # absolutely crucial to bail out so that we don't hose the system that
  121. # is running the script.
  122. ROOTFS=$(mktemp -d) || die "failed to create tempdir, exiting..."
  123. # Now that we have a directory for the ROOTFS, we can expand the
  124. # existing base filesystem into the directory
  125. if [ ! -e "$BASE_TARBALL" ]; then
  126. die "no valid base tarball given, exiting."
  127. fi
  128. info_msg "Expanding base tarball $BASE_TARBALL into $ROOTFS for $PLATFORM build."
  129. tar xf "$BASE_TARBALL" --xattrs --xattrs-include='*' -C "$ROOTFS"
  130. # This will install, but not configure, the packages specified by
  131. # $PKGS. After this step we will do an xbps-reconfigure -f $PKGS
  132. # under the correct architecture to ensure the system is setup
  133. # correctly.
  134. run_cmd_target "xbps-install -SU $XBPS_CONFFILE $XBPS_CACHEDIR $XBPS_REPOSITORY -r $ROOTFS -y $PKGS"
  135. # Now that the packages are installed, we need to chroot in and
  136. # reconfigure. This needs to be done as the right architecture.
  137. # Since this is the only thing we're doing in the chroot, we clean up
  138. # right after.
  139. run_cmd_chroot "$ROOTFS" "xbps-reconfigure -a"
  140. # Before final cleanup the ROOTFS needs to be checked to make sure it
  141. # contains an initrd and if its a platform with arch 'arm*' it needs
  142. # to also have a uInitrd. For this to work the system needs to have
  143. # the uboot-mkimage package installed. Base system packages that do
  144. # not provide this must provide the uInitrd pre-prepared if they are
  145. # arm based. x86 images will have this built using native dracut
  146. # using post unpacking steps for platforms that consume the x86
  147. # tarballs. This check is very specific and ensures that applicable
  148. # tooling is present before proceeding.
  149. if [ ! -f "$ROOTFS/boot/uInitrd" ] ||
  150. [ ! -f "$ROOTFS/boot/initrd" ] &&
  151. [ -z "${XBPS_TARGET_ARCH##*arm*}" ] &&
  152. [ -x "$ROOTFS/usr/bin/dracut" ] &&
  153. [ -x "$ROOTFS/usr/bin/mkimage" ]; then
  154. # Dracut needs to know the kernel version that will be using this
  155. # initrd so that it can install the kernel drivers in it. Normally
  156. # this check is quite complex, but since this is a clean rootfs and we
  157. # just installed exactly one kernel, this check can get by with a
  158. # really niave command to figure out the kernel version
  159. KERNELVERSION=$(ls "$ROOTFS/usr/lib/modules/")
  160. # Some platforms also have special arguments that need to be set
  161. # for dracut. This allows us to kludge around issues that may
  162. # exist on certain specific platforms we build for.
  163. set_dracut_args_from_platform
  164. # Now that things are setup, we can call dracut and build the initrd.
  165. # This will pretty much step through the normal process to build
  166. # initrd with the exception that the autoinstaller and netmenu are
  167. # force added since no module depends on them.
  168. info_msg "Building initrd for kernel version $KERNELVERSION"
  169. run_cmd_chroot "$ROOTFS" "env -i /usr/bin/dracut $dracut_args /boot/initrd $KERNELVERSION"
  170. [ $? -ne 0 ] && die "Failed to generate the initramfs"
  171. 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"
  172. fi
  173. cleanup_chroot
  174. # The cache isn't that useful since by the time the ROOTFS will be
  175. # used it is likely to be out of date. Rather than shipping it around
  176. # only for it to be out of date, we remove it now.
  177. rm -rf "$ROOTFS/var/cache/*" 2>/dev/null
  178. # Now we can run the POST_CMD script. This user-supplied script gets the
  179. # $ROOTFS as a parameter.
  180. if [ -n "$POST_CMD" ]; then
  181. info_msg "Running user supplied command: $POST_CMD"
  182. run_cmd $POST_CMD $ROOTFS
  183. fi
  184. # Compress the tarball or just print out the path?
  185. if [ "$COMPRESSION" = "y" ]; then
  186. # Finally we can compress the tarball, the name will include the
  187. # platform and the date on which the tarball was built.
  188. tarball=${FILENAME:-void-${PLATFORM}-PLATFORMFS-$(date -u '+%Y%m%d').tar.xz}
  189. run_cmd "tar cp --posix --xattrs --xattrs-include='*' -C $ROOTFS . | xz -T${COMPRESSOR_THREADS:-0} -9 > $tarball "
  190. [ $? -ne 0 ] && die "Failed to compress tarball"
  191. # Now that we have the tarball we don't need the rootfs anymore, so we
  192. # can get rid of it.
  193. rm -rf "$ROOTFS"
  194. # Last thing to do before closing out is to let the user know that
  195. # this succeeded. This also ensures that there's something visible
  196. # that the user can look for at the end of the script, which can make
  197. # it easier to see what's going on if something above failed.
  198. info_msg "Successfully created $tarball ($PLATFORM)"
  199. else
  200. # User requested just printing out the path to the rootfs, here it comes.
  201. info_msg "Successfully created rootfs under $ROOTFS"
  202. fi