Linux error - cannot create or write to a file: Read-only file system

 

If you are getting an error message similar to the following when trying to write to a filesystem.

touch: cannot touch 'testfile': Read-only file system

Then the filesystem has probably been corrupted and if so linux sets the filesystem as read-only.

In this case there were some errors in /var/logs/messages that show there was an error on the disk.

Oct 14 13:41:12 web-01 kernel: ext3_abort called.
Oct 14 13:41:12 web-01 kernel: EXT3-fs error (device dm-1): ext3_put_super: Couldn't clean up the journal
Oct 14 13:45:18 web-01 kernel: sd 0:0:0:0: SCSI error: return code = 0x08000002
Oct 14 13:45:18 web-01 kernel: sda: Current: sense key: Hardware Error
Oct 14 13:45:18 web-01 kernel:     Add. Sense: Internal target failure
Oct 14 13:45:18 web-01 kernel:
Oct 14 13:45:18 web-01 kernel: Info fld=0x0
Oct 14 13:45:18 web-01 kernel: end_request: I/O error, dev sda, sector 399069309
Oct 14 13:45:18 web-01 kernel: printk: 975 messages suppressed.
Oct 14 13:45:18 web-01 kernel: Buffer I/O error on device dm-1, logical block 30745574
Oct 14 13:45:18 web-01 kernel: sd 0:0:0:0: SCSI error: return code = 0x08000002
Oct 14 13:45:18 web-01 kernel: sda: Current: sense key: Hardware Error
Oct 14 13:45:18 web-01 kernel:     Add. Sense: Internal target failure
Oct 14 13:45:18 web-01 kernel:
Oct 14 13:45:18 web-01 kernel: Info fld=0x0
Oct 14 13:45:18 web-01 kernel: end_request: I/O error, dev sda, sector 399069309
Oct 14 13:45:18 web-01 kernel: Buffer I/O error on device dm-1, logical block 30745574
Oct 14 13:45:45 web-01 kernel: sd 0:0:0:0: SCSI error: return code = 0x08000002
Oct 14 13:45:45 web-01 kernel: sda: Current: sense key: Hardware Error
Oct 14 13:45:45 web-01 kernel:     Add. Sense: Internal target failure
Oct 14 13:45:45 web-01 kernel:
Oct 14 13:45:45 web-01 kernel: Info fld=0x0
Oct 14 13:45:45 web-01 kernel: end_request: I/O error, dev sda, sector 399069309
Oct 14 13:45:45 web-01 kernel: Buffer I/O error on device dm-1, logical block 30745574
Oct 14 13:45:45 web-01 kernel: lost page write due to I/O error on dm-1
Oct 14 13:51:18 web-01 kernel: kjournald starting.  Commit interval 5 seconds
Oct 14 13:51:18 web-01 kernel: EXT3 FS on dm-1, internal journal
Oct 14 13:51:18 web-01 kernel: EXT3-fs: mounted filesystem with ordered data mode.

To fix use the lsof command to check for open files on the read-only filesystem (in this case /www) as follows.

lsof /www

Stop any processes that are using this filesystem.

In this case lsof showed that httpd was using /www and therefore to stop httpd the following command was used.

service httpd stop

Once lsof shows there are no open files in /www we can umount it.

umount /www

Then run filesystem checker against the filesystem device (in this case /dev/VolGroup00/lv01)

fsck /dev/VolGroup00/lv01

Answer yes to any questions and then re-mount the filesysten once fsck has finished.

mount /www

Then restart any services

service httpd start

And then hopefully you should now be able to write to the filesystem, unless of course the fault was a serious hardware fault then the disk may need replacing.