create_device.sh 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. #!/bin/sh
  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. DEB_DIR=./olinux/debootstrap
  20. UBOOT_FILE=./olinux/sunxi/u-boot/u-boot-sunxi-with-spl.bin
  21. while getopts ":s:d:t:b:u:" opt; do
  22. case $opt in
  23. d)
  24. DEVICE=$OPTARG
  25. ;;
  26. s)
  27. IMGSIZE=$OPTARG
  28. ;;
  29. t)
  30. TARGET=$OPTARG
  31. ;;
  32. b)
  33. DEB_DIR=$OPTARG
  34. ;;
  35. u)
  36. UBOOT_FILE=$OPTARG
  37. ;;
  38. \?)
  39. show_usage
  40. ;;
  41. esac
  42. done
  43. if [ -z $DEVICE ] ; then
  44. show_usage
  45. fi
  46. if [ "$DEVICE" = "img" ] && [ -z $IMGSIZE ] ; then
  47. show_usage
  48. fi
  49. if [ "${DEVICE}" = "img" ] ; then
  50. echo "- Create image."
  51. rm -f ${TARGET}
  52. # create image file
  53. dd if=/dev/zero of=${TARGET} bs=1MB count=$IMGSIZE status=noxfer >/dev/null 2>&1
  54. # find first avaliable free device
  55. DEVICE=$(losetup -f)
  56. IMGSIZE="100%"
  57. TYPE="loop"
  58. # mount image as block device
  59. losetup $DEVICE ${TARGET}
  60. sync
  61. elif [ ! -z $IMGSIZE ] ; then
  62. IMGSIZE=${IMGSIZE}"MiB"
  63. else
  64. IMGSIZE="100%"
  65. fi
  66. # create one partition starting at 2048 which is default
  67. echo "- Partitioning"
  68. parted --script $DEVICE mklabel msdos
  69. parted --script $DEVICE mkpart primary ext4 2048s ${IMGSIZE}
  70. parted --script $DEVICE align-check optimal 1
  71. if [ "${TYPE}" = "loop" ] ; then
  72. DEVICEP1=${DEVICE}p1
  73. else
  74. DEVICEP1=${DEVICE}1
  75. fi
  76. echo "- Formating"
  77. # create filesystem
  78. mkfs.ext4 $DEVICEP1 >/dev/null 2>&1
  79. # tune filesystem
  80. tune2fs -o journal_data_writeback $DEVICEP1 >/dev/null 2>&1
  81. echo "- Mount filesystem"
  82. # mount image to already prepared mount point
  83. mount -t ext4 $DEVICEP1 $MNT
  84. echo "- Copy bootstrap files"
  85. if [ -d ${DEB_DIR} ] ; then
  86. cp -ar ${DEB_DIR}/* $MNT/
  87. else
  88. # Assume that is a tarball file
  89. tar --same-owner --preserve-permissions -xvf ${DEB_DIR} -C $MNT/ .
  90. fi
  91. sync
  92. echo "- Write sunxi-with-spl"
  93. dd if=${UBOOT_FILE} of=${DEVICE} bs=1024 seek=8 >/dev/null 2>&1
  94. sync
  95. echo "- Umount"
  96. if [ "${TYPE}" = "loop" ] ; then
  97. umount $MNT
  98. losetup -d $DEVICE
  99. else
  100. umount $MNT
  101. fi