Disk Management in Linux

Storage

The storage is a hard disk of our computer.

Storage is of three types: Local storage, SAN(Storage Area Network), and NAS(Network Attached Storage).

Disk Partition

The commands for disk partition are df, fdisk.

To get information about filesystems, their sizes, used, available spaces, and where they are mounted, run the command:

df

To get information of filesystems, their sizes, used, available spaces, and where they are mounted in a more readable format, run the command:

df -h

linux-df

To get information on a disk partition, sizes, mounted items, run the command:

fdisk -l

Add Disk and Create Standard Partition

We add new disk space when we run out of space or require additional applications.

For additional storage follow the steps :

  • Click on the Settings tab from the Oracle VirtualBox Manager.
    linux-settings
  • Click on the Storage tab from the Settings window.
    linux-storage
  • Select Controller link. Then click on the Add button.
    linux-storagecontroller
  • Select the Add Hard Disk option.
    linux-harddisk
  • Click on Create new Disk from the pop-up.
    linux-virtualboxpop
  • Select the default value VDI from the Hard disk file type pop-up.
    linux-harddisktype
  • Select the default value Dynamically allocated from the Storage on physical hard disk pop-up.
    linux-virtualboxstorage
  • Give a name to the new virtual hard disk file and select the size of the virtual hard disk.
    linux-lvmfilesizeset
  • The new virtual disk now gets displayed under the Storage tab.
    linux-newstorage
  • Click on Start from the VirtualBox Manager.
    linux-virtualboxstart
  • Become root with the command:
    su -​
  • To get the details of the storage we created, run the command:
    fdisk -l | more​

linux-fdisk

  • To get into the new storage /dev/sdb, run the command:
    fdisk /dev/sdb​
  • Run the command n to add a new partition.
  • Then select the partition type. For primary type p(default), for extended type e. Press Enter select the default value.
  • Select the partition number(1-4, default is 1). Press Enter select the default value.
  • Select value for the First sector. Press Enter select the default value.
  • Select the value for the Last sector. Press Enter select the default value.
    linux-partitionval
  • Run the command w to write table to disk and exit.
  • To set the file system for the new storage run the command:
    mkfs.xfs /dev/sdb1​
  • To mount, first of all, create a directory with the command:
    mkdir data​
  • To mount the directory to the storage, run the command:
    mount /dev/sdb1 /data​
  • To verify the partition, run the command:
    df -h​

linux-diskmount

  • To enable the disk mounting at the boot time, run the command:
    vi /etc/fstab​
  • Add the entries one by one after pressing the TAB:
    /dev/sdb1 /data xfs defaults 0 0​
  • Save and quit with :wq!
  • To unmount the /data, run the command:
    unmount /data​

Logical Volume Management (LVM)

LVM allows disks to be clubbed together. It is software-based. A disk can be converted to a volume group with the help of the LVM software. Then this volume group can be partitioned into logical volumes.

Also, multiple disks can be together to a volume group by the LVM software. Then the volume group can be partitioned into logical volumes. In case there is a shortage of spaces, we can extend the group.

LVM Configuration During Installation

Let us install CentOS with LVM configuration:-

  • Click on New from the VirtualBox Manager.
    linux-lvminstallation
  • Add a name to the Name and operating system pop-up.
    linux-lvminstallation1
  • Keep the memory size to 1024 MB.
    linux-lvminstallation2
  • Select Create a virtual hard disk now from the Hard disk options.
    linux-lvminstallation3
  • Select VDI from the Hard disk file type.
    linux-lvminstallation4
  • Choose Dynamically allocated from the options of Storage on the physical disk.
    linux-lvminstallation5
  • Note the file location and size of the virtual box.
    linux-lvminstallation6
  • The virtual box got created. Click on the Start button.
    linux-lvminstallation7
  • Browse and attach the CentOS image, then click on Start.
    linux-lvminstallation8
  • Select Install CentOS 7 option and click on Enter.
    linux-lvminstallation10
  • After installation, CENTOS 7 Installation screen comes up. Select the default values for language and click on Continue.
    linux-lvminstallation11
  • Click on NETWORK & HOSTNAME from the INSTALLATION SUMMARY PAGE.
    linux-lvminstallation12
  • Enter a Hostname and click on Apply. The hostname entered gets populated at the right. Click on Done.
    linux-lvminstallation13
  • Click on INSTALLATION DESTINATION from the INSTALLATION SUMMARY PAGE.
    linux-lvminstallation14
  • Select the Device from the Device Selection section.
    linux-lvminstallation15
  • From the Other Storage Options, select the option I will configure partitioning and proceed.
    linux-lvminstallation16
  • Select LVM from the partitioning scheme dropdown.
    linux-lvminstallation18
  • Click on +.
    linux-lvminstallation19
    • Select the /home option from the Mount Point dropdown, enter the Desired Capacity, and then click Add mount point.
      linux-lvminstallation20
    • All the information on Mount Point, Desired Capacity, Device Type, File System, and so on gets populated. We can modify them if needed.
      linux-lvminstallation21
    • We still have available space. Click on +.
      linux-lvminstallation22
    • Select the /var option from the Mount Point dropdown, enter the Desired Capacity, and then click Add mount point.

linux-lvminstallation23

  • All the information on Mount Point, Desired Capacity, Device Type, File System, and so on gets populated. We can modify them if needed.
    linux-lvminstallation24
  • We still have available space. Click on +.
    linux-lvminstallation25
  • Select the swap option from the Mount Point dropdown, enter the Desired Capacity and then click Add mount.
    linux-lvminstallation26
  • All the information on Mount Point, Desired Capacity, Device Type, File System, and so on gets populated. We can modify them if needed.
    linux-lvminstallation27
  • We still have available space. Click on +.
    linux-lvminstallation28
  • Select the /boot option from the Mount Point dropdown, enter the Desired Capacity, and then click Add mount.
    linux-lvminstallation31

  • All the information on Mount Point, Desired Capacity, Device Type, File System, and so on gets populated. We can modify them if needed.
    linux-lvminstallation32
  • Click on +.
    linux-lvminstallation33
  • Select the / option from the Mount Point dropdown, enter all in the Desired Capacity and then click Add mount.
    linux-lvminstallation29
  • All the information on Mount Point, Desired Capacity, Device Type, File System, and so on gets populated. We can modify them if needed.
    linux-lvminstallation34
  • Click on Done. SUMMARY OF CHANGES pop-up appears. Click on Accept Changes.
    linux-lvminstallation35
  • Click on Begin Installation.
    linux-lvminstallation36
  • After installation, set the ROOT and USER Password.
    linux-lvminstallation37
  • Installation success message gets displayed. Click on Reboot.
    linux-lvminstallation38
  • After reboot, login to the system, run the command to get all the partitions:
    df -h​

linux-lvminstallation39

All the partition information gets displayed.

Add Virtual Disk and Create New LVM Partition

We can add a virtual disk and then have an LVM partition by the following steps:

Step 1 -Create a virtual hard disk and partition it.

  • Click on the Settings tab from the Oracle VirtualBox Manager.
    linux-settings
  • Click on the Storage tab from the Settings window.
    linux-storage
  • Select Controller link. Then click on the Add button.
    linux-storagecontroller
  • Select the Add Hard Disk option.
    linux-harddisk
  • Click on Create new Disk from the pop-up.
    linux-virtualboxpop
  • Select the default value VDI from the Hard disk file type pop-up.
    linux-harddisktype
  • Select the default value Dynamically allocated from the Storage on physical hard disk pop-up.
    linux-virtualboxstorage
  • Give a name to the new virtual hard disk file and select the size of the virtual hard disk.
    linux-lvmfilesize
  • The new virtual disk now gets displayed under the Storage tab.
    linux-lvmfiledisk
  • Click on Start from the VirtualBox Manager.
    linux-virtualboxstart
  • After login, become root with the command:
    su -​
  • To get the information of the disk we created, run the command:
    fdisk -l | more​

The disk we have created, shall be available: linux-lvmdiskinfo

  • To create a partition of the disk we created, run the command:
    fdisk /dev/sdc​
  • Run the command n to add a new partition.
  • Then select the partition type. For primary, type p(default), for extended, type e. Press Enter to select the default value.
  • Select the partition number(1-4, default is 1). Press Enter to select the default value.
  • Select value for the First sector. Press to Enter select the default value.
  • Select the value for the Last sector. Press to Enter select the default value.
  • Run the command p to create the new partition.
  • Run the command t to change the new partition's system id.
  • Run the command L to get the hex code associated with LVM.
    linux-lvmhexcode
  • Enter the hex code of the LVM partition, 8e.
  • Run the command p to create the new partition.
  • Run the command w to write table to disk and exit.

Step 2 -Create a physical volume.

  • To create a physical volume of the partition, run the command:
    pvcreate /dev/sdc1​
  • To verify if the physical volume is created, run the command:
    pvdisplay​

Step 3 -Create a volume group.

  • To create a volume group, run the command:
    vgcreate oracle_vg /dev/sdc1​

Here, the oracle_vg is the name of the volume group.

  • To verify if the volume group created, run the command:
    vgdisplay oracle_vg​

Step 4 -Create logical volumes.

  • To create a logical volume, run the command:
    lvcreate -n oracle_lv --size 1000M oracle_vg

Here, the oracle_lv is the name of the logical volume.

  • To verify if the logical volume is created, run the command:
    lvdisplay

Step 5 -Mount the file systems.

  • To format the disk and assign file systems to it, run the command:
    mkfs.xfs /dev/oracle_vg/oracle_lv
  • To mount a directory named oracle, run the commands:
    mkdir /oracle
    mount /dev/oracle_vg/oracle_lv /oracle​
  • To get the mounting information, run the command:
    df -h​

linux-lvmdiskmount

Extend Disk using LVM Partition

We can extend a disk if it becomes full. Once the disk is full we can do the following:

  • Delete older files to free up disk space.
  • Add a new physical disk and then mount it.
  • Create a new disk and then mount it.
  • Or extend disk through LVM.

Let us discuss how to extend disk through LVM:

Step 1 -Create a virtual hard disk and partition it.

  • Click on the Settings tab from the Oracle VirtualBox Manager.
    linux-settings
  • Click on the Storage tab from the Settings window.
    linux-storage
  • Select Controller link. Then click on the Add button.
    linux-storagecontroller
  • Select the Add Hard Disk option.
    linux-harddisk
  • Click on Create new Disk from the pop-up.
    linux-virtualboxpop
  • Select the default value VDI from the Hard disk file type pop-up.
    linux-harddisktype
  • Select the default value Dynamically allocated from the Storage on physical hard disk pop-up.
    linux-virtualboxstorage
  • Give a name to the new virtual hard disk file and select the size of the virtual hard disk.
    linux-lvmdiskextend
  • The new virtual disk now gets displayed under the Storage tab.
    linux-lvmdiskextend1
  • Click on Start from the VirtualBox Manager.
    linux-virtualboxstart
  • After login, become root with the command:
    su -​
  • To get the information of the disk, run the command:
    df -h

We shall extend the disk as highlighted. Also, we are assuming that Use% has become 100%: linux-lvmdiskextend2

  • To get the information of the disk we created, run the command:
    fdisk -l | more​

linux-lvmdiskextend3

  • To create a partition of the disk we created, run the command:
    fdisk /dev/sdd
  • Run the command n to add a new partition.
  • Then select the partition type. For primary, type p(default), for extended, type e. Press Enter to select the default value.
  • Select the partition number(1-4, default is 1). Press Enter to select the default value.
  • Select value for the First sector. Press Enter to select the default value.
  • Select the value for the Last sector. Press Enter to select the default value.
  • Run the command p to create the new partition.
  • Run the command t to change the new partition's system id.
  • Run the command L to get the hex code associated with LVM.
    linux-lvmhexcode
  • Enter the hex code of the LVM partition as 8e.
  • Run the command p to create the new partition.
  • Run the command w to write table to disk and exit.
  • To get the information of the partition created, run the command:
    fdisk -l | more​

linux-lvmdiskextend5

  • Reboot the system with the command:
    init 6​
  • Become root, with the command:
    su -​
  • Check the partition created, with the command:
    fdisk -l /dev/sdd1​

linux-lvmdiskextend6

  • To get the information of the disk, run the command:
    df -h

We shall extend the disk as highlighted. Also, we are assuming that Use% has become 100%. Note, the size is 997M. linux-lvmdiskextend2

  • To know the group association of oracle_vg , run the command:
    pvdisplay​

linux-lvmdiskextend8

  • To create physical volume, run the command:
    pvcreate /dev/sdd1​
  • To extend the group oracle_vg and associate it to the /dev/sdd1, run the command:
    vgextend oracle_vg /dev/sdd1​
  • To extend the logical volume to 1024MB, run the command:
    lvextend -L+1024M /dev/mapper/oracle_vg-oracle_lv​
  • To extend the file system, run the command:
    xfs_growfs ​/dev/mapper/oracle_vg-oracle_lv​
  • To confirm the size extension, run the command:
    dh -f​
    linux-lvmdiskextend9

Note, the size is now extended to 2.0G.

RAID

RAID stands for Redundant Array of Independent Disks. It is used for creating redundancy. If one of the disks dies, we can have another one for use.

The basic difference between LVM and RAID is that - LVM is configured on a virtual or logical disk while RAID is configured on a physical disk.

Types of RAIDs are:

  • RAID0 - Suppose there are two disks of each space 5G. Together the capacity is increased to 10G. However, if one of them is lost, the data is also lost. It is mostly used for Dataware house or backups.
  • RAID1 - Suppose there are two disks of each space 5G. One disk is mirrored with the other. Together, the capacity remains at 5G. So if we lose one disk, we shall not lose the data and it shall be available on the other disk. However, it is slow in action.
  • RAID5 - It contains three or more disks. The disk1 shall write some data to the disk2. Then disk2 writes some data to the disk3 and so on.
About Author :

Myself Debomita Bhattacharjee, an IT employee with 6+ years of experience in Software industry. My area of interest is Automation testing and Front End Development.

Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions