Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!


Click here to return to the 'Revised script...' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Revised script...
Authored by: jg on Mar 24, '02 11:01:43AM
Here's a moderately modified version of the same script. First of all, when running the script the user will be prompted for an administrator's password. I have also changed the calculations to allow for an overhead of 5 per cent above the source directory's size. The copy command has been changed from using cp to using ditto because of resource forks. Bear careful though, as ditto does not preserve unix permissions, in particular group information. The script could be made a little cleaner and newbie friendlier, but I think that if the user is not familiar with the command line, Disk Copy becomes a better proposition any way. ;-)
#!/bin/sh
#
#12/03/02- Modified script, originally posted by Mr. Sharumpe to Mac OS X Hints - JG
#
# Creates a disk image (dmg) on Mac OS X from the command line.
# usage:
# mkdmg
#
# Where is the name to use for the mounted image, is the version
# number of the volume and is where the contents to put on the dmg are.
#
# The result will be a file called -.dmg
#
if [ $# != 3 ]; then
echo "Usage: mkdmg volname vers srcdir"
exit 0
fi
VOL="$1"
VER="$2"
SRC="$3"
DMG="tmp-$VOL.dmg"

## The newfs command can only be run as root, so we need to prompt for an administrative
## password. By default, the admin group is listed in /etc/sudoers give its memebers the
## right to run commands as root, albeit still having to authenticate.
#
echo ">"
echo ""
echo ""
sudo -v

## Create temporary disk image and format, ejecting when done
#
echo ""
echo ">"

## The total size of the disk image is calculated in here to be 5% percent more than the
## total data size. This allows for the partition tables overhead (64 sectors, 512 bytes each)
## plus some spare.
#
SIZE=`du -sk ${SRC} | awk '{printf "%.3gn",($1*10.5)/(10240)}'`
echo "..................................................................................................."
echo ""
echo ">"
echo ""
hdiutil create "$DMG" -megabytes ${SIZE} -ov -type UDIF

## Create /dev entries but do not mount it, else the Finder will prompt to initialize the disk
#
DISK=`hdid -nomount "$DMG" |awk '/scheme/ {print substr ($1, 6, length)}'`

## Make a new filesystem and eject the disk
#
sudo newfs_hfs -v "$VOL" /dev/r${DISK}s2

hdiutil eject $DISK

## Mount and copy files onto volume, making sure that there are no locked files before running ditto
#
hdid "$DMG"
echo "..................................................................................................."
echo ""
chflags -R nouchg,noschg "${SRC}"
ditto -rsrcFork -v "${SRC}" "/Volumes/${VOL}" && echo ">"
echo ""
echo "..................................................................................................."
hdiutil eject $DISK

## Convert to compressed image, delete temp image if it exists
#
if [ -e "${VOL}-${VER}.dmg" ]; then
rm -f "${VOL}-${VER}.dmg"
fi
hdiutil convert "$DMG" -format UDZO -o "${VOL}-${VER}.dmg"
rm -f "$DMG"


[ Reply to This | # ]
Revised script...
Authored by: flumignan on Sep 25, '02 02:37:17PM

This seems to have broken under 10.2; disk images aren't being ejected like they used to. Any tips?



[ Reply to This | # ]