create_device.sh 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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" ] ; 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. DEVICEP2=${DEVICE}2
  102. cryptsetup -y -v luksFormat $DEVICEP2
  103. cryptsetup luksOpen $DEVICEP2 olinux
  104. mkfs.ext4 /dev/mapper/olinux >/dev/null 2>&1
  105. echo "- Mount filesystem"
  106. # mount image to already prepared mount point
  107. mount -t ext4 /dev/mapper/olinux $MNT1
  108. mkdir $MNT1/boot
  109. mount -t ext4 $DEVICEP1 $MNT1/boot
  110. fi
  111. echo "- Copy bootstrap files"
  112. if [ -d ${DEB_DIR} ] ; then
  113. # Assume that directly the debootstrap directory
  114. cp -ar ${DEB_DIR}/* $MNT1/
  115. elif [[ `file ${DEB_DIR} | grep 'DOS/MBR'` ]] ; then
  116. # Assume that is a .img file
  117. # find first avaliable free device
  118. DEVICE1=$(losetup -f)
  119. # mount image as block device
  120. losetup -o 1048576 $DEVICE1 ${DEB_DIR}
  121. mount ${DEVICE1} $MNT2/
  122. cp -ar $MNT2/* $MNT1/
  123. else
  124. # Assume that is a tarball file
  125. tar --same-owner --preserve-permissions -xvf ${DEB_DIR} -C $MNT1/ .
  126. fi
  127. sync
  128. echo "- Write sunxi-with-spl"
  129. if [[ "${UBOOT_FILE}" == *.bin ]] ; then
  130. dd if=${UBOOT_FILE} of=${DEVICE} bs=1024 seek=8 >/dev/null 2>&1
  131. else
  132. BOARD=${UBOOT_FILE}
  133. . ${REP}/config_board.sh
  134. 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
  135. fi
  136. sync
  137. if [ "${DEVICE}" = "img" ] ; then
  138. echo "- Sfill"
  139. sfill -z -l -l -f $MNT
  140. fi
  141. echo "- Umount"
  142. if [ "${TYPE}" = "loop" ] ; then
  143. echo "- Sfill"
  144. sfill -z -l -l -f $MNT1
  145. umount $MNT1
  146. losetup -d $DEVICE
  147. else
  148. if [ -z $ENCRYPT ] ; then
  149. umount $MNT1
  150. else
  151. umount $MNT1/boot
  152. umount $MNT1
  153. cryptsetup luksClose olinux
  154. fi
  155. if [[ `file ${DEB_DIR} | grep 'DOS/MBR'` ]] ; then
  156. umount $MNT2
  157. losetup -d $DEVICE1
  158. fi
  159. fi