create_device.sh 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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 final image name (default: /olinux/olinux.img)
  12. -b debootstrap directory, .img or tarball (default: /olinux/debootstrap)
  13. -u uboot file or board name (default: a20lime)
  14. -e encrypt partition (default: false)
  15. EOF
  16. exit 1
  17. }
  18. TARGET=./olinux/olinux.img
  19. MNT1=/mnt/dest
  20. MNT2=/mnt/source
  21. DEB_DIR=./olinux/debootstrap
  22. UBOOT_FILE="a20lime"
  23. REP=$(dirname $0)
  24. while getopts ":s:d:t:b:u:e" opt; do
  25. case $opt in
  26. d)
  27. DEVICE=$OPTARG
  28. ;;
  29. s)
  30. IMGSIZE=$OPTARG
  31. ;;
  32. t)
  33. TARGET=$OPTARG
  34. ;;
  35. b)
  36. DEB_DIR=$OPTARG
  37. ;;
  38. u)
  39. UBOOT_FILE=$OPTARG
  40. ;;
  41. e)
  42. ENCRYPT=yes
  43. ;;
  44. \?)
  45. show_usage
  46. ;;
  47. esac
  48. done
  49. if [ ! -r "${DEB_DIR}" ]; then
  50. echo "[ERR] Cannot read ${DEB_DIR}" >&2
  51. exit 1
  52. fi
  53. if [ -z $DEVICE ] ; then
  54. show_usage
  55. fi
  56. if [ "$DEVICE" = "img" ] && [ -z $IMGSIZE ] ; then
  57. show_usage
  58. fi
  59. mkdir -p $MNT1
  60. mkdir -p $MNT2
  61. if [ "${DEVICE}" = "img" ] ; then
  62. echo "- Create image."
  63. rm -f ${TARGET}
  64. # create image file
  65. dd if=/dev/zero of=${TARGET} bs=1MB count=$IMGSIZE status=noxfer >/dev/null 2>&1
  66. # find first avaliable free device
  67. DEVICE=$(losetup -f)
  68. IMGSIZE="100%"
  69. TYPE="loop"
  70. # mount image as block device
  71. losetup $DEVICE ${TARGET}
  72. sync
  73. elif [ ! -z $IMGSIZE ] ; then
  74. IMGSIZE=${IMGSIZE}"MiB"
  75. else
  76. IMGSIZE="100%"
  77. fi
  78. if [ -z $ENCRYPT ] ; then
  79. # create one partition starting at 2048 which is default
  80. echo "- Partitioning"
  81. parted --script $DEVICE mklabel msdos
  82. parted --script $DEVICE mkpart primary ext4 2048s ${IMGSIZE}
  83. parted --script $DEVICE align-check optimal 1
  84. else
  85. parted --script $DEVICE mklabel msdos
  86. parted --script $DEVICE mkpart primary ext4 2048s 512MB
  87. parted --script $DEVICE mkpart primary ext4 512MB ${IMGSIZE}
  88. parted --script $DEVICE align-check optimal 1
  89. fi
  90. if [[ "${TYPE}" == "loop" || "${DEVICE}" =~ mmcblk[0-9] ]] ; then
  91. DEVICEP1=${DEVICE}p1
  92. else
  93. DEVICEP1=${DEVICE}1
  94. fi
  95. echo "- Formating"
  96. # create filesystem
  97. mkfs.ext4 $DEVICEP1 >/dev/null 2>&1
  98. # tune filesystem
  99. tune2fs -o journal_data_writeback $DEVICEP1 >/dev/null 2>&1
  100. finish() {
  101. echo "- Umount"
  102. if [ "${TYPE}" = "loop" ] ; then
  103. if mountpoint -q $MNT1 ; then
  104. umount $MNT1
  105. losetup -d $DEVICE
  106. fi
  107. else
  108. if [ -z $ENCRYPT ] ; then
  109. if mountpoint -q $MNT1 ; then
  110. umount $MNT1
  111. fi
  112. else
  113. if mountpoint -q $MNT1 ; then
  114. umount $MNT1/boot
  115. umount $MNT1
  116. cryptsetup luksClose olinux
  117. fi
  118. if [[ "${DEB_DIR}" =~ \.img$ ]] ; then
  119. if mountpoint -q $MNT2 ; then
  120. umount $MNT2
  121. losetup -d $DEVICE1
  122. fi
  123. fi
  124. fi
  125. fi
  126. }
  127. trap finish EXIT
  128. if [ -z $ENCRYPT ] ; then
  129. echo "- Mount filesystem"
  130. # mount image to already prepared mount point
  131. mount -t ext4 $DEVICEP1 $MNT1
  132. else
  133. if [[ "${DEVICE}" =~ mmcblk[0-9] ]] ; then
  134. DEVICEP2=${DEVICE}p2
  135. else
  136. DEVICEP2=${DEVICE}2
  137. fi
  138. cryptsetup -y -v luksFormat $DEVICEP2
  139. cryptsetup luksOpen $DEVICEP2 olinux
  140. mkfs.ext4 /dev/mapper/olinux >/dev/null 2>&1
  141. echo "- Mount filesystem"
  142. # mount image to already prepared mount point
  143. mount -t ext4 /dev/mapper/olinux $MNT1
  144. mkdir $MNT1/boot
  145. mount -t ext4 $DEVICEP1 $MNT1/boot
  146. fi
  147. echo "- Copy bootstrap files"
  148. if [ -d ${DEB_DIR} ] ; then
  149. # Assume that directly the debootstrap directory
  150. cp -ar ${DEB_DIR}/* $MNT1/
  151. elif [[ "${DEB_DIR}" =~ \.img$ ]] ; then
  152. # Assume that is a .img file
  153. # find first avaliable free device
  154. DEVICE1=$(losetup -f)
  155. # mount image as block device
  156. losetup -o 1048576 $DEVICE1 ${DEB_DIR}
  157. mount ${DEVICE1} $MNT2/
  158. cp -ar $MNT2/* $MNT1/
  159. fi
  160. sync
  161. echo "- Write sunxi-with-spl"
  162. if [[ "${UBOOT_FILE}" == *.bin ]] ; then
  163. dd if=${UBOOT_FILE} of=${DEVICE} bs=1024 seek=8 >/dev/null 2>&1
  164. else
  165. BOARD=${UBOOT_FILE}
  166. . ${REP}/config_board.sh
  167. dd if=$MNT1/usr/lib/u-boot/${U_BOOT}/u-boot-sunxi-with-spl.bin of=${DEVICE} bs=1024 seek=8 >/dev/null 2>&1
  168. fi
  169. sync
  170. if [[ "${DEVICE}" == "img" || "${TYPE}" = "loop" ]] ; then
  171. echo "- Sfill"
  172. sfill -z -l -l -f $MNT
  173. fi