mkrootfs.sh.in 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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. # This function ensures that the psuedofs mountpoints are present
  62. # in the chroot. Strictly they are not necessary to have for many
  63. # commands, but bind-mounts are cheap and it isn't too bad to just
  64. # mount them all the time.
  65. for f in dev proc sys; do
  66. # In a naked chroot there is nothing to bind the mounts to, so
  67. # we need to create directories for these first.
  68. [ ! -d "$ROOTFS/$f" ] && mkdir -p "$ROOTFS/$f"
  69. if ! mountpoint -q "$ROOTFS/$f" ; then
  70. # It is VERY important that this only happen if the
  71. # pseudofs isn't already mounted. If it already is then
  72. # this is virtually impossible to troubleshoot because it
  73. # looks like the subsequent umount just isn't working.
  74. mount -r --bind /$f "$ROOTFS/$f"
  75. fi
  76. done
  77. }
  78. umount_pseudofs() {
  79. # This function cleans up the mounts in the chroot. Failure to
  80. # clean up these mounts will prevent the tmpdir from being
  81. # deletable instead throwing the error "Device or Resource Busy".
  82. # The '-f' option is passed to umount to account for the
  83. # contingency where the psuedofs mounts are not present.
  84. if [ -d "${ROOTFS}" ]; then
  85. for f in dev proc sys; do
  86. umount -f "$ROOTFS/$f" >/dev/null 2>&1
  87. done
  88. fi
  89. }
  90. run_cmd_target() {
  91. info_msg "Running $* for target $XBPS_TARGET_ARCH ..."
  92. if [ "$XBPS_TARGET_ARCH" = "$(xbps-uhelper arch)" ] ; then
  93. # This is being run on the same architecture as the host,
  94. # therefore we should set XBPS_ARCH.
  95. if ! eval XBPS_ARCH="$XBPS_TARGET_ARCH" "$@" ; then
  96. die "Could not run command $*"
  97. fi
  98. else
  99. # This is being run on a foriegn arch, therefore we should set
  100. # XBPS_TARGET_ARCH. In this case XBPS will not attempt
  101. # certain actions and will require reconfiguration later.
  102. if ! eval XBPS_TARGET_ARCH="$XBPS_TARGET_ARCH" "$@" ; then
  103. die "Could not run command $*"
  104. fi
  105. fi
  106. }
  107. run_cmd() {
  108. info_msg "Running $*"
  109. eval "$@"
  110. }
  111. run_cmd_chroot() {
  112. # General purpose chroot function which makes sure the chroot is
  113. # prepared. This function takes 2 arguments, the location to
  114. # chroot to and the command to run.
  115. # This is an idempotent function, it is safe to call every time
  116. # before entering the chroot. This has the advantage of making
  117. # execution in the chroot appear as though it "Just Works(tm)".
  118. register_binfmt
  119. # Before we step into the chroot we need to make sure the
  120. # pseudo-filesystems are ready to go. Not all commands will need
  121. # this, but its still a good idea to call it here anyway.
  122. mount_pseudofs
  123. # With assurance that things will run now we can jump into the
  124. # chroot and run stuff!
  125. chroot "$1" sh -c "$2"
  126. }
  127. cleanup_chroot() {
  128. # This function cleans up the chroot shims that are used by QEMU
  129. # to allow builds on alien platforms. It takes no arguments but
  130. # expects the global $ROOTFS variable to be set.
  131. # Un-Mount the pseudofs mounts if they were mounted
  132. umount_pseudofs
  133. # If a QEMU binary was copied in, remove that as well
  134. if [ -x "$ROOTFS/usr/bin/$QEMU_BIN" ] ; then
  135. rm "$ROOTFS/usr/bin/$QEMU_BIN"
  136. fi
  137. }
  138. # TODO: Figure out how to register the binfmt for x86_64 and for i686
  139. # to facilitate building on alien build systems.
  140. register_binfmt() {
  141. # This function sets up everything that is needed to be able to
  142. # chroot into a ROOTFS and be able to run commands there. This
  143. # really matters on platforms where the host architecture is
  144. # different from the target, and you wouldn't be able to run
  145. # things like xbps-reconfigure -a. This function is idempotent
  146. # (You can run it multiple times without modifying state). This
  147. # function takes no arguments, but does expect the global variable
  148. # $XBPS_TARGET_ARCH to be set.
  149. # This select sets up the "magic" bytes in /proc that let the
  150. # kernel select an alternate interpreter. More values for this
  151. # map can be obtained from here:
  152. # https://github.com/qemu/qemu/blob/master/scripts/qemu-binfmt-conf.sh
  153. case "${XBPS_TARGET_ARCH}" in
  154. armv*)
  155. _cpu=arm
  156. _magic="\x7fELF\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x28\x00"
  157. _mask="\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff"
  158. QEMU_BIN=qemu-arm-static
  159. ;;
  160. aarch64*)
  161. _cpu=aarch64
  162. _magic="\x7fELF\x02\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\xb7"
  163. _mask="\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff"
  164. QEMU_BIN=qemu-aarch64-static
  165. ;;
  166. mipsel*)
  167. _cpu=mipsel
  168. _magic="\x7fELF\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x08\x00"
  169. _mask="\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff"
  170. QEMU_BIN=qemu-mipsel-static
  171. ;;
  172. *86*)
  173. info_msg "FIXME: Assuming that x86 instructions are native"
  174. QEMU_BIN=NATIVE
  175. ;;
  176. *)
  177. die "Unknown target architecture!"
  178. ;;
  179. esac
  180. # In the special case where the build is native we can return
  181. # without doing anything else
  182. if [ "$QEMU_BIN" = "NATIVE" ] ; then
  183. return
  184. fi
  185. # For builds that do not match the host architecture, the correct
  186. # qemu binary will be required.
  187. if ! $QEMU_BIN -version >/dev/null 2>&1; then
  188. die "$QEMU_BIN binary is missing in your system, exiting."
  189. fi
  190. # In order to use the binfmt system the binfmt_misc mountpoint
  191. # must exist inside of proc
  192. if ! mountpoint -q /proc/sys/fs/binfmt_misc ; then
  193. modprobe -q binfmt_misc
  194. mount -t binfmt_misc binfmt_misc /proc/sys/fs/binfmt_misc 2>/dev/null
  195. fi
  196. # Only register if the map is incomplete
  197. if [ ! -f /proc/sys/fs/binfmt_misc/qemu-$_cpu ] ; then
  198. echo ":qemu-$_cpu:M::$_magic:$_mask:$QEMU_BIN:" > /proc/sys/fs/binfmt_misc/register 2>/dev/null
  199. fi
  200. # If the static binary isn't in the chroot then the chroot will
  201. # fail. The kernel knows about the map but without the static
  202. # version there's no interpreter in the chroot, only the
  203. # dynamically linked one in the host. To simplify things we just
  204. # use the static one always and make sure it shows up at the same
  205. # place in the host and the chroot.
  206. if [ ! -x "$ROOTFS/usr/bin/$QEMU_BIN" ] ; then
  207. cp -f "$(which "$QEMU_BIN")" "$ROOTFS/usr/bin" ||
  208. die "Could not install $QEMU_BIN to $ROOTFS/usr/bin/"
  209. fi
  210. }
  211. #
  212. # main()
  213. #
  214. while getopts "C:c:h:r:V" opt; do
  215. case $opt in
  216. C) XBPS_CONFFILE="-C $OPTARG";;
  217. c) XBPS_CACHEDIR="--cachedir=$OPTARG";;
  218. h) usage; exit 0;;
  219. r) XBPS_REPOSITORY="$XBPS_REPOSITORY --repository=$OPTARG";;
  220. V) echo "$PROGNAME @@MKLIVE_VERSION@@"; exit 0;;
  221. esac
  222. done
  223. shift $((OPTIND - 1))
  224. XBPS_TARGET_ARCH="$1"
  225. # This is an aweful hack since the script isn't using privesc
  226. # mechanisms selectively. This is a TODO item.
  227. if [ "$(id -u)" -ne 0 ]; then
  228. die "need root perms to continue, exiting."
  229. fi
  230. # If the arch wasn't set let's bail out now, nothing else in this
  231. # script will work without knowing what we're trying to build for.
  232. if [ -z "$XBPS_TARGET_ARCH" ]; then
  233. echo "$PROGNAME: arch was not set!"
  234. usage; exit 1
  235. fi
  236. # If the repository hasn't already been set, we set it to a sane value
  237. # here. These should all resolve even if they won't have the
  238. # appropriate repodata files for the selected architecture.
  239. : "${XBPS_REPOSITORY:=--repository=http://repo.voidlinux.eu/current \
  240. --repository=http://repo.voidlinux.eu/current/musl \
  241. --repository=http://repo.voidlinux.eu/current/aarch64}"
  242. # The package artifacts are cacheable, but they need to be isolated
  243. # from the host cache.
  244. : "${XBPS_CACHEDIR:=--cachedir=$PWD/xbps-cache/${XBPS_TARGET_ARCH}}"
  245. # The following binaries are required to proceed
  246. for f in chroot tar xbps-install xbps-reconfigure xbps-query; do
  247. if ! which $f >/dev/null ; then
  248. die "$f binary is missing in your system, exiting."
  249. fi
  250. done
  251. # We need to operate on a tempdir, if this fails to create, it is
  252. # absolutely crucial to bail out so that we don't hose the system that
  253. # is running the script.
  254. ROOTFS=$(mktemp -d) || die "failed to create tempdir, exiting..."
  255. # This maintains the chain of trust, the keys in the repo are known to
  256. # be good and so we copy those. Why don't we just use the ones on the
  257. # host system? That's a good point, but there's no promise that the
  258. # system running the script is Void, or that those keys haven't been
  259. # tampered with. Its much easier to use these since the will always
  260. # exist.
  261. mkdir -p "$ROOTFS/var/db/xbps/keys"
  262. cp keys/*.plist "$ROOTFS/var/db/xbps/keys"
  263. # This sets up files that are important for XBPS to work on the new
  264. # filesystem. It does not actually install anything.
  265. run_cmd_target "xbps-install -S $XBPS_CONFFILE $XBPS_CACHEDIR $XBPS_REPOSITORY -r $ROOTFS"
  266. # Later scripts expect the permissions on / to be the canonical 755,
  267. # so we set this here.
  268. chmod 755 "$ROOTFS"
  269. # The pseudofs mountpoints are needed for the qemu support in cases
  270. # where we are running things that aren't natively executable.
  271. mount_pseudofs
  272. # With everything setup, we can now run the install to load the
  273. # base-voidstrap package into the rootfs. This will not produce a
  274. # bootable system but will instead produce a base component that can
  275. # be quickly expanded to perform other actions on.
  276. run_cmd_target "xbps-install -S $XBPS_CONFFILE $XBPS_CACHEDIR $XBPS_REPOSITORY -r $ROOTFS -y base-voidstrap"
  277. # Enable en_US.UTF-8 locale and generate it into the target ROOTFS.
  278. # This is a bit of a hack since some glibc stuff doesn't really work
  279. # correctly without a locale being generated. While some could argue
  280. # that this is an arbitrary or naive choice to enable the en_US
  281. # locale, most people using Void are able to work with the English
  282. # language at least enough to enable thier preferred locale. If this
  283. # truly becomes an issue in the future this hack can be revisited.
  284. if [ -e "$ROOTFS/etc/default/libc-locales" ]; then
  285. LOCALE=en_US.UTF-8
  286. sed -e "s/\#\(${LOCALE}.*\)/\1/g" -i "$ROOTFS/etc/default/libc-locales"
  287. fi
  288. # The reconfigure step needs to execute code that's been compiled for
  289. # the target architecture. Since the target isn't garanteed to be the
  290. # same as the host, this needs to be done via qemu.
  291. info_msg "Reconfiguring packages for ${XBPS_TARGET_ARCH} ..."
  292. case "$XBPS_TARGET_ARCH" in
  293. # TODO: Rather than asserting that x86 code will work, check
  294. # instead if the system that is hosting this script is the same as
  295. # the target, using binfmt if it is not.
  296. i686*|x86_64*)
  297. run_cmd "XBPS_ARCH=${XBPS_TARGET_ARCH} xbps-reconfigure -r $ROOTFS base-files"
  298. ;;
  299. *)
  300. # This step sets up enough of the base-files that the chroot
  301. # will work and they can be reconfigured natively. Without
  302. # this step there isn't enough configured for ld to work.
  303. # This step runs as the host architecture.
  304. run_cmd "xbps-reconfigure -r $ROOTFS base-files"
  305. # Now running as the target system, this step reconfigures the
  306. # base-files completely. Certain things just won't work in
  307. # the first pass, so this cleans up any issues that linger.
  308. run_cmd_chroot "$ROOTFS" "env -i xbps-reconfigure -f base-files"
  309. # TODO: determine why these lines are here. What is the harm
  310. # in having them and what do they remove. Do they interact
  311. # adversely with the alien build support discussed above.
  312. rmdir "$ROOTFS/usr/lib32" 2>/dev/null
  313. rm -f "$ROOTFS/lib32" "$ROOTFS/lib64" "$ROOTFS/usr/lib64"
  314. ;;
  315. esac
  316. # Once base-files is configured and functional its possible to
  317. # configure the rest of the system.
  318. run_cmd_chroot "$ROOTFS" "xbps-reconfigure -a"
  319. # At this point we're done running things in the chroot and we can
  320. # clean up the shims. Failure to do this can result in things hanging
  321. # when we try to delete the tmpdir.
  322. cleanup_chroot
  323. # Set the default password. Previous versions of this script used a
  324. # chroot to do this, but that is unnecessary since chpasswd
  325. # understands how to operate on chroots without actually needing to be
  326. # chrooted. We also remove the lock file in this step to clean up the
  327. # lock on the passwd database, lest it be left in the system and
  328. # propogated to other points.
  329. echo root:voidlinux | chpasswd -c SHA512 --root "$ROOTFS" || die "Could not set default credentials"
  330. rm -f "$ROOTFS/etc/.pwd.lock"
  331. # The cache isn't that useful since by the time the ROOTFS will be
  332. # used it is likely to be out of date. Rather than shipping it around
  333. # only for it to be out of date, we remove it now.
  334. rm -rf "$ROOTFS/var/cache/*" 2>/dev/null
  335. # Finally we can compress the tarball, the name will include the
  336. # architecture and the date on which the tarball was built.
  337. tarball=void-${XBPS_TARGET_ARCH}-ROOTFS-$(date '+%Y%m%d').tar.xz
  338. run_cmd "tar -cp --posix --xattrs -C $ROOTFS . | xz -T0 -9 > $tarball "
  339. # Now that we have the tarball we don't need the rootfs anymore, so we
  340. # can get rid of it.
  341. rm -rf "$ROOTFS"
  342. # Last thing to do before closing out is to let the user know that
  343. # this succeeded. This also ensures that there's something visible
  344. # that the user can look for at the end of the script, which can make
  345. # it easier to see what's going on if something above failed.
  346. info_msg "Successfully created $tarball ($XBPS_TARGET_ARCH)"