mkrootfs.sh 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. #!/bin/sh
  2. #-
  3. # Copyright (c) 2013-2015 Juan Romero Pardines.
  4. # Copyright (c) 2017 Google
  5. # All rights reserved.
  6. #
  7. # Redistribution and use in source and binary forms, with or without
  8. # modification, are permitted provided that the following conditions
  9. # are met:
  10. # 1. Redistributions of source code must retain the above copyright
  11. # notice, this list of conditions and the following disclaimer.
  12. # 2. Redistributions in binary form must reproduce the above copyright
  13. # notice, this list of conditions and the following disclaimer in the
  14. # documentation and/or other materials provided with the distribution.
  15. #
  16. # THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  17. # IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  18. # OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  19. # IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  20. # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  21. # NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  22. # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  23. # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  25. # THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. #-
  27. readonly PROGNAME=$(basename "$0")
  28. readonly ARCH=$(uname -m)
  29. readonly REQTOOLS="xbps-install xbps-reconfigure tar xz"
  30. # This source pulls in all the functions from lib.sh. This set of
  31. # functions makes it much easier to work with chroots and abstracts
  32. # away all the problems with running binaries with QEMU.
  33. # shellcheck source=./lib.sh
  34. . ./lib.sh
  35. # Die is a function provided in lib.sh which handles the cleanup of
  36. # the mounts and removal of temporary directories if the running
  37. # program exists unexpectedly.
  38. trap 'die "Interrupted! exiting..."' INT TERM HUP
  39. # Even though we only support really one target for most of these
  40. # architectures this lets us refer to these quickly and easily by
  41. # XBPS_ARCH. This makes it a lot more obvious what is happening later
  42. # in the script, and it makes it easier to consume the contents of
  43. # these down the road in later scripts.
  44. usage() {
  45. cat <<-EOH
  46. Usage: $PROGNAME [options] <arch>
  47. Generate a Void Linux ROOTFS tarball for the specified architecture.
  48. Supported architectures:
  49. i686, i686-musl, x86_64, x86_64-musl,
  50. armv5tel, armv5tel-musl, armv6l, armv6l-musl, armv7l, armv7l-musl
  51. aarch64, aarch64-musl,
  52. mipsel, mipsel-musl,
  53. ppc, ppc-musl, ppc64le, ppc64le-musl, ppc64, ppc64-musl
  54. riscv64, riscv64-musl
  55. OPTIONS
  56. -b <system-pkg> Set an alternative base-system package (default: base-container-full)
  57. -c <cachedir> Set XBPS cache directory (default: ./xbps-cachedir-<arch>)
  58. -C <file> Full path to the XBPS configuration file
  59. -r <repo> Use this XBPS repository. May be specified multiple times
  60. -o <file> Filename to write the ROOTFS to (default: automatic)
  61. -x <num> Number of threads to use for image compression (default: dynamic)
  62. -h Show this help and exit
  63. -V Show version and exit
  64. EOH
  65. }
  66. # ########################################
  67. # SCRIPT EXECUTION STARTS HERE
  68. # ########################################
  69. # Set the default system package.
  70. SYSPKG="base-container-full"
  71. # Boilerplate option parsing. This script supports the bare minimum
  72. # needed to build an image.
  73. while getopts "b:C:c:hr:x:o:V" opt; do
  74. case $opt in
  75. b) SYSPKG="$OPTARG";;
  76. C) XBPS_CONFFILE="-C $OPTARG";;
  77. c) XBPS_CACHEDIR="--cachedir=$OPTARG";;
  78. r) XBPS_REPOSITORY="$XBPS_REPOSITORY --repository=$OPTARG";;
  79. x) COMPRESSOR_THREADS="$OPTARG" ;;
  80. o) FILENAME="$OPTARG" ;;
  81. V) version; exit 0;;
  82. h) usage; exit 0;;
  83. *) usage >&2; exit 1;;
  84. esac
  85. done
  86. shift $((OPTIND - 1))
  87. XBPS_TARGET_ARCH="$1"
  88. if [ -z "$XBPS_TARGET_ARCH" ]; then
  89. usage >&2
  90. exit 1
  91. fi
  92. # Set the XBPS cache
  93. set_cachedir
  94. # This is an aweful hack since the script isn't using privesc
  95. # mechanisms selectively. This is a TODO item.
  96. if [ "$(id -u)" -ne 0 ]; then
  97. die "need root perms to continue, exiting."
  98. fi
  99. # Before going any further, check that the tools that are needed are
  100. # present. If we delayed this we could check for the QEMU binary, but
  101. # its a reasonable tradeoff to just bail out now.
  102. check_tools
  103. # If the arch wasn't set let's bail out now, nothing else in this
  104. # script will work without knowing what we're trying to build for.
  105. if [ -z "$XBPS_TARGET_ARCH" ]; then
  106. echo "$PROGNAME: arch was not set!"
  107. usage >&2; exit 1
  108. fi
  109. # We need to operate on a tempdir, if this fails to create, it is
  110. # absolutely crucial to bail out so that we don't hose the system that
  111. # is running the script.
  112. ROOTFS=$(mktemp -d) || die "failed to create tempdir, exiting..."
  113. # This maintains the chain of trust, the keys in the repo are known to
  114. # be good and so we copy those. Why don't we just use the ones on the
  115. # host system? That's a good point, but there's no promise that the
  116. # system running the script is Void, or that those keys haven't been
  117. # tampered with. Its much easier to use these since the will always
  118. # exist.
  119. mkdir -p "$ROOTFS/var/db/xbps/keys"
  120. cp keys/*.plist "$ROOTFS/var/db/xbps/keys"
  121. # This sets up files that are important for XBPS to work on the new
  122. # filesystem. It does not actually install anything.
  123. run_cmd_target "xbps-install -S $XBPS_CONFFILE $XBPS_CACHEDIR $XBPS_REPOSITORY -r $ROOTFS"
  124. # Later scripts expect the permissions on / to be the canonical 755,
  125. # so we set this here.
  126. chmod 755 "$ROOTFS"
  127. # The binfmt setup and pseudofs mountpoints are needed for the qemu
  128. # support in cases where we are running things that aren't natively
  129. # executable.
  130. register_binfmt
  131. mount_pseudofs
  132. # With everything setup, we can now run the install to load the
  133. # system package into the rootfs. This will not produce a
  134. # bootable system but will instead produce a base component that can
  135. # be quickly expanded to perform other actions on.
  136. run_cmd_target "xbps-install -SU $XBPS_CONFFILE $XBPS_CACHEDIR $XBPS_REPOSITORY -r $ROOTFS -y $SYSPKG"
  137. # Enable en_US.UTF-8 locale and generate it into the target ROOTFS.
  138. # This is a bit of a hack since some glibc stuff doesn't really work
  139. # correctly without a locale being generated. While some could argue
  140. # that this is an arbitrary or naive choice to enable the en_US
  141. # locale, most people using Void are able to work with the English
  142. # language at least enough to enable thier preferred locale. If this
  143. # truly becomes an issue in the future this hack can be revisited.
  144. if [ -e "$ROOTFS/etc/default/libc-locales" ]; then
  145. LOCALE=en_US.UTF-8
  146. sed -e "s/\#\(${LOCALE}.*\)/\1/g" -i "$ROOTFS/etc/default/libc-locales"
  147. fi
  148. # The reconfigure step needs to execute code that's been compiled for
  149. # the target architecture. Since the target isn't garanteed to be the
  150. # same as the host, this needs to be done via qemu.
  151. info_msg "Reconfiguring packages for ${XBPS_TARGET_ARCH} ..."
  152. # This step sets up enough of the base-files that the chroot will work
  153. # and they can be reconfigured natively. Without this step there
  154. # isn't enough configured for ld to work. This step runs as the host
  155. # architecture, but we may need to set up XBPS_ARCH for the target
  156. # architecture (but only when compatible).
  157. if is_target_native "$XBPS_TARGET_ARCH"; then
  158. run_cmd_target "xbps-reconfigure --rootdir $ROOTFS base-files"
  159. else
  160. run_cmd "xbps-reconfigure --rootdir $ROOTFS base-files"
  161. fi
  162. # Now running as the target system, this step reconfigures the
  163. # base-files completely. Certain things just won't work in the first
  164. # pass, so this cleans up any issues that linger.
  165. run_cmd_chroot "$ROOTFS" "env -i xbps-reconfigure -f base-files"
  166. # Once base-files is configured and functional its possible to
  167. # configure the rest of the system.
  168. run_cmd_chroot "$ROOTFS" "xbps-reconfigure -a"
  169. # Set the default password. Previous versions of this script used a
  170. # chroot to do this, but that is unnecessary since chpasswd
  171. # understands how to operate on chroots without actually needing to be
  172. # chrooted. We also remove the lock file in this step to clean up the
  173. # lock on the passwd database, lest it be left in the system and
  174. # propogated to other points.
  175. info_msg "Setting the default root password ('voidlinux')"
  176. if [ ! -f "$ROOTFS/etc/shadow" ] ; then
  177. run_cmd_chroot "$ROOTFS" pwconv
  178. fi
  179. echo root:voidlinux | run_cmd_chroot "$ROOTFS" "chpasswd -c SHA512" || die "Could not set default credentials"
  180. rm -f "$ROOTFS/etc/.pwd.lock"
  181. # At this point we're done running things in the chroot and we can
  182. # clean up the shims. Failure to do this can result in things hanging
  183. # when we try to delete the tmpdir.
  184. cleanup_chroot
  185. # The cache isn't that useful since by the time the ROOTFS will be
  186. # used it is likely to be out of date. Rather than shipping it around
  187. # only for it to be out of date, we remove it now.
  188. rm -rf "$ROOTFS/var/cache/*" 2>/dev/null
  189. # Finally we can compress the tarball, the name will include the
  190. # architecture and the date on which the tarball was built.
  191. : "${FILENAME:=void-${XBPS_TARGET_ARCH}-ROOTFS-$(date -u '+%Y%m%d').tar.xz}"
  192. run_cmd "tar cp --posix --xattrs --xattrs-include='*' -C $ROOTFS . | xz -T${COMPRESSOR_THREADS:-0} -9 > $FILENAME "
  193. # Now that we have the tarball we don't need the rootfs anymore, so we
  194. # can get rid of it.
  195. rm -rf "$ROOTFS"
  196. # Last thing to do before closing out is to let the user know that
  197. # this succeeded. This also ensures that there's something visible
  198. # that the user can look for at the end of the script, which can make
  199. # it easier to see what's going on if something above failed.
  200. info_msg "Successfully created $FILENAME ($XBPS_TARGET_ARCH)"