mkrootfs.sh.in 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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 PKGBASE="base-system"
  29. readonly VOIDRSAPUBKEY="60:ae:0c:d6:f0:95:17:80:bc:93:46:7a:89:af:a3:2d"
  30. trap 'die "Interrupted! exiting..."' INT TERM HUP
  31. info_msg() {
  32. printf "\033[1m$@\n\033[m"
  33. }
  34. die() {
  35. echo "FATAL: $@"
  36. umount_pseudofs
  37. [ -d "$rootfs" ] && rm -rf $rootfs
  38. exit 1
  39. }
  40. usage() {
  41. cat <<_EOF
  42. Usage: $PROGNAME [options] <platform>
  43. Supported platforms: cubieboard2, odroid-u2, rpi
  44. Options
  45. -C <file> Full path to the XBPS configuration file
  46. -h Show this help
  47. -k <dir> Full path to a directory with the XBPS RSA public keys for repositories
  48. -p <pkgs> Additional packages to install into the rootfs (separated by blanks)
  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. #
  88. # main()
  89. #
  90. while getopts "C:hk:p:V" opt; do
  91. case $opt in
  92. C) XBPS_CONFFILE="-C $OPTARG";;
  93. h) usage; exit 0;;
  94. k) KEYSDIR="$OPTARG";;
  95. p) EXTRA_PKGS="$OPTARG";;
  96. V) echo "$PROGNAME @@MKLIVE_VERSION@@"; exit 0;;
  97. esac
  98. done
  99. shift $(($OPTIND - 1))
  100. PLATFORM="$1"
  101. if [ -z "$PLATFORM" ]; then
  102. echo "$PROGNAME: platform was not set!"
  103. usage; exit 1
  104. fi
  105. case "$PLATFORM" in
  106. cubieboard2) _ARCH="armv7l"; QEMU_BIN=qemu-arm-static;;
  107. odroid-u2) _ARCH="armv7l"; QEMU_BIN=qemu-arm-static;;
  108. rpi) _ARCH="armv6l"; QEMU_BIN=qemu-arm-static;;
  109. *) die "$PROGNAME: invalid platform!";;
  110. esac
  111. if [ "$(id -u)" -ne 0 ]; then
  112. die "need root perms to continue, exiting."
  113. fi
  114. #
  115. # Check for required binaries.
  116. #
  117. for f in chroot tar xbps-install xbps-reconfigure xbps-query; do
  118. if ! $f --version >/dev/null 2>&1; then
  119. die "$f binary is missing in your system, exiting."
  120. fi
  121. done
  122. if [ -z "$KEYSDIR" ]; then
  123. KEYSDIR="/var/db/xbps/keys" # use void default
  124. fi
  125. #
  126. # Check if package base-system is available.
  127. #
  128. rootfs=$(mktemp -d || die "FATAL: failed to create tempdir, exiting...")
  129. if [ ! -f "$KEYSDIR/${VOIDRSAPUBKEY}.plist" ]; then
  130. die "cannot find xbps keys to install binary packages, exiting."
  131. fi
  132. mkdir -p $rootfs/var/db/xbps/keys
  133. cp $KEYSDIR/${VOIDRSAPUBKEY}.plist $rootfs/var/db/xbps/keys
  134. run_cmd "xbps-install -S $XBPS_CONFFILE -r $rootfs"
  135. run_cmd "xbps-query -R -r $rootfs $XBPS_CONFFILE -ppkgver $PKGBASE"
  136. chmod 755 $rootfs
  137. PKGS="${PKGBASE} ${PLATFORM}-base"
  138. [ -n "$EXTRA_PKGS" ] && PKGS="${PKGS} ${EXTRA_PKGS}"
  139. mount_pseudofs
  140. #
  141. # Install base-system to the rootfs directory.
  142. #
  143. run_cmd_target "xbps-install -S $XBPS_CONFFILE -r $rootfs -y ${PKGS}"
  144. # Enable en_US.UTF-8 locale and generate it into the target rootfs.
  145. LOCALE=en_US.UTF-8
  146. sed -e "s/\#\(${LOCALE}.*\)/\1/g" -i $rootfs/etc/default/libc-locales
  147. #
  148. # Reconfigure packages for target architecture: must be reconfigured
  149. # thru the qemu user mode binary.
  150. #
  151. if [ -n "${_ARCH}" ]; then
  152. info_msg "Reconfiguring packages for ${_ARCH} ..."
  153. register_binfmt
  154. run_cmd "xbps-reconfigure -r $rootfs base-directories"
  155. run_cmd "chroot $rootfs xbps-reconfigure shadow"
  156. run_cmd "chroot $rootfs xbps-reconfigure systemd"
  157. run_cmd "chroot $rootfs xbps-reconfigure -a"
  158. rmdir $rootfs/usr/lib32
  159. rm -f $rootfs/lib32 $rootfs/lib64 $rootfs/usr/lib64
  160. else
  161. run_cmd "chroot $rootfs xbps-reconfigure systemd"
  162. fi
  163. #
  164. # Setup default root password.
  165. #
  166. run_cmd "chroot $rootfs sh -c 'echo "root:voidlinux" | chpasswd -c SHA512'"
  167. umount_pseudofs
  168. #
  169. # Cleanup rootfs.
  170. #
  171. rm -f $rootfs/etc/.pwd.lock 2>/dev/null
  172. rm -rf $rootfs/var/cache/xbps 2>/dev/null
  173. #
  174. # Generate final tarball.
  175. #
  176. arch=$ARCH
  177. if [ -n "$TARGET_ARCH" ]; then
  178. rm -f $rootfs/usr/bin/qemu-*-static
  179. arch=$TARGET_ARCH
  180. fi
  181. tarball=void-${PLATFORM}-rootfs-$(date '+%Y%m%d').tar.xz
  182. run_cmd "tar cp -C $rootfs . | xz -9 > $tarball"
  183. rm -rf $rootfs
  184. info_msg "Successfully created $tarball ($PLATFORM)"
  185. # vim: set ts=4 sw=4 et: