When you add a new blank disk to your server, you will probably need to do some steps before you can use it.

Table of Contents

Creating partition

  1. List all available disks and its partitions with lsblk, look for your blank disk and note its name (in this case, vdb).
      $ lsblk
      NAME                      MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
      loop0                       7:0    0 55.4M  1 loop /snap/core18/1944
      loop1                       7:1    0 31.1M  1 loop /snap/snapd/10707
      loop2                       7:2    0 69.9M  1 loop /snap/lxd/19188
      loop3                       7:3    0 32.3M  1 loop /snap/snapd/12704
      loop4                       7:4    0 55.4M  1 loop /snap/core18/2128
      sr0                        11:0    1 1024M  0 rom
      vda                       252:0    0   20G  0 disk
      ├─vda1                    252:1    0    1M  0 part
      ├─vda2                    252:2    0    1G  0 part /boot
      └─vda3                    252:3    0   19G  0 part
     └─ubuntu--vg-ubuntu--lv 253:0    0   19G  0 lvm  /
      vdb                       252:16   0   10G  0 disk
    
  2. We are going to use fdisk to create a new partition on vdb. Type fdisk /dev/ + your disk name.
      sudo fdisk /dev/vdb
    
  3. fdisk allows you to do several tasks (you can type m to see every option) but now you are only going to create a new partition. Type n and press Enter.
  4. If you want to create one partition that fills up entire disk, simply press Enter to all prompts.
      Command (m for help): n
      Partition type
     p   primary (0 primary, 0 extended, 4 free)
     e   extended (container for logical partitions)
      Select (default p):
    
      Using default response p.
      Partition number (1-4, default 1):
      First sector (2048-20971519, default 2048):
      Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-20971519, default 20971519):
    
      Created a new partition 1 of type 'Linux' and of size 10 GiB.
    
  5. Type w and press Enter to save the changes.
  6. If you type lsblk again you will notice there is a new partition on your disk (in this case, vdb1). You will need this name for the next step.

Formatting and labelling the partition

  1. Format the new partition to ext4 (default format for Linux systems) by typing:
      sudo mkfs.ext4 /dev/vdb1
    
    • Replace partition name for yours.
  2. You can label the partition with e2label:
      sudo e2label /dev/vdb1 "New partition"
    
    • For NTFS partitions, use ntfslabel.
    • For FAT (MS-DOS) partitions, use fatlabel.

Mount the partition

  1. Now you can mount your formatted partition. First, create a folder where you will mount the partition. Mount points are usually created under /mnt or /media folder, but you can use any other path.
      sudo mkdir /mnt/mydisk
    
  2. Finally, mount the disk partition.
      sudo mount /dev/vdb1 /mnt/mydisk
    
    • You can mount a filesystem as “read-only” by using the command parameter -o ro.
  3. When you want to unmount, simply type sudo umount + mount point or partition path.
      sudo umount /mnt/mydisk
      #or
      sudo umount /dev/vdb1
    

Mounting partition automatically when system boots up

  1. If you want your disk partition automatically mounts when system boots up, you will need to edit /etc/fstab file. First, note filesystem UUID by typing:
      $ sudo blkid
      /dev/vdb1: UUID="14bf9ffc-708c-4cf8-b7b2-7a5fb205a6d0" TYPE="ext4" PARTUUID="0a77ac42-01"
    
    • You can also type lsblk -o name,uuid.
  2. Open /etc/fstab with your preferred text editor.
      sudo vim /etc/fstab
    
  3. At the end of the file, type this (using your UUID and mount point):
      UUID=14bf9ffc-708c-4cf8-b7b2-7a5fb205a6d0 /mnt/mydisk ext4 defaults 0 0
    
  4. Save changes and close the file.
  5. You can check if everything works as expected, typing sudo mount -a (first unmount the partition if it’s mounted) and checking if partition is mounted.
  6. Next time you boot up your system, disk partition will be automatically mounted.

If you have any suggestion, feel free to contact me via social media or email.