1
0

create-image.sh 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. tools='qemu-img'
  42. ostype=$(uname -s)
  43. case $ostype in
  44. (Darwin)
  45. tools="$tools genext2fs"
  46. ;;
  47. (Linux)
  48. tools="$tools mke2fs parted"
  49. if [ $(id -u) -ne 0 ];then
  50. printf "Installation is only possible as root\n"
  51. exit 1
  52. fi
  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 "Create partition and filesystem\n"
  85. case $ostype in
  86. (Darwin)
  87. offset=16384
  88. ;;
  89. (Linux)
  90. printf "Generate qemu image (768 MB)\n"
  91. qemu-img create -f raw $1 768M >/dev/null
  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. if [ "$filesystem" = "ext2" -o "$filesystem" = "ext3" -o "$filesystem" = "ext4" ];then
  103. mkfsopts=-F
  104. fi
  105. case $ostype in
  106. (Darwin)
  107. tmp=$(mktemp -d -t xxx)
  108. tar -C $tmp -xzpf $2
  109. printf "Fixing permissions\n"
  110. chmod 1777 $tmp/tmp
  111. chmod 4755 $tmp/bin/busybox
  112. printf "Creating filesystem $filesystem\n"
  113. genext2fs -q -b 709600 -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. printf "Creating filesystem $filesystem\n"
  121. mkfs.$filesystem $mkfsopts ${1}.new >/dev/null
  122. cat mbr ${1}.new > $1
  123. rm ${1}.new
  124. #rm mbr
  125. tmp=$(mktemp -d)
  126. mount -o loop,offset=$offset -t $filesystem $1 $tmp
  127. if [ -z $initramfs ];then
  128. printf "Extracting install archive\n"
  129. tar -C $tmp -xzpf $2
  130. printf "Fixing permissions\n"
  131. chmod 1777 $tmp/tmp
  132. chmod 4755 $tmp/bin/busybox
  133. else
  134. printf "Copying kernel/initramfs\n"
  135. mkdir $tmp/boot $tmp/dev
  136. cp $2-kernel $tmp/boot/kernel
  137. cp $2-initramfs $tmp/boot/initramfs
  138. fi
  139. umount $tmp
  140. ;;
  141. (*)
  142. printf Sorry, not ported to the OS "'$ostype'" yet.\n
  143. exit 1
  144. ;;
  145. esac
  146. printf "Successfully installed.\n"
  147. printf "Be sure $1 is writable for the user which use qemu\n"
  148. exit 0