Skip to content

Instantly share code, notes, and snippets.

@islammohamed
Forked from koshigoe/mount-ram.sh
Created May 12, 2016 07:52
Show Gist options
  • Select an option

  • Save islammohamed/3e62d149b73f8b19a24d82c7e32b4130 to your computer and use it in GitHub Desktop.

Select an option

Save islammohamed/3e62d149b73f8b19a24d82c7e32b4130 to your computer and use it in GitHub Desktop.

Revisions

  1. koshigoe revised this gist Feb 11, 2011. 1 changed file with 7 additions and 7 deletions.
    14 changes: 7 additions & 7 deletions mount-ram.sh
    Original file line number Diff line number Diff line change
    @@ -1,16 +1,15 @@
    #!/bin/sh

    # This program has two features.
    # This program has two feature.
    #
    # 1. Create a disk image on RAM.
    # 2. Mount that disk image.
    #
    # Usage:
    # $0 <dir> <sector>
    # $0 <dir> <size>
    #
    # sector:
    # The `sector' is block unit (1 sector = 512 B).
    # This value used when creat a disk image.
    # size:
    # The `size' is a size of disk image (MB).
    #
    # dir:
    # The `dir' is a directory, the dir is used to mount the disk image.
    @@ -20,15 +19,16 @@
    #

    mount_point=${1}
    sectors=${2:-128000}
    size=${2:-64}

    mkdir -p $mount_point
    if [ $? -ne 0 ]; then
    echo "The mount point didn't available." >&2
    exit $?
    fi

    device_name=$(hdid -nomount "ram://${sectors}" | awk '{print $1}')
    sector=$(expr $size \* 1024 \* 1024 / 512)
    device_name=$(hdid -nomount "ram://${sector}" | awk '{print $1}')
    if [ $? -ne 0 ]; then
    echo "Could not create disk image." >&2
    exit $?
  2. koshigoe revised this gist Feb 11, 2011. 2 changed files with 2 additions and 2 deletions.
    2 changes: 1 addition & 1 deletion mount-ram.sh
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    #!/bin/sh

    # This program has two feature.
    # This program has two features.
    #
    # 1. Create a disk image on RAM.
    # 2. Mount that disk image.
    2 changes: 1 addition & 1 deletion umount-ram.sh
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    #!/bin/sh

    # This program has two feature.
    # This program has two features.
    #
    # 1. Unmount a disk image.
    # 2. Detach the disk image from RAM.
  3. koshigoe created this gist Feb 11, 2011.
    47 changes: 47 additions & 0 deletions mount-ram.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,47 @@
    #!/bin/sh

    # This program has two feature.
    #
    # 1. Create a disk image on RAM.
    # 2. Mount that disk image.
    #
    # Usage:
    # $0 <dir> <sector>
    #
    # sector:
    # The `sector' is block unit (1 sector = 512 B).
    # This value used when creat a disk image.
    #
    # dir:
    # The `dir' is a directory, the dir is used to mount the disk image.
    #
    # See also:
    # - hdid(8)
    #

    mount_point=${1}
    sectors=${2:-128000}

    mkdir -p $mount_point
    if [ $? -ne 0 ]; then
    echo "The mount point didn't available." >&2
    exit $?
    fi

    device_name=$(hdid -nomount "ram://${sectors}" | awk '{print $1}')
    if [ $? -ne 0 ]; then
    echo "Could not create disk image." >&2
    exit $?
    fi

    newfs_hfs $device_name > /dev/null
    if [ $? -ne 0 ]; then
    echo "Could not format disk image." >&2
    exit $?
    fi

    mount -t hfs $device_name $mount_point
    if [ $? -ne 0 ]; then
    echo "Could not mount disk image." >&2
    exit $?
    fi
    37 changes: 37 additions & 0 deletions umount-ram.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,37 @@
    #!/bin/sh

    # This program has two feature.
    #
    # 1. Unmount a disk image.
    # 2. Detach the disk image from RAM.
    #
    # Usage:
    # $0 <dir>
    #
    # dir:
    # The `dir' is a directory, the dir is mounting a disk image.
    #
    # See also:
    # - hdid(8)
    #

    mount_point=$1
    if [ ! -d "${mount_point}" ]; then
    echo "The mount point didn't available." >&2
    exit 1
    fi
    mount_point=$(cd $mount_point && pwd)

    device_name=$(df "${mount_point}" 2>/dev/null | tail -1 | grep "${mount_point}" | cut -d' ' -f1)
    if [ -z "${device_name}" ]; then
    echo "The mount point didn't mount disk image." >&2
    exit 1
    fi

    umount "${mount_point}"
    if [ $? -ne 0 ]; then
    echo "Could not unmount." >&2
    exit $?
    fi

    hdiutil detach -quiet $device_name