mkrootfs.sh.in 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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. readonly VOIDRSAPUBKEY="60:ae:0c:d6:f0:95:17:80:bc:93:46:7a:89:af:a3:2d"
  29. trap 'die "Interrupted! exiting..."' INT TERM HUP
  30. info_msg() {
  31. printf "\033[1m$@\n\033[m"
  32. }
  33. die() {
  34. echo "FATAL: $@"
  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: 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. -k <dir> Full path to a directory with the XBPS RSA public keys for repositories
  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. -V Show version
  52. _EOF
  53. }
  54. mount_pseudofs() {
  55. for f in dev proc sys; do
  56. [ ! -d $rootfs/$f ] && mkdir -p $rootfs/$f
  57. mount -r --bind /$f $rootfs/$f
  58. done
  59. }
  60. umount_pseudofs() {
  61. for f in dev proc sys; do
  62. umount -f $rootfs/$f >/dev/null 2>&1
  63. done
  64. }
  65. run_cmd_target() {
  66. info_msg "Running $@ for target $_ARCH ..."
  67. eval XBPS_TARGET_ARCH=${_ARCH} "$@"
  68. [ $? -ne 0 ] && die "Failed to run $@"
  69. }
  70. run_cmd() {
  71. info_msg "Running $@ ..."
  72. eval "$@"
  73. [ $? -ne 0 ] && die "Failed to run $@"
  74. }
  75. register_binfmt() {
  76. if [ "$ARCH" = "${_ARCH}" ]; then
  77. return 0
  78. fi
  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_CACHEDIR:=--cachedir=/var/cache/xbps}
  90. : ${PKGBASE:=base-system}
  91. #
  92. # main()
  93. #
  94. while getopts "b:C:c:hk:p:r:V" opt; do
  95. case $opt in
  96. b) PKGBASE="$OPTARG";;
  97. C) XBPS_CONFFILE="-C $OPTARG";;
  98. c) XBPS_CACHEDIR="--cachedir=$OPTARG";;
  99. h) usage; exit 0;;
  100. k) KEYSDIR="$OPTARG";;
  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. cubieboard2) _ARCH="armv7l"; QEMU_BIN=qemu-arm-static;;
  114. odroid-u2) _ARCH="armv7l"; QEMU_BIN=qemu-arm-static;;
  115. rpi) _ARCH="armv6l"; QEMU_BIN=qemu-arm-static;;
  116. *) die "$PROGNAME: invalid platform!";;
  117. esac
  118. if [ "$(id -u)" -ne 0 ]; then
  119. die "need root perms to continue, exiting."
  120. fi
  121. #
  122. # Check for required binaries.
  123. #
  124. for f in chroot tar xbps-install xbps-reconfigure xbps-query; do
  125. if ! $f --version >/dev/null 2>&1; then
  126. die "$f binary is missing in your system, exiting."
  127. fi
  128. done
  129. if [ -z "$KEYSDIR" ]; then
  130. KEYSDIR="/var/db/xbps/keys" # use void default
  131. fi
  132. #
  133. # Check if package base-system is available.
  134. #
  135. rootfs=$(mktemp -d || die "FATAL: failed to create tempdir, exiting...")
  136. if [ ! -f "$KEYSDIR/${VOIDRSAPUBKEY}.plist" ]; then
  137. die "cannot find xbps keys to install binary packages, exiting."
  138. fi
  139. mkdir -p $rootfs/var/db/xbps/keys
  140. cp $KEYSDIR/${VOIDRSAPUBKEY}.plist $rootfs/var/db/xbps/keys
  141. run_cmd_target "xbps-install -S $XBPS_CONFFILE $XBPS_CACHEDIR $XBPS_REPOSITORY -r $rootfs"
  142. run_cmd_target "xbps-query -R -r $rootfs $XBPS_CONFFILE $XBPS_CACHEDIR $XBPS_REPOSITORY -ppkgver $PKGBASE"
  143. chmod 755 $rootfs
  144. PKGS="${PKGBASE} ${PLATFORM}-base"
  145. [ -n "$EXTRA_PKGS" ] && PKGS="${PKGS} ${EXTRA_PKGS}"
  146. mount_pseudofs
  147. #
  148. # Install base-system to the rootfs directory.
  149. #
  150. run_cmd_target "xbps-install -S $XBPS_CONFFILE $XBPS_CACHEDIR $XBPS_REPOSITORY -r $rootfs -y ${PKGS}"
  151. # Enable en_US.UTF-8 locale and generate it into the target rootfs.
  152. LOCALE=en_US.UTF-8
  153. sed -e "s/\#\(${LOCALE}.*\)/\1/g" -i $rootfs/etc/default/libc-locales
  154. #
  155. # Reconfigure packages for target architecture: must be reconfigured
  156. # thru the qemu user mode binary.
  157. #
  158. if [ -n "${_ARCH}" ]; then
  159. info_msg "Reconfiguring packages for ${_ARCH} ..."
  160. register_binfmt
  161. run_cmd "xbps-reconfigure -r $rootfs base-directories"
  162. run_cmd "chroot $rootfs xbps-reconfigure shadow"
  163. if [ "$PKGBASE" = "base-system" ]; then
  164. run_cmd "chroot $rootfs xbps-reconfigure systemd"
  165. fi
  166. run_cmd "chroot $rootfs xbps-reconfigure -a"
  167. rmdir $rootfs/usr/lib32
  168. rm -f $rootfs/lib32 $rootfs/lib64 $rootfs/usr/lib64
  169. else
  170. if [ "$PKGBASE" = "base-system" ]; then
  171. run_cmd "chroot $rootfs xbps-reconfigure systemd"
  172. fi
  173. fi
  174. #
  175. # Setup default root password.
  176. #
  177. run_cmd "chroot $rootfs sh -c 'echo "root:voidlinux" | chpasswd -c SHA512'"
  178. umount_pseudofs
  179. #
  180. # Cleanup rootfs.
  181. #
  182. rm -f $rootfs/etc/.pwd.lock 2>/dev/null
  183. rm -rf $rootfs/var/cache/* 2>/dev/null
  184. #
  185. # Generate final tarball.
  186. #
  187. arch=$ARCH
  188. if [ -n "$TARGET_ARCH" ]; then
  189. rm -f $rootfs/usr/bin/qemu-*-static
  190. arch=$TARGET_ARCH
  191. fi
  192. tarball=void-${PLATFORM}-rootfs-$(date '+%Y%m%d').tar.xz
  193. run_cmd "tar cp -C $rootfs . | xz -9 > $tarball"
  194. rm -rf $rootfs
  195. info_msg "Successfully created $tarball ($PLATFORM)"
  196. # vim: set ts=4 sw=4 et: