DEV Community

samsepi0l
samsepi0l

Posted on

Rescue "broken" USB

Rescue "broken" USB

Rescuing USBs that don't allow writing/reading after plugging them in

according to GNU Parted manual , let check some we will use


Partitioning is the process of dividing a storage device into local sections, called partitions, which help organize multiple filesystems and their associated operating systems.

A storage device presents itself as a sequence of bytes, numbered starting from zero and increasing until the maximum capacity of the device is reached. Bytes are normally read and written a sector at a time, rather than individually. Each sector contains a fixed number of bytes, with the number determined by the device.

+------------------------------------------------------------+

| storage device with no partitions |

+------------------------------------------------------------+

0 start end

In order to store multiple filesystems, a storage device can be divided up in to multiple partitions. Each partition can be thought of as an area which contains a real filesystem inside of it. To show where these partitions are on the device a small table is written at the start, shown as PT in the diagram below. This table is called a partition table, or disklabel, and also stores the type of each partition and some flags.

+--+---------------+----------------+------------------------+

|PT| Partition 1 | Partition 2 | Partition 3 |

+--+---------------+----------------+------------------------+

0 start end
Enter fullscreen mode Exit fullscreen mode

we need to low-level format USB device , deleting the partition table at the start of the flash memory

# parted mklabel msdos
Enter fullscreen mode Exit fullscreen mode

and with this new partition table, you can format the filesystem as you would usually format USB (continuing with parted or using GUI a GParted)


ANOTHER WAY

If it's not recognized by Windows or Linux, you can try to \dd\\ it

In order to zero-fill USB

sudo dd if=/dev/zero of=/dev/sdc bs=1M
Enter fullscreen mode Exit fullscreen mode

And format it the usual way


IF THAT DOESN'T WORK

then check messages

dmesg

insert usb, and check them again

see for any errors, and if there are some, act accordingly

otherwise:


OTHERWISE

1. Remove all of your USB devices except for the 8GB USB flash drive that you want to reformat, so you won't get confused about the device name of the USB flash drive later on.

2. List all the partitions.

sudo fdisk -l
Enter fullscreen mode Exit fullscreen mode

And then insert USB, and run above command again, locate your USB mount point, so we can use it

Disk /dev/sdc: 7864 MB, 7864320000 bytes

30 heads, 33 sectors/track, 15515 cylinders, total 15360000 sectors

Units = sectors of 1 \* 512 = 512 bytes

Sector size (logical/physical): 512 bytes / 512 bytes

I/O size (minimum/optimal): 512 bytes / 512 bytes

Disk identifier: 0x00016288

Device Boot Start End Blocks Id System

/dev/sdc1 \* 2048 15359999 7678976 b W95 FAT32
Enter fullscreen mode Exit fullscreen mode

In this case, my USB is mounted on /dev/sdc1

3. Create a partition table on the disk of type msdos, sometimes known as Master Boot Record (MBR).

Run it on whole device, not just partition sdc not sdc1

verify the device name before running this step!

sudo parted /dev/sdc mklabel msdos
Enter fullscreen mode Exit fullscreen mode

4. Add an empty "primary" partition, which will hold a FAT filesystem later.

sudo parted -a none /dev/sdc mkpart primary fat32 0 8192
Enter fullscreen mode Exit fullscreen mode

I used /dev/sdc for the name of the device which is what was found in the results of step 2. The command specifies the start point (from 0 MB) to the endpoint (8192 MB). If the 8GB USB flash drive does not have the full 8192 MB space, parted will adjust it automatically. If the terminal returns a message that the start point can't start at 0 MB and you have to use some other small number close to 0 MB, type Y to accept this. Note the command is creating a single, primary partition on the whole disk.

This newly created partition will have the ID /dev/sdc1. That is because the device name in this example is /dev/sdc and the 1 at the end is because it is the first partition on that device.

5. Create a FAT filesystem on the /dev/sdc1 partition by formatting the partition.

mkfs.vfat -n "8GB-USB" /dev/sdc1
Enter fullscreen mode Exit fullscreen mode

/dev/sdc1 is the partition ID from step 4. "8GB-USB" is the partition label, which can be your own choice of label, just enclose the label inside two double quote characters.

You now have a ready-to-use reformatted USB flash drive with an 8GB FAT partition.


Tip: These techniques can be used to reformat lost USB drive capacity after attempting to make it bootable EFI, or multiboot, best way of recovering it's capacity is using dd method


FORMATTING VIA fdisk

List Partitions


fdisk -l
Enter fullscreen mode Exit fullscreen mode

you would get output like this:


Disk /dev/nvme0n1: 232.91 GiB, 250059350016 bytes, 488397168 sectors

Disk model: Samsung SSD 960 EVO 250GB

Units: sectors of 1 \* 512 = 512 bytes

Sector size (logical/physical): 512 bytes / 512 bytes

I/O size (minimum/optimal): 512 bytes / 512 bytes

Disklabel type: gpt

Disk identifier: 6907D1B3-B3AB-7E43-AD20-0707A656A1B5

Device Start End Sectors Size Type

/dev/nvme0n1p1 2048 1050623 1048576 512M EFI System

/dev/nvme0n1p2 1050624 34605055 33554432 16G Linux swap

/dev/nvme0n1p3 34605056 488397134 453792079 216.4G Linux filesystem

Disk /dev/sda: 465.78 GiB, 500107862016 bytes, 976773168 sectors

Disk model: WDC WD5000AAKS-0

Units: sectors of 1 \* 512 = 512 bytes

Sector size (logical/physical): 512 bytes / 512 bytes

I/O size (minimum/optimal): 512 bytes / 512 bytes

Disklabel type: dos

Disk identifier: 0x0001cca3

Device Boot Start End Sectors Size Id Type

/dev/sda1 2048 976771071 976769024 465.8G 83 Linux
Enter fullscreen mode Exit fullscreen mode

Creating Partition Table

To start partitioning the drive, run fdisk with the device name. In this example, I’ll work on /dev/sdb

The command prompt will change, and the fdisk dialogue where you can type in commands will open:


Welcome to fdisk (util-linux 2.34).

Changes will remain in memory only, until you decide to write them.

Be careful before using the write command.

Command (m for help):
Enter fullscreen mode Exit fullscreen mode

> Changes you make to the partition table won’t take effect until you write them with the w command. You can exit the fdisk dialogue without saving the changes using the q command.

To get a list of all available commands enter m

If you are partitioning a new drive, before starting to create partitions first, you need to create a partition table. Skip this step if the device already has a partition table and you want to keep it.

fdisk supports several partitioning schemes, check them in man page, or in help within fdisk

I use gpt, for this example

Command (m for help): g
Enter fullscreen mode Exit fullscreen mode

Output:

Created a new GPT disklabel (GUID: 4649EE36-3013-214E-961C-51A9187A7503).
Enter fullscreen mode Exit fullscreen mode

Now we need to create the new partitions.

We will create two partitions. The first one with a size of 100 GiB and the second one will take the rest of the disk space.

Run the n command to create a new partition:

Command (m for help): n
Enter fullscreen mode Exit fullscreen mode

You’ll be prompted to enter the partition number. Hit "Enter" to use the default value (1):

Partition number (1-128, default 1):
Enter fullscreen mode Exit fullscreen mode

Next, the command will ask you to specify the first sector. Generally, it is always recommended to use the default values for the first value. Hit “Enter” to use the default value (2048):

First sector (2048-500118158, default 2048):
Enter fullscreen mode Exit fullscreen mode

On the next prompt, you’ll need to enter the last sector. You can use an absolute value for the last sector or a relative value for the start sector, using the + symbol followed by the partition size. The size can be specified in kibibytes (K), mebibytes (M), gibibytes (G), tebibytes (T), or pebibytes (P).

Enter +100G to set the partition size to 100 GiB:

Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-500118158, default 500118158): +100G
Enter fullscreen mode Exit fullscreen mode

Output

Created a new partition 1 of type 'Linux filesystem' and of size 100 GiB.
Enter fullscreen mode Exit fullscreen mode

By default, the type of the new partition is set to “Linux filesystem”, which should be fine for most cases. If you want to change the type, press l to get a list of partition types and then press t to change the type. (also, check the manual, or help inside fdisk, for all available options

Create the second partition that will take the rest of the disk space:

Command (m for help): n
Enter fullscreen mode Exit fullscreen mode

Use the default values for the partition number, first and last sectors. This will create a partition that will use all available space on the disk.

(you can set up your own, custom values if you wish, no need for defaults)

Partition number (2-128, default 2):

First sector (209717248-625142414, default 209717248):

Last sector, +/-sectors or +/-size{K,M,G,T,P} (209717248-625142414, default 625142414):
Enter fullscreen mode Exit fullscreen mode

When you finish making and manipulating USB formatting, it's not written to USB yet, it's still just a scheme of what will be done

To check and see your final work, partitions that will be created, etc. use p to show them

Command (m for help): p
Enter fullscreen mode Exit fullscreen mode

Output

Disk /dev/sdb: 298.9 GiB, 320072933376 bytes, 625142448 sectors

Disk model: nal USB 3.0

Units: sectors of 1 \* 512 = 512 bytes

Sector size (logical/physical): 512 bytes / 4096 bytes

I/O size (minimum/optimal): 4096 bytes / 4096 bytes

Disklabel type: gpt

Disk identifier: F8365250-AF58-F74E-B592-D56E3A5DEED1

Device Start End Sectors Size Type

/dev/sdb1 2048 209717247 209715200 100G Linux filesystem

/dev/sdb2 209717248 625142414 415425167 198.1G Linux filesystem
Enter fullscreen mode Exit fullscreen mode

> If you want to delete a partition, use the d command.

Save changes by running the w command:

Command (m for help): w
Enter fullscreen mode Exit fullscreen mode

The command will write the table to the disk and exit the fdisk menu.

The partition table has been altered.

Calling ioctl() to re-read the partition table.

Syncing disks.
Enter fullscreen mode Exit fullscreen mode

The kernel will read the device partition table without the need to reboot the system

If kernel don't recognise changes, type

# partprobe
Enter fullscreen mode Exit fullscreen mode

or with specifying the device name

# partprobe /dev/sdX
Enter fullscreen mode Exit fullscreen mode

Creating filesystem

next step is to format the partitions and mount them to the system’s directory tree.

here is some of available filesystems supported by mkfs tool, this varies by kernel support

mkfs.bfs

mkfs.btrfs

mkfs.exfat //exFAT

mkfs.ext2 //ext2

mkfs.ext3 //ext3

mkfs.ext4 //ext4

mkfs.ext4dev

mkfs.f2fs

mkfs.gfs2

mkfs.hfs

mkfs.hfsplus

mkfs.jfs

mkfs.logfs

mkfs.lustre

mkfs.minix

mkfs.msdos //MBR

mkfs.nilfs2

mkfs.ntfs //NTFS

mkfs.ocfs2

mkfs.reiser4

mkfs.reiserfs

mkfs.ufs

mkfs.vfat //FAT32

mkfs.xfs

mkntfs

mkreiser4

mkreiserfs
Enter fullscreen mode Exit fullscreen mode

I’ll format both partitions to ext4:

sudo mkfs.ext4 /dev/sdb1

sudo mkfs.ext4 /dev/sdb2
Enter fullscreen mode Exit fullscreen mode
mke2fs 1.45.5 (07-Jan-2022)

Creating a filesystem with 51928145 4k blocks and 12984320 inodes

Filesystem UUID: 63a3457e-c3a1-43f4-a0e6-01a7dbe7dfed

Superblock backups stored on blocks:

32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,

4096000, 7962624, 11239424, 20480000, 23887872

Allocating group tables: done

Writing inode tables: done

Creating journal (262144 blocks): done

Writing superblocks and filesystem accounting information: done
Enter fullscreen mode Exit fullscreen mode

In this example, I will mount the partitions to /mnt/audio and /mnt/video directories.

Create the mount points

sudo mkdir -p /mnt/audio /mnt/video
Enter fullscreen mode Exit fullscreen mode

And then mount a new partition:

sudo mount /dev/sdb1 /mnt/audio

sudo mount /dev/sdb2 /mnt/video
Enter fullscreen mode Exit fullscreen mode

To automatically mount a partition when your Linux system starts up, define the mount in the /etc/fstab file

-----------------------------------------------------------

FORMATTING VIA gparted

you can see manual

Top comments (0)