mkrootfs.sh.in 9.5 KB

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