mkplatformfs.sh.in 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. -h Show this help
  58. -V Show version
  59. _EOF
  60. }
  61. # ########################################
  62. # SCRIPT EXECUTION STARTS HERE
  63. # ########################################
  64. BASEPKG=base-system
  65. while getopts "b:p:k:c:C:r:h:V" opt; do
  66. case $opt in
  67. b) BASEPKG="$OPTARG" ;;
  68. p) EXTRA_PKGS="$OPTARG" ;;
  69. k) POST_CMD="$OPTARG" ;;
  70. c) XBPS_CACHEDIR="--cachedir=$OPTARG" ;;
  71. C) XBPS_CONFFILE="-C $OPTARG" ;;
  72. r) XBPS_REPOSITORY="$XBPS_REPOSITORY --repository=$OPTARG" ;;
  73. h) usage; exit 0 ;;
  74. V) echo "$PROGNAME @@MKLIVE_VERSION@@"; exit 0 ;;
  75. esac
  76. done
  77. shift $((OPTIND - 1))
  78. PLATFORM="$1"
  79. BASE_TARBALL="$2"
  80. # This is an aweful hack since the script isn't using privesc
  81. # mechanisms selectively. This is a TODO item.
  82. if [ "$(id -u)" -ne 0 ]; then
  83. die "need root perms to continue, exiting."
  84. fi
  85. # Before going any further, check that the tools that are needed are
  86. # present. If we delayed this we could check for the QEMU binary, but
  87. # its a reasonable tradeoff to just bail out now.
  88. check_tools
  89. # Most platforms have a base system package that includes specific
  90. # packages for bringing up the hardware. In the case of the cloud
  91. # platforms the base package includes the components needed to inject
  92. # SSH keys and user accounts. The base platform packages are always
  93. # noarch though, so we strip off the -musl extention if it was
  94. # provided.
  95. case "$PLATFORM" in
  96. bananapi*) PKGS="$BASEPKG ${PLATFORM%-*}-base" ;;
  97. beaglebone*) PKGS="$BASEPKG ${PLATFORM%-*}-base" ;;
  98. cubieboard2*|cubietruck*) PKGS="$BASEPKG ${PLATFORM%-*}-base" ;;
  99. dockstar*) PKGS="$BASEPKG ${PLATFORM%-*}-base" ;;
  100. odroid-u2*) PKGS="$BASEPKG ${PLATFORM%-*}-base" ;;
  101. odroid-c2*) PKGS="$BASEPKG ${PLATFORM%-musl}-base" ;;
  102. rpi3*) PKGS="$BASEPKG rpi3-base" ;;
  103. rpi2*) PKGS="$BASEPKG rpi-base" ;;
  104. rpi*) PKGS="$BASEPKG ${PLATFORM%-*}-base" ;;
  105. usbarmory*) PKGS="$BASEPKG ${PLATFORM%-*}-base" ;;
  106. ci20*) PKGS="$BASEPKG ${PLATFORM%-*}-base" ;;
  107. i686*) PKGS="$BASEPKG" ;;
  108. x86_64*) PKGS="$BASEPKG" ;;
  109. GCP*) PKGS="$BASEPKG ${PLATFORM%-*}-base" ;;
  110. *) die "$PROGNAME: invalid platform!";;
  111. esac
  112. # Derive the target architecture using the static map
  113. set_target_arch_from_platform
  114. # Append any additional packages if they were requested
  115. if [ -z "$EXTRA_PKGS" ] ; then
  116. PKGS="$PKGS $EXTRA_PKGS"
  117. fi
  118. # We need to operate on a tempdir, if this fails to create, it is
  119. # absolutely crucial to bail out so that we don't hose the system that
  120. # is running the script.
  121. ROOTFS=$(mktemp -d) || die "failed to create tempdir, exiting..."
  122. # Now that we have a directory for the ROOTFS, we can expand the
  123. # existing base filesystem into the directory
  124. info_msg "Expanding base tarball $BASE_TARBALL into $ROOTFS for $PLATFORM build."
  125. tar xf "$BASE_TARBALL" -C "$ROOTFS"
  126. # This will install, but not configure, the packages specified by
  127. # $PKGS. After this step we will do an xbps-reconfigure -f $PKGS
  128. # under the correct architecture to ensure the system is setup
  129. # correctly.
  130. run_cmd_target "xbps-install -S $XBPS_CONFFILE $XBPS_CACHEDIR $XBPS_REPOSITORY -r $ROOTFS -y $PKGS"
  131. # Now that the packages are installed, we need to chroot in and
  132. # reconfigure. This needs to be done as the right architecture.
  133. # Since this is the only thing we're doing in the chroot, we clean up
  134. # right after.
  135. run_cmd_chroot "$ROOTFS" "xbps-reconfigure -a"
  136. cleanup_chroot
  137. # The cache isn't that useful since by the time the ROOTFS will be
  138. # used it is likely to be out of date. Rather than shipping it around
  139. # only for it to be out of date, we remove it now.
  140. rm -rf "$ROOTFS/var/cache/*" 2>/dev/null
  141. # Finally we can compress the tarball, the name will include the
  142. # platform and the date on which the tarball was built.
  143. tarball=void-${PLATFORM}-PLATFORMFS-$(date '+%Y%m%d').tar.xz
  144. run_cmd "tar -cp --posix --xattrs -C $ROOTFS . | xz -T0 -9 > $tarball "
  145. # Now that we have the tarball we don't need the rootfs anymore, so we
  146. # can get rid of it.
  147. rm -rf "$ROOTFS"
  148. # Last thing to do before closing out is to let the user know that
  149. # this succeeded. This also ensures that there's something visible
  150. # that the user can look for at the end of the script, which can make
  151. # it easier to see what's going on if something above failed.
  152. info_msg "Successfully created $tarball ($PLATFORM)"