void-mkrootfs.sh 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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 'printf "\nInterrupted! exiting...\n"; exit $?' INT TERM HUP
  30. info_msg() {
  31. printf "\033[1m$@\n\033[m"
  32. }
  33. die() {
  34. echo "FATAL: $@"
  35. [ -d "$rootfs" ] && rm -rf $rootfs
  36. exit 1
  37. }
  38. usage() {
  39. echo "Usage: $PROGNAME [-a raspberrypi]"
  40. }
  41. run_cmd() {
  42. info_msg "Running $@ ..."
  43. if [ -n "${_ARCH}" ]; then
  44. eval XBPS_TARGET_ARCH=${_ARCH} "$@" >/dev/null 2>&1
  45. else
  46. eval "$@" >/dev/null 2>&1
  47. fi
  48. [ $? -ne 0 ] && die "Failed to run $@"
  49. }
  50. register_binfmt() {
  51. if [ "$ARCH" = "${_ARCH}" ]; then
  52. return 0
  53. fi
  54. case "${_ARCH}" in
  55. armv6l)
  56. 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
  57. cp -f $(which qemu-arm-static) $rootfs/usr/bin || die "failed to copy qemu-arm-static to the rootfs"
  58. ;;
  59. *)
  60. die "Unknown target architecture!"
  61. ;;
  62. esac
  63. }
  64. #
  65. # main()
  66. #
  67. while getopts "a:h" opt; do
  68. case $opt in
  69. a) TARGET_ARCH="$OPTARG";;
  70. h) usage; exit 0;;
  71. esac
  72. done
  73. shift $(($OPTIND - 1))
  74. if [ "$(id -u)" -ne 0 ]; then
  75. die "need root perms to continue, exiting."
  76. fi
  77. #
  78. # Check for required binaries.
  79. #
  80. for f in systemd-nspawn xbps-install xbps-reconfigure xbps-query; do
  81. if ! $f --version >/dev/null 2>&1; then
  82. die "$f binary is missing in your system, exiting."
  83. fi
  84. done
  85. #
  86. # Sanitize target arch.
  87. #
  88. case "$TARGET_ARCH" in
  89. raspberrypi) _ARCH=armv6l; QEMU_BIN=qemu-arm-static;;
  90. *) ;;
  91. esac
  92. #
  93. # Check if package base-system is available.
  94. #
  95. run_cmd "xbps-query -R -ppkgver $PKGBASE"
  96. rootfs=$(mktemp -d || die "FATAL: failed to create tempdir, exiting...")
  97. chmod 755 $rootfs
  98. #
  99. # Install base-system to the rootfs directory.
  100. #
  101. run_cmd "xbps-install -S -r $rootfs -y $PKGBASE"
  102. #
  103. # Reconfigure packages for target architecture: must be reconfigured
  104. # thru the qemu user mode binary.
  105. #
  106. if [ -n "$TARGET_ARCH" ]; then
  107. info_msg "Reconfiguring packages for $TARGET_ARCH ..."
  108. register_binfmt
  109. xbps-reconfigure -r $rootfs base-directories
  110. run_cmd "systemd-nspawn -D $rootfs xbps-reconfigure shadow"
  111. run_cmd "systemd-nspawn -D $rootfs xbps-reconfigure systemd"
  112. run_cmd "systemd-nspawn -D $rootfs xbps-reconfigure -a"
  113. (rm -f $rootfs/lib32; rm -f $rootfs/lib64) >/dev/null 2>&1
  114. else
  115. run_cmd "systemd-nspawn -D $rootfs xbps-reconfigure -f systemd"
  116. fi
  117. #
  118. # Setup default root password.
  119. #
  120. chroot $rootfs sh -c 'echo "root:voidlinux" | chpasswd -c SHA512'
  121. #
  122. # Cleanup rootfs.
  123. #
  124. rm -rf $rootfs/dev/* $rootfs/run/* $rootfs/tmp/* $rootfs/tmp/.* 2>/dev/null
  125. rm -f $rootfs/etc/.pwd.lock 2>/dev/null
  126. rm -rf $rootfs/var/cache/xbps 2>/dev/null
  127. #
  128. # Generate final tarball.
  129. #
  130. arch=$ARCH
  131. if [ -n "$TARGET_ARCH" ]; then
  132. rm -f $rootfs/usr/bin/qemu-*-static
  133. arch=$TARGET_ARCH
  134. fi
  135. tarball=void-${arch}-rootfs-$(date '+%Y%m%d').tar.xz
  136. run_cmd "tar cp -C $rootfs . | xz -9 > $tarball"
  137. rm -rf $rootfs
  138. info_msg "Successfully created $tarball"
  139. # vim: set ts=4 sw=4 et: