| 1 | #!/bin/bash | 
|---|
| 2 | # Author: Cesar FUGUET | 
|---|
| 3 | # Date  : Novembre 2013 | 
|---|
| 4 |  | 
|---|
| 5 | clusters=65536 | 
|---|
| 6 | sector_size=512 | 
|---|
| 7 | sectors_per_cluster=8 | 
|---|
| 8 | reserved_sectors=512 | 
|---|
| 9 |  | 
|---|
| 10 | # FAT32 SPEC: The first two clusters are not present in the data region but | 
|---|
| 11 | # they are used in the computation of the FAT sectors | 
|---|
| 12 | data_region_clusters=$clusters-2 | 
|---|
| 13 |  | 
|---|
| 14 | let "data_sectors = data_region_clusters * sectors_per_cluster" | 
|---|
| 15 | let "fat_sectors  = (clusters * 4) / 512" | 
|---|
| 16 |  | 
|---|
| 17 | # The disk image contains: | 
|---|
| 18 | #   - MBR sector : this sector is removed by this script as it will be copied | 
|---|
| 19 | #                  after) | 
|---|
| 20 | #   - Reserved region sectors (usually 32) | 
|---|
| 21 | #   - FAT region sectors | 
|---|
| 22 | #   - DATA region sectors | 
|---|
| 23 | let "sectors      = data_sectors + fat_sectors + reserved_sectors + 1"  | 
|---|
| 24 |  | 
|---|
| 25 | platform=`uname` | 
|---|
| 26 | echo "Detected platform: $platform" | 
|---|
| 27 |  | 
|---|
| 28 | if [[ $platform == "Darwin" ]]; then | 
|---|
| 29 |         case $1 in | 
|---|
| 30 |                 create ) | 
|---|
| 31 |                         # Create disk image | 
|---|
| 32 |                         # -fs        <fs>   : use filesystem <fs> | 
|---|
| 33 |                         # -volname   <name> : use volume name <name> | 
|---|
| 34 |                         # -srcfolder <src>  : copy every file in <src> into image | 
|---|
| 35 |                         # -format    <fmt>  : format of image. UDRW (UDIF read/write image) | 
|---|
| 36 |                         # -sectors   <sect> : size of image in 512-byte sectors | 
|---|
| 37 |                         # -ov                           : overwrite existing image file | 
|---|
| 38 |                         # -fargs         <args> : additional args to pass to newfs program  | 
|---|
| 39 |                         #                            -F : FAT type (FAT32) | 
|---|
| 40 |                         #                                -c : sector per cluster | 
|---|
| 41 |                         #                                -n : number of FAT | 
|---|
| 42 |  | 
|---|
| 43 |                         if [[ -z $2 ]]; then | 
|---|
| 44 |                                 echo "Create action need second argument: <srcdir_path>" | 
|---|
| 45 |                                 exit 1; | 
|---|
| 46 |                         fi; | 
|---|
| 47 |  | 
|---|
| 48 |                         if [[ -z $3 ]]; then | 
|---|
| 49 |                                 echo "Create action need third argument: <disk_image>" | 
|---|
| 50 |                                 exit 1; | 
|---|
| 51 |                         fi; | 
|---|
| 52 |  | 
|---|
| 53 |                         # RAW DISK IMAGE READ-ONLY | 
|---|
| 54 |                         # The not used space is not considered (smaller disk image) | 
|---|
| 55 |                         # format="UDRO" | 
|---|
| 56 |  | 
|---|
| 57 |                         # RAW DISK IMAGE READ-WRITE | 
|---|
| 58 |                         # None compression is made | 
|---|
| 59 |                         format="UDRW"  | 
|---|
| 60 |  | 
|---|
| 61 |                         # -F FAT type = 32 (FAT32) | 
|---|
| 62 |                         # -c sectors/cluster | 
|---|
| 63 |                         # -n number of FATs | 
|---|
| 64 |                         # -r reserved sectors | 
|---|
| 65 |                         # -k backup boot sector (VBR and FS INFO) = 0xffff (no backup) | 
|---|
| 66 |                         echo "Creating partition file $3.dmg from $2 directory" | 
|---|
| 67 |                         fsargs="-F32 -c $sectors_per_cluster -n 1 -r $reserved_sectors -k 0xffff" | 
|---|
| 68 |                         hdiutil create \ | 
|---|
| 69 |                                 -fs MS-DOS \ | 
|---|
| 70 |                                 -volname virtualdisk \ | 
|---|
| 71 |                                 -srcfolder $2 \ | 
|---|
| 72 |                                 -sectors $sectors \ | 
|---|
| 73 |                                 -ov \ | 
|---|
| 74 |                                 -fsargs "$fsargs" \ | 
|---|
| 75 |                                 -format $format \ | 
|---|
| 76 |                                 $3;  | 
|---|
| 77 | #                               -verbose \ | 
|---|
| 78 |  | 
|---|
| 79 |                         echo "Removing first sector of diskimage" | 
|---|
| 80 |  | 
|---|
| 81 |                         if [[ -e "/tmp" ]]; then | 
|---|
| 82 |                                 if [[ -e "/tmp/diskimage" ]]; then | 
|---|
| 83 |                                         echo "Warning /tmp/diskimage exists already. Rename it or" | 
|---|
| 84 |                                         echo "remove it before executing this script" | 
|---|
| 85 |                                         exit | 
|---|
| 86 |                                 fi; | 
|---|
| 87 |  | 
|---|
| 88 |                                 dd if=$3.dmg of=/tmp/diskimage iseek=1 oseek=0 | 
|---|
| 89 |                                 mv /tmp/diskimage $3.dmg | 
|---|
| 90 |                         fi;; | 
|---|
| 91 |  | 
|---|
| 92 |                 attach ) | 
|---|
| 93 |                         # Attach created disk image to the system and mount it | 
|---|
| 94 |                         hdiutil attach $2;; | 
|---|
| 95 |  | 
|---|
| 96 |                 detach ) | 
|---|
| 97 |                         # Detach attached device. Must pass as parameter the /dev/<device> | 
|---|
| 98 |                         hdiutil detach $2;; | 
|---|
| 99 |  | 
|---|
| 100 |                 info ) | 
|---|
| 101 |                         hdiutil imageinfo $2;; | 
|---|
| 102 |  | 
|---|
| 103 |                 * ) | 
|---|
| 104 |                         echo "Pass the command as the first parameter: " | 
|---|
| 105 |                         echo "  - create: create disk image" | 
|---|
| 106 |                         echo "            First parameter after create action is the path" | 
|---|
| 107 |                     echo "            of the directory to copy in the created " | 
|---|
| 108 |                         echo "            partition" | 
|---|
| 109 |                         echo "  - attach: attach created disk image to the system and " | 
|---|
| 110 |                         echo "            mount it (arg: path to disk_image)" | 
|---|
| 111 |                         echo "  - detach: detach specified device " | 
|---|
| 112 |                         echo "            (arg: path to /dev/<device>)" | 
|---|
| 113 |                         echo "  - info  : print information about created disk image" | 
|---|
| 114 |                         echo "            (arg: path to disk_image)" | 
|---|
| 115 |                         echo "" | 
|---|
| 116 |                         echo "EXAMPLES:" | 
|---|
| 117 |                         echo " ./create_diskimage.sh create /path/to/source_dir disk_image" | 
|---|
| 118 |                         echo " ./create_diskimage.sh attach /path/to/disk_image.dmg" | 
|---|
| 119 |                         echo " ./create_diskimage.sh detach /dev/disk5" | 
|---|
| 120 |                         echo " ./create_diskimage.sh info   /path/to/disk_image.dmg" | 
|---|
| 121 |         esac; | 
|---|
| 122 |  | 
|---|
| 123 | elif [[ $platform == "Linux" ]]; then | 
|---|
| 124 |         case $1 in | 
|---|
| 125 |                 create ) | 
|---|
| 126 |                         if [[ -z $2 ]]; then | 
|---|
| 127 |                                 echo "Create action need second argument: <srcdir_path>" | 
|---|
| 128 |                                 exit 1; | 
|---|
| 129 |                         fi; | 
|---|
| 130 |  | 
|---|
| 131 |                         if [[ -z $3 ]]; then | 
|---|
| 132 |                                 echo "Create action need third argument: <disk_image>" | 
|---|
| 133 |                                 exit 1; | 
|---|
| 134 |                         fi; | 
|---|
| 135 |  | 
|---|
| 136 |                         echo "Creating partition file: $3" | 
|---|
| 137 |  | 
|---|
| 138 |                         let "sectors = (sectors - 1) / 2"  | 
|---|
| 139 |                     mkfs.vfat \ | 
|---|
| 140 |                                 -C \ | 
|---|
| 141 |                                 -F32 \ | 
|---|
| 142 |                                 -f 1 \ | 
|---|
| 143 |                                 -R $reserved_sectors \ | 
|---|
| 144 |                                 -S $sector_size \ | 
|---|
| 145 |                                 -s $sectors_per_cluster \ | 
|---|
| 146 |                                 -v \ | 
|---|
| 147 |                                 $3.dmg $sectors | 
|---|
| 148 |  | 
|---|
| 149 |                         if [[ -e $2 ]]; then | 
|---|
| 150 |                                 echo "Copying files from $2 into partition" | 
|---|
| 151 |                                 mcopy -s -i $3.dmg $2/* ::// | 
|---|
| 152 |                         fi;; | 
|---|
| 153 |  | 
|---|
| 154 |                 * ) | 
|---|
| 155 |                         echo "Pass the command as the first parameter: " | 
|---|
| 156 |                         echo "  - create: create disk image" | 
|---|
| 157 |                         echo "            First parameter after create action is the path" | 
|---|
| 158 |                     echo "            of the directory to copy in the created " | 
|---|
| 159 |                         echo "            partition" | 
|---|
| 160 |                         echo "" | 
|---|
| 161 |                         echo "EXAMPLES:" | 
|---|
| 162 |                         echo " ./create_diskimage.sh create /path/to/source_dir disk_image" | 
|---|
| 163 |         esac; | 
|---|
| 164 |  | 
|---|
| 165 | else | 
|---|
| 166 |         echo "Your platform is not supported: $platform" | 
|---|
| 167 | fi; | 
|---|
| 168 |  | 
|---|