| [258] | 1 | #!/bin/bash | 
|---|
|  | 2 | # Author: Cesar FUGUET | 
|---|
|  | 3 | # Date  : Novembre 2013 | 
|---|
|  | 4 |  | 
|---|
|  | 5 | clusters=65536 | 
|---|
|  | 6 | sector_size=512 | 
|---|
|  | 7 | sectors_per_cluster=8 | 
|---|
| [364] | 8 | reserved_sectors=2048 | 
|---|
| [258] | 9 |  | 
|---|
| [259] | 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 | 
|---|
| [258] | 13 |  | 
|---|
| [259] | 14 | let "data_sectors = data_region_clusters * sectors_per_cluster" | 
|---|
|  | 15 | let "fat_sectors  = (clusters * 4) / 512" | 
|---|
| [258] | 16 |  | 
|---|
| [259] | 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 |  | 
|---|
| [258] | 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 | 
|---|
| [302] | 44 | echo "Create action need second argument: <disk_image>" | 
|---|
| [258] | 45 | exit 1; | 
|---|
|  | 46 | fi; | 
|---|
|  | 47 |  | 
|---|
| [300] | 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) | 
|---|
| [302] | 53 | echo "Creating empty disk image: $2.dmg" | 
|---|
| [300] | 54 | fsargs="-F32 -c $sectors_per_cluster -n 1 -r $reserved_sectors -k 0xffff" | 
|---|
| [302] | 55 |  | 
|---|
|  | 56 | # -layout = NONE (No partition scheme. Hence no Master Boot Record) | 
|---|
| [258] | 57 | hdiutil create \ | 
|---|
|  | 58 | -fs MS-DOS \ | 
|---|
|  | 59 | -volname virtualdisk \ | 
|---|
| [302] | 60 | -layout NONE \ | 
|---|
| [258] | 61 | -sectors $sectors \ | 
|---|
|  | 62 | -ov \ | 
|---|
| [300] | 63 | -fsargs "$fsargs" \ | 
|---|
| [302] | 64 | -type "UDIF" \ | 
|---|
|  | 65 | $2; | 
|---|
|  | 66 | ;; | 
|---|
| [258] | 67 |  | 
|---|
|  | 68 | attach ) | 
|---|
|  | 69 | # Attach created disk image to the system and mount it | 
|---|
| [302] | 70 | hdiutil attach $2 | 
|---|
|  | 71 | ;; | 
|---|
| [258] | 72 |  | 
|---|
|  | 73 | detach ) | 
|---|
|  | 74 | # Detach attached device. Must pass as parameter the /dev/<device> | 
|---|
| [302] | 75 | hdiutil detach $2 | 
|---|
|  | 76 | ;; | 
|---|
| [258] | 77 |  | 
|---|
|  | 78 | info ) | 
|---|
| [302] | 79 | hdiutil imageinfo $2 | 
|---|
|  | 80 | ;; | 
|---|
| [258] | 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:" | 
|---|
| [302] | 93 | echo " ./create_diskimage.sh create disk_image" | 
|---|
| [258] | 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" | 
|---|
| [302] | 97 | ;; | 
|---|
| [258] | 98 | esac; | 
|---|
|  | 99 |  | 
|---|
|  | 100 | elif [[ $platform == "Linux" ]]; then | 
|---|
| [259] | 101 | case $1 in | 
|---|
|  | 102 | create ) | 
|---|
| [302] | 103 |  | 
|---|
| [259] | 104 | if [[ -z $2 ]]; then | 
|---|
| [302] | 105 | echo "Create action need second argument: <disk_image>" | 
|---|
| [259] | 106 | exit 1; | 
|---|
|  | 107 | fi; | 
|---|
| [258] | 108 |  | 
|---|
| [302] | 109 | echo "Creating empty disk image: $2" | 
|---|
| [258] | 110 |  | 
|---|
| [259] | 111 | let "sectors = (sectors - 1) / 2" | 
|---|
|  | 112 | mkfs.vfat \ | 
|---|
|  | 113 | -C \ | 
|---|
|  | 114 | -F32 \ | 
|---|
|  | 115 | -f 1 \ | 
|---|
|  | 116 | -R $reserved_sectors \ | 
|---|
|  | 117 | -S $sector_size \ | 
|---|
|  | 118 | -s $sectors_per_cluster \ | 
|---|
|  | 119 | -v \ | 
|---|
| [302] | 120 | $2.dmg $sectors | 
|---|
|  | 121 | ;; | 
|---|
| [258] | 122 |  | 
|---|
| [259] | 123 | * ) | 
|---|
|  | 124 | echo "Pass the command as the first parameter: " | 
|---|
|  | 125 | echo "  - create: create disk image" | 
|---|
|  | 126 | echo "" | 
|---|
|  | 127 | echo "EXAMPLES:" | 
|---|
| [302] | 128 | echo " ./create_diskimage.sh create disk_image" | 
|---|
|  | 129 | ;; | 
|---|
| [259] | 130 | esac; | 
|---|
|  | 131 |  | 
|---|
| [258] | 132 | else | 
|---|
|  | 133 | echo "Your platform is not supported: $platform" | 
|---|
|  | 134 | fi; | 
|---|
|  | 135 |  | 
|---|