How to recover USB drive/flash drive after flashing an ISO image or installing OS (SOLVED)

Experienced users can do various non-trivial things with USB media (disks and flash drives):

  • install OS on them
  • split a USB flash drive into several partitions
  • burn a Live image and make a bootable USB flash drive
  • destroy partition table

All these actions are, on the one hand, reversible, but, on the other hand, can cause unforeseen consequences. for instance:

  1. If you burned a Live or an installation image to a USB flash drive, you may find that it is impossible to format the USB flash drive and return it to its normal state for normal use, that is, to write files, using the usual means.
  2. Installing an OS on a USB flash drive also leads to the creation of several partitions, as a result of which it may be difficult to delete and format them.
  3. The USB flash drive on which the partition table was deleted cannot be used.
  4. The operating system is not installed on the USB flash drive due to a disk partitioning error, although the USB flash drive is in working order

This article is devoted to similar questions – how to return a USB flash drive to its normal state, delete all partitions and format it so that you can write files and/or install the OS on it.

Recovering a USB flash drive in Windows

To view the disks, run (press Win+r and type, either find in the Start menu, or run in the command line):

diskmgmt.msc

The screenshot shows a USB flash drive with an ISO image with a Live Linux system. As you can see, no actions are available for this media.

It is also not possible to delete or format partitions.

To fix it, open a command prompt with administrator rights – this can be done by pressing Win+x and selecting Windows Terminal (Admin).

Run the diskpart utility, to do this, in the command line that opens, run:

diskpart

List the disks with the following command:

list disk

Now select the disk that needs to be restored, this is done with a command like

select disk X

Where as “X” you need to specify the number of your USB flash drive that you want to restore.

For example, my disk number is 2, then the command is as follows:

select disk 2

The following command cleans up the disk:

clean

If everything went well, then a message will be displayed

DiskPart succeeded in cleaning the disk.

You may encounter an error:

DiskPart has encountered an error: Access is denied.
See the System Event Log for more information.

One of the most common reasons for this is that the disk is being used in another program, for example, the disk is currently open in the file manager. Close the file manager and other programs that might be using the disk and resubmit the command.

Now create the partition table:

create partition primary

If everything went well, the following message will be displayed:

DiskPart succeeded in creating the specified partition.

Now create the filesystem by choosing one of the following commands:

format fs=fat32 quick
# OR
format fs=exfat quick
# OR
format fs=ntfs quick

To exit the program, enter:

exit

As you can see, the flash drive has returned to its normal state – a letter has been assigned to it, you can perform normal actions with it, for example, write files to it.

Recovering a USB flash drive in Linux

In Linux, in addition to the fact that disks and flash drives with partitions are restored by other utilities, it should be borne in mind that in most modern distributions media partitions are mounted automatically, and for the successful completion of cleaning and recovery operations, it is necessary that the flash drive is not used by any program.

By the way, automatic mounting can cause some problems: for example, if you try to install Linux on a USB flash drive and disk partitioning fails, then the most likely reason for this is automatic mounting of flash drive partitions. The cleaning shown in this guide will help you avoid this problem with disk partitioning.

1. Cleaning a USB flash drive using wipefs

These methods, on the one hand, are the fastest and simplest, but, on the other hand, should be used very deliberately so as not to destroy data from the wrong medium.

The wipefs program cleans up the file system, array, or partition table signatures from the specified device, making them invisible to libblkid. wipefs does not remove the filesystem itself or any data from the device. In short, wipefs will delete all partitions and to return the flash drive to normal operation, it will be enough to format it into the file system you need.

We need to start by determining what the name has a flash drive among disk devices. Another way to determine the name is shown below. Alternatively, you can unplug/plug the flash drive and see which device was removed/added.

The following command shows a list of devices and monitors the appearance of new ones:

watch -t lsblk -d

As soon as the flash drive was connected to the computer, we can see that the sdb device has been added, therefore, this is the device name we are looking for.

Unmount all partitions that might be mounted:

sudo umount -A /dev/sdb*

We delete all partitions and file systems from the specified device:

sudo wipefs --all /dev/sdb

Device name lookup and cleaning can be automated with the following small script – you can copy/paste the entire code snippet into the console in one go. For the script to work, you must have the fzf package installed.

disk=`lsblk -dpo "NAME,PTTYPE,TRAN,SIZE,VENDOR,MODEL" | fzf --bind 'ctrl-r:reload(lsblk -dpo "NAME,PTTYPE,TRAN,SIZE,VENDOR,MODEL")' --header $'Ctrl+c - exit\nCtrl+r - refresh\nEnter - wipe\n\n' --header-lines=1 --layout=reverse | awk '{print $1}'`
echo -n "This will clean up the $disk disk. If everything is correct, enter “yes” in capital letters > "
read
if [ "$REPLY" == "YES" ]; then
	echo 'You have agreed to cleanup ' $disk
	sudo umount -qA ${disk}*; 
	sudo wipefs --all $disk
fi

In the screenshot below, you can see two disks:

Connect the USB flash drive and press Ctrl+r.

You can see that a USB stick has been added. Select it and press ENTER.

If everything is correct, type “YES” and press ENTER again. As a result, the selected disk will be cleaned up.

If you encounter an error

wipefs: error: /dev/sdb: probing initialization failed: Device or resource busy

to fix it, unmount all partitions of this disk and close all programs that use this disk or files on it.

2. Finding a disk name and unmounting partitions

Let's consider another method of finding the name of a flash drive among disk devices.

Regardless of which program you choose, you need to be firmly confident in the name of the flash drive that you will clean and restore, and not in any way confuse it with another permanent storage (hard drive or SSD).

You can display the list of disks with the command:

sudo fdisk -l

The screenshot below shows two disks (1), to find your way, look at the disk size (2), disk model (3), list of partitions (4).

Suppose I want to reset a disk named /dev/sdb with the following partition:

Device     Boot       Start       End   Sectors   Size  Id  Type
/dev/sdb1  *             64   7866239   7866176   3,8G  17  Hidden HPFS/NTFS
/dev/sdb2           7866240   7867711      1472   736K   1  FAT12
/dev/sdb3           7867712 195312500 187444789  89,4G  83  Linux

It is recommended to unmount its partitions that might have been mounted automatically. To find mounted partitions:

mount | grep sdb

Output example:

/dev/sdb1 on /run/media/mial/Kali Live type iso9660 (ro,nosuid,nodev,relatime,nojoliet,check=s,map=n,blocksize=2048,uid=1000,gid=985,dmode=500,fmode=400,iocharset=utf8,uhelper=udisks2)
/dev/sdb2 on /run/media/mial/6170-006B type vfat (rw,nosuid,nodev,relatime,uid=1000,gid=985,fmask=0022,dmask=0022,codepage=437,iocharset=ascii,shortname=mixed,showexec,utf8,flush,errors=remount-ro,uhelper=udisks2)

That is, the /dev/sdb1 and /dev/sdb2 partitions are mounted. To unmount them, run the following commands:

sudo umount /dev/sdb1
sudo umount /dev/sdb2

In fact, some programs (for example, GParted) will automatically unmount the partitions of the flash drive. Some programs clean up the disk regardless of whether its partitions are mounted or not. But in order not to need to disconnect and reconnect the USB flash drive (or restart the computer if you are cleaning an inner disk), it is recommended to unmount the partitions first.

3. Recovering a USB flash drive in a graphical interface using GParted

The GParted program is designed to manage disk partitioning, it allows you to create, delete, modify and format disk partitions.

If your system does not have GParted, then install the gparted package.

You can run the program through the menu or on the command line:

sudo gparted

Working with the program is quite intuitive. Start by selecting the flash drive you want to clean or restore from the drop-down menu located in the upper right corner. Then right-click on the disk and select the desired operations, for example, format to exFAT.

You can choose several actions, for example, create or delete partitions, select a file system for formatting – at this stage, in fact, no action is yet taken. To apply the selected changes, click on the button with a green checkmark (“Apply All Operations”).

4. Cleaning the USB stick in Disks

Disks is a disk management utility. It allows you to mount, unmount partitions, create, delete, modify and format disk partitions.

To get Disks, install the gnome-disk-utility package.

You can run the program through the menu or on the command line:

sudo gnome-disks

The program has many different buttons and menus that allow you to do all the necessary actions, but the program's interface is intuitive.

5. Destroying any partition table with dd

Next, we will show you two more ways to restore and completely clean the disk from partitions. These methods are not identical: the first is more universal and works in all cases.

Use dd to delete the partition table:

sudo dd if=/dev/zero of=/dev/sdX bs=1M count=1

Replace sdX with a valid drive name, for example:

sudo dd if=/dev/zero of=/dev/sdb bs=1M count=1

To format a USB flash drive to the FAT32 file system, use the command (replace the name of the disk or partition with the desired one):

sudo mkfs.vfat /dev/sdb

To format a USB flash drive to the NTFS file system, use the command (replace the name of the disk or partition with the desired one):

sudo mkfs.ntfs /dev/sdb

To format a USB flash drive to the exFAT file system, use the command (replace the disk or partition name with the one you need):

sudo mkfs.exfat /dev/sdb

6. Destroying GPT and MBR Partition Tables with gdisk

This method does a good job of deleting GPT and MBR partition tables, but it is useless against Live systems written as ISO images to a USB flash drive, since gdisk is not designed to work with this file system.

Open the USB flash drive for cleaning with gdisk:

sudo gdisk /dev/sdb

To switch to expert mode, enter

x

Then to wipe the GPT enter

z

Agree twice to completely wipe the disk.

Press Ctrl+c or Ctrl+d to exit the program.

You can check the markup of the flash drive again to make sure that the changes are saved:

sudo fdisk -l

Recommended for you:

Leave a Reply

Your email address will not be published.