Created
February 21, 2024 12:48
-
-
Save noomorph/6d2e943d8dd7a5c03b3c12ee51a4e2d0 to your computer and use it in GitHub Desktop.
Serve *.tar as RAM disk
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Path to the tar archive | |
TarPath="$1" | |
# Ensure a tar path is provided | |
if [[ -z "$TarPath" || ! -f "$TarPath" ]]; then | |
echo "Usage: $0 <path_to_tar>" | |
exit 1 | |
fi | |
# Extract the basename of the tar file and remove the .tar extension for the RAM disk name | |
RamDiskName=$(basename "$TarPath" .tar) | |
# Step 1: Measure the size of the tar file using du | |
# -k option makes du report size in kilobytes | |
SizeInKB=$(du -k "$TarPath" | cut -f1) | |
SizeInMB=$((SizeInKB / 1024)) | |
# Add a small buffer to account for filesystem overhead and tar metadata | |
# This is a rough estimation; adjust the buffer size as needed | |
BufferMB=10 | |
TotalSizeMB=$((SizeInMB + BufferMB)) | |
# Convert size to blocks (512 bytes per block) | |
SizeInBlocks=$((TotalSizeMB * 2048)) | |
# Step 2: Create and mount a RAM disk | |
echo "Creating a RAM disk named '$RamDiskName' of size ${TotalSizeMB}MB..." | |
DiskInfo=$(hdiutil attach -nomount ram://$SizeInBlocks) | |
Device=$(echo "$DiskInfo" | cut -f 1) | |
# Step 3: Format the RAM disk (HFS+ format) | |
echo "Formatting the RAM disk..." | |
diskutil eraseVolume HFS+ "$RamDiskName" $Device | |
# Step 4: Extract the tar archive into the RAM disk | |
echo "Extracting the tar archive to the RAM disk..." | |
tar -xf "$TarPath" -C /Volumes/"$RamDiskName" | |
echo "Tar contents are now available in /Volumes/$RamDiskName" | |
# Cleanup function to detach the RAM disk when done | |
cleanup() { | |
echo "Detaching RAM disk '$RamDiskName'..." | |
hdiutil detach $Device | |
} | |
# Trap script exit for cleanup | |
trap cleanup EXIT | |
# Keep the script running until manually stopped to keep the RAM disk mounted | |
echo "Press Ctrl+C to detach the RAM disk and exit..." | |
while true; do sleep 1; done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment