| 1 |
#!/usr/bin/env bash |
|---|
| 2 |
# $MirOS: ports/infrastructure/scripts/darwindiskimage,v 1.3 2007/07/02 13:18:21 tg Exp $ |
|---|
| 3 |
# $NetBSD: darwindiskimage,v 1.2 2006/08/30 04:36:10 schmonz Exp $ |
|---|
| 4 |
#- |
|---|
| 5 |
# Copyright (c) 2006 |
|---|
| 6 |
# Thorsten Glaser <tg@mirbsd.de> |
|---|
| 7 |
# Derived from NetBSD(R) pkgsrc(R) |
|---|
| 8 |
# |
|---|
| 9 |
# Provided that these terms and disclaimer and all copyright notices |
|---|
| 10 |
# are retained or reproduced in an accompanying document, permission |
|---|
| 11 |
# is granted to deal in this work without restriction, including un- |
|---|
| 12 |
# limited rights to use, publicly perform, distribute, sell, modify, |
|---|
| 13 |
# merge, give away, or sublicence. |
|---|
| 14 |
# |
|---|
| 15 |
# This work is provided "AS IS" and WITHOUT WARRANTY of any kind, to |
|---|
| 16 |
# the utmost extent permitted by applicable law, neither express nor |
|---|
| 17 |
# implied; without malicious intent or gross negligence. In no event |
|---|
| 18 |
# may a licensor, author or contributor be held liable for indirect, |
|---|
| 19 |
# direct, other damage, loss, or other issues arising in any way out |
|---|
| 20 |
# of dealing in the work, even if advised of the possibility of such |
|---|
| 21 |
# damage or existence of a defect, except proven that it results out |
|---|
| 22 |
# of said person's immediate fault when using the work as intended. |
|---|
| 23 |
#- |
|---|
| 24 |
# Use this to create a disk image, for use with a MirPorts Framework |
|---|
| 25 |
# installation on a default case-insensitive Mac OSX installation. |
|---|
| 26 |
# |
|---|
| 27 |
# $ /usr/bin/cvs -d anoncvs@anoncvs.mirbsd.org:/cvs co ports/infrastructure |
|---|
| 28 |
# $ cp ports/infrastructure/scripts/darwindiskimage ~ |
|---|
| 29 |
# $ bash ~/darwindiskimage create ~/Documents/MirPorts 512 |
|---|
| 30 |
# # Mebibytes - season to taste |
|---|
| 31 |
# $ bash ~/darwindiskimage mount ~/Documents/MirPorts |
|---|
| 32 |
# $ sudo chown $(id -u):$(id -g) /Volumes/MirPorts |
|---|
| 33 |
# $ cd /Volumes/MirPorts |
|---|
| 34 |
# $ /usr/bin/cvs -d anoncvs@anoncvs.mirbsd.org:/cvs co ports |
|---|
| 35 |
# $ bash ./Setup.sh -uel /Volumes/MirPorts/mpkg |
|---|
| 36 |
# $ . /Volumes/MirPorts/mpkg/db/SetEnv.sh |
|---|
| 37 |
# $ cd devel/cvs |
|---|
| 38 |
# $ mmake install clean |
|---|
| 39 |
# $ cd ../.. |
|---|
| 40 |
# $ /Volumes/MirPorts/mpkg/bin/cvs -Rq up -PAd |
|---|
| 41 |
# |
|---|
| 42 |
# To use: |
|---|
| 43 |
# $ bash ~/darwindiskimage mount ~/Documents/MirPorts |
|---|
| 44 |
# $ . /Volumes/MirPorts/mpkg/db/SetEnv.sh |
|---|
| 45 |
# $ cd /Volumes/MirPorts/ports/foo/bar; mmake install clean |
|---|
| 46 |
# |
|---|
| 47 |
# Have fun... |
|---|
| 48 |
|
|---|
| 49 |
|
|---|
| 50 |
_getdevice_and_halfway_mount() { |
|---|
| 51 |
hdid -nomount "$1" | _getdevicebasename | tail -1 |
|---|
| 52 |
} |
|---|
| 53 |
|
|---|
| 54 |
_getdevicebasename() { |
|---|
| 55 |
awk '{print $1}' | sed -e 's|^/dev/||' |
|---|
| 56 |
} |
|---|
| 57 |
|
|---|
| 58 |
_normalise_filename() { |
|---|
| 59 |
echo "$1" | sed -e 's|\.dmg$||' -e 's|$|.dmg|' |
|---|
| 60 |
} |
|---|
| 61 |
|
|---|
| 62 |
dmg_create() { |
|---|
| 63 |
local fstype fs osmajor file mountedname megabytes device |
|---|
| 64 |
[ $# -eq 2 ] || die 1 "Usage: $0 create <file> <megabytes>" |
|---|
| 65 |
|
|---|
| 66 |
# Use case-sensitive HFS+ where available (Darwin >= 7) |
|---|
| 67 |
fstype='Apple_UFS' |
|---|
| 68 |
fs='UFS' |
|---|
| 69 |
osmajor=`uname -r | awk 'BEGIN {FS="."} {print $1}'` |
|---|
| 70 |
if [ ${osmajor} -ge 7 ]; then |
|---|
| 71 |
fstype='Apple_HFSX' |
|---|
| 72 |
fs='HFSX' |
|---|
| 73 |
fi |
|---|
| 74 |
|
|---|
| 75 |
file="`_normalise_filename \"$1\"`" |
|---|
| 76 |
mountedname="`basename \"${file}\" .dmg`" |
|---|
| 77 |
megabytes=$2 |
|---|
| 78 |
|
|---|
| 79 |
# create |
|---|
| 80 |
hdiutil create -quiet "${file}" -megabytes ${megabytes} \ |
|---|
| 81 |
-partitionType ${fstype} -layout SPUD -fs ${fs} |
|---|
| 82 |
|
|---|
| 83 |
# rename |
|---|
| 84 |
device=`_getdevice_and_halfway_mount "${file}"` |
|---|
| 85 |
hdiutil mount "${file}" |
|---|
| 86 |
disktool -n "${device}" "${mountedname}" |
|---|
| 87 |
hdiutil eject -quiet "${device}" |
|---|
| 88 |
} |
|---|
| 89 |
|
|---|
| 90 |
dmg_mount() { |
|---|
| 91 |
local file device exitcode |
|---|
| 92 |
[ $# -eq 1 ] || die 1 "Usage: $0 mount <file>" |
|---|
| 93 |
|
|---|
| 94 |
file="`_normalise_filename \"$1\"`" |
|---|
| 95 |
|
|---|
| 96 |
hdiutil mount ${file} |
|---|
| 97 |
} |
|---|
| 98 |
|
|---|
| 99 |
|
|---|
| 100 |
dmg_umount() { |
|---|
| 101 |
local mountpoint device |
|---|
| 102 |
[ $# -eq 1 ] || die 1 "Usage: $0 umount <mount-point>" |
|---|
| 103 |
|
|---|
| 104 |
mountpoint="$1" |
|---|
| 105 |
device=`mount | grep "${mountpoint} (local" | _getdevicebasename` |
|---|
| 106 |
|
|---|
| 107 |
[ "${device}" ] || die 1 "error: no device mounted at ${mountpoint}" |
|---|
| 108 |
|
|---|
| 109 |
hdiutil eject -quiet "${device}" |
|---|
| 110 |
} |
|---|
| 111 |
|
|---|
| 112 |
die() { |
|---|
| 113 |
local exitcode |
|---|
| 114 |
exitcode=$1; shift |
|---|
| 115 |
warn "$@" |
|---|
| 116 |
exit ${exitcode} |
|---|
| 117 |
} |
|---|
| 118 |
|
|---|
| 119 |
warn() { |
|---|
| 120 |
echo >&2 "$@" |
|---|
| 121 |
} |
|---|
| 122 |
|
|---|
| 123 |
try() { |
|---|
| 124 |
exitcode=$1; shift |
|---|
| 125 |
action=$1; shift |
|---|
| 126 |
error=`"${action}" "$@" 2>&1` || die ${exitcode} "${error}" |
|---|
| 127 |
} |
|---|
| 128 |
|
|---|
| 129 |
main() { |
|---|
| 130 |
[ $# -eq 0 ] && die 1 "Usage: $0 <create|mount|umount>" |
|---|
| 131 |
ACTION="$1"; shift |
|---|
| 132 |
case ${ACTION} in |
|---|
| 133 |
create|mount|umount) |
|---|
| 134 |
try 1 "dmg_${ACTION}" "$@" |
|---|
| 135 |
return 0 |
|---|
| 136 |
;; |
|---|
| 137 |
*) |
|---|
| 138 |
die 1 "Usage: $0 <create|mount|umount>" |
|---|
| 139 |
;; |
|---|
| 140 |
esac |
|---|
| 141 |
} |
|---|
| 142 |
|
|---|
| 143 |
PATH=${PATH}:/sbin:/usr/sbin |
|---|
| 144 |
main "$@" |
|---|
| 145 |
exit $? |
|---|