create-image.sh 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 98%
  68. $parted -s $1 set 1 boot on
  69. dd if=$1 of=mbr bs=16384 count=1 2>/dev/null
  70. dd if=$1 skip=16384 of=$1.new 2>/dev/null
  71. if [ "$filesystem" = "ext2" -o "$filesystem" = "ext3" -o "$filesystem" = "ext4" ];then
  72. mkfsopts=-F
  73. fi
  74. mkfs.$filesystem $mkfsopts ${1}.new >/dev/null
  75. if [ $? -eq 0 ];then
  76. printf "Successfully created partition\n"
  77. #$parted $1 print
  78. else
  79. printf "Partition creation failed, Exiting.\n"
  80. exit 1
  81. fi
  82. cat mbr ${1}.new > $1
  83. rm ${1}.new
  84. rm mbr
  85. tmp=$(mktemp -d)
  86. mount -o loop,offset=16384 -t $filesystem $1 $tmp
  87. if [ -z $initramfs ];then
  88. printf "Extracting install archive\n"
  89. tar -C $tmp -xzpf $2
  90. printf "Fixing permissions\n"
  91. chmod 1777 $tmp/tmp
  92. chmod 4755 $tmp/bin/busybox
  93. else
  94. printf "Copying kernel/initramfs\n"
  95. mkdir $tmp/boot $tmp/dev
  96. cp $2-kernel $tmp/boot/kernel
  97. cp $2-initramfs $tmp/boot/initramfs
  98. fi
  99. umount $tmp
  100. printf "Successfully installed.\n"
  101. printf "Be sure $1 is writable for the user which use qemu\n"
  102. exit 0