#!/bin/bash # Author: Cesar FUGUET # Date : Novembre 2013 clusters=65536 sector_size=512 sectors_per_cluster=8 reserved_sectors=32 let "data_sectors = (clusters - 2) * sectors_per_cluster" let "fat_sectors = (clusters * 4) / 512" let "sectors=data_sectors + fat_sectors + reserved_sectors" platform=`uname` echo "Detected platform: $platform" if [[ $platform == "Darwin" ]]; then case $1 in create ) # Create disk image # -fs : use filesystem # -volname : use volume name # -srcfolder : copy every file in into image # -format : format of image. UDRW (UDIF read/write image) # -sectors : size of image in 512-byte sectors # -ov : overwrite existing image file # -fargs : additional args to pass to newfs program # -F : FAT type (FAT32) # -c : sector per cluster # -n : number of FAT if [[ -z $2 ]]; then echo "Create action need second argument: " exit 1; fi; if [[ -z $3 ]]; then echo "Create action need third argument: " exit 1; fi; # RAW DISK IMAGE READ-ONLY # The not used space is not considered (smaller disk image) # format="UDRO" # RAW DISK IMAGE READ-WRITE # None compression is made format="UDRW" echo "Creating partition file $3 from $2 directory" hdiutil create \ -fs MS-DOS \ -volname virtualdisk \ -srcfolder $2 \ -sectors $sectors \ -ov \ -fsargs "-F32 -c$sectors_per_cluster -n1 -r$reserved_sectors" \ -format $format \ $3; # -verbose \ echo "Removing first sector of diskimage" if [[ -e "/tmp" ]]; then if [[ -e "/tmp/diskimage" ]]; then echo "Warning /tmp/diskimage exists already. Rename it or" echo "remove it before executing this script" exit fi; dd if=$3.dmg of=/tmp/diskimage iseek=1 oseek=0 mv /tmp/diskimage $3.dmg fi;; attach ) # Attach created disk image to the system and mount it hdiutil attach $2;; detach ) # Detach attached device. Must pass as parameter the /dev/ hdiutil detach $2;; info ) hdiutil imageinfo $2;; * ) echo "Pass the command as the first parameter: " echo " - create: create disk image" echo " First parameter after create action is the path" echo " of the directory to copy in the created " echo " partition" echo " - attach: attach created disk image to the system and " echo " mount it (arg: path to disk_image)" echo " - detach: detach specified device " echo " (arg: path to /dev/)" echo " - info : print information about created disk image" echo " (arg: path to disk_image)" echo "" echo "EXAMPLES:" echo " ./create_diskimage.sh create /path/to/source_dir disk_image" echo " ./create_diskimage.sh attach /path/to/disk_image.dmg" echo " ./create_diskimage.sh detach /dev/disk5" echo " ./create_diskimage.sh info /path/to/disk_image.dmg" esac; elif [[ $platform == "Linux" ]]; then echo "Creating partion file: partition.dmg" mkfs.vfat -C -F32 -S $sector_size -s $sectors_per_cluster \ partition.dmg $sectors echo "Copying files into the partition" mcopy -s -i partition.dmg build ::// mcopy -i partition.dmg map.bin ::// echo "Copying MBR on the first sector" dd if=mbr.dmg of=disk.dmg count=1 bs=$sector_size echo "Copying partition file on the sector 200 of the disk" dd if=partition.dmg of=disk.dmg seek=200 bs=$sector_size else echo "Your platform is not supported: $platform" fi;