USB format (part 1)
Well, one could try zero’ing the raw block device to see if that can work. If you can write to that then you may be able to create a clean partition table, create a new partition and format that.
Suppose the USB stick is on /dev/sdc
, first make sure /dev/sdc1
is unmounted:
umount /dev/sdc1
See if you can then clear the partition table, say by copying a bunch of zeros over the first few K
sudo dd if=/dev/zero of=/dev/sdc bs=512 count=16
If that works, see if you can write zeros to the whole device without it failing. To easily see if the kernel can’t write to the device, first clear the current kernel messages and throw them away using:
sudo dmesg -c > /dev/null
then zero the entire device:
sudo dd if=/dev/zero of=/dev/sdc bs=1M
This will take a while. dd
will complete when the raw block device is completely written to, or an error has occurred.
Then, check to see if the kernel has complained about the device, using:
dmesg
If you see a load of error messages then you know that the USB stick is probably not in good condition.
However, if this works fine run fdisk or parted to create the partitions from clean, e.g. with fdisk
:
sudo fdisk /dev/sdc
Command (m for help): n
Select (default p): p
Partition number (1-4, default 1): 1
First sector (2048-7796735, default 2048): <return>
Last sector, +sectors or +size{K,M,G} (2048-7796735, default 7796735): <return>
Command (m for help): t
Hex code (type L to list codes): 6
Command (m for help): w
Note just press return for the First and Last sector questions, fdisk
will chose the correct defaults (which will be different to my example above). And then format the partition with VFAT:
sudo mkfs.vfat /dev/sdc1
and then remove and re-insert the drive. It should be cleanly formatted.
USB format (part 2)
As suggested by hellocatfood, after formatting the drive, one will have to take ownership of it as well… So type in the terminal:
sudo chown yourusername: /media/mountpoint
One more thing: Your partition should be mounted in /media/mountpoint
To find the mountpoint, run mount
in the terminal, and locate the line that corresponds to the drive or partition in question. If you have never named (or set the label for) it, it will probably look something like:
/dev/sdb1 on /media/bf9a2c45-491a-4778-9d76-47832fe38820
If you have set the label with something descriptive, it should like similar to:
/dev/sdb1 on /media/1Tb Pocket Drive
After you have the mount point:
sudo chown hellocatfood: /media/bf9a2c45-491a-4778-9d76-47832fe38820
or
sudo chown hellocatfood: /media/1Tb Pocket Drive
(hellocatfood is the name of the user)
If the drive name has spaces it will need to be escaped or quoted: e.g.
sudo chown $USER: /media/"1Tb Pocket Drive"
Note also that you dont have to specify your user name here; the $USER
variable will automatically get your username from the system.
This should give the ownership to the current user, and it would be persistent after reboots as well.
P.S. I took help for the permissions part from the following link and for finding out the mount point, from the following link
USB format (part 3)
Assuming your usb key is /dev/sdz
Clean the usb drive
dd if=/dev/zero of=/dev/sdz
or something more modest, like:
dd if=/dev/zero of=/dev/sdz bs=1024 count=1
Create the partition with fdisk.
fdisk /dev/sdz
In fdisk
, press m
and read the help. To make a partition and make it bootable,
press (double check though):
(fdisk)> n, p, 1, <enter>, <enter>, a, 1, t, b, w
Format the partition with a coherent filesystem according to the partition type
chosen before with fdisk
:
mkfs.vfat /dev/sdz1
Mount a unit
Privileged user (root) can mount with specific permissions:
sudo mount -t vfat -o umask=0022,gid=group,uid=user /dev/sdz /mnt
An unpriviledged user can use pmount
:
pmount sda1
or udisks
:
udisksctl mount -b /dev/sda1
Safely remove USB disk
You can do this using udisks
with the following commands…
sudo udisks --unmount /dev/sdb1
sudo udisks --detach /dev/sdb
The first line unmounts it. Just like any other partition you can still remount it at this point. The second line detaches it. After this it is powered down and you have to remove/reinsert it to remount.
To clarify, sdb
is the device and sdb1/2/3/etc
are partitions on the device. Also, you will need to unmount all mount points before you attempt to detach the device. Usually with a USB stick/drive there is only one but it is a warning you should know about none the less.
Getting the device name from the mount point would require you pull it from mount or something.