mkrootfs.sh.in 5.6 KB

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