Updated on 2026-02-06 GMT+08:00

Creating and Mounting a File System

Scenarios

After the logical volume is created, you need to create a file system on the logical volume and mount the file system on the corresponding directory. This section shows how to create an ext4 file system on a logical volume and mount the file system on /Data1.

Procedure

  1. Log in to the ECS as user root.

    For how to log in to an ECS, see How Do I Log In to My ECS?

  2. Create a file system.

    mkfs.<file-system-format> <logical-volume-path>

    Example command:

    mkfs.ext4 /dev/vgdata/lvdata1

    Information similar to the following is displayed:

    [root@ecs-lvmtest ~]# mkfs.ext4 /dev/vgdata/lvdata1
    mke2fs 1.42.9 (28-Dec-2013)
    Filesystem label=
    OS type: Linux
    Block size=4096 (log=2)
    Fragment size=4096 (log=2)
    Stride=0 blocks, Stripe width=0 blocks
    983040 inodes, 3932160 blocks
    196608 blocks (5.00%) reserved for the super user
    First data block=0
    Maximum filesystem blocks=2151677952
    120 block groups
    32768 blocks per group, 32768 fragments per group
    8192 inodes per group
    Superblock backups stored on blocks:
            32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208
    
    Allocating group tables: done
    Writing inode tables: done
    Creating journal (32768 blocks): done
    Writing superblocks and filesystem accounting information: done
    

  3. Create a mount point.

    mkdir <mount-point>

    Example command:

    mkdir /Data1

  4. Mount the file system on the mount point.

    mount <logical-volume-path> <mount-point>

    Example command:

    mount /dev/vgdata/lvdata1 /Data1

  5. Query the file system mount information.

    mount | grep <mount-point>

    Example command:

    mount | grep /Data1

    Information similar to the following is displayed:

    [root@ecs-lvmtest ~]# mount | grep /Data1
    /dev/mapper/vgdata-lvdata1 on /Data1 type ext4 (rw,relatime,data=ordered)

    In the command output, dev/mapper/vgdata-lvdata1 indicates the file system path. Take note of this path, which will be used in 6.

  6. Use the partition UUID to configure file system auto mount at startup.

    UUIDs are the unique character strings for identifying partitions in Linux. Mounts become invalid after a system reboot. You can configure auto mount at startup by adding information of the new partitions into the /etc/fstab file.

    • If the logical volume is created on a partition and that partition is accidentally deleted after auto mount is configured, the server will fail to restart.
    • If the logical volume is created on a partition but auto mount is not configured, residual files may be generated in LVM when that partition is accidentally deleted. You can re-create the partition and clear the residual files.
    1. Query the file system UUID.

      blkid <file-system-path>

      In this example, the UUID of dev/mapper/vgdata-lvdata1 is queried.

      blkid /dev/mapper/vgdata-lvdata1

      Information similar to the following is displayed:

      [root@ecs-lvmtest ~]# blkid /dev/mapper/vgdata-lvdata1
      /dev/mapper/vgdata-lvdata1: UUID="c6a243ce-5150-41ac-8816-39db54d1a4b8" TYPE="ext4"

      In the command output, the UUID is c6a243ce-5150-41ac-8816-39db54d1a4b8.

    2. Open the /etc/fstab file.

      vi /etc/fstab

      Information similar to the following is displayed:
      [root@ecs-lvmtest ~]# vi /etc/fstab
      
      #
      # /etc/fstab
      # Created by anaconda on Tue Nov  7 14:28:26 2017
      #
      # Accessible filesystems, by reference, are maintained under '/dev/disk'
      # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
      #
      UUID=27f9be47-838b-4155-b20b-e4c5e013cdf3 /                       ext4    defaults        1 1
      UUID=2b2000b1-f926-4b6b-ade8-695ee244a901 /boot                   ext4    defaults        1 2
    3. Press i to enter editing mode.
    4. Move the cursor to the end of the file and press Enter. Then add the following information:
      UUID=c6a243ce-5150-41ac-8816-39db54d1a4b8 /Data1                  ext4    defaults        0 0

      The file content is described as follows:

      • Column 1: indicates the UUID. Enter the UUID queried in 1.
      • Column 2: indicates the file system's mounting directory. Enter mounting directory /Data1 created in 3.
      • Column 3: indicates the file system format. Enter file system format ext4 configured in 2.
      • Column 4: indicates the mounting option. In this example, defaults is used.
      • Column 5: indicates the backup option. Enter either 1 (the system automatically backs up the file system) or 0 (does not back up the file system). In this example, 0 is used.
      • Column 6: indicates the scanning option. Enter either 1 (the system automatically scans the file system at system start) or 0 (does not scan the file system). In this example, 0 is used.
    5. Press Esc, enter :wq!, and press Enter.

      The system saves the modifications and exits the vi editor.

  7. Verify that the file system can be auto-mounted at startup.

    1. Unmount the file system.

      umount <logical-volume-path>

      Example command:

      umount /dev/vgdata/lvdata1

    2. Reload all the content in the /etc/fstab file.

      mount -a

    3. Query the file system mount information.

      mount | grep <mount-point>

      Example command:

      mount | grep /Data1

      If information similar to the following is displayed, auto mount has taken effect:

      [root@ecs-lvmtest ~]# mount | grep /Data1
      /dev/mapper/vgdata-lvdata1 on /Data1 type ext4 (rw,relatime,data=ordered)

FAQs

How do I clear residual files generated in LVM after the partition of a logical volume is deleted?

  1. Log in to the server as user root.

    For how to log in to an ECS, see How Do I Log In to My ECS?

    For how to log in to a BMS, see Linux BMS Login Methods.

  2. Create a new partition with the same size as the deleted one. For how to create partitions, see Initializing a Linux Data Disk.
  3. After the partition is created, clear the residual data from the logical volume, volume group, and physical volume respectively.

    1. Clear the residual data from the logical volume.

      lvremove <logical-volume-name>

      Example command:

      lvremove /dev/vgdata/lvdata1

      [root@ecs-lvmtest ~]#  lvremove /dev/vgdata/lvdata1
       Logical volume "lvdatal" successfully removed

      Check whether the residual data is cleared.

      lvdisplay

      If no lvdatal is returned, the cleanup is complete.

    2. Clear the residual data from the volume group.

      vgremove <volume-group-name>

      Example command:

      vgremove vgdata

      [root@ecs-lvmtest ~]#   vgremove vgdata
       Volume group "vgdata" successfully removed

      Check whether the residual data is cleared.

      vgdisplay

      If no vgdata is returned, the cleanup is complete.

    3. Clear the residual data from the physical volume.

      pvremove <disk-device-name>

      Example command:

      pvremove /dev/vdb1

      [root@ecs-lvmtest ~]#   pvremove /dev/vdb1
       Labels on physical volume "/dev/vdb1" successfully wiped.

      Check whether the residual data is cleared.

      pvdisplay

      If no /dev/vdb1 is returned, the cleanup is complete.