create-image.sh 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. #!/usr/bin/env bash
  2. #-
  3. # Copyright © 2010-2012
  4. # Waldemar Brodkorb <wbx@openadk.org>
  5. #
  6. # Provided that these terms and disclaimer and all copyright notices
  7. # are retained or reproduced in an accompanying document, permission
  8. # is granted to deal in this work without restriction, including un‐
  9. # limited rights to use, publicly perform, distribute, sell, modify,
  10. # merge, give away, or sublicence.
  11. #
  12. # This work is provided “AS IS” and WITHOUT WARRANTY of any kind, to
  13. # the utmost extent permitted by applicable law, neither express nor
  14. # implied; without malicious intent or gross negligence. In no event
  15. # may a licensor, author or contributor be held liable for indirect,
  16. # direct, other damage, loss, or other issues arising in any way out
  17. # of dealing in the work, even if advised of the possibility of such
  18. # damage or existence of a defect, except proven that it results out
  19. # of said person’s immediate fault when using the work as intended.
  20. #
  21. # Alternatively, this work may be distributed under the terms of the
  22. # General Public License, any version, as published by the Free Soft-
  23. # ware Foundation.
  24. filesystem=ext2
  25. while getopts "f:i" option
  26. do
  27. case $option in
  28. f)
  29. filesystem=$OPTARG
  30. ;;
  31. i)
  32. initramfs=1
  33. ;;
  34. *)
  35. printf "Option not recognized\n"
  36. exit 1
  37. ;;
  38. esac
  39. done
  40. shift $(($OPTIND - 1))
  41. if [ $(id -u) -ne 0 ];then
  42. printf "Installation is only possible as root\n"
  43. exit 1
  44. fi
  45. tools='qemu-img'
  46. ostype=$(uname -s)
  47. case $ostype in
  48. (Darwin)
  49. tools="$tools genext2fs"
  50. ;;
  51. (Linux)
  52. tools="$tools mke2fs parted"
  53. ;;
  54. (*)
  55. printf Sorry, not ported to the OS "'$ostype'" yet.\n
  56. exit 1
  57. ;;
  58. esac
  59. for tool in $tools; do
  60. printf "Checking if $tool is installed..."
  61. if which $tool >/dev/null; then
  62. printf " okay\n"
  63. else
  64. printf " failed\n"
  65. f=1
  66. fi
  67. done
  68. (( f )) && exit 1
  69. if [ -z $1 ];then
  70. printf "Please give the name of the image file\n"
  71. exit 1
  72. fi
  73. if [ -z $initramfs ];then
  74. if [ -z $2 ];then
  75. printf "Please give the name of the openadk archive file\n"
  76. exit 1
  77. fi
  78. else
  79. if [ -z $2 ];then
  80. printf "Please give the full path prefix to kernel/initramfs\n"
  81. exit 1
  82. fi
  83. fi
  84. printf "Generate qemu image (512 MB)\n"
  85. qemu-img create -f raw $1 512M >/dev/null
  86. printf "Create partition and filesystem\n"
  87. case $ostype in
  88. (Darwin)
  89. offset=16384
  90. ;;
  91. (Linux)
  92. parted -s $1 mklabel msdos
  93. parted -s $1 -- mkpart primary ext2 0 -0
  94. parted -s $1 set 1 boot on
  95. offset=$(parted $1 unit b print | tail -2 | head -1 | cut -f 1 --delimit="B" | cut -c 9-)
  96. ;;
  97. (*)
  98. printf Sorry, not ported to the OS "'$ostype'" yet.\n
  99. exit 1
  100. ;;
  101. esac
  102. printf "Creating filesystem $filesystem\n"
  103. if [ "$filesystem" = "ext2" -o "$filesystem" = "ext3" -o "$filesystem" = "ext4" ];then
  104. mkfsopts=-F
  105. fi
  106. case $ostype in
  107. (Darwin)
  108. tmp=$(mktemp -d -t xxx)
  109. tar -C $tmp -xzpf $2
  110. printf "Fixing permissions\n"
  111. chmod 1777 $tmp/tmp
  112. chmod 4755 $tmp/bin/busybox
  113. genext2fs -b 409600 -d $tmp ${1}.new
  114. cat scripts/mbr ${1}.new > $1
  115. rm ${1}.new
  116. ;;
  117. (Linux)
  118. dd if=$1 of=mbr bs=$offset count=1 2>/dev/null
  119. dd if=$1 skip=$offset of=$1.new 2>/dev/null
  120. mkfs.$filesystem $mkfsopts ${1}.new >/dev/null
  121. cat mbr ${1}.new > $1
  122. rm ${1}.new
  123. rm mbr
  124. tmp=$(mktemp -d)
  125. mount -o loop,offset=$offset -t $filesystem $1 $tmp
  126. if [ -z $initramfs ];then
  127. printf "Extracting install archive\n"
  128. tar -C $tmp -xzpf $2
  129. printf "Fixing permissions\n"
  130. chmod 1777 $tmp/tmp
  131. chmod 4755 $tmp/bin/busybox
  132. else
  133. printf "Copying kernel/initramfs\n"
  134. mkdir $tmp/boot $tmp/dev
  135. cp $2-kernel $tmp/boot/kernel
  136. cp $2-initramfs $tmp/boot/initramfs
  137. fi
  138. umount $tmp
  139. ;;
  140. (*)
  141. printf Sorry, not ported to the OS "'$ostype'" yet.\n
  142. exit 1
  143. ;;
  144. esac
  145. printf "Successfully installed.\n"
  146. printf "Be sure $1 is writable for the user which use qemu\n"
  147. exit 0