[1] | 1 | #!/bin/bash |
---|
| 2 | |
---|
| 3 | # This script creates a new FAT32 virtual disk for a MacOS or a Linux |
---|
| 4 | # platform. |
---|
| 5 | |
---|
| 6 | # Defining some variables. |
---|
| 7 | clusters_nr=65536 # Total number of clusters of the new virtual disk. |
---|
| 8 | sector_size=512 # Size of a sector (in bytes). |
---|
| 9 | secsperclus=8 # Count of sectors per cluster. |
---|
| 10 | rsectors_nr=4096 # Count of Reserved Region sectors. |
---|
| 11 | |
---|
| 12 | # FAT32: the DATA Region starts at cluster #2. |
---|
| 13 | data_clust=$clusters_nr-2 |
---|
| 14 | |
---|
| 15 | # Computing the count of DATA Region sectors. |
---|
| 16 | let "dsectors_nr = data_clust * secsperclus" |
---|
| 17 | |
---|
| 18 | # Computing the count of FAT Region sectors. |
---|
| 19 | let "fsectors_nr = (clusters_nr * 4) / 512" |
---|
| 20 | |
---|
| 21 | # An FAT32 disk image is traditionally structured as follows: |
---|
| 22 | # - MBR sector, |
---|
| 23 | # - Reserved Region sectors, |
---|
| 24 | # - FAT Region sectors, |
---|
| 25 | # - DATA Region sectors. |
---|
| 26 | # Since our virtual disk image is not partitioned, its first sector is not a |
---|
| 27 | # MBR but a VBR. |
---|
| 28 | let "sectors_nr = dsectors_nr + fsectors_nr + rsectors_nr + 1" |
---|
| 29 | |
---|
| 30 | # Getting the system kernel name then print it out. |
---|
| 31 | platform=`uname` |
---|
| 32 | echo "Dectected platform: $platform" |
---|
| 33 | |
---|
| 34 | # If we are running on MacOS: |
---|
| 35 | if [[ $platform == "Darwin" ]]; then |
---|
| 36 | case $1 in |
---|
| 37 | # Creating a disk image. |
---|
| 38 | create) |
---|
| 39 | # Checking if the string given by the second script argument size |
---|
| 40 | # is 0. |
---|
| 41 | if [[ -z $2 ]]; then |
---|
| 42 | echo "Please enter the name of the virtual disk image to be " |
---|
| 43 | echo "created as the second argument of this script!" |
---|
| 44 | exit 1 |
---|
| 45 | fi |
---|
| 46 | |
---|
| 47 | echo "Creating <$2.dmg>" |
---|
| 48 | # Defining additional arguments to format the virtual disk in the |
---|
| 49 | # filesystem implied by -fs option. |
---|
| 50 | # -F <fat_type> : FAT type. |
---|
| 51 | # -c <secs_per_clus> : count of sectors per cluster. |
---|
| 52 | # -n <fats_nr> : count of FAT copies. |
---|
| 53 | # -r <rsvd_secs_nr> : count of Reserved Region sectors. |
---|
| 54 | # -k <bbs> : Backup Boot Sector (0xFFFF if no backup). |
---|
| 55 | fsargs="-F 32 -c $secsperclus -n 1 -r $rsectors_nr -k 0xffff" |
---|
| 56 | |
---|
| 57 | # Invoking the appropriate MacOS command to get the work done. |
---|
| 58 | # -fs <filesystem> : causes a filesystem of type |
---|
| 59 | # <filesystem> to be written to the |
---|
| 60 | # image. |
---|
| 61 | # -volname <volname> : the newly created filesystem will |
---|
| 62 | # be named <volname>. |
---|
| 63 | # -layout <layout> : specifies the partition layout of the |
---|
| 64 | # image (NONE creates an image with no |
---|
| 65 | # partition map). |
---|
| 66 | # -sectors <sector_count> : specifies the size of the image file |
---|
| 67 | # in 512-byte sectors. |
---|
| 68 | # -ov : overwrites an existing file. |
---|
| 69 | # -type : specifies the format of empty |
---|
| 70 | # read/write images. |
---|
| 71 | hdiutil create -fs MS-DOS \ |
---|
| 72 | -volname virtualdisk \ |
---|
| 73 | -layout NONE \ |
---|
| 74 | -sectors $sectors_nr \ |
---|
| 75 | -ov \ |
---|
| 76 | -fsargs "$fsargs" \ |
---|
| 77 | -type "UDIF" \ |
---|
| 78 | $2 |
---|
| 79 | ;; |
---|
| 80 | |
---|
| 81 | # Attaching the disk image as a device. |
---|
| 82 | attach) |
---|
| 83 | hdiutil attach $2 |
---|
| 84 | ;; |
---|
| 85 | |
---|
| 86 | # Detaching the disk image and terminating any associated process. |
---|
| 87 | detach) |
---|
| 88 | hdiutil detach $2 |
---|
| 89 | ;; |
---|
| 90 | |
---|
| 91 | # Printing out information about the disk image. |
---|
| 92 | info) |
---|
| 93 | hdiutil imageinfo $2 |
---|
| 94 | ;; |
---|
| 95 | |
---|
| 96 | # The first argument of this script should tell it what to do. |
---|
| 97 | *) |
---|
| 98 | echo "Enter one of the following verbs as the first argument:" |
---|
| 99 | echo " - create <image> : create the new disk image <image>." |
---|
| 100 | echo " - attach <image> : attach the created disk image " |
---|
| 101 | echo " <image> to the system and mount it." |
---|
| 102 | echo " - detach <dev_name> : detach the device <dev_name>." |
---|
| 103 | echo " - info <image> : print out information about the " |
---|
| 104 | echo " <image> disk image." |
---|
| 105 | echo "" |
---|
| 106 | echo "EXAMPLES:" |
---|
| 107 | echo "./create_dmg create disk_image" |
---|
| 108 | echo "./create_dmg attach /path/to/disk_image.dmg" |
---|
| 109 | echo "./create_dmg detach /dev/disk5" |
---|
| 110 | echo "./create_dmg info /path/to/disk_image.dmg" |
---|
| 111 | ;; |
---|
| 112 | esac |
---|
| 113 | |
---|
| 114 | # If we are running on Linux: |
---|
| 115 | elif [[ $platform == "Linux" ]]; then |
---|
| 116 | case $1 in |
---|
| 117 | create) |
---|
| 118 | if [[ -z $2 ]]; then |
---|
| 119 | echo "Please enter the name of the virtual disk image to be " |
---|
| 120 | echo " created as the second argument of this script!" |
---|
| 121 | exit 1 |
---|
| 122 | fi |
---|
| 123 | |
---|
| 124 | echo "Creating disk image <$2>" |
---|
| 125 | let "sectors_nr = (sectors_nr - 1) / 2" |
---|
| 126 | |
---|
| 127 | # Looking for the appropriate Linux command which will perform the |
---|
| 128 | # work. |
---|
| 129 | CREATE_DISK=$(command -v mkfs.vfat || \ |
---|
| 130 | command -v /sbin/mkfs.vfat || \ |
---|
| 131 | command -v /usr/sbin/mkfs.vfat || \ |
---|
| 132 | command -v /usr/local/sbin/mkfs.vfat) |
---|
| 133 | |
---|
| 134 | if [[ -z ${CREATE_DISK} ]]; then |
---|
| 135 | echo "ERROR: command mkfs.vfat not found!" |
---|
| 136 | exit 1 |
---|
| 137 | fi |
---|
| 138 | |
---|
| 139 | # Invoking the previously found command to create an MS-DOS |
---|
| 140 | # filesystem on the device <device> of size <block_count>. |
---|
| 141 | # -C : creates the file given as <device> on the |
---|
| 142 | # command line and writes the to-be-created |
---|
| 143 | # filesystem to it. Used to create the new |
---|
| 144 | # filesystem in a file instead of on a real |
---|
| 145 | # device. |
---|
| 146 | # -F <fat_type> : specifies the type of FAT used. |
---|
| 147 | # -f <fats_nr> : specifies the number of FAT copies in the |
---|
| 148 | # filesystem. |
---|
| 149 | # -R <rsvd_secs_nr> : selects the number of reserved sectors. |
---|
| 150 | # -s <secs_per_clus> : specifies the number of disk sectors per |
---|
| 151 | # cluster. |
---|
| 152 | # -S <sector_size> : specifies the number of bytes per logical |
---|
| 153 | # sector. |
---|
| 154 | # -v : verbose execution. |
---|
| 155 | # <device> : special file corresponding to the device. |
---|
| 156 | # <block_count> : number of blocks on the the device. |
---|
| 157 | ${CREATE_DISK} -C \ |
---|
| 158 | -F 32 \ |
---|
| 159 | -f 1 \ |
---|
| 160 | -R $rsectors_nr \ |
---|
| 161 | -S $sector_size \ |
---|
| 162 | -s $secsperclus \ |
---|
| 163 | -v \ |
---|
| 164 | $2.dmg \ |
---|
| 165 | $sectors_nr \ |
---|
| 166 | ;; |
---|
| 167 | |
---|
| 168 | *) |
---|
| 169 | echo "Enter 'create' as the first argument to create a disk image." |
---|
| 170 | echo "" |
---|
| 171 | echo "EXAMPLES:" |
---|
| 172 | echo "./create_dmg create disk_image" |
---|
| 173 | ;; |
---|
| 174 | esac |
---|
| 175 | |
---|
| 176 | else |
---|
| 177 | echo "The following platform is not supported: $platform" |
---|
| 178 | fi |
---|