1
0

create-image.sh 3.1 KB

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