mount RO
remount block device RO caltropsunknown
sh
a year ago
1.6 kB
4
Indexable
#!/bin/bash # Function to check if a device is read-only is_readonly() { local device=$1 if sudo blockdev --getro "$device" | grep -q "1"; then return 0 else return 1 fi } # Function to attempt a write and verify read-only status verify_readonly() { local mount_point=$1 local test_file="$mount_point/.readonly_test" # Attempt to create a file (should fail if read-only) if touch "$test_file" 2>/dev/null; then echo "ERROR: Device is not read-only, write operation succeeded!" # Cleanup the test file if it was created rm -f "$test_file" exit 1 else echo "Device is successfully set to read-only, write operation failed as expected." fi } # Main script if [ $# -ne 2 ]; then echo "Usage: $0 <device> <mount_point>" exit 1 fi DEVICE=$1 MOUNT_POINT=$2 # Remount the filesystem as read-only echo "Remounting $MOUNT_POINT as read-only..." if ! sudo mount -o remount,ro "$MOUNT_POINT"; then echo "ERROR: Failed to remount $MOUNT_POINT as read-only." exit 1 fi # Set the block device to read-only echo "Setting $DEVICE to read-only..." if ! sudo blockdev --setro "$DEVICE"; then echo "ERROR: Failed to set $DEVICE to read-only." exit 1 fi # Verify the block device is read-only if is_readonly "$DEVICE"; then echo "$DEVICE is set to read-only." else echo "ERROR: $DEVICE is not set to read-only." exit 1 fi # Verify the filesystem is read-only verify_readonly "$MOUNT_POINT" echo "All checks passed. $DEVICE is now read-only." exit 0
Editor is loading...
Leave a Comment