summaryrefslogtreecommitdiff
path: root/bootstrap/darwindiskimage
blob: 944832db7c4265bb4cb10545340d676feccafcc0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#!/bin/sh
#
# $NetBSD: darwindiskimage,v 1.2 2006/08/30 04:36:10 schmonz Exp $

_getdevice_and_halfway_mount()
{
	hdid -nomount "$1" | _getdevicebasename | tail -1
}

_getdevicebasename()
{
	awk '{print $1}' | sed -e 's|^/dev/||'
}

_normalize_filename()
{
	echo "$1" | sed -e 's|\.dmg$||' -e 's|$|.dmg|'
}

dmg_create()
{
	local fstype fs osmajor file mountedname megabytes device
	[ $# -eq 2 ] || die 1 "Usage: $0 create <file> <megabytes>"

	# Use case-sensitive HFS+ where available (Darwin >= 7)
	fstype='Apple_UFS'
	fs='UFS'
	osmajor=`uname -r | awk 'BEGIN {FS="."} {print $1}'`
	if [ ${osmajor} -ge 7 ]; then
		fstype='Apple_HFSX'
		fs='HFSX'
	fi

	file="`_normalize_filename \"$1\"`"
	mountedname="`basename \"${file}\" .dmg`"
	megabytes=$2

	# create
	hdiutil create -quiet "${file}" -megabytes ${megabytes} \
		-partitionType ${fstype} -layout SPUD -fs ${fs}

	# rename
	device=`_getdevice_and_halfway_mount "${file}"`
	hdiutil mount "${file}"
	disktool -n "${device}" "${mountedname}"
	hdiutil eject -quiet "${device}"
}

dmg_mount()
{
	local file device exitcode
	[ $# -eq 1 ] || die 1 "Usage: $0 mount <file>"

	file="`_normalize_filename \"$1\"`"

	hdiutil mount ${file}
}


dmg_umount()
{
	local mountpoint device
	[ $# -eq 1 ] || die 1 "Usage: $0 umount <mount-point>"

	mountpoint="$1"
	device=`mount | grep "${mountpoint} (local" | _getdevicebasename`

	[ "${device}" ] || die 1 "error: no device mounted at ${mountpoint}"

	hdiutil eject -quiet "${device}"
}

die()
{
	local exitcode
	exitcode=$1; shift
	warn "$@"
	exit ${exitcode}
}

warn()
{
	echo >&2 "$@"
}

try()
{
	exitcode=$1; shift
	action=$1; shift
	error=`"${action}" "$@" 2>&1` || die ${exitcode} "${error}"
}

main()
{
	[ $# -eq 0 ] && die 1 "Usage: $0 <create|mount|umount>"
	ACTION="$1"; shift
	case ${ACTION} in
		create|mount|umount)
			try 1 "dmg_${ACTION}" "$@"
			return 0
			;;
		*)
			die 1 "Usage: $0 <create|mount|umount>"
			;;
	esac
}

PATH=${PATH}:/sbin:/usr/sbin
main "$@"
exit $?