mkrootfs.sh.in 5.0 KB

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