create-image.sh 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. #!/usr/bin/env bash
  2. filesystem=ext2
  3. while getopts "f:i" option
  4. do
  5. case $option in
  6. f)
  7. filesystem=$OPTARG
  8. ;;
  9. i)
  10. initramfs=1
  11. ;;
  12. *)
  13. printf "Option not recognized\n"
  14. exit 1
  15. ;;
  16. esac
  17. done
  18. shift $(($OPTIND - 1))
  19. if [ $(id -u) -ne 0 ];then
  20. printf "Installation is only possible as root\n"
  21. exit 1
  22. fi
  23. printf "Checking if mkfs is installed"
  24. mkfs=$(which mkfs.$filesystem)
  25. if [ ! -z $mkfs -a -x $mkfs ];then
  26. printf "...okay\n"
  27. else
  28. printf "...failed\n"
  29. exit 1
  30. fi
  31. printf "Checking if parted is installed"
  32. parted=$(which parted)
  33. if [ ! -z $parted -a -x $parted ];then
  34. printf "...okay\n"
  35. else
  36. printf "...failed\n"
  37. exit 1
  38. fi
  39. printf "Checking if qemu-img is installed"
  40. qimg=$(which qemu-img)
  41. if [ ! -z $qimg -a -x $qimg ];then
  42. printf "...okay\n"
  43. else
  44. printf "...failed\n"
  45. exit 1
  46. fi
  47. if [ -z $1 ];then
  48. printf "Please give the name of the image file\n"
  49. exit 1
  50. fi
  51. if [ -z $initramfs ];then
  52. if [ -z $2 ];then
  53. printf "Please give the name of the openadk archive file\n"
  54. exit 1
  55. fi
  56. else
  57. if [ -z $2 ];then
  58. printf "Please give the full path prefix to kernel/initramfs\n"
  59. exit 1
  60. fi
  61. fi
  62. printf "Generate qemu image (512 MB)\n"
  63. $qimg create -f raw $1 512M >/dev/null
  64. printf "Creating filesystem $filesystem\n"
  65. printf "Create partition and filesystem\n"
  66. $parted -s $1 mklabel msdos
  67. $parted -s $1 -- mkpart primary ext2 0 -0
  68. $parted -s $1 set 1 boot on
  69. offset=$(parted $1 unit b print | tail -2 | head -1 | cut -f 1 --delimit="B" | cut -c 9-)
  70. dd if=$1 of=mbr bs=$offset count=1 2>/dev/null
  71. dd if=$1 skip=$offset of=$1.new 2>/dev/null
  72. if [ "$filesystem" = "ext2" -o "$filesystem" = "ext3" -o "$filesystem" = "ext4" ];then
  73. mkfsopts=-F
  74. fi
  75. mkfs.$filesystem $mkfsopts ${1}.new >/dev/null
  76. if [ $? -eq 0 ];then
  77. printf "Successfully created partition\n"
  78. #$parted $1 print
  79. else
  80. printf "Partition creation failed, Exiting.\n"
  81. exit 1
  82. fi
  83. cat mbr ${1}.new > $1
  84. rm ${1}.new
  85. rm mbr
  86. tmp=$(mktemp -d)
  87. mount -o loop,offset=$offset -t $filesystem $1 $tmp
  88. if [ -z $initramfs ];then
  89. printf "Extracting install archive\n"
  90. tar -C $tmp -xzpf $2
  91. printf "Fixing permissions\n"
  92. chmod 1777 $tmp/tmp
  93. chmod 4755 $tmp/bin/busybox
  94. else
  95. printf "Copying kernel/initramfs\n"
  96. mkdir $tmp/boot $tmp/dev
  97. cp $2-kernel $tmp/boot/kernel
  98. cp $2-initramfs $tmp/boot/initramfs
  99. fi
  100. umount $tmp
  101. printf "Successfully installed.\n"
  102. printf "Be sure $1 is writable for the user which use qemu\n"
  103. exit 0