mkplatformfs.sh.in 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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*) XBPS_TARGET_ARCH="armv7l"; PKGS="$BASEPKG ${PLATFORM%-*}-base" ;;
  97. beaglebone*) XBPS_TARGET_ARCH="armv7l"; PKGS="$BASEPKG ${PLATFORM%-*}-base" ;;
  98. cubieboard2*|cubietruck*) XBPS_TARGET_ARCH="armv7l"; PKGS="$BASEPKG ${PLATFORM%-*}-base" ;;
  99. dockstar*) XBPS_TARGET_ARCH="armv5tel"; PKGS="$BASEPKG ${PLATFORM%-*}-base" ;;
  100. odroid-u2*) XBPS_TARGET_ARCH="armv7l"; PKGS="$BASEPKG ${PLATFORM%-*}-base" ;;
  101. odroid-c2*) XBPS_TARGET_ARCH="aarch64"; PKGS="$BASEPKG ${PLATFORM%-musl}-base" ;;
  102. rpi3*) XBPS_TARGET_ARCH="aarch64"; PKGS="$BASEPKG rpi3-base" ;;
  103. rpi2*) XBPS_TARGET_ARCH="armv7l"; PKGS="$BASEPKG rpi-base" ;;
  104. rpi*) XBPS_TARGET_ARCH="armv6l"; PKGS="$BASEPKG ${PLATFORM%-*}-base" ;;
  105. usbarmory*) XBPS_TARGET_ARCH="armv7l"; PKGS="$BASEPKG ${PLATFORM%-*}-base" ;;
  106. ci20*) XBPS_TARGET_ARCH="mipsel"; PKGS="$BASEPKG ${PLATFORM%-*}-base" ;;
  107. i686*) XBPS_TARGET_ARCH="i686"; PKGS="$BASEPKG" ;;
  108. x86_64*) XBPS_TARGET_ARCH="x86_64"; PKGS="$BASEPKG" ;;
  109. GCP*) XBPS_TARGET_ARCH="x86_64"; PKGS="$BASEPKG ${PLATFORM%-*}-base" ;;
  110. *) die "$PROGNAME: invalid platform!";;
  111. esac
  112. # Check if we should be using the -musl variant
  113. if [ -z "${BASE_TARBALL##*-musl-*}" ] ; then
  114. XBPS_TARGET_ARCH="${XBPS_TARGET_ARCH}-musl"
  115. PLATFORM="${PLATFORM}-musl"
  116. fi
  117. # Append any additional packages if they were requested
  118. if [ -z "$EXTRA_PKGS" ] ; then
  119. PKGS="$PKGS $EXTRA_PKGS"
  120. fi
  121. # We need to operate on a tempdir, if this fails to create, it is
  122. # absolutely crucial to bail out so that we don't hose the system that
  123. # is running the script.
  124. ROOTFS=$(mktemp -d) || die "failed to create tempdir, exiting..."
  125. # Now that we have a directory for the ROOTFS, we can expand the
  126. # existing base filesystem into the directory
  127. info_msg "Expanding base tarball $BASE_TARBALL into $ROOTFS for $PLATFORM build."
  128. tar xf "$BASE_TARBALL" -C "$ROOTFS"
  129. # This will install, but not configure, the packages specified by
  130. # $PKGS. After this step we will do an xbps-reconfigure -f $PKGS
  131. # under the correct architecture to ensure the system is setup
  132. # correctly.
  133. run_cmd_target "xbps-install -S $XBPS_CONFFILE $XBPS_CACHEDIR $XBPS_REPOSITORY -r $ROOTFS -y $PKGS"
  134. # Now that the packages are installed, we need to chroot in and
  135. # reconfigure. This needs to be done as the right architecture.
  136. # Since this is the only thing we're doing in the chroot, we clean up
  137. # right after.
  138. run_cmd_chroot "$ROOTFS" "xbps-reconfigure -a"
  139. cleanup_chroot
  140. # The cache isn't that useful since by the time the ROOTFS will be
  141. # used it is likely to be out of date. Rather than shipping it around
  142. # only for it to be out of date, we remove it now.
  143. rm -rf "$ROOTFS/var/cache/*" 2>/dev/null
  144. # Finally we can compress the tarball, the name will include the
  145. # platform and the date on which the tarball was built.
  146. tarball=void-${PLATFORM}-ROOTFS-$(date '+%Y%m%d').tar.xz
  147. run_cmd "tar -cp --posix --xattrs -C $ROOTFS . | xz -T0 -9 > $tarball "
  148. # Now that we have the tarball we don't need the rootfs anymore, so we
  149. # can get rid of it.
  150. rm -rf "$ROOTFS"
  151. # Last thing to do before closing out is to let the user know that
  152. # this succeeded. This also ensures that there's something visible
  153. # that the user can look for at the end of the script, which can make
  154. # it easier to see what's going on if something above failed.
  155. info_msg "Successfully created $tarball ($PLATFORM)"