build-image-qemu.sh 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #!/bin/sh
  2. set -e
  3. die() {
  4. echo "die: $*"
  5. exit 1
  6. }
  7. if [ "$(id -u)" != 0 ]; then
  8. die "this script needs to run as root (or with fakeroot)"
  9. fi
  10. if [ "$(uname -s)" = "Darwin" ]; then
  11. export PATH="/usr/local/opt/e2fsprogs/bin:$PATH"
  12. export PATH="/usr/local/opt/e2fsprogs/sbin:$PATH"
  13. fi
  14. echo "setting up disk image..."
  15. qemu-img create _disk_image "${DISK_SIZE:-600}"m || die "could not create disk image"
  16. chown "$SUDO_UID":"$SUDO_GID" _disk_image || die "could not adjust permissions on disk image"
  17. echo "done"
  18. use_genext2fs=0
  19. if command -v genext2fs 1>/dev/null ; then
  20. use_genext2fs=1
  21. else
  22. printf "creating new filesystem... "
  23. if [ "$(uname -s)" = "OpenBSD" ]; then
  24. VND=$(vnconfig _disk_image)
  25. (echo "e 0"; echo 83; echo n; echo 0; echo "*"; echo "quit") | fdisk -e "$VND"
  26. mkfs.ext2 -I 128 -F "/dev/${VND}i" || die "could not create filesystem"
  27. elif [ "$(uname -s)" = "FreeBSD" ]; then
  28. MD=$(mdconfig _disk_image)
  29. mke2fs -q -I 128 _disk_image || die "could not create filesystem"
  30. else
  31. if [ -x /sbin/mke2fs ]; then
  32. /sbin/mke2fs -q -I 128 _disk_image || die "could not create filesystem"
  33. else
  34. mke2fs -q -I 128 _disk_image || die "could not create filesystem"
  35. fi
  36. fi
  37. echo "done"
  38. printf "mounting filesystem... "
  39. mkdir -p mnt
  40. if [ "$(uname -s)" = "Darwin" ]; then
  41. fuse-ext2 _disk_image mnt -o rw+,allow_other,uid=501,gid=20 || die "could not mount filesystem"
  42. elif [ "$(uname -s)" = "OpenBSD" ]; then
  43. mount -t ext2fs "/dev/${VND}i" mnt/ || die "could not mount filesystem"
  44. elif [ "$(uname -s)" = "FreeBSD" ]; then
  45. fuse-ext2 -o rw+ "/dev/${MD}" mnt/ || die "could not mount filesystem"
  46. else
  47. mount _disk_image mnt/ || die "could not mount filesystem"
  48. fi
  49. echo "done"
  50. fi
  51. cleanup() {
  52. if [ -d mnt ]; then
  53. if [ $use_genext2fs = 0 ] ; then
  54. printf "unmounting filesystem... "
  55. umount mnt || ( sleep 1 && sync && umount mnt )
  56. fi
  57. rm -rf mnt
  58. if [ $use_genext2fs = 0 ] ; then
  59. if [ "$(uname -s)" = "OpenBSD" ]; then
  60. vnconfig -u "$VND"
  61. elif [ "$(uname -s)" = "FreeBSD" ]; then
  62. mdconfig -d -u "$MD"
  63. fi
  64. fi
  65. echo "done"
  66. fi
  67. }
  68. trap cleanup EXIT
  69. ./build-root-filesystem.sh
  70. if [ $use_genext2fs = 1 ]; then
  71. # regenerate new image, since genext2fs is unable to reuse the previously written image.
  72. # genext2fs is very slow in generating big images, so I use a smaller image here. size can be updated
  73. # if it's not enough.
  74. # not using "-i 128" since it hangs. Serenity handles whatever default this uses instead.
  75. printf "generating filesystem... "
  76. genext2fs -b 250000 -d mnt _disk_image || die "try increasing image size (genext2fs -b)"
  77. echo "done"
  78. # if using docker with shared mount, file is created as root, so make it writable for users
  79. chmod 0666 _disk_image
  80. fi