Ubuntu – How to create and tune an ext4 partition from the command-line

ext4fdiskfstabhard drivepartitioning

PARTITIONING (e.g. fdisk)
What are some useful commands used for partitioning an additional hard drive from the command-line? Since most published guides steer towards GParted and other graphical utilities, a summary of some command-line sequences would be helpful.

TUNING – RESERVED SPACE, WRITE-BACK & ACCESS TIMES
How to remove the reserved disk space set aside (by default) in case the drive becomes 100% full. Since I is a secondary drive simply for 'data', removing the reserved area ensures the whole drive is available for use.

I'm also curious about write-through vs. write-back and skipping the update of access times (on files and directories) so as to improve performance.

Best Answer

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!

Related Question