create-image.sh 3.1 KB

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