Guide: using the linux liveCD/USB command line to wipe the beginning of a disk / partition

So you have this shiny new drive, but when you put it into your Windows PC and fire it up it seems that Windows doesn't want to work with it; it just displays it as a 'RAW' partition, regardless of what you do to try and correct the problem.  You've checked cables, you've re-seated the card (in the case of NVMe's), and you're about to pull it out to send it back for a replacement (or worse, write it up as a loss).

Before that, however, there is one last option...

OBLIGATORY DISCLAIMER:  the method I'm about to describe here is as barebones as it gets.  What we're about to use is an old command line program that does absolutely no hand holding or warnings whatsoever.  If you type the final command line wrong the chances of you losing data just jumped to almost 100% (unless it just kicks back an error, in which case consider yourself lucky)!  That being said, figuring out the correct command line isn't difficult, and this works with just about any PC, mac, etc., out there.  Carry on, friend, but consider yourself warned!

Okay, now that I've made you nervous it's time to describe how to get this done properly...

This guide uses an old command that's in Linux called dd.  It's been around since the early days of UNIX, and it officially stands for 'Data Description', but it has a few other unofficial names in the *nix world ('data duplicator' and 'data destroyer' immediately come to mind).  It can be used to make byte for byte copies and disk images of data, or it can be used to clear out - 'destroy' - part or all of a disk's data, which is what we're using it for in this case.  Since it's such an old program, it assumes that the person running it knows what they're doing (after all, if you were using UNIX in the 70's and 80's you were on big time equipment, so chances were you were a (hopefully) knowledgeable administrator of some sort).  You can read more about the dd command and it's usage in a Linux environment here, but at this point I'm going to go ahead and proceed with the assumption that we're going ahead with this.  Otherwise, well, it's been real...

Step 1: Obtaining the needed components


First thing's first:  to use a Linux live environment we need, well, a Linux environment.  Being a Windows user you don't have one yet, so we'll need to make a Linux live CD/USB boot disk.  For the purposes of this guide we'll be working with a USB thumb drive.  Obviously, we'll need a USB thumb drive of some sort.  I usually tell people to go for a drive that's at least 4GB in size, but the size of the disk image I'm going to have you download is small, so you could probably get away with using one as small as 2GB.

Once you have that, let's go ahead and download the disk image we want to write to that disk.  Since this is a command line program we'll be using we really don't need a GUI environment at all, so we're going to keep it small, barebones, and (relatively) simple:  we're going to download an Arch Linux disk image.  The Arch Linux website is located at http://archlinux.org.  You could go to that site, click on the 'Download' link in the upper right, then scroll down the next page and find a mirror site for you to download the ISO file from ... or you could just click this to get the ISO from kernel.org.  Your choice.  Download the file and make note of where you save it to.

Okay, we have a drive and we have a disk image.  Now we need a way to write the disk image to the drive.  There are a few different ways to do this, but in this guide we're going to get a utility called Rufus.  Rufus is, as far as I'm concerned, as good as it gets when it comes to writing something to disk reliably.  Go to the Rufus website (https://rufus.ie), scroll down, download the Rufus installer (or click here to get it), then run the installer so it's made available to you.

Next up:  plug in your USB drive and run the Rufus program (in that order), which will present you with the following:


The top of the window has an area marked 'Device'; it should be showing the USB drive you wish to install Arch to.  If it doesn't, change it accordingly.  Just below that, In the upper right hand area of that window, you'll see a button that simply says 'SELECT'; click it and point it to the Arch Linux .iso file that you just downloaded earlier.  Afterwards, to the left of the 'SELECT' button, in an area marked 'Boot selection', it should show the name of your Arch Linux .iso file. Just a little bit under that, in an area that says 'Partition scheme', make sure to change that to 'GPT' if you're using a recent machine (obviously if you're using an old machine, stick to the 'MBR' scheme).  The rest of your Rufus window should reflect what is shown in the example above (Don't change the Volume label; the arch USB boot sequence is a picky little bastard that doesn't react well to change).  Once everything looks good, click the 'START' button at the bottom.  After a bit, it'll say it's done.  At this point, we have what we need to enter the linux live environment.

Step 2: Taking notes


Before we jump in, let's make a quick but critical note.  Open up Windows Disk Management ([WIN+X ]--> 'Disk Management').  Let's make note of any thing unique about the partition we want to work with that'll identify it later:


In the example above, Let's say we wanted to work with the partition marked drive 'D:'.  There are no drive letters in Linux, so we'll need to make note of some more information.  By looking at the partition in the window we can tell that the target partition is named "Stuff", it's the second partition on the second physical disk, it's 833.85GB in size, and it's the only partition of that size in this PC.  If we had a situation where there were multiple partitions of the same size we could go further by obtaining the partition's UUID via a command in Powershell, but I think we have enough information to reliably identify this partition in a list.

If we have a situation where our target is the only partition on a disk and it takes up the whole disk, like the example below:


Well then, things just became a bit easier:  we don't have to worry about what partition it is at all.  We'll address the disk as a whole, which is a bit more thorough in my opinion.   In the example above we see that the disk itself is 14.32GB in size, and of course it's the only disk that only has one partition on it.

Either way, make a note and set it aside for a bit later.

Step 3: Booting into the Linux live environment


Now it's time to go into our Linux environment.  Put your Linux USB drive in a port and restart your computer.  Some PC's will automatically detect a USB drive on startup and boot to it, while with others you may need to hit a key to pull up a menu and choose what device to boot from (usually F12, but it varies from system to system).  Check out the manual for your particular PC/motherboard to figure it out if you're having trouble.

NOTE: if your system's boot menu presents you with multiple options for the same USB drive, choose the 'UEFI' option.

When you first boot to the USB drive, you should see something like this:


You will want to choose the first option in the menu to proceed.  The machine will do some basic startup screens, then it'll come to a very, *very* basic looking command prompt.


NOTE: The examples here will look slightly different, since they're from an already existing Linux environment, but the minor differences won't affect the results of this guide.

Step 4: Comparing notes


Now that we're here, let's get right to it.  Type 'lsblk', followed by [ENTER].  You should see a tree view of the block devices (i.e. drives and partitions) in your PC:


Let's go back to the notes we made about the target partition:  we know that the partition we want to work with is 833.85GB in size.  The only partition in this list that matches that is the seventh option in this list (and yes, it actually says 833.9GB in size; it rounded up to the nearest tenth).  Under the 'NAME' column it says 'nvme1n1p2'; the naming scheme for stuff like this in Linux is /dev/<name>, so our target partition, for the purposes of this guide, is named '/dev/nvme1n1p2'.

If, like in our second Windows disk management example, there was only one partition branched off from the physical disk we would want to work with the disk as a whole.  In that case, we would look not at the partition entry, but the disk entry that the partition branches off of; in this case, 'nvme1n1' (leaving off the 'p2'), which would make our target disk named '/dev/nvme1n1'.

Step 5: Here we go...


Now that we have the target partition/disk name, it's time to do the proverbial deed.  Here is a screenshot of the command, which I'll break down for better understanding below:


The command is: 'sudo dd if=/dev/zero of=/dev/[Name] bs=1M count=2'.  Here's the breakdown:
  • 'sudo' = this Linux command lets someone run whatever comes after it as an administrator (known as 'root').  This is the only 'safeguard' that dd has, if you can call it that in this case.  Usually using sudo would prompt you for a password, but this being an Arch live environment it shouldn't do that; if it does, simply press [ENTER].
  • 'dd' = the command itself that we're attempting to run
  • 'if=/dev/zero' = if is for 'input file'; it defines what we're copying from.  In this case, /dev/zero is a special device in Linux/*NIX that does nothing but write zeros.  If we wanted to make sure no one would recover what's being deleted we could write random stuff on the target by specifying '/dev/random' instead, but for the purposes of this guide we'll be fine with zeros.
  • 'of=/dev/[Name]' = of is for 'output file'; it defines what we're copying to.  In this case, [Name] would be replaced with what we found above with lsblk; in the first case of the second partition, it would be 'of=/dev/nvme1n1p2'; in the case of a single partition on the whole disk, it would be 'of=/dev/nvme1n1'.  Double-check this and make sure you enter it right, because it's going to run with whatever you give it without confirmation!
  • 'bs=1M' = bs is for 'bytes per sector'.  This is situational; depending on what you're doing with dd you might want to match this to whatever your filesystem's bytes per sector is, or in other cases you might match this to the amount of cache on your drive to speed up dd considerably.  On a GPT disk the first megabyte of data contains both the boot sector and the partition structure info, which is what we want to get rid of.  On older MBR disk setups, all of that is contained in the first 512k of the disk.  Even if we're just getting rid of the structure on an individual partition, this will suffice.
  • 'count=2' = count refers to how many of those sectors we just defined to write.  It's technically optional; if you wanted to wipe the entire disk or partition you'd just leave this part off and it'd continue until it ran out of space on the target area.  Technically 'count=1' would be enough, but I'm one of those strange weirdos that likes to take that one step extra, just to be sure.
Once you've double-checked what you typed and you're good with it, press [ENTER].  The process should be almost instantaneous; you're only erasing a couple of individual sectors.  When it's done, you should get something similar to this:


Note:  I modified my dd line just a bit to ensure I didn't destroy a perfectly good partition.

Once you see the three lines of output and you get your prompt back, that's it; you're done.  Now type 'sudo reboot', followed by [ENTER], and remove your Arch USB drive.  At this point, if all goes well, you should be able to do whatever you need to do in Windows with the newly erased partition.

It's not pretty and it's definitely not for speed readers, but in a pinch as long as you have the thumb drive handy it works well enough.  I hope this guide helps out.


Comments

Popular posts from this blog