mkrootfs.sh.in 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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] [-V]"
  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:hV" opt; do
  68. case $opt in
  69. a) TARGET_ARCH="$OPTARG";;
  70. h) usage; exit 0;;
  71. V) echo "$PROGNAME @@MKLIVE_VERSION@@"; exit 0;;
  72. esac
  73. done
  74. shift $(($OPTIND - 1))
  75. if [ "$(id -u)" -ne 0 ]; then
  76. die "need root perms to continue, exiting."
  77. fi
  78. #
  79. # Check for required binaries.
  80. #
  81. for f in systemd-nspawn xbps-install xbps-reconfigure xbps-query; do
  82. if ! $f --version >/dev/null 2>&1; then
  83. die "$f binary is missing in your system, exiting."
  84. fi
  85. done
  86. #
  87. # Sanitize target arch.
  88. #
  89. case "$TARGET_ARCH" in
  90. raspberrypi) _ARCH=armv6l; QEMU_BIN=qemu-arm-static;;
  91. *) ;;
  92. esac
  93. #
  94. # Check if package base-system is available.
  95. #
  96. run_cmd "xbps-query -R -ppkgver $PKGBASE"
  97. rootfs=$(mktemp -d || die "FATAL: failed to create tempdir, exiting...")
  98. chmod 755 $rootfs
  99. #
  100. # Install base-system to the rootfs directory.
  101. #
  102. run_cmd "xbps-install -S -r $rootfs -y $PKGBASE"
  103. #
  104. # Reconfigure packages for target architecture: must be reconfigured
  105. # thru the qemu user mode binary.
  106. #
  107. if [ -n "$TARGET_ARCH" ]; then
  108. info_msg "Reconfiguring packages for $TARGET_ARCH ..."
  109. register_binfmt
  110. xbps-reconfigure -r $rootfs base-directories
  111. run_cmd "systemd-nspawn -D $rootfs xbps-reconfigure shadow"
  112. run_cmd "systemd-nspawn -D $rootfs xbps-reconfigure systemd"
  113. run_cmd "systemd-nspawn -D $rootfs xbps-reconfigure -a"
  114. (rm -f $rootfs/lib32; rm -f $rootfs/lib64) >/dev/null 2>&1
  115. else
  116. run_cmd "systemd-nspawn -D $rootfs xbps-reconfigure -f systemd"
  117. fi
  118. #
  119. # Setup default root password.
  120. #
  121. chroot $rootfs sh -c 'echo "root:voidlinux" | chpasswd -c SHA512'
  122. #
  123. # Cleanup rootfs.
  124. #
  125. rm -rf $rootfs/dev/* $rootfs/run/* $rootfs/tmp/* $rootfs/tmp/.* 2>/dev/null
  126. rm -f $rootfs/etc/.pwd.lock 2>/dev/null
  127. rm -rf $rootfs/var/cache/xbps 2>/dev/null
  128. #
  129. # Generate final tarball.
  130. #
  131. arch=$ARCH
  132. if [ -n "$TARGET_ARCH" ]; then
  133. rm -f $rootfs/usr/bin/qemu-*-static
  134. arch=$TARGET_ARCH
  135. fi
  136. tarball=void-${arch}-rootfs-$(date '+%Y%m%d').tar.xz
  137. run_cmd "tar cp -C $rootfs . | xz -9 > $tarball"
  138. rm -rf $rootfs
  139. info_msg "Successfully created $tarball"
  140. # vim: set ts=4 sw=4 et: