create_device.sh 3.7 KB

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