mkrootfs.sh.in 7.3 KB

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