#!/bin/sh ## # Make a disk image # # $Id$ ## # Copyright (c) 2002 Wilfredo Sanchez Vega, wsanchez@mit.edu. # All rights reserved. # # Permission to use, copy, modify, and distribute this software for # any purpose with or without fee is hereby granted, provided that the # above copyright notice, this permission, and the following # disclaimer notice appear in all copies. # # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL # WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED # WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE # AUTHORS BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR # CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS # OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, # NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION # WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. ## set -u set -e usage() { echo "$(basename $0): usage: $(basename $0) target source" echo " target specifies target volume name and image name" echo " source specifies source directory to place at root of volume" echo " Do not include .dmg extention in target" exit 1 } if [ $# != 0 ]; then Target=$1 && shift; else usage; fi if [ $# != 0 ]; then Source=$1 && shift; else usage; fi if [ $# != 0 ]; then usage; fi if [ -e "${Target}.dmg" ]; then echo "${Target}.dmg exists." exit 1 fi # Get a unique temporary image file TempImage="/tmp/make_image_dmg.$$" while [ -e "${TempImage}.dmg" ]; do TempImage="${TempImage}x"; done TempImage="${TempImage}.dmg" # Get size in MB of the source directory, add 4 Size=$(($(du -s "${Source}" | cut -f 1) / 1024 + 4)) # Create a disk image of the right size hdiutil create -megabytes "${Size}" -layout NONE "${TempImage}" # Attach the image to a device node without mounting it # The cut is needed because hdid spits out crap after the device name DiskDevice=$(hdid -nomount "${TempImage}" | cut -f 1 -d ' ') # Format the mounted volume as HFS+ newfs_hfs -v "${Target}" "${DiskDevice}" # Get a unique temporary mount point MountPoint="/tmp/make_image_mnt.$$" while [ -e "${MountPoint}" ]; do MountPoint="${MountPoint}x"; done # Mount the image mkdir -p "${MountPoint}" mount -t hfs "${DiskDevice}" "${MountPoint}" # Copy data pax -s '|'"${Source}"'||' -rw "${Source}" "${MountPoint}" # Umount the image umount "${DiskDevice}" rm -rf "${MountPoint}" # Detach the device hdiutil eject "${DiskDevice}" # Create a read-only version, use zlib compression hdiutil convert -format UDZO "${TempImage}" -o "${Target}" # Delete the temporary image rm -f "${TempImage}"