mkrootfs.sh.in 8.9 KB

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