Recently I bought a 2TB external HDD that I'm planning on using as backup and family media storage (photos, videos, etc). The very first thing I noticed after connecting the drive on the USB port was the fact that the Linux system identified the partition as NTFS/HPFS/extFAT. This might suffice those who plan to move around the external hard drives between different system but for me this is not a concern,
I want to get the best performance possible.
Identifying the Correct Drive
Before starting with the process of removing, creating and formatting partition you'll need to make sure which device is associated with the external USB drive, for this you need to run the following command:
$ sudo fdisk -l | grep "^Disk /dev"
Make sure to write down the device name associated to the drive by looking into the size of the hardware. In my case, my Linux system attached the drive as /dev/sdb.
Removing the old partition
Once you've identified the proper device name you should proceed to remove the existing partitions on the drive. In order to identify how many partitions are configured, use the following command, replace /dev/sdb with your device:
$ sudo fdisk -l /dev/sdb
This is an example of the output:
$ sudo fdisk -l /dev/sdb
Disk /dev/sdb: 2000.4 GB, 2000398934016 bytes
1 heads, 63 sectors/track, 62016336 cylinders, total 3907029168 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
Device Boot Start End Blocks Id System
/dev/sdb1 * 63 3907024127 1953512032+ 7 HPFS/NTFS/exFAT
I proceeded and deleted all partitions in /dev/sdb by using:
$ for PART in $(fdisk -l /dev/sdb | grep ^/dev | awk '{print $1}' | sed 's/.*\(\w\)$/\1/' | xargs echo)
do
echo "Deleting partition /dev/sdb$PART";
echo "d\n$PAT\nw\n" | sudo fdisk /dev/sdb
done
Creating New Partition
I created a single new partition on my hard drive by running:
$ echo "n\np\n1\n\n\nw\n" | sudo fdisk /dev/sdb
This will create a new partition of type Linux (83) and will use all the sectors identified by the system.
Formatting New Partition
After executing the previous command we have now a new partition which needs to be formatted. In my case I decided to format my drive as ext4 using the following command:
$ sudo mkfs.ext4 /dev/sdb1
A few minutes later the partition is ready to use.
Removing Reserved Space
When the hard drive was formatted, the system threats it as if it were going to host an operative system and then reserves some space for it. Since this USB drive will be used to store/backup data there is no point in allow that configuration. The next command will set the reserved blocks percentage to 0%:
$ sudo tune2fs -m 0 /dev/sdb1
At this time the drive is ready for use.
If you're looking to access the hard drive sporadically, I'll recommend to setup automount so the device will be connected and mounted automatically on demand. In addition, this solves the headache of having USB devices attaching to different USB device after device reset or system reboot.