Quote:
Originally Posted by ardabro
Hi all
I have an old USB stick formatted to FAT. Haven't used it for a long time
Now it reports errors when I try do read files from it.
I don't care about these files, but I'd like to make it usable again.
I think simple mkdosfs is not sufficient, and full wrtite/read/scan is necessary. What is a proper tool to use?
It should be something in command line. I have to do it by ssh, because I'm in another location for some time.
|
I would do like this:
A) If i wanted to use the stick on any computer, inluding 'Widows':
Insert the stick in a computer running linux. Log in as root, or use 'su' or 'sudo su -' or anything that gives you root access.
Use 'df' or 'findmnt' to see if it gets automounted to some directory.
If so, then unmount it.
Lazy method is to open a terminal window and issue command 'tail -f /var/log/messages'.
If you then insert the stick, the log will tell you which device it gets assoxiated with, and if it gets automounted to some directory.
Lets say the stick happens to get associated with device '/dev/sdk' when plugged in.
If it got automounted, just type 'umount /dev/sdk' to unmount it before going any further.
Typing 'blkid' will show you any filesystem labels and UUID's associated with that device.
Typing 'blockdev --report' will list the block devices, and you will see the actual size of the stick, and also any partitions on the stick.
Doing a 'fdisk /dev/sdk' and then typing the command 'p' will show you the partitions on the stick.
if you want to keep boot sector and/or partition table of the stick, you can copy it to a small file like this: 'dd if=/dev/sdk bs=1b count=1 of=mbr.copy.from.stick.bin'
To wipe the stick completely, do one of the following:
1) 'dd if=/dev/zero bs=2k of=/dev/sdk ; sync'
2) 'badblocks -wsb 2048 -t 0x00 /dev/sdk'
Using badblocks, you will get a progress indication, and also a check on that the contents really are zeroed out without errors.
If you use 'badblocks -wsb 2048 /dev/sdk' you will have the stick zeroed out with the "DOD method" of first writing pattern 0xAA, then patterns 0x55, 0xFF and finally 0x00. If this doesn't fail, the stick is proven usable. (Please note that it is best for the FLASH stick lifetime to just write the zeroes).
Now we have a wiped USB FLASH stick. Put it in a Windows PC and let windows format it for you. It will put a fresh partition table on it, (without boot code), and then format it as FAT32. Usable on all computers.
B) Just like alternative A above, except you partition and format the stick under linux.
Re-add the boot sector and partition table that we made a backup to a file:
'dd if=mbr.copy.from.stick.bin bs=1b count=1 of=/dev/sdk ; sync ; partprobe /dev/sdk'
Or use fdisk to make a partition table on the stick. Please note that most common is (strange enough) to only have a primary partition number 4, with FAT32 on it. But you can of course partition it however you like. It is also possible to use the stick without partitioning it. Just to put the filesystem on the whole /dev/sdk. There are some compatibility issues with this, but it can be used if one wants to use as much of the stick as possible for data.
If used only for Linux, i would put reiserfs on the stick. Or possibly ext3/ext4 if reiserfs happens not to be installed on a particular computer i will use the stick with. (unlikely for me).
You can of course add a FAT32 filesystem using Linux. You can also make several partitions, and have one small FAT16 partition usable on an ancient DOS machine, and one FAT32 partition. Or a combo with half the stick as FAT32 and the second half as EXT3, reiserfs, UFS, XFS or hatever you prefer.
Backup of a USB stick:
One can also use 'dd if=/dev/sdk bs=2k | gzip -9 > image.backup.of.stick.bin.gz' to make an image copy of a sticks contents to a file. Which later can be put back to a stick of same size or bigger using 'gzip < image.backup.of.stick.bin.gz | dd bs=2k of=/dev/sdk ; sync'.
This is very useful if you want to temporarily use a stick for something else, and then restore the previous contents again.
(Please note again that '/dev/sdk' is an example, and that the stick might as well be associated with any other device name, like '/dev/sdd').