Written in front

In the past, the Linux system in the production environment used one disk to store the system and business data. However, the amount of logs generated by business data was too large, and the entire disk space usage rate often reached 100%, causing the system to fail to operate normally. Even the execution of shell commands was abnormal. Therefore, the system and data were later stored separately. For example, the Linux system root environment used the /dev/sda disk, and the business storage environment used the /dev/sdb disk. Even if the /dev/sdb disk space usage rate reached 100%, it would not affect the normal operation of the Linux system. Of course, if there is a monitoring and alarm mechanism, it is okay not to partition, and it can be cleaned up in time, but partitioning can serve as a double insurance.

View current disk usage information

Terminal window
root@localhost:~# df -Th
Filesystem Type Size Used Avail Use% Mounted on
tmpfs tmpfs 794M 1.3M 793M 1% /run
/dev/mapper/ubuntu-root xfs 41G 3.9G 38G 10% /
tmpfs tmpfs 3.9G 0 3.9G 0% /dev/shm
tmpfs tmpfs 5.0M 0 5.0M 0% /run/lock
tmpfs tmpfs 3.9G 0 3.9G 0% /run/qemu
/dev/sda2 xfs 1014M 171M 844M 17% /boot
tmpfs tmpfs 794M 4.0K 794M 1% /run/user/1000

You can see that the root partition disk space mounted by /dev/mapper/ubuntu-root is only tens of GB.

Terminal window
root@localhost:~# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
loop0 7:0 0 63.9M 1 loop /snap/core20/2318
loop1 7:1 0 87M 1 loop /snap/lxd/29351
loop2 7:2 0 38.8M 1 loop /snap/snapd/21759
sda 8:0 0 50G 0 disk
├─sda1 8:1 0 1M 0 part
├─sda2 8:2 0 1G 0 part /boot
├─sda3 8:3 0 8G 0 part [SWAP]
└─sda4 8:4 0 41G 0 part
└─ubuntu-root 253:0 0 41G 0 lvm /
sdb 8:16 0 100G 0 disk #Here it shows that the sdb disk is not mounted
sr0 11:0 1 2G 0 rom
Terminal window
root@localhost:~# fdisk -l
Disk /dev/sdb: 100 GiB, 107374182400 bytes, 209715200 sectors
Disk model: Virtual disk
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes

Use the fdisk -l command to check that /dev/sdb is not partitioned

View lvm information

Terminal window
root@localhost:~# pvdisplay
--- Physical volume ---
PV Name /dev/sda4
VG Name ubuntu
PV Size <41.00 GiB / not usable 0
Allocatable yes (but full)
PE Size 4.00 MiB
Total PE 10495
Free PE 0
Allocated PE 10495
PV UUID 3540f107-f2e4-49a1-8659-163dbfc3f99e
root@localhost:~# vgdisplay
--- Volume group ---
VG Name ubuntu
System ID
Format lvm2
Metadata Areas 1
Metadata Sequence No 2
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 1
Open LV 1
Max PV 0
Cur PV 1
Act PV 1
VG Size <41.00 GiB
PE Size 4.00 MiB
Total PE 10495
Alloc PE / Size 10495 / <41.00 GiB
Free PE / Size 0 / 0
VG UUID b2eadbf1-7c3a-451e-b348-6993c44305dc
root@localhost:~# lvdisplay
--- Logical volume ---
LV Path /dev/ubuntu/root
LV Name root
VG Name ubuntu
LV UUID 58a7626d-2836-4ff4-8ed6-59f8a88e7728
LV Write Access read/write
LV Creation host, time ubuntu-server, 2025-02-18 02:11:35 +0800
LV Status available
# open 1
LV Size <41.00 GiB
Current LE 10495
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:0

Use parted to partition and set up lvm

Terminal window
root@localhost:~# parted /dev/sdb #Specify the disk device name
GNU Parted 3.4
Using /dev/sdb
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) mklabel gpt #Set the disk partition to gpt format
(parted) mkpart primary 1% 100% #Create a new partition as the primary partition,
occupying disk space from 1% to 100% of the starting
position。
(parted) set 1 lvm on #Set the first partition to lvm
(parted) print #Print disk partition information
Model: VMware Virtual disk (scsi)
Disk /dev/sdb: 107GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:
Number Start End Size File system Name Flags
1 1074MB 107GB 106GB primary lvm
(parted) quit #quit
Information: You may need to update /etc/fstab.
root@localhost:~#

Use fdisk -l to view /dev/sdb disk information

Terminal window
root@localhost:~# fdisk -l
Disk /dev/sdb: 100 GiB, 107374182400 bytes, 209715200 sectors
Disk model: Virtual disk
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: gpt
Disk identifier: DDD8AFA8-0671-48FB-B103-F3FE9E165BB2
Device Start End Sectors Size Type
/dev/sdb1 2097152 209713151 207616000 99G Linux LVM

Create lvm logical volume and mount it

Create a PV volume

Terminal window
root@localhost:~# pvcreate /dev/sdb1
Physical volume "/dev/sdb1" successfully created.
root@localhost:~#

Create a VG group

Terminal window
root@localhost:~# vgcreate storage /dev/sdb1
Volume group "storage" successfully created
root@localhost:~#

Create LV logical volume

Terminal window
root@localhost:~# lvcreate -l +100%FREE -n data storage
Logical volume "data" created.
root@localhost:~#

Format LV logical volume

Terminal window
root@localhost:~# mkfs.xfs /dev/storage/data
meta-data=/dev/storage/data isize=512 agcount=4, agsize=6487808 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=1, sparse=1, rmapbt=0
= reflink=1 bigtime=0 inobtcount=0
data = bsize=4096 blocks=25951232, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0, ftype=1
log =internal log bsize=4096 blocks=12671, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
Discarding blocks...Done.

Create a mount directory and mount the LVM logical volume

Terminal window
root@localhost:~# mkdir -pv /data
mkdir: created directory '/data'
root@localhost:~#
root@localhost:~# mount /dev/mapper/storage-data /data

View mounted disks and add auto-startup

View the mounted disk information

Terminal window
root@localhost:~# df -Th
Filesystem Type Size Used Avail Use% Mounted on
tmpfs tmpfs 794M 1.3M 793M 1% /run
/dev/mapper/ubuntu-root xfs 41G 5.0G 37G 13% /
tmpfs tmpfs 3.9G 0 3.9G 0% /dev/shm
tmpfs tmpfs 5.0M 0 5.0M 0% /run/lock
tmpfs tmpfs 3.9G 0 3.9G 0% /run/qemu
/dev/sda2 xfs 1014M 295M 720M 29% /boot
tmpfs tmpfs 794M 4.0K 794M 1% /run/user/1000
/dev/mapper/storage-data xfs 99G 739M 99G 1% /data #This is a newly mounted

Add auto-startup

Terminal window
echo "/dev/mapper/storage-data /data xfs defaults 0 0" >> /etc/fstab