mkplatformfs.sh 10 KB

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