create_device.sh 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #!/bin/bash
  2. set -e
  3. show_usage() {
  4. cat <<EOF
  5. # NAME
  6. $(basename $0) -- Script format device and copy rootfs on it
  7. # OPTIONS
  8. -d device name (img, /dev/sdc, /dev/mmc) (mandatory)
  9. -s size of img in MB (mandatory only for img device option)
  10. EOF
  11. exit 1
  12. }
  13. DEST=./olinux
  14. MNT=/mnt
  15. IMAGE=olinux.img
  16. while getopts "s:d:" opt; do
  17. case $opt in
  18. d)
  19. DEVICE=$OPTARG
  20. ;;
  21. s)
  22. IMGSIZE=$OPTARG
  23. ;;
  24. \?)
  25. show_usage
  26. ;;
  27. esac
  28. done
  29. if [ -z $DEVICE ] ; then
  30. show_usage
  31. fi
  32. if [ "$DEVICE" == "img" ] && [ -z $IMGSIZE ] ; then
  33. show_usage
  34. fi
  35. if [ "${DEVICE}" == "img" ] ; then
  36. echo "- Create image."
  37. rm -f olinux/$IMAGE
  38. # create image file
  39. dd if=/dev/zero of=olinux/$IMAGE bs=1MB count=$IMGSIZE status=noxfer >/dev/null 2>&1
  40. # find first avaliable free device
  41. DEVICE=$(losetup -f)
  42. IMGSIZE="100%"
  43. TYPE="loop"
  44. # mount image as block device
  45. losetup $DEVICE $DEST/$IMAGE >/dev/null 2>&1
  46. sync
  47. elif [ ! -z $IMGSIZE ] : then
  48. IMGSIZE=${IMGSIZE}"MiB"
  49. else
  50. IMGSIZE="100%"
  51. fi
  52. # create one partition starting at 2048 which is default
  53. echo "- Partitioning"
  54. parted --script -a optimal $DEVICE unit GB mklabel msdos
  55. parted --script -a optimal $DEVICE unit GB mkpart primary ext4 2048s ${IMGSIZE}
  56. parted --script -a optimal $DEVICE unit GB align-check optimal 1
  57. if [ "${TYPE}" == "loop" ] ; then
  58. DEVICEP1=${DEVICE}p1
  59. else
  60. DEVICEP1=${DEVICE}1
  61. fi
  62. echo "- Formating"
  63. # create filesystem
  64. mkfs.ext4 $DEVICEP1 >/dev/null 2>&1
  65. # tune filesystem
  66. tune2fs -o journal_data_writeback $DEVICEP1 >/dev/null 2>&1
  67. echo "- Mount filesystem"
  68. # mount image to already prepared mount point
  69. mount -t ext4 $DEVICEP1 $MNT
  70. echo "- Copy bootstrap files"
  71. # copy debootstrap
  72. cp -ar olinux/debootstrap/* $MNT/
  73. sync
  74. echo "- Write sunxi-with-spl"
  75. dd if=olinux/sunxi/u-boot/u-boot-sunxi-with-spl.bin of=${DEVICE} bs=1024 seek=8 >/dev/null 2>&1
  76. echo "- Umount"
  77. if [ "${TYPE}" == "loop" ] ; then
  78. umount $MNT
  79. losetup -d $DEVICE
  80. else
  81. umount $MNT
  82. fi