Wednesday, April 8, 2015

formatting - USB flash drive showing less GiB of capacity (no partition)


Ok so my problem is that I had a Jetflash 32 GB USB flashdrive which used to have 32,000,000,000 bytes, or in other words its capacity was 29.8 GiB. I used to be able to right click on windows and see 29.8 as a number.


Now I did make a bootable USB via Linux to install Manjaro on somebody's computer. I can't remember what exactly I did back then to the flash drive. Long story short, when I formatted the USB in my computer (which happened a few weeks after the aforementioned incident) my flash drive now has 28 GiB of storage.


So I lost almost 2 GiB! I don't see any partitions nor unallocated space using any Windows tool or gparted in Linux.


Is there any low level tool that can delete any mbr/lba or whatever flag data and check my sectors one by one to determine my original capacity?


I did not find anything on the Internet. All the "my USB pen shows less GB" related topics are for solving issues where there was a smaller partition showing up and the rest of the space was just unallocated but "visible" to diskmanager or partitionmanager or gparted etc which is not my case.


Answer



I used a dd-inspired tool like mkusb on a flash drive before, like the mkusb Ubuntu help page says they:



'use the whole device', actually only the head end (size of the iso file), but the rest of the device is not available. mkusb simply clones the ISO 9660 file system with its content from the iso file. This ISO 9660 file system works from CD/DVD disks, and also from USB drives. After using a USB pendrive like this, you make a new partition table and file system, if you want to use it for another purpose.



Down the the link chain leads to Help to Format a USB pendrive. Creating a new partition table, then partition(s) should do the trick, unless you run into the "special cases" of a problem flash device. The one I had just wouldn't really listen and would occasionally pick up remnants of the old ISO filesystem months after formatting & reusing it.


Overwriting the first gigabyte (where the ISO originally was) solved my problem, but if you wanted to overwrite the entire USB that should work too (at the expense of one less lifetime writes to the flash memory), or just the first megabyte is supposed to work too...


Using plan dd from linux should do it. First make 100% certain you have the correct device (like /dev/sdx, using lsblk or gparted or gnome-disk-utility or watching dmesg/the syslog when plugging in the device should tell you)



  • To overwrite just the first megabyte (1M, where M =1024*1024) you'd do


    dd if=/dev/zero of=/dev/sdx bs=1M count=1

    To overwrite more M's use a larger count.


  • To overwrite the first gigabyte (1G, where G =1024*1024*1024) do


    dd if=/dev/zero of=/dev/sdx bs=1G count=1

  • To overwrite the entire device, don't use any bs or count, just do


    dd if=/dev/zero of=/dev/sdx

    When it's finished dd will tell you how much it was able to write before reaching the end of the device, giving you an idea of how much is really writeable, similar to this:


    1+0 records in
    1+0 records out
    1048576 bytes (1.0 MB) copied, 0.000838339 s, 1.3 GB/s


If dd is taking a long time you can "Send a USR1 signal to a running 'dd' process mak[ing] it print I/O statistics to standard error and then resume copying." Use kill and pgrep or ps pkill or htop or maybe even killall if you're careful, or see man dd for an example like:


$ dd if=/dev/zero of=/dev/null& pid=$!
$ kill -USR1 $pid; sleep 1; kill $pid
18335302+0 records in 18335302+0 records out 9387674624 bytes (9.4 GB) copied,
34.6279 seconds, 271 MB/s



Once dd is finished, write a new partition table and make a new partition and format it. I'd use gparted, it's got a create partition table option in a menu, and usually works well.


No comments:

Post a Comment

linux - How to SSH to ec2 instance in VPC private subnet via NAT server

I have created a VPC in aws with a public subnet and a private subnet. The private subnet does not have direct access to external network. S...