-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathremount.sh
executable file
·89 lines (71 loc) · 2.42 KB
/
remount.sh
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
#!/bin/bash
# EXPERIMENTAL script
# Based on https://github.com/EthanArbuckle/Tmpfs-Overlay
# This tool needs to be run with sudo. It may need to be given Full Disk Access or Developer Tools permissions.
# Function to mount tmpfs to the target directory
mount_tmpfs_to_target_dir() {
local target_dir_path="$1"
if sudo mount_tmpfs "$target_dir_path"; then
return 0 # success
else
echo "An error occurred while mounting tmpfs onto $target_dir_path"
return 1
fi
}
# Function to check if tmpfs is mounted on the target directory
tmpfs_mounted_on_directory() {
local target_dir_path="$1"
mount_output=$(mount)
if echo "$mount_output" | grep -q "tmpfs on $target_dir_path"; then
return 0
else
return 1
fi
}
# Function to set up an overlay on the target directory
setup_overlay_on_directory() {
local target_dir_path="$1"
if [ ! -d "$target_dir_path" ]; then
echo "Cannot set up overlay on non-existent directory: $target_dir_path"
return 1
fi
if tmpfs_mounted_on_directory "$target_dir_path"; then
echo "tmpfs is already mounted on $target_dir_path"
return 0 # success (no need to mount again)
fi
# Create a temporary directory to copy contents to
temp_dir=$(mktemp -d -p /tmp)
if [ ! -d "$temp_dir" ]; then
echo "Failed to create temporary directory"
return 1
fi
echo "Created temporary directory: $temp_dir"
# Copy contents of the target directory to the temporary directory
echo "Copying contents of $target_dir_path to temporary directory"
rsync -a "$target_dir_path"/ "$temp_dir"/
# Create a tmpfs mount at the target directory
if ! mount_tmpfs_to_target_dir "$target_dir_path"; then
return 1
fi
# Move the contents of the temporary directory back to the tmpfs mount
echo "Moving contents back to $target_dir_path"
sudo rsync -a --exclude='*fsevent*' "$temp_dir"/ "$target_dir_path"/
# Clean up the temporary directory
rm -rf "$temp_dir"
return 0
}
# Main script execution
if [ $# -lt 1 ]; then
echo "Usage: $0 <path>"
exit 1
fi
input_path="$1"
if [ ! -d "$input_path" ]; then
echo "Error: $input_path does not exist"
exit 1
fi
if ! setup_overlay_on_directory "$input_path"; then
echo "Failed to set up overlay on directory $input_path"
exit 1
fi
echo "Successfully set up overlay on directory $input_path"