mirror of
https://github.com/insidegui/VirtualBuddy.git
synced 2026-12-02 01:10:04 +08:00
111 lines
4.0 KiB
Bash
Executable File
111 lines
4.0 KiB
Bash
Executable File
#!/bin/sh
|
|
|
|
: '
|
|
This script is used by VirtualBuddy to dynamically generate a disk image that can be mounted in a virtual machine,
|
|
containing the current version of the VirtualBuddyGuest app embedded into VirtualBuddy itself.
|
|
|
|
Images are stored in ~/Library/Application Support/VirtualBuddy/_GuestImage.
|
|
|
|
Alongside the images, the app stores a digest of the entire contents of the Guest app bundle,
|
|
so that it can be automatically updated whenever something changes in the Guest app.
|
|
'
|
|
|
|
GUEST_APP_PATH="$1"
|
|
GUEST_APP_DIGEST="$2"
|
|
GUEST_SIZE_ALLOCATION="$3"
|
|
GUEST_DMG_NAME="$4"
|
|
|
|
if [ -z "$GUEST_APP_PATH" ]; then
|
|
echo "Shell script invocation error: missing GUEST_APP_PATH value as first argument" 1>&2
|
|
exit 7
|
|
fi
|
|
|
|
if [ -z "$GUEST_APP_DIGEST" ]; then
|
|
echo "Shell script invocation error: missing GUEST_APP_DIGEST value as second argument" 1>&2
|
|
exit 7
|
|
fi
|
|
|
|
if [ -z "$GUEST_SIZE_ALLOCATION" ]; then
|
|
echo "Shell script invocation error: missing GUEST_SIZE_ALLOCATION value as third argument" 1>&2
|
|
exit 7
|
|
fi
|
|
|
|
if [ -z "$GUEST_DMG_NAME" ]; then
|
|
echo "Shell script invocation error: missing GUEST_DMG_NAME value as fourth argument" 1>&2
|
|
exit 7
|
|
fi
|
|
|
|
if [ ! -d "$GUEST_APP_PATH" ]; then
|
|
echo "Shell script invocation error: guest app bundle doesn't exist at $GUEST_APP_PATH" 1>&2
|
|
exit 7
|
|
fi
|
|
|
|
VBROOT="$HOME/Library/Application Support/VirtualBuddy"
|
|
GUEST_DMG_DEST_PATH="$VBROOT/_GuestImage"
|
|
|
|
GUEST_STAGING_PATH="$GUEST_DMG_DEST_PATH/staging_$GUEST_DMG_NAME"
|
|
GUEST_TEMP_MOUNT_PATH="$GUEST_STAGING_PATH/$GUEST_DMG_NAME"
|
|
GUEST_TEMP_DMG_PATH="$GUEST_STAGING_PATH/$GUEST_DMG_NAME.dmg"
|
|
|
|
GUEST_TEMP_DIGEST_PATH="$GUEST_TEMP_MOUNT_PATH/$GUEST_DMG_NAME.digest"
|
|
|
|
# Make sure the temporary mount point exists
|
|
|
|
mkdir -p "$GUEST_TEMP_MOUNT_PATH" 2>/dev/null || echo ""
|
|
|
|
# Unmount and remove any leftovers from previous script invocation
|
|
|
|
hdiutil detach -force "$GUEST_TEMP_MOUNT_PATH" 2>/dev/null || echo ""
|
|
|
|
rm "$GUEST_TEMP_DMG_PATH" 2>/dev/null || echo ""
|
|
|
|
# Create blank disk image
|
|
|
|
hdiutil create -layout MBRSPUD -size $GUEST_SIZE_ALLOCATION -fs HFS+ -volname Guest "$GUEST_TEMP_DMG_PATH" || \
|
|
{ echo "Failed to create VirtualBuddyGuest disk image: hdiutil exit code $?" 1>&2; exit 1; }
|
|
|
|
# Mount image at staging location
|
|
|
|
hdiutil attach -imagekey diskimage-class=CRawDiskImage -noverify "$GUEST_TEMP_DMG_PATH" -mountpoint "$GUEST_TEMP_MOUNT_PATH" || \
|
|
{ echo "Failed to mount empty VirtualBuddyGuest disk image: hdiutil exit code $?" 1>&2; exit 1; }
|
|
|
|
# Write digest to temporary mount
|
|
|
|
echo "$GUEST_APP_DIGEST" > "$GUEST_TEMP_DIGEST_PATH"
|
|
|
|
# Copy VirtualBuddyGuest.app into the temporary mount
|
|
|
|
cp -R "$GUEST_APP_PATH" "$GUEST_TEMP_MOUNT_PATH/" || \
|
|
{ echo "Failed to copy VirtualBuddyGuest.app into disk image: exit code $?" 1>&2; exit 1; }
|
|
|
|
# Eject the disk image
|
|
|
|
hdiutil detach -force "$GUEST_TEMP_MOUNT_PATH" || \
|
|
{ echo "Failed to eject VirtualBuddyGuest disk image: exit code $?" 1>&2; exit 1; }
|
|
|
|
# Remove any extended attributes from the disk image
|
|
|
|
xattr -cr "$GUEST_TEMP_DMG_PATH" 2>/dev/null || echo ""
|
|
|
|
# Move the finalized disk image to its final destination
|
|
# This must replace the existing image instead of writing over it, because the existing image might be attached to a running virtual machine,
|
|
# which would end up reading a mix of old and new contents if the file were to be modified in place
|
|
|
|
mv -f "$GUEST_TEMP_DMG_PATH" "$GUEST_DMG_DEST_PATH/$GUEST_DMG_NAME.dmg" || \
|
|
{ echo "Failed to move finalized disk image: exit code $?" 1>&2; exit 1; }
|
|
|
|
# Write the digest to its final destination
|
|
# This is done after the image is in place so that the digest never describes an image that's not there
|
|
|
|
GUEST_STAGING_DIGEST_PATH="$GUEST_STAGING_PATH/$GUEST_DMG_NAME.digest"
|
|
|
|
{ echo "$GUEST_APP_DIGEST" > "$GUEST_STAGING_DIGEST_PATH" && mv -f "$GUEST_STAGING_DIGEST_PATH" "$GUEST_DMG_DEST_PATH/$GUEST_DMG_NAME.digest"; } || \
|
|
{ echo "Failed to write guest digest: exit code $?" 1>&2; } # Failure to write digest is non-fatal
|
|
|
|
# Cleanup
|
|
|
|
rm -Rf "$GUEST_TEMP_MOUNT_PATH" 2>/dev/null || echo ""
|
|
rm -Rf "$GUEST_STAGING_PATH" 2>/dev/null || echo ""
|
|
|
|
echo "OK"
|