Note:

Work with logical volumes on Oracle Linux 8

Introduction

In this tutorial you work with the Oracle Linux Volume Manager to create Logical Volume Manager (LVM) partitions and logical volumes. You learn to mount logical volumes, and increase the capacity of a logical volume.

Background

The Linux device mapper (DM) supplies an abstraction layer on top of the actual storage block devices and provides the foundation for the Logical Volume Manager version 2 (LVM2). LVM2 manages multiple physical volumes and also supports mirroring and striping of logical volumes to provide redundancy and increase performance. To assist in understanding LVM, the following terms are defined:

With LVM, allocating storage space becomes more flexible. You can increase the size of a file system by allocating free space in a volume group to a logical volume, or adding new physical volumes to volume groups, and then resizing the logical volume. You can perform these tasks without needing to unmount any partitions.

Objectives

What Do You Need?

A client system with Oracle Linux 8 installed with at least three disk devices.

(Hands-on Lab) Connect to the Compute Instance

Note: This step is specific to the Oracle provided free lab environment.

The Desktop environment will display before the instances are ready. Deployment of this environment can take two to five minutes, depending on the number of resources and provisioning steps needed.

First, to access the lab compute instances, connect to the Oracle Cloud Console and copy the compute instance public IP address.

  1. Sign in to Oracle Cloud Console, and select your Compartment.

  2. Click Instances.

  3. Copy the public IP address to a temporary location (such as a text file) on your computer.

    copy public IP

    Note:

    • To copy, highlight the IP address with the mouse and press Ctrl+C.
    • Keep the Oracle Cloud Console open as you access it in Prepare Block Volumes.
  4. Right-click the virtual desktop and select Open Terminal Here.

  5. Connect to the instance.

    ssh oracle@<IP_ADDRESS_OF_COMPUTE_INSTANCE>
    

    Where <IP_ADDRESS_OF_COMPUTE_INSTANCE> is the IP address copied from the Oracle Cloud Console.

  6. Accept the ECDSA key fingerprint by typing yes at the prompt.

  7. You are now connected to the compute instance for this lab.

If the connection fails with the Permission denied (publickey,gssapi-keyex,gssapi-with-mic) message, wait a bit longer for the provisioning process to complete and try making the ssh connection again.

Task 1: Prepare Block Volumes

In this task, you run iscsiadm commands to prepare three block volumes for use in this tutorial.

Note: The iscsiadm utility is a command-line tool allowing discovery and login to iSCSI targets.

  1. Use the sudo command to switch user to the root user.

    sudo -i
    
  2. Open the Oracle Cloud Console.

  3. If necessary, click Compute and then Instances.

  4. Click the instance name link in the table to display its details page. The following image shows the instance name is ol8-server.

    copy public IP

  5. Scroll down the page and select Attached Block Volumes under Resources to display the attached block volumes. The following image lists the block volumes attached to the instance.

    copy attached

  6. For each block volume, click the three dot menu (on the right side) and select iSCSI Commands & Information from the list. The following image shows the iSCSI Commands & Information window and the three iscsiadm commands to copy:

    copy iscsiadm

    1. Copy and paste the three commands for connecting the block volume into a terminal window.

    2. Press Enter after the third (last) iscsiadm command.

    3. Repeat step 5 for the other block volumes in the list.

  7. Use the lsblk command to verify the block volumes are available on the system.

    lsblk
    

    Note: The command output shows four block devices: sda, sdb, sdc, and sdd.

Task 2: Create Linux LVM Partitions

In this task, you create new partitions and change the system ID to Linux LVM on /dev/sdc and /dev/sdd.

  1. Use the fdisk command to create two partitions on /dev/sdc.

    fdisk /dev/sdc
    

    The interactive program opens with a short statement on its use, information about the device selected, and a command prompt.

    1. Enter n to add a new primary partition.

    2. Enter p, followed by the number 1 for the first primary partition.

    3. Press Enter to accept the default (2048) as the start of the first sector.

    4. Enter +1G to set the last sector using the size notation.

      Note: fdisk returns a message telling you that the new partition is of the type ‘Linux’ with a size of 1 GiB.

    5. Enter n, to create a second primary partition.

    6. Enter p, followed by the number 2 for the second primary partition.

    7. Press Enter to accept the default as the start of the second sector.

    8. Enter +1G to set the last sector using the size notation.

      Note: fdisk returns a message telling you of the new partition of the type ‘Linux’ with a size of 1 GiB.

    9. Enter t to change the partition type on partition 2.

    10. Press Enter to accept the default of partition 2.

    11. Enter 8e as the Hex code for the Linux LVM partition type.

      Note: Pressing the letter L displays the Hex codes for the partition types.

    12. Enter p to print the new partition table.

      Note: The output lists two partitions, /dev/sdc1 and /dev/sdc2, with sdc2 being an Linux LVM partition.

    13. Enter w to save the new partition table.

  2. Use the fdisk command to create two partitions on /dev/sdd.

    fdisk /dev/sdd
    
    1. Enter n to add a new primary partition.

    2. Enter p, followed by the number 1 for the first primary partition.

    3. Press Enter to accept the default (2048) as the start of the first sector.

    4. Enter +1G to set the last sector using the size notation.

      Note: fdisk returns a message telling you that the new partition is of the type ‘Linux filesystem’ with a size of 1 GiB.

    5. Enter nfor the second new partition.

    6. Enter p, followed by the number 2 for the second primary partition.

    7. Press Enter to accept the default as the start of the second sector.

    8. Enter +1G to set the last sector using the size notation.

      Note: fdisk returns a message telling you of the new partition of the type ‘Linux’ with a size of 1 GiB.

    9. Enter t to change the partition type on partition 2.

    10. Press Enter to accept the default of partition 2.

    11. Enter 8e as the Hex code for the Linux LVM partition type.

      Note: Pressing the letter L displays the Hex codes for all partition types.

    12. Enter p to print the new partition table.

      Note: The output lists two partitions, /dev/sdd1 and /dev/sdd2, with sdd2 being an Linux LVM partition.

    13. Enter w to save the new partition table.

Task 3: Create Logical Volume

In this task, you create physical volumes, a volume group, and a logical volume. You also use LVM utilities to display information about the LVM entities.

The first step in implementing LVM is to create physical volumes.

  1. Use the fdisk command and pipe the output to grep to list the ‘Linux LVM’ partitions. The command uses the lowercase letter l and not the numeral one.

    fdisk -l | grep 8e
    
  2. Use the pvcreate command to create physical volumes on sdc2 and sdd2. You can initialize multiple disks or partitions in the same command.

    pvcreate -v /dev/sdc2 /dev/sdd2
    

    Note: Review the command output and notice the actions taken to set up the physical volume.

    1. Use the pvdisplay command to display attributes of the new physical volume.

      pvdisplay /dev/sdc2 /dev/sdd2
      
    2. Use the pvs command to report information in a more condensed form.

      pvs /dev/sdc2 /dev/sdd2
      

      Note: The physical volumes are not part of a volume group.

    3. Use the pvscan command to scan all disks for all physical volumes on the system.

      pvscan
      
  3. The next step in implementing LVM is to assign the physical volumes to an existing or new volume group.

    1. Use the vgcreate command to create a volume group named myvolg from the /dev/sdc2 physical volume.

      vgcreate -v myvolg /dev/sdc2
      

      Note: Review the command output and notice the actions taken to create a volume group.

    2. Use the vgdisplay command to display attributes of the volume group, and its associated physical volumes and logical volumes.

      vgdisplay myvolg
      

      Note: The volume group contains one physical volume and zero logical volumes.

    3. Use the vgs command to report information in a more condensed form.

      vgs myvolg
      
    4. Use the vgscan command to scan all supported LVM devices in the system for volume groups.

      vgscan
      

      Note: The output confirms that myvolg uses a LVM device.

    5. Use the pvs command to display information about the physical volumes.

      pvs /dev/sdc2
      

      Note: The format of /dev/sdc2 is lvm2 and assigned to the myvolg volume group, with 1020 MB of free space.

  4. The next step in implementing LVM is to create a logical volume from the space allocated to the volume group.

    1. Use the lvcreate command to create a 500 MB logical volume named myvol from the myvolg volume group.

      lvcreate -v -L 500m -n myvol myvolg
      

      Note: Review the command output for the actions taken to create a logical volume.

    2. Use the lvdisplay command to display attributes of the logical volume. Some of the attributes to note are its directory pach, its size, and its status.

      lvdisplay
      
    3. Use the lvs command to report information in a more condensed form.

      lvs
      
    4. Use the lvscan command to scan all disks for logical volumes.

      lvscan
      
    5. Use the pvs command to display information about the physical volumes.

      pvs
      

      Note: The free space in the /dev/sdc2 physical volume has been reduced to 520 MB.

    6. Use the vgs command to display information about the volume group.

      vgs
      

      Note: The free space in the myvolg volume group has also been reduced.

Task 4: Create a File System

The next two sections represent the remaining tasks in implementing LVM. You first create a file system on the logical volume.

  1. List the /dev entries for the myvol logical volume. The lvcreate command created two entries in the /dev directory for the logical volume.

    ls -l /dev/myvolg/myvol
    ls -l /dev/mapper/myvolg-myvol
    

    Note: The two entries created automatically are both symbolic links to /dev/dm-2.

  2. List the /dev/dm-2 entry.

    ls -l /dev/dm-2
    

    Note: /dev/dm-2 is a block device.

  3. Use the mkfs.ext4 command to create an ext4 file system on the myvol logical volume.

    mkfs.ext4 /dev/mapper/myvolg-myvol
    

    Note: Alternatively, you could use /dev/myvolg/myvol when making a file system.

Task 5: Mount the File System

In this task, you create a mount point, and attach the logical volume to the directory hierarchy. Next, you update the /etc/fstab file.

  1. Use the mkdir command to create a /myvol mount point.

    mkdir /myvol
    
  2. Use the mount command to mount the file system, and then use the df command to display the mounted file systems.

    mount /dev/mapper/myvolg-myvol /myvol
    df -h
    
  3. Use the command vi /etc/fstab and add the following entry:

    /dev/mapper/myvolg-myvol /myvol ext4  defaults,_netdev  0 0
    

    Note: When you add an iSCSI remote block volume, you must include the _netdev mount option or your instance will become unavailable after the next reboot.

  4. Use the umount command to unmount the file system.

    umount /myvol
    
  5. Use the mount –a command to mount all file system in /etc/fstab file.

    mount -a
    
  6. Use the command df -h to verify your new file system is mounted.

    df -h
    

    Note: The file system entries in fstab will automatically mount on system reboots.

Task 6: Increase the Size of a Logical Volume

In this practice, you increase the size of a logical volume.

  1. Use the vgs command to list the volume groups.

    vgs
    

    Note: The myvolg volume group has 520 MB of free space.

  2. Use the lvs command to list the logical volume.

    lvs
    

    Note: The myvol logical volume is 500 MB in size.

  3. Use the df command to display the /myvol file system.

    df -h /myvol
    

    Note: The size of the file system is 477 MB.

  4. Use the lventend command to increase the size of the myvolg/myvol logical volume and the file system by 500 MB.

    lvextend -L +500m -r myvolg/myvol
    

    Note: The -r option resizes the file system.

  5. Use the vgs command to list the volume groups and add the following entry:

    vgs
    

    Note: The myvolg volume group now has only 20 MB of free space.

  6. Use the lvs command to list the logical volumes.

    lvs
    

    Note: The myvol logical volume is now 1000 MB.

  7. Use the df command to display the /myvol file system.

    df -h /myvol
    

    Note: The size of the file system is now 961 MB.

  8. Use the command ls –l to list the contents of the /etc/lvm/backup and /etc/lvm/archive directories.

    ls -l /etc/lvm/backup
    ls -l /etc/lvm/archive
    

    Note: A new myvolg backup file was automatically created when the logical volume was extended (notice the time stamp). Also note that a new archive file was automatically created (notice the time stamp on the myvolg_0000n file). For more information on these files refer to the man pages for vgcfgbackup and vgcfgrestore.

Task 7: Add a Physical Volume to a Volume Group

In this task, you add a physical volume to a volume group.

  1. Use the pvs command to list the available physical volumes.

    pvs
    

    Note The /dev/sdd2 physical volume is not assigned to a volume group.

  2. Use the vgs command to list the volume groups.

    vgs myvolg
    

    Note: The myvolg volume group is 1020 MB in size.

  3. Use the vgextend command to add the /dev/sdd2 physical volume to the myvolg volume group.

    vgextend -v myvolg /dev/sdd2
    
  4. List the physical volume.

    pvs /dev/sdd2
    

    Note: The /dev/sdd2 physical volume is now assigned to the myvolg volume group.

  5. List the volume group.

    vgs myvolg
    

    Note: The myvolg volume group now has two physical volumes (PVs). Also note that VSize and VFree have also increased.

  6. Use the command ls –l to list the contents of the /etc/lvm/backup and /etc/lvm/archive directories.

    ls -l /etc/lvm/backup
    ls -l /etc/lvm/archive
    

    Note: A new myvolg backup file was automatically created when the physical volume was added to the volume group (notice the time stamp). Also note that a new archive file was automatically created (notice the time stamp on the myvolg_0000n+1 file). For more information on these files, refer to the man pages for vgcfgbackup and vgcfgrestore.

Task 8: Resize the Logical Volume

In this task, you increase the size of the logical volume and the file system by another 500 MB.

  1. Use the lvextend command to increase the size of the myvolg/myvol logical volume and the file system.

    lvextend -L +500m -r myvolg/myvol
    
  2. List the logical volume.

    lvs
    

    Note: The myvol logical volume is now 1.46 GB.

  3. Display the /myvol file system.

    df -h /myvol
    

Task 9: Complete an Optional Lab

If time allows, there is third disk device on which you can practice any of the above activities.

  1. Use the fdisk command to list the available device.

    fdisk -l /dev/sdb
    

    Note: /dev/sdb is a 50 GiB disk without any partitions.

  2. Experiment and have fun.

More Learning Resources

Explore other labs on docs.oracle.com/learn or access more free learning content on the Oracle Learning YouTube channel. Additionally, visit education.oracle.com/learning-explorer to become an Oracle Learning Explorer.

For product documentation, visit Oracle Help Center.