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
root@localhost:~# df -ThFilesystem Type Size Used Avail Use% Mounted ontmpfs 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/shmtmpfs tmpfs 5.0M 0 5.0M 0% /run/locktmpfs tmpfs 3.9G 0 3.9G 0% /run/qemu/dev/sda2 xfs 1014M 171M 844M 17% /boottmpfs tmpfs 794M 4.0K 794M 1% /run/user/1000You can see that the root partition disk space mounted by /dev/mapper/ubuntu-root is only tens of GB.
root@localhost:~# lsblkNAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTSloop0 7:0 0 63.9M 1 loop /snap/core20/2318loop1 7:1 0 87M 1 loop /snap/lxd/29351loop2 7:2 0 38.8M 1 loop /snap/snapd/21759sda 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 mountedsr0 11:0 1 2G 0 romroot@localhost:~# fdisk -lDisk /dev/sdb: 100 GiB, 107374182400 bytes, 209715200 sectorsDisk model: Virtual diskUnits: sectors of 1 * 512 = 512 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesUse the fdisk -l command to check that /dev/sdb is not partitioned
View lvm information
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:0Use parted to partition and set up lvm
root@localhost:~# parted /dev/sdb #Specify the disk device nameGNU Parted 3.4Using /dev/sdbWelcome 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 informationModel: VMware Virtual disk (scsi)Disk /dev/sdb: 107GBSector size (logical/physical): 512B/512BPartition Table: gptDisk Flags:
Number Start End Size File system Name Flags 1 1074MB 107GB 106GB primary lvm
(parted) quit #quitInformation: You may need to update /etc/fstab.
root@localhost:~#Use fdisk -l to view /dev/sdb disk information
root@localhost:~# fdisk -lDisk /dev/sdb: 100 GiB, 107374182400 bytes, 209715200 sectorsDisk model: Virtual diskUnits: sectors of 1 * 512 = 512 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisklabel type: gptDisk identifier: DDD8AFA8-0671-48FB-B103-F3FE9E165BB2
Device Start End Sectors Size Type/dev/sdb1 2097152 209713151 207616000 99G Linux LVMCreate lvm logical volume and mount it
Create a PV volume
root@localhost:~# pvcreate /dev/sdb1 Physical volume "/dev/sdb1" successfully created.root@localhost:~#Create a VG group
root@localhost:~# vgcreate storage /dev/sdb1 Volume group "storage" successfully createdroot@localhost:~#Create LV logical volume
root@localhost:~# lvcreate -l +100%FREE -n data storage Logical volume "data" created.root@localhost:~#Format LV logical volume
root@localhost:~# mkfs.xfs /dev/storage/datameta-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=0data = bsize=4096 blocks=25951232, imaxpct=25 = sunit=0 swidth=0 blksnaming =version 2 bsize=4096 ascii-ci=0, ftype=1log =internal log bsize=4096 blocks=12671, version=2 = sectsz=512 sunit=0 blks, lazy-count=1realtime =none extsz=4096 blocks=0, rtextents=0Discarding blocks...Done.Create a mount directory and mount the LVM logical volume
root@localhost:~# mkdir -pv /datamkdir: created directory '/data'root@localhost:~#root@localhost:~# mount /dev/mapper/storage-data /dataView mounted disks and add auto-startup
View the mounted disk information
root@localhost:~# df -ThFilesystem Type Size Used Avail Use% Mounted ontmpfs 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/shmtmpfs tmpfs 5.0M 0 5.0M 0% /run/locktmpfs tmpfs 3.9G 0 3.9G 0% /run/qemu/dev/sda2 xfs 1014M 295M 720M 29% /boottmpfs tmpfs 794M 4.0K 794M 1% /run/user/1000/dev/mapper/storage-data xfs 99G 739M 99G 1% /data #This is a newly mountedAdd auto-startup
echo "/dev/mapper/storage-data /data xfs defaults 0 0" >> /etc/fstab