mkrootfs.sh 9.4 KB

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