How do I make a USB flash drive writable on Linux?

0

I want to reformat a SanDisk Cruzer Edge USB flash drive but it appears read-only:

$ sudo dd if=/dev/zero of=/dev/sdg
dd: failed to open ‘/dev/sdg’: Read-only file system

I checked the readonly status with hdparm:

$ sudo /usr/sbin/hdparm /dev/sdg
SG_IO: bad/missing sense data, sb[]:  70 00 05 00 00 00 00 14 00 00 00 00 20 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
 multcount     =  0 (off)
 readonly      =  0 (off)
 readahead     = 256 (on)
 geometry      = 1017/124/62, sectors = 7821312, start = 0

The readonly flag displays off but that I still cannot write to the drive. How do I solve this?


After running dosfsck on the device I get this output:

$ sudo /usr/sbin/dosfsck /dev/sdg
fsck.fat 3.0.22 (2013-07-19)
Logical sector size (1766 bytes) is not a multiple of the physical sector size.

Jephir

Posted 2014-01-03T18:48:09.527

Reputation: 931

Question was closed 2016-09-19T21:24:22.407

What file system is on the device ? Sometimes if the file system is corrupt, it will be mounted ro. You can check the disk for errors. – Panther – 2014-01-03T21:30:15.013

The file system is fat32. I have added dosfsck output to the question. – Jephir – 2014-01-04T19:17:37.350

Answers

1

My guess is your GUI is automounting the device to some location when you plug it in. For example, when I plug a USB stick in to my Mate/Marco 1.12 system:

[root@frog ~]# mount
   ...
   ...
/dev/sdg on /run/media/pgoetz/4474-E825 type vfat (rw,nosuid,nodev,relatime,uid=
1001,gid=1001,fmask=0022,dmask=0022,codepage=437,iocharset=iso8859-1,
shortname=mixed,showexec,utf8,flush,errors=remount-ro,uhelper=udisks2)

When mounted, you won't be able to actually dd to the device until the partition is unmounted:

umount /dev/sdg

Somewhat oddly, if I try running dd while the partition is mounted; e.g.

dd bs=4M if=./archlinux-2016.04.01-dual.iso of=/dev/sdg

it claims to be copying the files, but the copy doesn't actually take place until the partition is unmounted. The system must be caching the write on account of being blocked from actually writing to the device.

pgoetz

Posted 2014-01-03T18:48:09.527

Reputation: 141

0

Try to repartition it with gparted or similar tool. I assume you only need one partition, so delete everything and create one with FS of your choice.

Oh, and by the way, didn't you mean /dev/sdg1 or smth. As far as I know there has to be a number at the end, not just sdg

ALSO: consider how long have you been using this flashdrive and how intensively have you been writing data to it. Flash memory has a total limit of write operations per cell (read here). When the limit is reached the drive should normally become read-only. I say normally, because there are some cases when the drive fails completely - this can happen if you are using cheap low-quality device.

Art Gertner

Posted 2014-01-03T18:48:09.527

Reputation: 6 417

You end dd with just sdg. It will then start at sector 0 wiping the patition table and all other important file system info. sdg1 would force it to skip to the beginning of the first partition usually sector 63. – cybernard – 2014-01-04T06:38:32.763

When using gparted I get the error "Can't write to /dev/sdg, because it is opened read-only." – Jephir – 2014-01-04T19:12:58.347

which operation exactly produces this message "Can't write to /dev/sdg, because it is opened read-only." ? Does it happen when you are trying to format or re-partition the disk? Also, do you run gparted as root? – Art Gertner – 2014-01-05T13:35:03.063

ALSO, read the update to my initial answer. Your drive might have worn out. If this is the case you will no longer be able to write to it (and obviously not able to format it either) – Art Gertner – 2014-01-05T13:47:41.893