mkrootfs.sh.in 6.3 KB

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