create_device.sh 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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 [ -z $DEVICE ] ; then
  50. show_usage
  51. fi
  52. if [ "$DEVICE" = "img" ] && [ -z $IMGSIZE ] ; then
  53. show_usage
  54. fi
  55. mkdir -p $MNT1
  56. mkdir -p $MNT2
  57. if [ "${DEVICE}" = "img" ] ; then
  58. echo "- Create image."
  59. rm -f ${TARGET}
  60. # create image file
  61. dd if=/dev/zero of=${TARGET} bs=1MB count=$IMGSIZE status=noxfer >/dev/null 2>&1
  62. # find first avaliable free device
  63. DEVICE=$(losetup -f)
  64. IMGSIZE="100%"
  65. TYPE="loop"
  66. # mount image as block device
  67. losetup $DEVICE ${TARGET}
  68. sync
  69. elif [ ! -z $IMGSIZE ] ; then
  70. IMGSIZE=${IMGSIZE}"MiB"
  71. else
  72. IMGSIZE="100%"
  73. fi
  74. if [ -z $ENCRYPT ] ; then
  75. # create one partition starting at 2048 which is default
  76. echo "- Partitioning"
  77. parted --script $DEVICE mklabel msdos
  78. parted --script $DEVICE mkpart primary ext4 2048s ${IMGSIZE}
  79. parted --script $DEVICE align-check optimal 1
  80. else
  81. parted --script $DEVICE mklabel msdos
  82. parted --script $DEVICE mkpart primary ext4 2048s 512MB
  83. parted --script $DEVICE mkpart primary ext4 512MB ${IMGSIZE}
  84. parted --script $DEVICE align-check optimal 1
  85. fi
  86. if [[ "${TYPE}" == "loop" || "${DEVICE}" =~ mmcblk[0-9] ]] ; then
  87. DEVICEP1=${DEVICE}p1
  88. else
  89. DEVICEP1=${DEVICE}1
  90. fi
  91. echo "- Formating"
  92. # create filesystem
  93. mkfs.ext4 $DEVICEP1 >/dev/null 2>&1
  94. # tune filesystem
  95. tune2fs -o journal_data_writeback $DEVICEP1 >/dev/null 2>&1
  96. if [ -z $ENCRYPT ] ; then
  97. echo "- Mount filesystem"
  98. # mount image to already prepared mount point
  99. mount -t ext4 $DEVICEP1 $MNT1
  100. else
  101. if [[ "${DEVICE}" =~ mmcblk[0-9] ]] ; then
  102. DEVICEP2=${DEVICE}p2
  103. else
  104. DEVICEP2=${DEVICE}2
  105. fi
  106. cryptsetup -y -v luksFormat $DEVICEP2
  107. cryptsetup luksOpen $DEVICEP2 olinux
  108. mkfs.ext4 /dev/mapper/olinux >/dev/null 2>&1
  109. echo "- Mount filesystem"
  110. # mount image to already prepared mount point
  111. mount -t ext4 /dev/mapper/olinux $MNT1
  112. mkdir $MNT1/boot
  113. mount -t ext4 $DEVICEP1 $MNT1/boot
  114. fi
  115. echo "- Copy bootstrap files"
  116. if [ -d ${DEB_DIR} ] ; then
  117. # Assume that directly the debootstrap directory
  118. cp -ar ${DEB_DIR}/* $MNT1/
  119. elif [[ `file ${DEB_DIR} | grep 'DOS/MBR'` ]] ; then
  120. # Assume that is a .img file
  121. # find first avaliable free device
  122. DEVICE1=$(losetup -f)
  123. # mount image as block device
  124. losetup -o 1048576 $DEVICE1 ${DEB_DIR}
  125. mount ${DEVICE1} $MNT2/
  126. cp -ar $MNT2/* $MNT1/
  127. else
  128. # Assume that is a tarball file
  129. tar --same-owner --preserve-permissions -xvf ${DEB_DIR} -C $MNT1/ .
  130. fi
  131. sync
  132. echo "- Write sunxi-with-spl"
  133. if [[ "${UBOOT_FILE}" == *.bin ]] ; then
  134. dd if=${UBOOT_FILE} of=${DEVICE} bs=1024 seek=8 >/dev/null 2>&1
  135. else
  136. BOARD=${UBOOT_FILE}
  137. . ${REP}/config_board.sh
  138. 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
  139. fi
  140. sync
  141. if [ "${DEVICE}" = "img" ] ; then
  142. echo "- Sfill"
  143. sfill -z -l -l -f $MNT
  144. fi
  145. echo "- Umount"
  146. if [ "${TYPE}" = "loop" ] ; then
  147. echo "- Sfill"
  148. sfill -z -l -l -f $MNT1
  149. umount $MNT1
  150. losetup -d $DEVICE
  151. else
  152. if [ -z $ENCRYPT ] ; then
  153. umount $MNT1
  154. else
  155. umount $MNT1/boot
  156. umount $MNT1
  157. cryptsetup luksClose olinux
  158. fi
  159. if [[ `file ${DEB_DIR} | grep 'DOS/MBR'` ]] ; then
  160. umount $MNT2
  161. losetup -d $DEVICE1
  162. fi
  163. fi