mkplatformfs.sh.in 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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
  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 rpi3-base" ;;
  105. rpi2*) PKGS="$BASEPKG rpi-base" ;;
  106. rpi*) PKGS="$BASEPKG ${PLATFORM%-*}-base" ;;
  107. usbarmory*) PKGS="$BASEPKG ${PLATFORM%-*}-base" ;;
  108. ci20*) PKGS="$BASEPKG ${PLATFORM%-*}-base" ;;
  109. i686*) PKGS="$BASEPKG" ;;
  110. x86_64*) PKGS="$BASEPKG" ;;
  111. GCP*) PKGS="$BASEPKG ${PLATFORM%-*}-base" ;;
  112. *) die "$PROGNAME: invalid platform!";;
  113. esac
  114. # Derive the target architecture using the static map
  115. set_target_arch_from_platform
  116. # And likewise set the cache
  117. set_cachedir
  118. # Append any additional packages if they were requested
  119. if [ -z "$EXTRA_PKGS" ] ; then
  120. PKGS="$PKGS $EXTRA_PKGS"
  121. fi
  122. # We need to operate on a tempdir, if this fails to create, it is
  123. # absolutely crucial to bail out so that we don't hose the system that
  124. # is running the script.
  125. ROOTFS=$(mktemp -d) || die "failed to create tempdir, exiting..."
  126. # Now that we have a directory for the ROOTFS, we can expand the
  127. # existing base filesystem into the directory
  128. info_msg "Expanding base tarball $BASE_TARBALL into $ROOTFS for $PLATFORM build."
  129. tar xf "$BASE_TARBALL" -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 -S $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. cleanup_chroot
  141. # The cache isn't that useful since by the time the ROOTFS will be
  142. # used it is likely to be out of date. Rather than shipping it around
  143. # only for it to be out of date, we remove it now.
  144. rm -rf "$ROOTFS/var/cache/*" 2>/dev/null
  145. # Finally we can compress the tarball, the name will include the
  146. # platform and the date on which the tarball was built.
  147. tarball=void-${PLATFORM}-PLATFORMFS-$(date '+%Y%m%d').tar.xz
  148. run_cmd "tar -cp --posix --xattrs -C $ROOTFS . | xz -T${COMPRESSOR_THREADS:-0} -9 > $tarball "
  149. # Now that we have the tarball we don't need the rootfs anymore, so we
  150. # can get rid of it.
  151. rm -rf "$ROOTFS"
  152. # Last thing to do before closing out is to let the user know that
  153. # this succeeded. This also ensures that there's something visible
  154. # that the user can look for at the end of the script, which can make
  155. # it easier to see what's going on if something above failed.
  156. info_msg "Successfully created $tarball ($PLATFORM)"