40

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.

62

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!

  • 3
    There should be a space between storage and ext4. So the command is like /dev/sdx1 /storage ext4 relatime,errors=remount-ro 0 1 – Ganesh Krishnan May 5 '15 at 1:18
  • Great guide! It would be nice to have some references here on how to determine appropriate fstab options (just a link to some list would be great) – Sander Aug 27 '18 at 6:35
13

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
  • instead os telling the end MB at sudo parted --align optimal /dev/sdb mkpart primary ext4 0% 6001175MB it would be better to put ext4 0% 100% to avoid the Warning: The resulting partition is not properly aligned for best performance. – Pablo Marin-Garcia Aug 26 '18 at 15:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.