mkrootfs.sh.in 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. #!/bin/sh
  2. #-
  3. # Copyright (c) 2013-2015 Juan Romero Pardines.
  4. # All rights reserved.
  5. #
  6. # Redistribution and use in source and binary forms, with or without
  7. # modification, are permitted provided that the following conditions
  8. # are met:
  9. # 1. Redistributions of source code must retain the above copyright
  10. # notice, this list of conditions and the following disclaimer.
  11. # 2. Redistributions in binary form must reproduce the above copyright
  12. # notice, this list of conditions and the following disclaimer in the
  13. # documentation and/or other materials provided with the distribution.
  14. #
  15. # THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  16. # IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  17. # OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  18. # IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  19. # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  20. # NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  21. # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  22. # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  23. # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  24. # THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. #-
  26. readonly PROGNAME=$(basename $0)
  27. readonly ARCH=$(uname -m)
  28. trap 'die "Interrupted! exiting..."' INT TERM HUP
  29. info_msg() {
  30. printf "\033[1m$@\n\033[m"
  31. }
  32. die() {
  33. echo "FATAL: $@"
  34. umount_pseudofs
  35. [ -d "$rootfs" ] && rm -rf $rootfs
  36. exit 1
  37. }
  38. usage() {
  39. cat <<_EOF
  40. Usage: $PROGNAME [options] <platform>
  41. Supported platforms: i686, i686-musl, x86_64, x86_64-musl,
  42. dockstar, bananapi, beaglebone, cubieboard2, cubietruck,
  43. odroid-c2, odroid-u2, rpi, rpi2 (armv7), usbarmory, ci20
  44. Options
  45. -b <syspkg> Set an alternative base-system package (defaults to base-system)
  46. -c <dir> Set XBPS cache directory (defaults to \$PWD/xbps-cachedir-<arch>)
  47. -C <file> Full path to the XBPS configuration file
  48. -h Show this help
  49. -p <pkgs> Additional packages to install into the rootfs (separated by blanks)
  50. -r <repo> Set XBPS repository (may be set multiple times)
  51. -k <cmd> Call "cmd <ROOTFSPATH>" after building the rootfs
  52. -V Show version
  53. _EOF
  54. }
  55. mount_pseudofs() {
  56. for f in dev proc sys; do
  57. [ ! -d $rootfs/$f ] && mkdir -p $rootfs/$f
  58. mount -r --bind /$f $rootfs/$f
  59. done
  60. }
  61. umount_pseudofs() {
  62. for f in dev proc sys; do
  63. umount -f $rootfs/$f >/dev/null 2>&1
  64. done
  65. }
  66. run_cmd_target() {
  67. info_msg "Running $@ for target $_ARCH ..."
  68. case "${_TARGET_ARCH}" in
  69. i686*|x86_64*) eval XBPS_ARCH=${_TARGET_ARCH} "$@";;
  70. *) eval XBPS_TARGET_ARCH=${_TARGET_ARCH:=${_ARCH}} "$@";;
  71. esac
  72. [ $? -ne 0 ] && die "Failed to run $@"
  73. }
  74. run_cmd() {
  75. info_msg "Running $@ ..."
  76. eval "$@"
  77. [ $? -ne 0 ] && die "Failed to run $@"
  78. }
  79. register_binfmt() {
  80. if [ "$ARCH" = "${_ARCH}" ]; then
  81. return 0
  82. fi
  83. mountpoint -q /proc/sys/fs/binfmt_misc || modprobe -q binfmt_misc; mount -t binfmt_misc binfmt_misc /proc/sys/fs/binfmt_misc
  84. case "${_ARCH}" in
  85. armv*)
  86. 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
  87. ;;
  88. aarch*)
  89. 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
  90. ;;
  91. mipsel*)
  92. 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
  93. ;;
  94. *)
  95. die "Unknown target architecture!"
  96. ;;
  97. esac
  98. cp -f $(which $QEMU_BIN) $rootfs/usr/bin || die "failed to copy $QEMU_BIN to the rootfs"
  99. }
  100. #
  101. # main()
  102. #
  103. while getopts "b:C:c:hp:r:k:V" opt; do
  104. case $opt in
  105. b) PKGBASE="$OPTARG";;
  106. C) XBPS_CONFFILE="-C $OPTARG";;
  107. c) XBPS_CACHEDIR="--cachedir=$OPTARG";;
  108. h) usage; exit 0;;
  109. p) EXTRA_PKGS="$OPTARG";;
  110. r) XBPS_REPOSITORY="$XBPS_REPOSITORY --repository=$OPTARG";;
  111. k) POST_HOOK="$OPTARG";;
  112. V) echo "$PROGNAME @@MKLIVE_VERSION@@"; exit 0;;
  113. esac
  114. done
  115. shift $(($OPTIND - 1))
  116. PLATFORM="$1"
  117. SUBPLATFORM=$PLATFORM
  118. case "$PLATFORM" in
  119. i686*) _TARGET_ARCH="$PLATFORM"; _ARCH="i686";;
  120. x86_64*) _TARGET_ARCH="$PLATFORM"; _ARCH="x86_64";;
  121. dockstar) _TARGET_ARCH="armv5tel"; _ARCH="armv5tel";;
  122. rpi-musl) _TARGET_ARCH="armv6l-musl"; _ARCH="armv6l";;
  123. rpi) _TARGET_ARCH="armv6l"; _ARCH="armv6l";;
  124. ci20-musl) _TARGET_ARCH="mipselhf-musl"; _ARCH="mipsel-musl";;
  125. ci20) _TARGET_ARCH="mipselhf"; _ARCH="mipsel";;
  126. odroid-c2-musl) _TARGET_ARCH="aarch64-musl"; _ARCH="aarch64";;
  127. odroid-c2) _TARGET_ARCH="aarch64"; _ARCH="aarch64";;
  128. *-musl) _TARGET_ARCH="armv7l-musl"; _ARCH="armv7l";;
  129. *) _TARGET_ARCH="armv7l"; _ARCH="armv7l";;
  130. esac
  131. : ${XBPS_REPOSITORY:=--repository=http://repo.voidlinux.eu/current --repository=http://muslrepo.voidlinux.eu/current}
  132. : ${XBPS_CACHEDIR:=--cachedir=$PWD/xbps-cachedir-${_TARGET_ARCH}}
  133. case "$PLATFORM" in
  134. i686*|x86_64*) PKGBASE="base-voidstrap";;
  135. *) PKGBASE="base-system";;
  136. esac
  137. if [ -z "$PLATFORM" ]; then
  138. echo "$PROGNAME: platform was not set!"
  139. usage; exit 1
  140. fi
  141. case "$PLATFORM" in
  142. bananapi*) SUBPLATFORM=${PLATFORM%-*}; QEMU_BIN=qemu-arm-static;;
  143. beaglebone*) SUBPLATFORM=${PLATFORM%-*}; QEMU_BIN=qemu-arm-static;;
  144. cubieboard2*|cubietruck*) SUBPLATFORM=${PLATFORM%-*}; QEMU_BIN=qemu-arm-static;;
  145. dockstar*) SUBPLATFORM=${PLATFORM%-*}; QEMU_BIN=qemu-arm-static;;
  146. odroid-u2*) SUBPLATFORM=${PLATFORM%-*}; QEMU_BIN=qemu-arm-static;;
  147. rpi2*) SUBPLATFORM=rpi; QEMU_BIN=qemu-arm-static;;
  148. rpi*) SUBPLATFORM=${PLATFORM%-*}; QEMU_BIN=qemu-arm-static;;
  149. usbarmory*) SUBPLATFORM=${PLATFORM%-*}; QEMU_BIN=qemu-arm-static;;
  150. ci20*) SUBPLATFORM=${PLATFORM%-*}; QEMU_BIN=qemu-mipsel-static;;
  151. odroid-c2*) SUBPLATFORM=${PLATFORM%-*}; QEMU_BIN=qemu-aarch64-static;;
  152. i686*) QEMU_BIN=qemu-i386-static;;
  153. x86_64*) QEMU_BIN=qemu-x86_64-static;;
  154. *) die "$PROGNAME: invalid platform!";;
  155. esac
  156. if [ "$(id -u)" -ne 0 ]; then
  157. die "need root perms to continue, exiting."
  158. fi
  159. #
  160. # Check for required binaries.
  161. #
  162. for f in chroot tar xbps-install xbps-reconfigure xbps-query; do
  163. if ! $f --version >/dev/null 2>&1; then
  164. die "$f binary is missing in your system, exiting."
  165. fi
  166. done
  167. if ! $QEMU_BIN -version >/dev/null 2>&1; then
  168. die "$QEMU_BIN binary is missing in your system, exiting."
  169. fi
  170. #
  171. # Check if package base-system is available.
  172. #
  173. rootfs=$(mktemp -d || die "FATAL: failed to create tempdir, exiting...")
  174. mkdir -p $rootfs/var/db/xbps/keys
  175. cp keys/*.plist $rootfs/var/db/xbps/keys
  176. run_cmd_target "xbps-install -S $XBPS_CONFFILE $XBPS_CACHEDIR $XBPS_REPOSITORY -r $rootfs"
  177. run_cmd_target "xbps-query -R -r $rootfs $XBPS_CONFFILE $XBPS_CACHEDIR $XBPS_REPOSITORY -ppkgver $PKGBASE"
  178. chmod 755 $rootfs
  179. case "$PLATFORM" in
  180. i686*|x86_64*) PKGS="${PKGBASE}" ;;
  181. *) PKGS="${PKGBASE} ${SUBPLATFORM}-base" ;;
  182. esac
  183. [ -n "$EXTRA_PKGS" ] && PKGS="${PKGS} ${EXTRA_PKGS}"
  184. mount_pseudofs
  185. #
  186. # Install base-system to the rootfs directory.
  187. #
  188. run_cmd_target "xbps-install -S $XBPS_CONFFILE $XBPS_CACHEDIR $XBPS_REPOSITORY -r $rootfs -y ${PKGS}"
  189. # Enable en_US.UTF-8 locale and generate it into the target rootfs.
  190. if [ -e $rootfs/etc/default/libc-locales ]; then
  191. LOCALE=en_US.UTF-8
  192. sed -e "s/\#\(${LOCALE}.*\)/\1/g" -i $rootfs/etc/default/libc-locales
  193. fi
  194. #
  195. # Reconfigure packages for target architecture: must be reconfigured
  196. # thru the qemu user mode binary.
  197. #
  198. if [ -n "${_ARCH}" ]; then
  199. info_msg "Reconfiguring packages for ${_ARCH} ..."
  200. case "$PLATFORM" in
  201. i686*|x86_64*)
  202. run_cmd "XBPS_ARCH=${PLATFORM} xbps-reconfigure -r $rootfs base-files"
  203. ;;
  204. *)
  205. register_binfmt
  206. run_cmd "xbps-reconfigure -r $rootfs base-files"
  207. run_cmd "chroot $rootfs env -i xbps-reconfigure -f base-files"
  208. rmdir $rootfs/usr/lib32 2>/dev/null
  209. rm -f $rootfs/lib32 $rootfs/lib64 $rootfs/usr/lib64
  210. ;;
  211. esac
  212. run_cmd "chroot $rootfs xbps-reconfigure -a"
  213. fi
  214. #
  215. # Setup default root password.
  216. #
  217. run_cmd "chroot $rootfs sh -c 'echo "root:voidlinux" | chpasswd -c SHA512'"
  218. if [ -n "$POST_HOOK" ]; then
  219. run_cmd "$POST_HOOK $rootfs"
  220. fi
  221. umount_pseudofs
  222. #
  223. # Cleanup rootfs.
  224. #
  225. rm -f $rootfs/etc/.pwd.lock 2>/dev/null
  226. rm -rf $rootfs/var/cache/* 2>/dev/null
  227. #
  228. # Generate final tarball.
  229. #
  230. arch=$ARCH
  231. if [ -n "${_ARCH}" ]; then
  232. rm -f $rootfs/usr/bin/$QEMU_BIN
  233. arch=${_ARCH}
  234. fi
  235. tarball=void-${PLATFORM}-rootfs-$(date '+%Y%m%d').tar.xz
  236. run_cmd "tar -cp --posix -C $rootfs . | xz -T0 -9 > $tarball "
  237. rm -rf $rootfs
  238. info_msg "Successfully created $tarball ($PLATFORM)"
  239. # vim: set ts=4 sw=4 et: