Authors Top

If you have a few years of experience in the Linux ecosystem, and you’re interested in sharing that experience with the community, have a look at our Contribution Guidelines.

1. Overview

Read-only is a file system property that permits users to read or copy stored data but not write new information or edit the data. Alternatively, we can identify a Hard Disk Drive (HDD) in a read-only state as a write-protected device.

Usually, a write-protected disk shows the read-only filesystem error message on any attempt to modify its content. This error indicates that the system mounts the target drive’s file system in read-only mode. Hence, we cannot write to or alter its content.

The following conditions can corrupt or set an HDD in read-only mode:

  • Unexpected power loss
  • System crashes
  • Unsafe removal of the drive
  • Bootable disks
  • Absence of write permissions

In this tutorial, we’ll explain how to change the HDD state from read-only after a temporary crash in Linux.

2. Locate the Drive

The first step to fixing a read-only error is to locate the HDD mount point. To locate a drive, we can run the mount command and pass it through grep. The grep command helps to filter the output and highlight the argument given to it:

$ mount | grep /dev/
devpts on /dev/pts type devpts (rw,nosuid,noexec,relatime,gid=5,mode=620,ptmxmode=000)
/dev/sda5 on / type ext4 (rw,relatime,errors=remount-ro)
tmpfs on /dev/shm type tmpfs (rw,nosuid,nodev,inode64)
hugetlbfs on /dev/hugepages type hugetlbfs (rw,relatime,pagesize=2M)
mqueue on /dev/mqueue type mqueue (rw,nosuid,nodev,noexec,relatime)
/dev/sda1 on /boot/efi type vfat
  (rw,relatime,fmask=0077,dmask=0077,codepage=437,iocharset=iso8859-1,shortname=mixed,errors=remount-ro)
/dev/fuse on /run/user/1000/doc type fuse (rw,nosuid,nodev,relatime,user_id=1000,group_id=1000)
/dev/sdb4 on /media/chris/writable type ext4 (rw,nosuid,nodev,relatime,uhelper=udisks2)
/dev/sdb1 on /media/chris/Ubuntu-Server 22.04 LTS amd64 type iso9660
  (ro,nosuid,nodev,relatime,nojoliet,check=s,map=n,blocksize=2048,uid=1000,gid=1000,dmode=500,fmode=400,iocharset=utf8,uhelper=udisks2)

Generally, SATA device names follow the pattern /dev/sd[a-z], while NVMe device names have the pattern /dev/nvme[1-9]n[1-9].

From the output, we can identify the read-only devices with ro as the initial attribute in the parentheses. Similarly, rw indicates a device mounted as read/write. Also, the output of the mount command shows the mount point of each HDD partition.

The drive /dev/sdb1 mounted on /media/chris/Ubuntu-Server 22.04 LTS amd64 is a read-only device.

3. Restore Write Permission

To determine the permissions on a drive, we can use the ls command with the -l option and the device name. The following command lists the disk and its permissions:

$ ls -l /dev/sdb1
brw-r----- 1 root disk 8, 17 Jul 22 11:34 /dev/sdb1

Accordingly, with appropriate remount flags, the mount command can quickly repair a read-only error. To illustrate, let’s execute the following line of command to remount the drive:

$ sudo mount -o remount,uid=1000,gid=1000,rw /dev/sdb1
...

The -o flag allows the mount command to use a comma-separated list of options. The ensuing are the roles of the comma-separated options in the command:

  • remount – remounts the drive over the same mount point with the same previous options
  • uid=1000 – makes the user with id=1000 the owner of the drive
  • gid=1000 – assigns the group with id=1000 as the group owner of the drive
  • rw – mounts the drive as read/write
  • /dev/sdb1 – indicates the name of the device

The values of these options come from the output of the previous mount command in section 2.

However, this quick-fix does not work for all HDD crash situations. For example, the mount command gives the following output when the command fails:

$ sudo mount -o remount,uid=1000,gid=1000,rw /dev/sdb1
mount: /media/chris/Ubuntu-Server 22.04 LTS amd64:
  cannot remount /dev/sdb1 read-write, is write-protected.

If the remounting command does not fix the error, then we can proceed to repair the file system on the drive.

4. Fix Read-Only HDD

Again, different cases of HDD crashes can render its file system read-only. The procedure to fix such a device requires that we:

  1. Unmount the drive
  2. Check and repair the file system
  3. Remount the drive
  4. Reboot the system

4.1. Unmount Drive

The umount command unmounts an HDD from its mount point. It takes the mount point of the drive as an attribute. Again, we recall that we can use the mount command to locate devices mounted as read-only. The following command unmounts /dev/sdb1:

$ sudo umount /dev/sdb1

However, we cannot unmount the root file system on which the device is running. Hence, let’s ensure to boot from an external bootable device when aiming to fix a system HDD. Next, let’s check and repair the file system on the device.

4.2. Check and Repair File System

The fsck program checks and repairs one or more Linux file systems. With the following fsck command and the device name attribute, we can check and repair the file system on the faulty drive:

$ sudo fsck /dev/sdb1
fsck from util-linux 2.34
e2fsck 1.44.1 (23-Jul-2022)
second_disk: clean 54/81280 files, 149903/325096 blocks

However, if we do not supply a device to the command, it will check file systems in the /etc/fstab file serially.

4.3. Remount Drive

After repair, we have to remount the device to a new mount point. To remount the HDD, we’ll:

  1. Create a new mount point
  2. Mount the drive to the new mount point
  3. Write the changes in the /etc/fstab file

To illustrate, let’s create a new mount point:

$ mkdir HDdevice

Further, the following command mounts the HDD to the new mount point:

$ sudo mount -o rw,uid=1000,gid=1000,user,exec,umask=003,blksize=4096 /dev/sdb1/media/HDdevice

In addition, here’s a rundown of what’s going on in the command:

  • user – permits any user to mount the HDD
  • exec – allows for the execution of binaries
  • umask=003 – assigns rwxrwxr– permissions to directories and files in the device
  • /media/HDdevice – indicates the name of the hard disk
  • blksize=4096 – assigns block size of 4096 to the drive

Lastly, let’s write the changes in the /etc/fstab file. It ensures our changes remain intact on system startup. First, let’s create a backup of the /etc/fstab file to safeguard its content:

$ sudo cp /etc/fstab /etc/fstab.bak

Secondly, using the nano text editor, let’s write the following changes in the /etc/fstab file:

$ sudo nano /etc/fstab
...
# line for mounting the Hddevice
 UUID=D04A-0AE4   /media/HDdevice  exfat
  rw,uid=1000,gid=1000,user,exec,umask=003,blksize=4096   0   0

Then, let’s press Ctrl+X, Y, and Enter to save and close the file.

4.4. Reboot the System

To confirm our changes, let’s restart the system with the shutdown command. The following shutdown command, along with the –r flag, restarts the system:

$ shutdown -r

Consequently, the shutdown command sends a signal to stop all running processes and unmount devices safely before shutdown.

5. Conclusion

In this article, we learned how we could change a device state from read-only after a temporary crash in Linux. Also, we’ve seen alternative programs that solve similar problems at each step in the repair process.

Authors Bottom

If you have a few years of experience in the Linux ecosystem, and you’re interested in sharing that experience with the community, have a look at our Contribution Guidelines.

Comments are closed on this article!