| 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=4096 | 
|---|
| 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: <disk_image>" | 
|---|
| 45 | exit 1; | 
|---|
| 46 | fi; | 
|---|
| 47 |  | 
|---|
| 48 | # -F FAT type = 32 (FAT32) | 
|---|
| 49 | # -c sectors/cluster | 
|---|
| 50 | # -n number of FATs | 
|---|
| 51 | # -r reserved sectors | 
|---|
| 52 | # -k backup boot sector (VBR and FS INFO) = 0xffff (no backup) | 
|---|
| 53 | echo "Creating empty disk image: $2.dmg" | 
|---|
| 54 | fsargs="-F32 -c $sectors_per_cluster -n 1 -r $reserved_sectors -k 0xffff" | 
|---|
| 55 |  | 
|---|
| 56 | # -layout = NONE (No partition scheme. Hence no Master Boot Record) | 
|---|
| 57 | hdiutil create \ | 
|---|
| 58 | -fs MS-DOS \ | 
|---|
| 59 | -volname virtualdisk \ | 
|---|
| 60 | -layout NONE \ | 
|---|
| 61 | -sectors $sectors \ | 
|---|
| 62 | -ov \ | 
|---|
| 63 | -fsargs "$fsargs" \ | 
|---|
| 64 | -type "UDIF" \ | 
|---|
| 65 | $2; | 
|---|
| 66 | ;; | 
|---|
| 67 |  | 
|---|
| 68 | attach ) | 
|---|
| 69 | # Attach created disk image to the system and mount it | 
|---|
| 70 | hdiutil attach $2 | 
|---|
| 71 | ;; | 
|---|
| 72 |  | 
|---|
| 73 | detach ) | 
|---|
| 74 | # Detach attached device. Must pass as parameter the /dev/<device> | 
|---|
| 75 | hdiutil detach $2 | 
|---|
| 76 | ;; | 
|---|
| 77 |  | 
|---|
| 78 | info ) | 
|---|
| 79 | hdiutil imageinfo $2 | 
|---|
| 80 | ;; | 
|---|
| 81 |  | 
|---|
| 82 | * ) | 
|---|
| 83 | echo "Pass the command as the first parameter: " | 
|---|
| 84 | echo "  - create: create disk image" | 
|---|
| 85 | echo "  - attach: attach created disk image to the system and " | 
|---|
| 86 | echo "            mount it (arg: path to disk_image)" | 
|---|
| 87 | echo "  - detach: detach specified device " | 
|---|
| 88 | echo "            (arg: path to /dev/<device>)" | 
|---|
| 89 | echo "  - info  : print information about created disk image" | 
|---|
| 90 | echo "            (arg: path to disk_image)" | 
|---|
| 91 | echo "" | 
|---|
| 92 | echo "EXAMPLES:" | 
|---|
| 93 | echo " ./create_diskimage.sh create disk_image" | 
|---|
| 94 | echo " ./create_diskimage.sh attach /path/to/disk_image.dmg" | 
|---|
| 95 | echo " ./create_diskimage.sh detach /dev/disk5" | 
|---|
| 96 | echo " ./create_diskimage.sh info   /path/to/disk_image.dmg" | 
|---|
| 97 | ;; | 
|---|
| 98 | esac; | 
|---|
| 99 |  | 
|---|
| 100 | elif [[ $platform == "Linux" ]]; then | 
|---|
| 101 | case $1 in | 
|---|
| 102 | create ) | 
|---|
| 103 |  | 
|---|
| 104 | if [[ -z $2 ]]; then | 
|---|
| 105 | echo "Create action need second argument: <disk_image>" | 
|---|
| 106 | exit 1; | 
|---|
| 107 | fi; | 
|---|
| 108 |  | 
|---|
| 109 | echo "Creating empty disk image: $2" | 
|---|
| 110 | let "sectors = (sectors - 1) / 2" | 
|---|
| 111 |  | 
|---|
| 112 | CREATE_DISK=$(command -v mkfs.vfat ||\ | 
|---|
| 113 | command -v /sbin/mkfs.vfat ||\ | 
|---|
| 114 | command -v /usr/sbin/mkfs.vfat ||\ | 
|---|
| 115 | command -v /usr/local/sbin/mkfs.vfat) | 
|---|
| 116 |  | 
|---|
| 117 | if [ -z ${CREATE_DISK} ]; then | 
|---|
| 118 | echo "error: command mkfs.vfat not found"; | 
|---|
| 119 | exit 1; | 
|---|
| 120 | fi; | 
|---|
| 121 |  | 
|---|
| 122 | ${CREATE_DISK} \ | 
|---|
| 123 | -C \ | 
|---|
| 124 | -F32 \ | 
|---|
| 125 | -f 1 \ | 
|---|
| 126 | -R $reserved_sectors \ | 
|---|
| 127 | -S $sector_size \ | 
|---|
| 128 | -s $sectors_per_cluster \ | 
|---|
| 129 | -v \ | 
|---|
| 130 | $2.dmg $sectors | 
|---|
| 131 | ;; | 
|---|
| 132 |  | 
|---|
| 133 | * ) | 
|---|
| 134 | echo "Pass the command as the first parameter: " | 
|---|
| 135 | echo "  - create: create disk image" | 
|---|
| 136 | echo "" | 
|---|
| 137 | echo "EXAMPLES:" | 
|---|
| 138 | echo " ./create_diskimage.sh create disk_image" | 
|---|
| 139 | ;; | 
|---|
| 140 | esac; | 
|---|
| 141 |  | 
|---|
| 142 | else | 
|---|
| 143 | echo "Your platform is not supported: $platform" | 
|---|
| 144 | fi; | 
|---|
| 145 |  | 
|---|