mkrootfs.sh.in 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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. usage() {
  40. cat <<_EOF
  41. Usage: $PROGNAME [options] <platform>
  42. Supported platforms: i686, i686-musl, x86_64, x86_64-musl, GCP, GCP-musl
  43. dockstar, bananapi, beaglebone, cubieboard2, cubietruck,
  44. odroid-c2, odroid-u2, rpi, rpi2 (armv7), rpi3 (aarch64),
  45. usbarmory, ci20
  46. Options
  47. -b <syspkg> Set an alternative base-system package (defaults to base-system)
  48. -c <dir> Set XBPS cache directory (defaults to \$PWD/xbps-cachedir-<arch>)
  49. -C <file> Full path to the XBPS configuration file
  50. -h Show this help
  51. -p <pkgs> Additional packages to install into the ROOTFS (separated by blanks)
  52. -r <repo> Set XBPS repository (may be set multiple times)
  53. -k <cmd> Call "cmd <ROOTFSPATH>" after building the ROOTFS
  54. -V Show version
  55. _EOF
  56. }
  57. mount_pseudofs() {
  58. for f in dev proc sys; do
  59. [ ! -d "$ROOTFS/$f" ] && mkdir -p "$ROOTFS/$f"
  60. mount -r --bind /$f "$ROOTFS/$f"
  61. done
  62. }
  63. umount_pseudofs() {
  64. if [ -d "${ROOTFS}" ]; then
  65. for f in dev proc sys; do
  66. umount -f "$ROOTFS/$f" >/dev/null 2>&1
  67. done
  68. fi
  69. }
  70. run_cmd_target() {
  71. info_msg "Running $* for target $_ARCH ..."
  72. case "${_TARGET_ARCH}" in
  73. i686*|x86_64*) eval XBPS_ARCH="${_TARGET_ARCH}" "$@";;
  74. *) eval XBPS_TARGET_ARCH="${_TARGET_ARCH:=${_ARCH}}" "$@";;
  75. esac
  76. [ $? -ne 0 ] && die "Failed to run $*"
  77. }
  78. run_cmd() {
  79. info_msg "Running $* ..."
  80. eval "$@"
  81. [ $? -ne 0 ] && die "Failed to run $*"
  82. }
  83. register_binfmt() {
  84. if [ "$ARCH" = "${_ARCH}" ]; then
  85. return 0
  86. fi
  87. mountpoint -q /proc/sys/fs/binfmt_misc || modprobe -q binfmt_misc; mount -t binfmt_misc binfmt_misc /proc/sys/fs/binfmt_misc
  88. case "${_ARCH}" in
  89. armv*)
  90. 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
  91. ;;
  92. aarch*)
  93. echo ':qemu-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
  94. ;;
  95. mipsel*)
  96. 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
  97. ;;
  98. *)
  99. die "Unknown target architecture!"
  100. ;;
  101. esac
  102. cp -f "$(which "$QEMU_BIN")" "$ROOTFS/usr/bin" || die "failed to copy $QEMU_BIN to the ROOTFS"
  103. }
  104. #
  105. # main()
  106. #
  107. while getopts "b:C:c:hp:r:k:V" opt; do
  108. case $opt in
  109. b) PKGBASE="$OPTARG";;
  110. C) XBPS_CONFFILE="-C $OPTARG";;
  111. c) XBPS_CACHEDIR="--cachedir=$OPTARG";;
  112. h) usage; exit 0;;
  113. p) EXTRA_PKGS="$OPTARG";;
  114. r) XBPS_REPOSITORY="$XBPS_REPOSITORY --repository=$OPTARG";;
  115. k) POST_HOOK="$OPTARG";;
  116. V) echo "$PROGNAME @@MKLIVE_VERSION@@"; exit 0;;
  117. esac
  118. done
  119. shift $((OPTIND - 1))
  120. PLATFORM="$1"
  121. SUBPLATFORM=$PLATFORM
  122. case "$PLATFORM" in
  123. i686*) _TARGET_ARCH="$PLATFORM"; _ARCH="i686";;
  124. x86_64*) _TARGET_ARCH="$PLATFORM"; _ARCH="x86_64";;
  125. GCP) _TARGET_ARCH="x86_64"; _ARCH="x86_64";;
  126. GCP-musl) _TARGET_ARCH="x86_64-musl"; _ARCH="x86_64";;
  127. dockstar) _TARGET_ARCH="armv5tel"; _ARCH="armv5tel";;
  128. rpi-musl) _TARGET_ARCH="armv6l-musl"; _ARCH="armv6l";;
  129. rpi) _TARGET_ARCH="armv6l"; _ARCH="armv6l";;
  130. rpi3-musl) _TARGET_ARCH="aarch64-musl"; _ARCH="aarch64";;
  131. rpi3) _TARGET_ARCH="aarch64"; _ARCH="aarch64";;
  132. ci20-musl) _TARGET_ARCH="mipselhf-musl"; _ARCH="mipsel-musl";;
  133. ci20) _TARGET_ARCH="mipselhf"; _ARCH="mipsel";;
  134. odroid-c2-musl) _TARGET_ARCH="aarch64-musl"; _ARCH="aarch64";;
  135. odroid-c2) _TARGET_ARCH="aarch64"; _ARCH="aarch64";;
  136. *-musl) _TARGET_ARCH="armv7l-musl"; _ARCH="armv7l";;
  137. *) _TARGET_ARCH="armv7l"; _ARCH="armv7l";;
  138. esac
  139. : "${XBPS_REPOSITORY:=--repository=http://repo.voidlinux.eu/current \
  140. --repository=http://repo.voidlinux.eu/current/musl \
  141. --repository=http://repo.voidlinux.eu/current/aarch64}"
  142. : "${XBPS_CACHEDIR:=--cachedir=$PWD/xbps-cachedir-${_TARGET_ARCH}}"
  143. case "$PLATFORM" in
  144. i686*|x86_64*) PKGBASE="base-voidstrap";;
  145. *) PKGBASE="base-system";;
  146. esac
  147. if [ -z "$PLATFORM" ]; then
  148. echo "$PROGNAME: platform was not set!"
  149. usage; exit 1
  150. fi
  151. case "$PLATFORM" in
  152. bananapi*) SUBPLATFORM=${PLATFORM%-*}; QEMU_BIN=qemu-arm-static;;
  153. beaglebone*) SUBPLATFORM=${PLATFORM%-*}; QEMU_BIN=qemu-arm-static;;
  154. cubieboard2*|cubietruck*) SUBPLATFORM=${PLATFORM%-*}; QEMU_BIN=qemu-arm-static;;
  155. dockstar*) SUBPLATFORM=${PLATFORM%-*}; QEMU_BIN=qemu-arm-static;;
  156. odroid-u2*) SUBPLATFORM=${PLATFORM%-*}; QEMU_BIN=qemu-arm-static;;
  157. rpi3*) SUBPLATFORM=rpi3; QEMU_BIN=qemu-aarch64-static;;
  158. rpi2*) SUBPLATFORM=rpi; QEMU_BIN=qemu-arm-static;;
  159. rpi*) SUBPLATFORM=${PLATFORM%-*}; QEMU_BIN=qemu-arm-static;;
  160. usbarmory*) SUBPLATFORM=${PLATFORM%-*}; QEMU_BIN=qemu-arm-static;;
  161. ci20*) SUBPLATFORM=${PLATFORM%-*}; QEMU_BIN=qemu-mipsel-static;;
  162. odroid-c2*) SUBPLATFORM=${PLATFORM%-musl}; QEMU_BIN=qemu-aarch64-static;;
  163. i686*) QEMU_BIN=qemu-i386-static;;
  164. x86_64*) QEMU_BIN=qemu-x86_64-static;;
  165. GCP*) SUBPLATFORM=${PLATFORM%-*}; QEMU_BIN=qemu-x86_64-static;;
  166. *) die "$PROGNAME: invalid platform!";;
  167. esac
  168. if [ "$(id -u)" -ne 0 ]; then
  169. die "need root perms to continue, exiting."
  170. fi
  171. #
  172. # Check for required binaries.
  173. #
  174. for f in chroot tar xbps-install xbps-reconfigure xbps-query; do
  175. if ! $f --version >/dev/null 2>&1; then
  176. die "$f binary is missing in your system, exiting."
  177. fi
  178. done
  179. if ! $QEMU_BIN -version >/dev/null 2>&1; then
  180. die "$QEMU_BIN binary is missing in your system, exiting."
  181. fi
  182. #
  183. # Check if package base-system is available.
  184. #
  185. ROOTFS=$(mktemp -d) || die "failed to create tempdir, exiting..."
  186. mkdir -p "$ROOTFS/var/db/xbps/keys"
  187. cp keys/*.plist "$ROOTFS/var/db/xbps/keys"
  188. run_cmd_target "xbps-install -S $XBPS_CONFFILE $XBPS_CACHEDIR $XBPS_REPOSITORY -r $ROOTFS"
  189. run_cmd_target "xbps-query -R -r $ROOTFS $XBPS_CONFFILE $XBPS_CACHEDIR $XBPS_REPOSITORY -ppkgver $PKGBASE"
  190. chmod 755 "$ROOTFS"
  191. case "$PLATFORM" in
  192. i686*|x86_64*) PKGS="${PKGBASE}" ;;
  193. *) PKGS="${PKGBASE} ${SUBPLATFORM}-base" ;;
  194. esac
  195. [ -n "$EXTRA_PKGS" ] && PKGS="${PKGS} ${EXTRA_PKGS}"
  196. mount_pseudofs
  197. #
  198. # Install base-system to the ROOTFS directory.
  199. #
  200. run_cmd_target "xbps-install -S $XBPS_CONFFILE $XBPS_CACHEDIR $XBPS_REPOSITORY -r $ROOTFS -y ${PKGS}"
  201. # Enable en_US.UTF-8 locale and generate it into the target ROOTFS.
  202. if [ -e "$ROOTFS/etc/default/libc-locales" ]; then
  203. LOCALE=en_US.UTF-8
  204. sed -e "s/\#\(${LOCALE}.*\)/\1/g" -i "$ROOTFS/etc/default/libc-locales"
  205. fi
  206. #
  207. # Reconfigure packages for target architecture: must be reconfigured
  208. # thru the qemu user mode binary.
  209. #
  210. if [ -n "${_ARCH}" ]; then
  211. info_msg "Reconfiguring packages for ${_ARCH} ..."
  212. case "$PLATFORM" in
  213. i686*|x86_64*|GCP*)
  214. run_cmd "XBPS_ARCH=${PLATFORM} xbps-reconfigure -r $ROOTFS base-files"
  215. ;;
  216. *)
  217. register_binfmt
  218. run_cmd "xbps-reconfigure -r $ROOTFS base-files"
  219. run_cmd "chroot $ROOTFS env -i xbps-reconfigure -f base-files"
  220. rmdir "$ROOTFS/usr/lib32" 2>/dev/null
  221. rm -f "$ROOTFS/lib32" "$ROOTFS/lib64" "$ROOTFS/usr/lib64"
  222. ;;
  223. esac
  224. run_cmd "chroot $ROOTFS xbps-reconfigure -a"
  225. fi
  226. #
  227. # Setup default root password.
  228. #
  229. run_cmd "chroot $ROOTFS sh -c 'echo root:voidlinux | chpasswd -c SHA512'"
  230. if [ -n "$POST_HOOK" ]; then
  231. run_cmd "$POST_HOOK $ROOTFS"
  232. fi
  233. umount_pseudofs
  234. #
  235. # Cleanup ROOTFS.
  236. #
  237. rm -f "$ROOTFS/etc/.pwd.lock" 2>/dev/null
  238. rm -rf "$ROOTFS/var/cache/*" 2>/dev/null
  239. #
  240. # Generate final tarball.
  241. #
  242. if [ -n "${_ARCH}" ]; then
  243. rm -f "$ROOTFS/usr/bin/$QEMU_BIN"
  244. fi
  245. tarball=void-${PLATFORM}-ROOTFS-$(date '+%Y%m%d').tar.xz
  246. run_cmd "tar -cp --posix --xattrs -C $ROOTFS . | xz -T0 -9 > $tarball "
  247. rm -rf "$ROOTFS"
  248. info_msg "Successfully created $tarball ($PLATFORM)"
  249. # vim: set ts=4 sw=4 et: