d
Amit DhamuSoftware Engineer
 

Reformat A Hard Drive on Ubuntu

2 minute read 00000 views

I have a headless server at home and needed to be able to reformat and recover a dodgy partition (testdisk came to my rescue) on one of my hard drives.

This will erase any data you already have on your drive so BE CAREFUL and ensure you have backups!

I will use /dev/sdb as the name of the drive. Run the following to check what your disk name is:

sudo fdisk -l

Delete The Filesystem

sudo wipefs /dev/sdb

Create Partition(s)

sudo fdisk /dev/sdb

Type n to create a new partition. You should be presented with a bunch of options. I selected the defaults for the first partition. If you would like more than 1 partition, ensure that when it asks you about your "last sector" size, you adjust it in bytes (the default will partition to the size of the entire disk).

Once you've finished, type w which will write all changes to the disk.

Format The Drive

sudo mkfs -t ext4 /dev/sdb1
❯ sudo mkfs -t ext4 /dev/sdb2 // if you created a second partition etc

Create Mount Points

mkdir -p /storage/DISK1
❯ mkdir -p /storage/DISK2

Add Entries To /etc/fstab to Auto Mount on Boot

# This will show you all disks. You need to grab the UUID for your new ones.sudo blkid

❯ sudo vim /etc/fstab

# Add the following lines such as (your UUIDs will be different)
UUID=474dnb73-7111-74YH-4857-9834uerkfnf1 /storage/DISK1 ext4 defaults 0 0
UUID=f8fjnrhg-b3c3-hg65-7472-mfiudfg74ng3 /storage/DISK2 ext4 defaults 0 0

# Test if your mount options are correctsudo mount -a

# Further test if your disks automountsudo reboot