create-image.sh 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. #!/usr/bin/env bash
  2. grubinstall=1
  3. filesystem=ext2
  4. while getopts "f:t:in" option
  5. do
  6. case $option in
  7. f)
  8. filesystem=$OPTARG
  9. ;;
  10. t)
  11. emul=$OPTARG
  12. ;;
  13. i)
  14. initramfs=1
  15. ;;
  16. n)
  17. grubinstall=0
  18. ;;
  19. *)
  20. printf "Option not recognized\n"
  21. exit 1
  22. ;;
  23. esac
  24. done
  25. shift $(($OPTIND - 1))
  26. if [ $(id -u) -ne 0 ];then
  27. printf "Installation is only possible as root\n"
  28. exit 1
  29. fi
  30. printf "Checking if grub is installed"
  31. grub=$(which grub)
  32. if [ ! -z $grub -a -x $grub ];then
  33. printf "...okay\n"
  34. else
  35. printf "...failed\n"
  36. exit 1
  37. fi
  38. printf "Checking if parted is installed"
  39. parted=$(which parted)
  40. if [ ! -z $parted -a -x $parted ];then
  41. printf "...okay\n"
  42. else
  43. printf "...failed\n"
  44. exit 1
  45. fi
  46. printf "Checking if qemu-img is installed"
  47. qimg=$(which qemu-img)
  48. if [ ! -z $qimg -a -x $qimg ];then
  49. printf "...okay\n"
  50. else
  51. printf "...failed\n"
  52. exit 1
  53. fi
  54. if [ -z $1 ];then
  55. printf "Please give the name of the image file\n"
  56. exit 1
  57. fi
  58. if [ -z $initramfs ];then
  59. if [ -z $2 ];then
  60. printf "Please give the name of the openadk archive file\n"
  61. exit 1
  62. fi
  63. else
  64. if [ -z $2 ];then
  65. printf "Please give the full path prefix to kernel/initramfs\n"
  66. exit 1
  67. fi
  68. fi
  69. printf "Generate qemu image\n"
  70. $qimg create -f raw $1 512M >/dev/null
  71. printf "Creating filesystem $filesystem\n"
  72. printf "Create partition and filesystem\n"
  73. $parted -s $1 mklabel msdos
  74. $parted -s $1 mkpart primary ext2 0 100%
  75. $parted -s $1 set 1 boot on
  76. dd if=$1 of=mbr bs=16384 count=1 2>/dev/null
  77. dd if=$1 skip=16384 of=$1.new 2>/dev/null
  78. if [ "$filesystem" = "ext2" -o "$filesystem" = "ext3" -o "$filesystem" = "ext4" ];then
  79. mkfsopts=-F
  80. fi
  81. mkfs.$filesystem $mkfsopts ${1}.new >/dev/null
  82. if [ $? -eq 0 ];then
  83. printf "Successfully created partition\n"
  84. #$parted $1 print
  85. else
  86. printf "Partition creation failed, Exiting.\n"
  87. exit 1
  88. fi
  89. cat mbr ${1}.new > $1
  90. rm ${1}.new
  91. rm mbr
  92. tmp=$(mktemp -d)
  93. mount -o loop,offset=16384 -t $filesystem $1 $tmp
  94. #mount -o loop -t $filesystem $1 $tmp
  95. if [ -z $initramfs ];then
  96. printf "Extracting install archive\n"
  97. tar -C $tmp -xzpf $2
  98. printf "Fixing permissions\n"
  99. chmod 1777 $tmp/tmp
  100. chmod 4755 $tmp/bin/busybox
  101. else
  102. printf "Copying kernel/initramfs\n"
  103. mkdir $tmp/boot $tmp/dev
  104. cp $2-kernel $tmp/boot/kernel
  105. cp $2-initramfs $tmp/boot/initramfs
  106. fi
  107. if [ $grubinstall -eq 1 ];then
  108. printf "Copying grub files\n"
  109. mkdir $tmp/boot/grub
  110. cp /boot/grub/stage1 $tmp/boot/grub
  111. cp /boot/grub/stage2 $tmp/boot/grub
  112. cp /boot/grub/e2fs_stage1_5 $tmp/boot/grub
  113. if [ -z $initramfs ];then
  114. cat << EOF > $tmp/boot/grub/menu.lst
  115. serial --unit=0 --speed=115200 --word=8 --parity=no --stop=1
  116. terminal --timeout=2 serial console
  117. timeout 2
  118. default 0
  119. hiddenmenu
  120. title linux
  121. root (hd0,0)
  122. kernel /boot/kernel root=/dev/sda1 init=/init console=ttyS0,115200 console=tty0 panic=10 rw
  123. EOF
  124. else
  125. cat << EOF > $tmp/boot/grub/menu.lst
  126. serial --unit=0 --speed=115200 --word=8 --parity=no --stop=1
  127. terminal --timeout=2 serial console
  128. timeout 4
  129. default 0
  130. hiddenmenu
  131. title linux
  132. root (hd0,0)
  133. kernel /boot/kernel root=/dev/sda1 console=ttyS0,115200 console=tty0 rw
  134. initrd /boot/initramfs
  135. EOF
  136. fi
  137. printf "Installing Grub bootloader\n"
  138. $grub --batch --no-curses --no-floppy --device-map=/dev/null >/dev/null << EOF
  139. device (hd0) $1
  140. root (hd0,0)
  141. setup (hd0)
  142. quit
  143. EOF
  144. fi
  145. printf "Creating device nodes\n"
  146. mknod -m 666 $tmp/dev/zero c 1 5
  147. mknod -m 666 $tmp/dev/null c 1 3
  148. mknod -m 622 $tmp/dev/console c 5 1
  149. mknod -m 666 $tmp/dev/tty c 5 0
  150. mknod -m 666 $tmp/dev/tty0 c 4 0
  151. mknod -m 660 $tmp/dev/hda b 3 0
  152. mknod -m 660 $tmp/dev/hda1 b 3 1
  153. mknod -m 666 $tmp/dev/ttyS0 c 4 64
  154. umount $tmp
  155. printf "Successfully installed.\n"
  156. printf "Be sure $1 is writable for the user which use qemu\n"
  157. exit 0