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=32 |
---|
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 | echo "Creating partition file $3 from $2 directory" |
---|
62 | hdiutil create \ |
---|
63 | -fs MS-DOS \ |
---|
64 | -volname virtualdisk \ |
---|
65 | -srcfolder $2 \ |
---|
66 | -sectors $sectors \ |
---|
67 | -ov \ |
---|
68 | -fsargs "-F32 -c$sectors_per_cluster -n1 -r$reserved_sectors" \ |
---|
69 | -format $format \ |
---|
70 | $3; |
---|
71 | # -verbose \ |
---|
72 | |
---|
73 | echo "Removing first sector of diskimage" |
---|
74 | |
---|
75 | if [[ -e "/tmp" ]]; then |
---|
76 | if [[ -e "/tmp/diskimage" ]]; then |
---|
77 | echo "Warning /tmp/diskimage exists already. Rename it or" |
---|
78 | echo "remove it before executing this script" |
---|
79 | exit |
---|
80 | fi; |
---|
81 | |
---|
82 | dd if=$3.dmg of=/tmp/diskimage iseek=1 oseek=0 |
---|
83 | mv /tmp/diskimage $3.dmg |
---|
84 | fi;; |
---|
85 | |
---|
86 | attach ) |
---|
87 | # Attach created disk image to the system and mount it |
---|
88 | hdiutil attach $2;; |
---|
89 | |
---|
90 | detach ) |
---|
91 | # Detach attached device. Must pass as parameter the /dev/<device> |
---|
92 | hdiutil detach $2;; |
---|
93 | |
---|
94 | info ) |
---|
95 | hdiutil imageinfo $2;; |
---|
96 | |
---|
97 | * ) |
---|
98 | echo "Pass the command as the first parameter: " |
---|
99 | echo " - create: create disk image" |
---|
100 | echo " First parameter after create action is the path" |
---|
101 | echo " of the directory to copy in the created " |
---|
102 | echo " partition" |
---|
103 | echo " - attach: attach created disk image to the system and " |
---|
104 | echo " mount it (arg: path to disk_image)" |
---|
105 | echo " - detach: detach specified device " |
---|
106 | echo " (arg: path to /dev/<device>)" |
---|
107 | echo " - info : print information about created disk image" |
---|
108 | echo " (arg: path to disk_image)" |
---|
109 | echo "" |
---|
110 | echo "EXAMPLES:" |
---|
111 | echo " ./create_diskimage.sh create /path/to/source_dir disk_image" |
---|
112 | echo " ./create_diskimage.sh attach /path/to/disk_image.dmg" |
---|
113 | echo " ./create_diskimage.sh detach /dev/disk5" |
---|
114 | echo " ./create_diskimage.sh info /path/to/disk_image.dmg" |
---|
115 | esac; |
---|
116 | |
---|
117 | elif [[ $platform == "Linux" ]]; then |
---|
118 | case $1 in |
---|
119 | create ) |
---|
120 | if [[ -z $2 ]]; then |
---|
121 | echo "Create action need second argument: <srcdir_path>" |
---|
122 | exit 1; |
---|
123 | fi; |
---|
124 | |
---|
125 | if [[ -z $3 ]]; then |
---|
126 | echo "Create action need third argument: <disk_image>" |
---|
127 | exit 1; |
---|
128 | fi; |
---|
129 | |
---|
130 | echo "Creating partition file: $3" |
---|
131 | |
---|
132 | let "sectors = (sectors - 1) / 2" |
---|
133 | mkfs.vfat \ |
---|
134 | -C \ |
---|
135 | -F32 \ |
---|
136 | -f 1 \ |
---|
137 | -R $reserved_sectors \ |
---|
138 | -S $sector_size \ |
---|
139 | -s $sectors_per_cluster \ |
---|
140 | -v \ |
---|
141 | $3.dmg $sectors |
---|
142 | |
---|
143 | if [[ -e $2 ]]; then |
---|
144 | echo "Copying files from $2 into partition" |
---|
145 | mcopy -s -i $3.dmg $2/* ::// |
---|
146 | fi;; |
---|
147 | |
---|
148 | * ) |
---|
149 | echo "Pass the command as the first parameter: " |
---|
150 | echo " - create: create disk image" |
---|
151 | echo " First parameter after create action is the path" |
---|
152 | echo " of the directory to copy in the created " |
---|
153 | echo " partition" |
---|
154 | echo "" |
---|
155 | echo "EXAMPLES:" |
---|
156 | echo " ./create_diskimage.sh create /path/to/source_dir disk_image" |
---|
157 | esac; |
---|
158 | |
---|
159 | else |
---|
160 | echo "Your platform is not supported: $platform" |
---|
161 | fi; |
---|
162 | |
---|