Skip to content

Instantly share code, notes, and snippets.

@Nugget2269
Created May 26, 2024 12:07
Show Gist options
  • Select an option

  • Save Nugget2269/b25fb0eacdd03c2431ecba0101c39ebb to your computer and use it in GitHub Desktop.

Select an option

Save Nugget2269/b25fb0eacdd03c2431ecba0101c39ebb to your computer and use it in GitHub Desktop.
writable dwarfs mount
#!/bin/bash
# Define paths
DWARFS_FILE="./new_file.dwarfs"
MOUNT_POINT="./mount"
READONLY_DIR="${MOUNT_POINT}/readonly"
UPPER_DIR="${MOUNT_POINT}/upper"
WORK_DIR="${MOUNT_POINT}/work"
MERGED_DIR="${MOUNT_POINT}/merged"
NEW_DWARFS_FILE="./new_file.dwarfs"
# Ensure necessary directories exist and have correct permissions
sudo mkdir -p "$READONLY_DIR" "$UPPER_DIR" "$WORK_DIR" "$MERGED_DIR"
# sudo chown -R root:root "$MOUNT_POINT"
# sudo chmod -R 755 "$MOUNT_POINT"
sudo chmod a+rw "$MOUNT_POINT"
# Function to mount and setup overlayfs
mount_dwarfs() {
echo "Creating a backup of DwarFS."
cp -f "$DWARFS_FILE" "${DWARFS_FILE}.bak"
echo "Mounting DwarFS filesystem..."
sudo dwarfs "$DWARFS_FILE" "$READONLY_DIR" -o ro
if [ $? -ne 0 ]; then
echo "Failed to mount DwarFS filesystem."
exit 1
fi
echo "Mounting OverlayFS..."
sudo mount -t overlay overlay -o lowerdir="$READONLY_DIR",upperdir="$UPPER_DIR",workdir="$WORK_DIR" "$MERGED_DIR"
if [ $? -ne 0 ]; then
echo "Failed to mount OverlayFS."
exit 1
fi
sudo chmod -R a+rw "$MERGED_DIR"
if [[ $? -ne 0 ]]; then
echo "Failed to set permissions for mount"
exit 1
fi
echo "Mounted DwarFS and OverlayFS successfully. You can now write to $MERGED_DIR"
}
# Function to unmount, merge, and create new dwarfs file
unmount_and_merge() {
echo "Unmounting OverlayFS..."
sudo umount "$MERGED_DIR"
if [ $? -ne 0 ]; then
echo "Failed to unmount OverlayFS."
exit 1
fi
echo "Unmounting DwarFS filesystem..."
sudo umount "$READONLY_DIR"
if [ $? -ne 0 ]; then
echo "Failed to unmount DwarFS filesystem."
exit 1
fi
echo "Merging changes..."
sudo mkdir -p "$MERGED_DIR/tmp"
sudo cp -a "$UPPER_DIR/." "$MERGED_DIR/tmp"
echo "Creating new DwarFS file..."
sudo mkdwarfs --force -i "$MERGED_DIR/tmp" -o "$NEW_DWARFS_FILE"
if [ $? -ne 0 ]; then
echo "Failed to create new DwarFS file."
exit 1
fi
echo "Cleaning up temporary directories..."
sudo rm -rf "$UPPER_DIR/*" "$WORK_DIR/*" "$MERGED_DIR/tmp" "$MOUNT_POINT"
echo "Merged changes and created new DwarFS file at $NEW_DWARFS_FILE"
}
# Main script logic
case "$1" in
mount)
mount_dwarfs
;;
unmount)
unmount_and_merge
;;
*)
echo "Usage: $0 {mount|unmount}"
exit 1
;;
esac
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment