mkrootfs.sh.in 9.5 KB

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