create_device.sh 2.3 KB

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