Skip to content

Instantly share code, notes, and snippets.

@mateuscelio
Last active July 22, 2021 02:27
Show Gist options
  • Save mateuscelio/304ff09d173564468a66efd4c1ecdae1 to your computer and use it in GitHub Desktop.
Save mateuscelio/304ff09d173564468a66efd4c1ecdae1 to your computer and use it in GitHub Desktop.

How to partition and format a hard drive in Linux

Will be created one partition with GPT as partition table using CLI program parted. The created partition will be formatted with NTFS filesystem.

Getting storage device information

First, list for connected storage devices to your computer and check for its location

sudo parted -l

This command will print information about the disks available


Model: ATA WDC WD5000LPVT-2 (scsi)
Disk /dev/sda: 500GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:

Number  Start  End  Size  File system  Name  Flags

Model: SD SC32G (sd/mmc)
Disk /dev/mmcblk0: 31.9GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:

Number  Start   End     Size    Type     File system  Flags
 1      1049kB  269MB   268MB   primary  fat32        boot, lba
 2      269MB   31.9GB  31.6GB  primary  ext4


There is two storage devices located in /dev/sda and /dev/mmcblk0. The first device dont have any partition while there are two partitions in the second.

Will be used the disk in /dev/sda. To start the partition process run parted specifying the disk location

Using partition tool parted

To use the partition tool run the program parted with superuser specifying the disk directory.

sudo parted /dev/sda

The program will start in interactive mode

sudo parted /dev/sda

GNU Parted 3.3
Using /dev/sda
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted)

Creating a partition table

Create a partition table before partitioning. Partition table will describe the partitions on the disk. To create a partition table in parted interactive mode run the command mklabel and specify the disk label used. Press TAB to see available options

(parted) mklabel
New disk label type?
aix    amiga  atari  bsd    dvh    gpt    loop   mac    msdos  pc98   sun

The most types used are msdos (MBR) and GPT. See here the difference between them and which one to use.

In this case I will use gpt.

Create Partition

To create the partition use the command mkpart following partition type, filesystem, start and end. The start and end can be expressed in terms of bytes or in percentage.

(parted) mkpart primary ntfs 0% 100%

Use the command print to see the created partition

(parted) print
Model: ATA WDC WD5000LPVT-2 (scsi)
Disk /dev/sda: 500GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:

Number  Start   End    Size   File system  Name     Flags
 1      1049kB  500GB  500GB  ntfs         primary

Enter the quit command to save and exit.

Checking the partition created

To check the created partition run sudo parted -l.

Model: ATA WDC WD5000LPVT-2 (scsi)
Disk /dev/sda: 500GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:

Number  Start   End    Size   File system  Name     Flags
 1      1049kB  500GB  500GB               primary  msftdata

Model: SD SC32G (sd/mmc)
Disk /dev/mmcblk0: 31.9GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:

Number  Start   End     Size    Type     File system  Flags
 1      1049kB  269MB   268MB   primary  fat32        boot, lba
 2      269MB   31.9GB  31.6GB  primary  ext4

Another way to see the partition is using the command lsblk

NAME        MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
.
.
.

sda           8:0    0 465.8G  0 disk
└─sda1        8:1    0 465.8G  0 part
mmcblk0     179:0    0  29.7G  0 disk
├─mmcblk0p1 179:1    0   256M  0 part /boot/firmware
└─mmcblk0p2 179:2    0  29.5G  0 part /
.
.
.

As we can see the disk location is /dev/sda while the partition is located in /dev/sda1.

Formatting the partition

After the partition creation, is necessary format it with a file system. To format use the program mkfs specifying the type in -t argument and the partition location. To speed the process when creating a NTFS, to avoid initializing the storage with zeroes use the argument -f

sudo mkfs -t ntfs -f /dev/sda1

Done!

References

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment