mkrootfs.sh.in 5.5 KB

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