create-disk-from-container.sh 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #!/usr/bin/env bash
  2. set -ve
  3. if [ $(id -u) != "0" ]
  4. then
  5. echo "Please run as root"
  6. exit 1
  7. fi
  8. OUTFILE=$(dirname "$0")/../../images/debian-bench.img
  9. CONTAINER_NAME=debian-bench
  10. dd if=/dev/zero of=$OUTFILE bs=1k count=600k
  11. (
  12. echo o # Create a new empty DOS partition table
  13. echo n # Add a new partition
  14. echo p # Primary partition
  15. echo 1 # Partition number
  16. echo 2048 # First sector
  17. echo # Last sector (Accept default: varies)
  18. echo a # make bootable
  19. echo w # Write changes
  20. echo q # quit
  21. ) | fdisk $OUTFILE
  22. # 1048576 is 2048 (first sector) * 512 (sector size)
  23. mkfs.ext4 -F -E offset=1048576 $OUTFILE
  24. kpartx -a -v $OUTFILE
  25. function finish_kpartx {
  26. kpartx -d $OUTFILE
  27. }
  28. trap finish_kpartx EXIT
  29. # XXX: Assumes loop0
  30. mount /dev/mapper/loop0p1 /mnt
  31. function finish_mount {
  32. umount /mnt
  33. finish_kpartx
  34. }
  35. trap finish_mount EXIT
  36. docker export $CONTAINER_NAME | tar -xvC /mnt/
  37. grub-install --recheck --target=i386-pc --locales= --themes= --fonts= --root-directory /mnt/ /dev/loop0
  38. cat > /mnt/boot/grub/grub.cfg << 'EOF'
  39. set root='hd0' # XXX: I believe this has no significance, but is required to exist by grub
  40. set timeout_style=menu
  41. set timeout=0
  42. menuentry 'Linux' {
  43. #insmod ext2
  44. #insmod gzio
  45. #insmod fat
  46. set root='hd0,msdos1'
  47. echo 'Loading Linux linux ...'
  48. linux /vmlinuz rw root=/dev/sda1 rootfstype=ext4 init=/bin/systemd
  49. #linux /boot/vmlinuz debug verbose rw root=/dev/sda1 rootfstype=ext4
  50. #linux /boot/vmlinuz-virthardened nosplash debug verbose rw root=/dev/sda1 rootfstype=ext4
  51. echo 'Loading initial ramdisk ...'
  52. initrd /initrd.img
  53. #initrd /boot/initramfs-vanilla
  54. #initrd /boot/initramfs-virthardened
  55. }
  56. EOF
  57. echo $OUTFILE created.