mkrootfs.sh.in 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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. trap 'die "Interrupted! exiting..."' INT TERM HUP
  30. info_msg() {
  31. printf "\033[1m%s\n\033[m" "$@"
  32. }
  33. die() {
  34. printf "FATAL: %s\n" "$@"
  35. umount_pseudofs
  36. [ -d "$ROOTFS" ] && rm -rf "$ROOTFS"
  37. exit 1
  38. }
  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. mount_pseudofs() {
  61. for f in dev proc sys; do
  62. [ ! -d "$ROOTFS/$f" ] && mkdir -p "$ROOTFS/$f"
  63. mount -r --bind /$f "$ROOTFS/$f"
  64. done
  65. }
  66. umount_pseudofs() {
  67. umount -f /proc/sys/fs/binfmt_misc >/dev/null 2>&1
  68. if [ -d "${ROOTFS}" ]; then
  69. for f in dev proc sys; do
  70. umount -f "$ROOTFS/$f" >/dev/null 2>&1
  71. done
  72. fi
  73. }
  74. run_cmd_target() {
  75. info_msg "Running $* for target $XBPS_TARGET_ARCH ..."
  76. if [ "$XBPS_TARGET_ARCH" = "$(xbps-uhelper arch)" ] ; then
  77. # This is being run on the same architecture as the host,
  78. # therefore we should set XBPS_ARCH.
  79. if ! eval XBPS_ARCH="$XBPS_TARGET_ARCH" "$@" ; then
  80. die "Could not run command $*"
  81. fi
  82. else
  83. # This is being run on a foriegn arch, therefore we should set
  84. # XBPS_TARGET_ARCH. In this case XBPS will not attempt
  85. # certain actions and will require reconfiguration later.
  86. if ! eval XBPS_TARGET_ARCH="$XBPS_TARGET_ARCH" "$@" ; then
  87. die "Could not run command $*"
  88. fi
  89. fi
  90. }
  91. run_cmd() {
  92. info_msg "Running $*"
  93. eval "$@"
  94. }
  95. # TODO: Figure out how to register the binfmt for x86_64 and for i686
  96. # to facilitate building on alien build systems.
  97. register_binfmt() {
  98. mountpoint -q /proc/sys/fs/binfmt_misc || modprobe -q binfmt_misc; mount -t binfmt_misc binfmt_misc /proc/sys/fs/binfmt_misc 2>/dev/null
  99. case "${QEMU_BIN}" in
  100. qemu-arm-static)
  101. echo ':arm:M::\x7fELF\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x28\x00:\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff:/usr/bin/qemu-arm-static:' > /proc/sys/fs/binfmt_misc/register 2>/dev/null
  102. ;;
  103. qemu-aarch64-static)
  104. echo ':arm64:M::\x7fELF\x02\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\xb7:\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff:/usr/bin/qemu-aarch64-static:' > /proc/sys/fs/binfmt_misc/register 2>/dev/null
  105. ;;
  106. qemu-mipsel-static)
  107. echo ':mipsel:M::\x7fELF\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x08\x00:\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff:/usr/bin/qemu-mipsel-static:' > /proc/sys/fs/binfmt_misc/register 2>/dev/null
  108. ;;
  109. *)
  110. die "Unknown target architecture!"
  111. ;;
  112. esac
  113. cp -f "$(which "$QEMU_BIN")" "$ROOTFS/usr/bin" || die "failed to copy $QEMU_BIN to the ROOTFS"
  114. }
  115. #
  116. # main()
  117. #
  118. while getopts "C:c:h:r:V" opt; do
  119. case $opt in
  120. C) XBPS_CONFFILE="-C $OPTARG";;
  121. c) XBPS_CACHEDIR="--cachedir=$OPTARG";;
  122. h) usage; exit 0;;
  123. r) XBPS_REPOSITORY="$XBPS_REPOSITORY --repository=$OPTARG";;
  124. V) echo "$PROGNAME @@MKLIVE_VERSION@@"; exit 0;;
  125. esac
  126. done
  127. shift $((OPTIND - 1))
  128. XBPS_TARGET_ARCH="$1"
  129. # This is an aweful hack since the script isn't using privesc
  130. # mechanisms selectively. This is a TODO item.
  131. if [ "$(id -u)" -ne 0 ]; then
  132. die "need root perms to continue, exiting."
  133. fi
  134. # If the arch wasn't set let's bail out now, nothing else in this
  135. # script will work without knowing what we're trying to build for.
  136. if [ -z "$XBPS_TARGET_ARCH" ]; then
  137. echo "$PROGNAME: arch was not set!"
  138. usage; exit 1
  139. fi
  140. # This select maps the architectures to the appropriate QEMU binaries
  141. # since this mapping isn't something that can just be subbed in for
  142. # easily.
  143. case "$XBPS_TARGET_ARCH" in
  144. i686*) QEMU_BIN=qemu-i386-static ;;
  145. x86_64*) QEMU_BIN=qemu-x86_64-static ;;
  146. armv*) QEMU_BIN=qemu-arm-static ;;
  147. aarch64*) QEMU_BIN=qemu-aarch64-static ;;
  148. mipsel*) QEMU_BIN=qemu-mipsel-static ;;
  149. *) die "Unknown target architecture" ;;
  150. esac
  151. # If the repository hasn't already been set, we set it to a sane value
  152. # here. These should all resolve even if they won't have the
  153. # appropriate repodata files for the selected architecture.
  154. : "${XBPS_REPOSITORY:=--repository=http://repo.voidlinux.eu/current \
  155. --repository=http://repo.voidlinux.eu/current/musl \
  156. --repository=http://repo.voidlinux.eu/current/aarch64}"
  157. # The package artifacts are cacheable, but they need to be isolated
  158. # from the host cache.
  159. : "${XBPS_CACHEDIR:=--cachedir=$PWD/xbps-cache/${XBPS_TARGET_ARCH}}"
  160. # The following binaries are required to proceed
  161. for f in chroot tar xbps-install xbps-reconfigure xbps-query; do
  162. if ! which $f >/dev/null ; then
  163. die "$f binary is missing in your system, exiting."
  164. fi
  165. done
  166. # For builds that do not match the host architecture, the correct qemu
  167. # binary will also be required.
  168. if ! $QEMU_BIN -version >/dev/null 2>&1; then
  169. die "$QEMU_BIN binary is missing in your system, exiting."
  170. fi
  171. # We need to operate on a tempdir, if this fails to create, it is
  172. # absolutely crucial to bail out so that we don't hose the system that
  173. # is running the script.
  174. ROOTFS=$(mktemp -d) || die "failed to create tempdir, exiting..."
  175. # This maintains the chain of trust, the keys in the repo are known to
  176. # be good and so we copy those. Why don't we just use the ones on the
  177. # host system? That's a good point, but there's no promise that the
  178. # system running the script is Void, or that those keys haven't been
  179. # tampered with. Its much easier to use these since the will always
  180. # exist.
  181. mkdir -p "$ROOTFS/var/db/xbps/keys"
  182. cp keys/*.plist "$ROOTFS/var/db/xbps/keys"
  183. # This sets up files that are important for XBPS to work on the new
  184. # filesystem. It does not actually install anything.
  185. run_cmd_target "xbps-install -S $XBPS_CONFFILE $XBPS_CACHEDIR $XBPS_REPOSITORY -r $ROOTFS"
  186. # Later scripts expect the permissions on / to be the canonical 755,
  187. # so we set this here.
  188. chmod 755 "$ROOTFS"
  189. # The pseudofs mountpoints are needed for the qemu support in cases
  190. # where we are running things that aren't natively executable.
  191. mount_pseudofs
  192. # With everything setup, we can now run the install to load the
  193. # base-voidstrap package into the rootfs. This will not produce a
  194. # bootable system but will instead produce a base component that can
  195. # be quickly expanded to perform other actions on.
  196. run_cmd_target "xbps-install -S $XBPS_CONFFILE $XBPS_CACHEDIR $XBPS_REPOSITORY -r $ROOTFS -y base-voidstrap"
  197. # Enable en_US.UTF-8 locale and generate it into the target ROOTFS.
  198. # This is a bit of a hack since some glibc stuff doesn't really work
  199. # correctly without a locale being generated. While some could argue
  200. # that this is an arbitrary or naive choice to enable the en_US
  201. # locale, most people using Void are able to work with the English
  202. # language at least enough to enable thier preferred locale. If this
  203. # truly becomes an issue in the future this hack can be revisited.
  204. if [ -e "$ROOTFS/etc/default/libc-locales" ]; then
  205. LOCALE=en_US.UTF-8
  206. sed -e "s/\#\(${LOCALE}.*\)/\1/g" -i "$ROOTFS/etc/default/libc-locales"
  207. fi
  208. # The reconfigure step needs to execute code that's been compiled for
  209. # the target architecture. Since the target isn't garanteed to be the
  210. # same as the host, this needs to be done via qemu.
  211. info_msg "Reconfiguring packages for ${XBPS_TARGET_ARCH} ..."
  212. case "$XBPS_TARGET_ARCH" in
  213. # TODO: Rather than asserting that x86 code will work, check
  214. # instead if the system that is hosting this script is the same as
  215. # the target, using binfmt if it is not.
  216. i686*|x86_64*)
  217. run_cmd "XBPS_ARCH=${XBPS_TARGET_ARCH} xbps-reconfigure -r $ROOTFS base-files"
  218. ;;
  219. *)
  220. # This case handles configuration of the system when it won't
  221. # work directly with the host ELF infrastructure. Before
  222. # continuing its necessary to determine the correct magic
  223. # numbers and load them into the kernel so that it will defer
  224. # to the appropriate interpreter as defined by $QEMU_BIN
  225. register_binfmt
  226. # This step sets up enough of the base-files that the chroot
  227. # will work and they can be reconfigured natively. Without
  228. # this step there isn't enough configured for ld to work.
  229. run_cmd "xbps-reconfigure -r $ROOTFS base-files"
  230. # Now running as the target system, this step reconfigures the
  231. # base-files completely. Certain things just won't work in
  232. # the first pass, so this cleans up any issues that linger.
  233. run_cmd "chroot $ROOTFS env -i xbps-reconfigure -f base-files"
  234. # TODO: determine why these lines are here. What is the harm
  235. # in having them and what do they remove. Do they interact
  236. # adversely with the alien build support discussed above.
  237. rmdir "$ROOTFS/usr/lib32" 2>/dev/null
  238. rm -f "$ROOTFS/lib32" "$ROOTFS/lib64" "$ROOTFS/usr/lib64"
  239. ;;
  240. esac
  241. # Once base-files is configured and functional its possible to
  242. # configure the rest of the system.
  243. run_cmd "chroot $ROOTFS xbps-reconfigure -a"
  244. # At this point we're done running things that needed to be done with
  245. # the pseudo filesystems to be mounted, so we can clean that up.
  246. umount_pseudofs
  247. # Set the default password. Previous versions of this script used a
  248. # chroot to do this, but that is unnecessary since chpasswd
  249. # understands how to operate on chroots without actually needing to be
  250. # chrooted. We also remove the lock file in this step to clean up the
  251. # lock on the passwd database, lest it be left in the system and
  252. # propogated to other points.
  253. echo root:voidlinux | chpasswd -c SHA512 --root "$ROOTFS" || die "Could not set default credentials"
  254. rm -f "$ROOTFS/etc/.pwd.lock"
  255. # The cache isn't that useful since by the time the ROOTFS will be
  256. # used it is likely to be out of date. Rather than shipping it around
  257. # only for it to be out of date, we remove it now.
  258. rm -rf "$ROOTFS/var/cache/*" 2>/dev/null
  259. # If we needed to copy in a QEMU_BIN executable, that needs to be
  260. # removed before packaging up the shiny new ROOTFS. This could be
  261. # wrapped in a conditional, but its much easier to just remove the
  262. # binary location on the off chance its there.
  263. rm -f "$ROOTFS/usr/bin/$QEMU_BIN"
  264. # Finally we can compress the tarball, the name will include the
  265. # architecture and the date on which the tarball was built.
  266. tarball=void-${XBPS_TARGET_ARCH}-ROOTFS-$(date '+%Y%m%d').tar.xz
  267. run_cmd "tar -cp --posix --xattrs -C $ROOTFS . | xz -T0 -9 > $tarball "
  268. # Now that we have the tarball we don't need the rootfs anymore, so we
  269. # can get rid of it.
  270. rm -rf "$ROOTFS"
  271. # Last thing to do before closing out is to let the user know that
  272. # this succeeded. This also ensures that there's something visible
  273. # that the user can look for at the end of the script, which can make
  274. # it easier to see what's going on if something above failed.
  275. info_msg "Successfully created $tarball ($XBPS_TARGET_ARCH)"