How do I create and tune an ext4 partition from the command-line?

Solution 1:

First and foremost:

!! WARNING !!

These commands are EXAMPLES. DELETING partitions, MODIFYING and FORMATTING filesystems destroys data and/or may prevent your machine from booting.  Make backups.  Use at own risk.   Try on a machine you don't mind losing all data on. caveat admin.


To quickly set up a drive up as a single ext4 partition...
  1. View detected devices of class "DISK"

    lshw -C disk
    
  2. View existing partition table(s)

    fdisk -l
    
  3. Edit the partition table for my chosen device (in this case, "sdx")

    fdisk /dev/sdx
    

    Within FDISK, press:

    • d ...to delete the current partition

    • n ...to create a new partition

    • p ...to specify it as a PRIMARY partition

    • 1 ...to set it as the 1ST primary partition

    • w ...to write the changes.

  4. Display the new partition table:

    fdisk -l
    
  5. Format the new partition's filesystem as type ext4

    mkfs -t ext4 /dev/sdx1
    
  6. Create a new directory where the new drive will mount into:

    mkdir /storage
    mount /dev/sdx1 /storage
    

TUNING

  1. Remove reserved blocks (i.e. set to 0%), since this drive is just for user data

    tune2fs -m 0 /dev/sdx1
    
  2. Since server is on UPS, Set write-back so apps don't wait for actual disk writes

    tune2fs -o journal_data_writeback /dev/sdx1
    
  3. Mount at boot up using /etc/fstab and also set write-back policy

    vi /etc/fstab
    
  4. Find (or add) the relevant line in fstab for your drive. Parameters in fstab are separated by white space, for example the drive described above might appear as:

    /dev/sdx1 /storage ext4 relatime,errors=remount-ro 0 1
    
    • The first parameter identifies the partition (either by /dev/ or a long UUID);
    • The second parameter is the path the partition will be mounted to;
    • Third is the filesystem type;
    • The fourth parameter contains the options;
    • Fifth is the dump schedule for backups; and,
    • The sixth parameter is pass-number (used to control fsck order).

Change the options (4th parameter) to:

noatime,nodiratime,data=writeback,barrier=0,nobh,errors=remount-ro

Reboot to check that everything went well.
Remember these commands are destructive! Have backups and be careful!

Solution 2:

Using parted

Below the instructions to create a new ext4 partition on a new hard drive with parted (tested on Ubuntu 14.04.4 LTS x64). parted supports GUID Partition Table (GPT) and subsequently can be used for partitions above 2TB, unlike fdisk.

Use sudo lshw -C disk *-disk to see the logical name of your new hard drive:

   description: ATA Disk
   product: ST6000NM0024-1HT
   vendor: Seagate
   physical id: 0.0.0
   bus info: scsi@1:0.0.0
   logical name: /dev/sdb
   version: SN05
   serial: Z4D30T30
   size: 5589GiB (6001GB)
   configuration: ansiversion=5 sectorsize=4096

The logical name is /dev/sdb. We can get started:

sudo parted /dev/sdb mklabel gpt # Creating the GUID Partition Table (GPT)

With sudo parted /dev/sdb print, we can see the GPT got created:

username@server:~$ sudo parted /dev/sdb print
Model: ATA ST6000NM0024-1HT (scsi)
Disk /dev/sdb: 6001GB
Sector size (logical/physical): 512B/4096B
Partition Table: gpt

Number  Start  End  Size  File system  Name  Flags

We also need to know the size of the disk (we will use MB as the unit):

sudo parted /dev/sdb print unit MB print free

which indicates:

Disk /dev/sdb: 6001175MB

We can now create the primary partition so that it takes the entire hard drive space. To set the partition label:

sudo parted --align optimal /dev/sdb mkpart primary ext4 0% 6001175MB 

(see https://unix.stackexchange.com/a/49274/16704 if you want to know why we use --align optimal)

With sudo parted /dev/sdb print, we can see the primary partition label got created:

username@server:~$ sudo parted /dev/sdb print
Model: ATA ST6000NM0024-1HT (scsi)
Disk /dev/sdb: 6001GB
Sector size (logical/physical): 512B/4096B
Partition Table: gpt

Number  Start   End     Size    File system  Name     Flags
 1      1049kB  6001GB  6001GB  xfs          primary

We then need to use mkfs to actually create the partition:

sudo mkfs.ext4 /dev/sdb1

The partition is now created, we need to mount it. To do so, sudo nano /etc/fstab and add the following line (/crimea is the folder in which we choose to mount the new partition):

/dev/sdb1       /crimea ext4 defaults   0       0      

We create the folder and reload /etc/fstab:

sudo mkdir /crimea
sudo mount -a # Remount /etc/fstab without rebooting in Linux

You can see the new mounted partition using df -h:

/dev/sdb1                      5.5T   58M  5.2T   1% /crimea

In sudo nano /etc/fstab, instead of using /dev/sdb1 you could use its UUID, which you can find with sudo lshw -C volume:

*-volume
       description: EXT4 volume
       vendor: Linux
       physical id: 1
       bus info: scsi@1:0.0.0,1
       logical name: /dev/sdb1
       logical name: /crimea
       version: 1.0
       serial: c3559307-795b-66db-9844-8e974c88a1cf
       size: 200MiB
       capacity: 5589GiB
       capabilities: journaled extended_attributes huge_files dir_nlink extents ext4 ext2 initialized
       configuration: created=2016-06-24 14:56:55 filesystem=ext4 lastmountpoint=/boot modified=2016-07-01 17:15:55 mount.fstype=ext4 mount.options=rw,relatime,data=ordered mounted=2016-07-01 17:07:19 name=primary state=mounted

Otherwise you could simply do sudo blkid /dev/sdb1:

/dev/sdb1: UUID="c3559307-795b-66db-9844-8e974c88a1cf" TYPE="ext4"

The new line /etc/fstab will be:

UUID=c3559307-795b-66db-9844-8e974c88a1cf          5.5T   58M  5.2T   1% /crimea

https://unix.stackexchange.com/a/137868/16704 explains why it's best to use UUID (liquidat, cc by-sa 3.0):

The advantage of using the UUID is that it is independent from the actual device number the operating system gives your hard disk. Image you add another hard disk to the system, and for some reason the OS decides that your old disk is now sdb instead of sba. Your boot process would be screwed up if fstab would point to the device name. However, in case of the UUIDs, it would be fine.


Summary:

sudo lshw -C disk                # Checking the location of the new drive. It is /dev/sdb.     
sudo parted /dev/sdb mklabel gpt # Creating the GUID Partition Table (GPT)
sudo parted /dev/sdb print       # Checking that the GPT has been created
sudo parted /dev/sdb print unit MB print free # see 6001175MB size
sudo parted --align optimal /dev/sdb mkpart primary ext4 0% 6001175MB # Creating partition label
sudo mkfs.ext4 /dev/sdb1         # Creating the partition
sudo nano /etc/fstab             # Add line `/dev/sdb1 /crimea ext4 defaults 0 0`
sudo mount -a # Remount /etc/fstab without rebooting