build-image-qemu.sh 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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"
  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. printf "creating new filesystem... "
  19. if [ "$(uname -s)" = "OpenBSD" ]; then
  20. VND=$(vnconfig _disk_image)
  21. (echo "e 0"; echo 83; echo n; echo 0; echo "*"; echo "quit") | fdisk -e "$VND"
  22. mkfs.ext2 -I 128 -F "/dev/${VND}i" || die "could not create filesystem"
  23. else
  24. if [ -x /sbin/mke2fs ]; then
  25. /sbin/mke2fs -q -I 128 _disk_image || die "could not create filesystem"
  26. else
  27. mke2fs -q -I 128 _disk_image || die "could not create filesystem"
  28. fi
  29. fi
  30. echo "done"
  31. printf "mounting filesystem... "
  32. mkdir -p mnt
  33. if [ "$(uname -s)" = "Darwin" ]; then
  34. fuse-ext2 _disk_image mnt -o rw+,allow_other,uid=501,gid=20 || die "could not mount filesystem"
  35. elif [ "$(uname -s)" = "OpenBSD" ]; then
  36. mount -t ext2fs "/dev/${VND}i" mnt/ || die "could not mount filesystem"
  37. else
  38. mount _disk_image mnt/ || die "could not mount filesystem"
  39. fi
  40. echo "done"
  41. cleanup() {
  42. if [ -d mnt ]; then
  43. printf "unmounting filesystem... "
  44. umount mnt || ( sleep 1 && sync && umount mnt )
  45. rm -rf mnt
  46. if [ "$(uname -s)" = "OpenBSD" ]; then
  47. vnconfig -u "$VND"
  48. fi
  49. echo "done"
  50. fi
  51. }
  52. trap cleanup EXIT
  53. ./build-root-filesystem.sh