Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simulate mounted volume errors to cause read only

Few days ago we have encountered an unexpected error where one of the mounted drive on our RedHat linux machine became Read-Only. The issue was cause by the network outage in the datacenter.

Now I need to see if I can reproduce the same behavior where drive will be re-mounted as Read-Only while application is running.

I tried to remounted it was read-only but that didn't work because there are files that are opened (logs being written).

Is there a way to temporary cause the read-only if I have root access to the machine (but no access to the hypervisor).

That volume is mounted via /etc/fstab. Here is the record:

UUID=abfe2bbb-a8b6-4ae0-b8da-727cc788838f /                     ext4    defaults        1 1
UUID=8c828be6-bf54-4fe6-b68a-eec863d80133       /opt/sunapp     ext4    rw 0 2

Here are the output of few commands that shows details about our mounted drive. I can add more details as needed.

Output of fdisk -l

Disk /dev/vda: 268.4 GB, 268435456000 bytes, 524288000 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x0008ba5f

   Device Boot      Start         End      Blocks   Id  System
/dev/vda1   *        2048   524287966   262142959+  83  Linux

Disk /dev/vdb: 42.9 GB, 42949672960 bytes, 83886080 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes

Output of lsblk command:

NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
vda    253:0    0   80G  0 disk 
└─vda1 253:1    0   80G  0 part /
vdb    253:16   0  250G  0 disk /opt/sunup

Output of blkid command:

/dev/vda1: UUID="abfe2bbb-a8b6-4ae0-b8da-727cc788838f" TYPE="ext4" 
/dev/sr0: UUID="2017-11-13-13-33-07-00" LABEL="config-2" TYPE="iso9660" 
/dev/vdb: UUID="8c828be6-bf54-4fe6-b68a-eec863d80133" TYPE="ext4" 

Output of parted -l command:

Warning: Unable to open /dev/sr0 read-write (Read-only file system).  /dev/sr0
has been opened read-only.
Error: /dev/sr0: unrecognised disk label
Model: QEMU QEMU DVD-ROM (scsi)                                           
Disk /dev/sr0: 461kB
Sector size (logical/physical): 2048B/2048B
Partition Table: unknown
Disk Flags: 

Model: Virtio Block Device (virtblk)
Disk /dev/vda: 268GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags: 

Number  Start   End    Size   Type     File system  Flags
 1      1049kB  268GB  268GB  primary  ext4         boot


Model: Virtio Block Device (virtblk)
Disk /dev/vdb: 42.9GB
Sector size (logical/physical): 512B/512B
Partition Table: loop
Disk Flags: 

Number  Start  End     Size    File system  Flags
 1      0.00B  42.9GB  42.9GB  ext4
like image 572
Maksim Avatar asked Nov 12 '18 17:11

Maksim


People also ask

How do I know if my mount point is read only?

Command mount will list all mounted partitions and will indicate whether they are mounted read only (ro) or read-write (rw). There is no way to tell whether a filesystem is "healty" while mounted in a normal read-write mode.

How do I mount a read only file system in Linux?

Open up a second terminal, run lsblk -f and match the UUID code that appears next to the partition you'd like to edit in the lsblk output with the one in “/etc/fstab.” When you've found the line in the Fstab file, add in the read-only option to the file-system “ro” to the mount line.


1 Answers

Yes, you can do it. But the method proposed here may cause data loss, so use it only for testing.

Supposing you have /dev/vdb mounted as /opt/sunapp, do this:

  1. First, unmount it. You may need to shut down any applications using it first.
  2. Configure a loop device to mirror the contents of /dev/vdb:

    losetup /dev/loop0 /dev/vdb
    
  3. Then, mount /dev/loop0 instead of /dev/vdb:

    mount /dev/loop0 /opt/sunapp -o rw,errors=remount-ro
    
  4. Now, you can run your application. When it is time to make /opt/sunapp read-only, use this command:

    blockdev --setro /dev/vdb
    

    After that, attempts to write to /dev/loop0 will result in I/O errors. As soon as file system driver detects this, it will remount the file system as read-only.

To restore everything back, you will need to unmount /opt/sunapp, detach the loop device, and make /dev/vdb writable again:

umount /opt/sunapp
losetup -d /dev/loop0
blockdev --setrw /dev/vdb
like image 143
abacabadabacaba Avatar answered Nov 06 '22 22:11

abacabadabacaba