[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 |
---|
| 8 | reserved_sectors=32 |
---|
| 9 | |
---|
| 10 | |
---|
| 11 | let "data_sectors = (clusters - 2) * sectors_per_cluster" |
---|
| 12 | let "fat_sectors = (clusters * 4) / 512" |
---|
| 13 | let "sectors=data_sectors + fat_sectors + reserved_sectors" |
---|
| 14 | |
---|
| 15 | platform=`uname` |
---|
| 16 | echo "Detected platform: $platform" |
---|
| 17 | |
---|
| 18 | if [[ $platform == "Darwin" ]]; then |
---|
| 19 | case $1 in |
---|
| 20 | create ) |
---|
| 21 | # Create disk image |
---|
| 22 | # -fs <fs> : use filesystem <fs> |
---|
| 23 | # -volname <name> : use volume name <name> |
---|
| 24 | # -srcfolder <src> : copy every file in <src> into image |
---|
| 25 | # -format <fmt> : format of image. UDRW (UDIF read/write image) |
---|
| 26 | # -sectors <sect> : size of image in 512-byte sectors |
---|
| 27 | # -ov : overwrite existing image file |
---|
| 28 | # -fargs <args> : additional args to pass to newfs program |
---|
| 29 | # -F : FAT type (FAT32) |
---|
| 30 | # -c : sector per cluster |
---|
| 31 | # -n : number of FAT |
---|
| 32 | |
---|
| 33 | if [[ -z $2 ]]; then |
---|
| 34 | echo "Create action need second argument: <srcdir_path>" |
---|
| 35 | exit 1; |
---|
| 36 | fi; |
---|
| 37 | |
---|
| 38 | if [[ -z $3 ]]; then |
---|
| 39 | echo "Create action need third argument: <disk_image>" |
---|
| 40 | exit 1; |
---|
| 41 | fi; |
---|
| 42 | |
---|
| 43 | # RAW DISK IMAGE READ-ONLY |
---|
| 44 | # The not used space is not considered (smaller disk image) |
---|
| 45 | # format="UDRO" |
---|
| 46 | |
---|
| 47 | # RAW DISK IMAGE READ-WRITE |
---|
| 48 | # None compression is made |
---|
| 49 | format="UDRW" |
---|
| 50 | |
---|
| 51 | echo "Creating partition file $3 from $2 directory" |
---|
| 52 | hdiutil create \ |
---|
| 53 | -fs MS-DOS \ |
---|
| 54 | -volname virtualdisk \ |
---|
| 55 | -srcfolder $2 \ |
---|
| 56 | -sectors $sectors \ |
---|
| 57 | -ov \ |
---|
| 58 | -fsargs "-F32 -c$sectors_per_cluster -n1 -r$reserved_sectors" \ |
---|
| 59 | -format $format \ |
---|
| 60 | $3; |
---|
| 61 | # -verbose \ |
---|
| 62 | |
---|
| 63 | echo "Removing first sector of diskimage" |
---|
| 64 | |
---|
| 65 | if [[ -e "/tmp" ]]; then |
---|
| 66 | if [[ -e "/tmp/diskimage" ]]; then |
---|
| 67 | echo "Warning /tmp/diskimage exists already. Rename it or" |
---|
| 68 | echo "remove it before executing this script" |
---|
| 69 | exit |
---|
| 70 | fi; |
---|
| 71 | |
---|
| 72 | dd if=$3.dmg of=/tmp/diskimage iseek=1 oseek=0 |
---|
| 73 | mv /tmp/diskimage $3.dmg |
---|
| 74 | fi;; |
---|
| 75 | |
---|
| 76 | attach ) |
---|
| 77 | # Attach created disk image to the system and mount it |
---|
| 78 | hdiutil attach $2;; |
---|
| 79 | |
---|
| 80 | detach ) |
---|
| 81 | # Detach attached device. Must pass as parameter the /dev/<device> |
---|
| 82 | hdiutil detach $2;; |
---|
| 83 | |
---|
| 84 | info ) |
---|
| 85 | hdiutil imageinfo $2;; |
---|
| 86 | |
---|
| 87 | * ) |
---|
| 88 | echo "Pass the command as the first parameter: " |
---|
| 89 | echo " - create: create disk image" |
---|
| 90 | echo " First parameter after create action is the path" |
---|
| 91 | echo " of the directory to copy in the created " |
---|
| 92 | echo " partition" |
---|
| 93 | echo " - attach: attach created disk image to the system and " |
---|
| 94 | echo " mount it (arg: path to disk_image)" |
---|
| 95 | echo " - detach: detach specified device " |
---|
| 96 | echo " (arg: path to /dev/<device>)" |
---|
| 97 | echo " - info : print information about created disk image" |
---|
| 98 | echo " (arg: path to disk_image)" |
---|
| 99 | echo "" |
---|
| 100 | echo "EXAMPLES:" |
---|
| 101 | echo " ./create_diskimage.sh create /path/to/source_dir disk_image" |
---|
| 102 | echo " ./create_diskimage.sh attach /path/to/disk_image.dmg" |
---|
| 103 | echo " ./create_diskimage.sh detach /dev/disk5" |
---|
| 104 | echo " ./create_diskimage.sh info /path/to/disk_image.dmg" |
---|
| 105 | esac; |
---|
| 106 | |
---|
| 107 | elif [[ $platform == "Linux" ]]; then |
---|
| 108 | echo "Creating partion file: partition.dmg" |
---|
| 109 | mkfs.vfat -C -F32 -S $sector_size -s $sectors_per_cluster \ |
---|
| 110 | partition.dmg $sectors |
---|
| 111 | |
---|
| 112 | echo "Copying files into the partition" |
---|
| 113 | mcopy -s -i partition.dmg build ::// |
---|
| 114 | mcopy -i partition.dmg map.bin ::// |
---|
| 115 | |
---|
| 116 | echo "Copying MBR on the first sector" |
---|
| 117 | dd if=mbr.dmg of=disk.dmg count=1 bs=$sector_size |
---|
| 118 | |
---|
| 119 | echo "Copying partition file on the sector 200 of the disk" |
---|
| 120 | dd if=partition.dmg of=disk.dmg seek=200 bs=$sector_size |
---|
| 121 | |
---|
| 122 | else |
---|
| 123 | echo "Your platform is not supported: $platform" |
---|
| 124 | fi; |
---|
| 125 | |
---|