Thanks!
X
Paypal
Github sponsorship
Patreon

Linux-tips

Fork me on GitHub

I. Linux tips

10. Low level partition formatting

It can be useful to perform a low level partition formatting. However, it might be problematic if you want to format some specific partition (the one where /home is mounted for example). In here, I'll just show you how to do it without the "full boot". First, you need to boot just like explained in the Modify files of your linux without booting on it chapter. Once done, we can start!

Get partitions' list and their sizes

This is the easy part. Just use the fdisk command like this:

> fdisk -l /dev/sda
# or just:
# > fdisk -l

At the end of the output, you should have the following:

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: 0x136b232d

Device     Boot    Start      End  Sectors Size Id Type
/dev/sda1  *        2048 77596671 77594624  37G 83 Linux
/dev/sda2       77598718 94369791 16771074   8G  5 Extended
/dev/sda5       77598720 94369791 16771072   8G 82 Linux swap / Solaris

The important pieces of information here are:

Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes

And the Sectors column of the partitions.

Formatting

Now, let's see how to format one of the previous partitions. However: BE VERY CAREFUL!!!. Once done, you cannot get back the erased data!

In this example, I'll format the /dev/sda1 partition.

First, get the Sectors number corresponding to the partition you want to format (77594624 for me). Then take the I/O optimal size (512 here) and the Sector logical size (512 once again).

Now that you have your 3 numbers, you'll need to do 2 things:

  • First, divide the I/O optimal size by the Sector logical size (so for me: 512 / 512).
  • Second, divide the Sectors number by the result of the previous operation (so for me: 77594624 / 1).

All good? Great, now we can actually start the formatting!

First let's take a look to what we'll do:

dd bs=[I/O optimal size] [result of the second division (77594624 / 1)] if=[device on which we'll read new data] [partition on which we'll write them]

And now in practice:

dd bs=512 count=77594624 if=/dev/zero of=/dev/sda1

If you want a bit more security, you can replace /dev/zero by /dev/urandom if you want to have your partition filled with random numbers instead of zeros.

This operation can take quite a while depending on your hard drive writing speed, so don't worry if it takes time!