Two new disks need to be mounted under centos7. In order to facilitate subsequent expansion, it was decided to set this as the method of LVM management.
Check what new data disks are currently available, as follows:
[ root@runsdata-test-0004~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/vda1 40G 1.4G 36G 4% /
devtmpfs 7.8G 07.8G 0%/dev
tmpfs 7.8G 07.8G 0%/dev/shm
tmpfs 7.8G 328K 7.8G 1%/run
tmpfs 7.8G 07.8G 0%/sys/fs/cgroup
tmpfs 1.6G 01.6G 0%/run/user/0[root@runsdata-test-0004~]# fdisk -l
Disk /dev/vda:42.9 GB,42949672960 bytes,83886080 sectors
Units = sectors of1*512=512 bytes
Sector size(logical/physical):512 bytes /512 bytes
I/O size(minimum/optimal):512 bytes /512 bytes
Disk label type: dos
Disk identifier:0x0008d73a
Device Boot Start End Blocks Id System
/dev/vda1 *2048838840314194099283 Linux
Disk /dev/vdb:53.7 GB,53687091200 bytes,104857600 sectors
Units = sectors of1*512=512 bytes
Sector size(logical/physical):512 bytes /512 bytes
I/O size(minimum/optimal):512 bytes /512 bytes
Disk /dev/vdc:53.7 GB,53687091200 bytes,104857600 sectors
Units = sectors of1*512=512 bytes
Sector size(logical/physical):512 bytes /512 bytes
I/O size(minimum/optimal):512 bytes /512 bytes
[ root@runsdata-test-0004~]#
As can be seen from the above, there are currently three hard drives.
System disk: 40G
There are also two 50G hard drives that need to be mounted. I use one of the hard drives as an example.
LVM management concept diagram
In this chapter, this topology will be deployed step by step.
Choose one of the 50G data disks for partitioning, as long as the 50G is set to one partition, there is no need to consider setting up multiple partitions.
The configuration steps are as follows:
[ root@runsdata-test-0004~]# fdisk -l | grep dev
Disk /dev/vda:42.9 GB,42949672960 bytes,83886080 sectors
/dev/vda1 *2048838840314194099283 Linux
Disk /dev/vdb:53.7 GB,53687091200 bytes,104857600 sectors
Disk /dev/vdc:53.7 GB,53687091200 bytes,104857600 sectors
[ root@runsdata-test-0004~]#
[ root@runsdata-test-0004~]# fdisk /dev/vdb
Welcome to fdisk(util-linux 2.23.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Device does not contain a recognized partition table
Building a newDOS disklabel with disk identifier 0x051e2cc7.Command(m for help): m
Command action
a toggle a bootable flag
b edit bsd disklabel
c toggle the dos compatibility flag
d delete a partition
g create a newempty GPT partition table
G create an IRIX(SGI) partition table
l list known partition types
m print this menu
n add a newpartition
o create a newempty DOS partition table
p print the partition table
q quit without saving changes
s create a newempty Sun disklabel
t change a partition's system id
u change display/entry units
v verify the partition table
w write table to disk and exit
x extra functionality(experts only)Command(m for help):
Enter the m command to see which commands are available. Among them, you can first look at what partitions the data disk has.
p
command to check which partitions the data disk already hasCommand(m for help): p
Disk /dev/vdb:53.7 GB,53687091200 bytes,104857600 sectors
Units = sectors of1*512=512 bytes
Sector size(logical/physical):512 bytes /512 bytes
I/O size(minimum/optimal):512 bytes /512 bytes
Disk label type: dos
Disk identifier:0x051e2cc7
Device Boot Start End Blocks Id System
Command(m for help):
It can be seen that this data disk does not have any partitions yet, so let me partition it first.
Command(m for help): n
Partition type:
p primary(0 primary,0 extended,4 free)
e extended
Select(default p): p
Partition number(1-4,default1):1
First sector(2048-104857599,default2048):
Using default value 2048
Last sector,+sectors or +size{K,M,G}(2048-104857599,default104857599):
Using default value 104857599
Partition 1of type Linux and of size 50 GiB is setCommand(m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.[root@runsdata-test-0004~]#
At this point, check the data disk again, there should be a 50G partition. Then in the next step, change it to Linux LVM format partition.
Command(m for help): p
Disk /dev/vdb:53.7 GB,53687091200 bytes,104857600 sectors
Units = sectors of1*512=512 bytes
Sector size(logical/physical):512 bytes /512 bytes
I/O size(minimum/optimal):512 bytes /512 bytes
Disk label type: dos
Disk identifier:0x051e2cc7
Device Boot Start End Blocks Id System
/dev/vdb1 20481048575995242777683 Linux
Command(m for help): t
Selected partition 1
Hex code(type L to list all codes): 8e
Changed type of partition 'Linux' to 'Linux LVM'Command(m for help): p
Disk /dev/vdb:53.7 GB,53687091200 bytes,104857600 sectors
Units = sectors of1*512=512 bytes
Sector size(logical/physical):512 bytes /512 bytes
I/O size(minimum/optimal):512 bytes /512 bytes
Disk label type: dos
Disk identifier:0x051e2cc7
Device Boot Start End Blocks Id System
/dev/vdb1 204810485759952427776 8e Linux LVM
Command(m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.[root@runsdata-test-0004~]#
Partition:
[ root@runsdata-test-0004~]# fdisk -l | grep dev
Disk /dev/vda:42.9 GB,42949672960 bytes,83886080 sectors
/dev/vda1 *2048838840314194099283 Linux
Disk /dev/vdb:53.7 GB,53687091200 bytes,104857600 sectors
/dev/vdb1 204810485759952427776 8e Linux LVM
Disk /dev/vdc:53.7 GB,53687091200 bytes,104857600 sectors
[ root@runsdata-test-0004~]#
[ root@runsdata-test-0004~]# fdisk /dev/vdc
Welcome to fdisk(util-linux 2.23.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Device does not contain a recognized partition table
Building a newDOS disklabel with disk identifier 0x420d8007.Command(m for help): p
Disk /dev/vdc:53.7 GB,53687091200 bytes,104857600 sectors
Units = sectors of1*512=512 bytes
Sector size(logical/physical):512 bytes /512 bytes
I/O size(minimum/optimal):512 bytes /512 bytes
Disk label type: dos
Disk identifier:0x420d8007
Device Boot Start End Blocks Id System
Command(m for help): n
Partition type:
p primary(0 primary,0 extended,4 free)
e extended
Select(default p): p
Partition number(1-4,default1):1
First sector(2048-104857599,default2048):
Using default value 2048
Last sector,+sectors or +size{K,M,G}(2048-104857599,default104857599):
Using default value 104857599
Partition 1of type Linux and of size 50 GiB is setCommand(m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.[root@runsdata-test-0004~]#
Set to LVM
[ root@runsdata-test-0004~]# fdisk /dev/vdc
Welcome to fdisk(util-linux 2.23.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.Command(m for help): p
Disk /dev/vdc:53.7 GB,53687091200 bytes,104857600 sectors
Units = sectors of1*512=512 bytes
Sector size(logical/physical):512 bytes /512 bytes
I/O size(minimum/optimal):512 bytes /512 bytes
Disk label type: dos
Disk identifier:0x420d8007
Device Boot Start End Blocks Id System
/dev/vdc1 20481048575995242777683 Linux
Command(m for help): t
Selected partition 1
Hex code(type L to list all codes): 8e
Changed type of partition 'Linux' to 'Linux LVM'Command(m for help): p
Disk /dev/vdc:53.7 GB,53687091200 bytes,104857600 sectors
Units = sectors of1*512=512 bytes
Sector size(logical/physical):512 bytes /512 bytes
I/O size(minimum/optimal):512 bytes /512 bytes
Disk label type: dos
Disk identifier:0x420d8007
Device Boot Start End Blocks Id System
/dev/vdc1 204810485759952427776 8e Linux LVM
Command(m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.[root@runsdata-test-0004~]#
yum install lvm2 -y
Alibaba Cloud server does not install pv related commands by default.
[ root@runsdata-test-0004~]# pvcreate
- bash: pvcreate: command not found
[ root@runsdata-test-0004~]#
The tools that need to install lvm2 are as follows:
[ root@server81 ~]# rpm -qa | grep lvm
lvm2-2.02.171-8.el7.x86_64
lvm2-libs-2.02.171-8.el7.x86_64
[ root@server81 ~]#
The installation is as follows:
[ root@runsdata-test-0004~]# yum install lvm2 -y
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
Resolving Dependencies
- - > Running transaction check
- - - > Package lvm2.x86_64 7:2.02.180-10.el7_6.2 will be installed
.... Omit....
Installed:
lvm2.x86_64 7:2.02.180-10.el7_6.2
Dependency Installed:
device-mapper-event.x86_64 7:1.02.149-10.el7_6.2
device-mapper-event-libs.x86_64 7:1.02.149-10.el7_6.2
device-mapper-persistent-data.x86_64 0:0.7.3-3.el7
libaio.x86_64 0:0.3.109-13.el7
lvm2-libs.x86_64 7:2.02.180-10.el7_6.2
Dependency Updated:
device-mapper.x86_64 7:1.02.149-10.el7_6.2 device-mapper-libs.x86_64 7:1.02.149-10.el7_6.2
Complete![root@runsdata-test-0004~]#
[ root@runsdata-test-0004~]# rpm -qa | grep lvm
lvm2-libs-2.02.180-10.el7_6.2.x86_64
lvm2-2.02.180-10.el7_6.2.x86_64
[ root@runsdata-test-0004~]#
[ root@runsdata-test-0004~]# pv
pv pvck pvdisplay pvremove pvs
pvchange pvcreate pvmove pvresize pvscan
[ root@runsdata-test-0004~]# pv
It can be seen from the above that the tools needed for lvm have been installed. So let's continue the configuration.
[ root@runsdata-test-0004~]# fdisk -l | grep dev
Disk /dev/vda:42.9 GB,42949672960 bytes,83886080 sectors
/dev/vda1 *2048838840314194099283 Linux
Disk /dev/vdb:53.7 GB,53687091200 bytes,104857600 sectors
/dev/vdb1 204810485759952427776 8e Linux LVM
Disk /dev/vdc:53.7 GB,53687091200 bytes,104857600 sectors
[ root@runsdata-test-0004~]#
[ root@runsdata-test-0004~]# pvcreate /dev/vdb
vdb vdb1
[ root@runsdata-test-0004~]# pvcreate /dev/vdb1
Physical volume "/dev/vdb1" successfully created.[root@runsdata-test-0004~]#
[ root@runsdata-test-0004~]#
[ root@runsdata-test-0004~]# pvs
PV VG Fmt Attr PSize PFree
/dev/vdb1 lvm2 ---<50.00g <50.00g
[ root@runsdata-test-0004~]#
[ root@runsdata-test-0004~]# pvdisplay
" /dev/vdb1" is a newphysical volume of"<50.00 GiB"--- NEW Physical volume ---
PV Name /dev/vdb1
VG Name
PV Size <50.00 GiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID oMzorH-1mfB-0VEy-eZDB-HHU3-UM9C-0EoPTU
[ root@runsdata-test-0004~]#
PE (physical extent)
: The physical area is the smallest storage unit that can be used for allocation in a physical volume. The size of the physical area is specified when the volume group is created. Once it is determined that it cannot be changed, the physical area size of all physical volumes in the same volume group must be the same. After adding the pv to vg, the size of pe is automatically changed to the pe size defined in vg. LE (logical extent)
: The logical area is the smallest storage unit available for allocation in a logical volume. The size of the logical area depends on the size of the physical area in the volume group where the logical volume is located.[ root@runsdata-test-0004~]# fdisk -l | grep dev
Disk /dev/vda:42.9 GB,42949672960 bytes,83886080 sectors
/dev/vda1 *2048838840314194099283 Linux
Disk /dev/vdb:53.7 GB,53687091200 bytes,104857600 sectors
/dev/vdb1 204810485759952427776 8e Linux LVM
Disk /dev/vdc:53.7 GB,53687091200 bytes,104857600 sectors
/dev/vdc1 204810485759952427776 8e Linux LVM
[ root@runsdata-test-0004~]#
[ root@runsdata-test-0004~]# pvcreate /dev/vdc1
Physical volume "/dev/vdc1" successfully created.[root@runsdata-test-0004~]#
[ root@runsdata-test-0004~]# pvs
PV VG Fmt Attr PSize PFree
/dev/vdb1 vgdocker lvm2 a--<50.00g <50.00g
/dev/vdc1 lvm2 ---<50.00g <50.00g
[ root@runsdata-test-0004~]#
[ root@runsdata-test-0004~]# pvdisplay
- - - Physical volume ---
PV Name /dev/vdb1
VG Name vgdocker
PV Size <50.00 GiB / not usable 3.00 MiB
Allocatable yes
PE Size 4.00 MiB
Total PE 12799
Free PE 12799
Allocated PE 0
PV UUID oMzorH-1mfB-0VEy-eZDB-HHU3-UM9C-0EoPTU
" /dev/vdc1" is a newphysical volume of"<50.00 GiB"--- NEW Physical volume ---
PV Name /dev/vdc1
VG Name
PV Size <50.00 GiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID nzkwWq-h4KK-2g64-aAqT-3Ugz-qvrj-bq4GR3
[ root@runsdata-test-0004~]#
[ root@runsdata-test-0004~]# vg
vgcfgbackup vgconvert vgextend vgmknodes vgs
vgcfgrestore vgcreate vgimport vgreduce vgscan
vgchange vgdisplay vgimportclone vgremove vgsplit
vgck vgexport vgmerge vgrename
[ root@runsdata-test-0004~]# vg
VG
, the name can start at willThis volume group is specifically for docker and data storage data, so I named it vgdata
.
Next, create a volume group of vgdata
, and then use vgs
and vgdisplay
to view the information after creation.
[ root@runsdata-test-0004~]# vgcreate vgdocker /dev/vdb1 ##Create a test
Volume group "vgdocker" successfully created
[ root@runsdata-test-0004~]#
[ root@runsdata-test-0004~]# vgremove vgdocker ##Delete previously created
Volume group "vgdocker" successfully removed
## Create the previous two PVs into the logical group of vgdata.
[ root@runsdata-test-0004~]# vgcreate vgdata /dev/vdc1 /dev/vdb1
Volume group "vgdata" successfully created
[ root@runsdata-test-0004~]#
## View vg information
[ root@runsdata-test-0004~]# vgs
VG #PV #LV #SN Attr VSize VFree
vgdata 200 wz--n-99.99g 99.99g
[ root@runsdata-test-0004~]#
## View detailed information of vg
[ root@runsdata-test-0004~]# vgdisplay
- - - Volume group ---
VG Name vgdata
System ID
Format lvm2
Metadata Areas 2
Metadata Sequence No 1
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 0
Open LV 0
Max PV 0
Cur PV 2
Act PV 2
VG Size 99.99 GiB
PE Size 4.00 MiB
Total PE 25598
Alloc PE / Size 0/0
Free PE / Size 25598/99.99 GiB
VG UUID 52PHwY-0a16-1yPo-EYNE-YUv0-4LJx-CWZsZS
[ root@runsdata-test-0004~]#
After the volume group is created, the logical volume will be created next.
LV
First look at the relevant command parameters of lvcreate
[ root@runsdata-test-0004~]# lvcreate --help
lvcreate - Create a logical volume
Now I plan to use about 50G for the logical volume used by docker, and about 50G for the data directory. Then you need to create two 50G logical volumes.
## Create an lv with a size of 50G and the name docker,-L specifies the size,-n specifies the name
[ root@runsdata-test-0004~]# lvcreate -L 50G -n docker vgdata
Logical volume "docker" created.[root@runsdata-test-0004~]#
## Create a 50G data logical volume and report an error, indicating that the space is insufficient, because the logical group only has<99G size
[ root@runsdata-test-0004~]# lvcreate -L 50G -n data vgdata
Volume group "vgdata" has insufficient free space(12798 extents):12800 required.[root@runsdata-test-0004~]#
## Then create a 49G data logical volume LV
[ root@runsdata-test-0004~]# lvcreate -L 49G -n data vgdata
Logical volume "data" created.[root@runsdata-test-0004~]#
[ root@runsdata-test-0004~]# lvdisplay
- - - Logical volume ---
LV Path /dev/vgdata/docker
LV Name docker
VG Name vgdata
LV UUID XWcgud-iAYP-j2Mo-x8kq-SjQ2-edP2-Usa4RZ
LV Write Access read/write
LV Creation host, time runsdata-test-0004,2018-12-1013:57:01+0800
LV Status available
# open 0
LV Size 50.00 GiB
Current LE 12800
Segments 2
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 252:0--- Logical volume ---
LV Path /dev/vgdata/data
LV Name data
VG Name vgdata
LV UUID HVfQpR-Zs81-RFiZ-rAFC-lBMk-bMhe-MTB1zl
LV Write Access read/write
LV Creation host, time runsdata-test-0004,2018-12-1013:58:36+0800
LV Status available
# open 0
LV Size 49.00 GiB
Current LE 12544
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 252:1[root@runsdata-test-0004~]#
Then there are already two logical volumes of docker and data at this time. The following is to format and mount the directory.
lvdisplay
to view the logical volume information and obtain the path of the logical volume LVmkfs.ext4 /dev/vgdata/docker
## View details of lv
[ root@runsdata-test-0004~]# lvdisplay
- - - Logical volume ---
LV Path /dev/vgdata/docker
LV Name docker
VG Name vgdata
LV UUID XWcgud-iAYP-j2Mo-x8kq-SjQ2-edP2-Usa4RZ
LV Write Access read/write
LV Creation host, time runsdata-test-0004,2018-12-1013:57:01+0800
LV Status available
# open 0
LV Size 50.00 GiB
Current LE 12800
Segments 2
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 252:0--- Logical volume ---
LV Path /dev/vgdata/data
LV Name data
VG Name vgdata
LV UUID HVfQpR-Zs81-RFiZ-rAFC-lBMk-bMhe-MTB1zl
LV Write Access read/write
LV Creation host, time runsdata-test-0004,2018-12-1013:58:36+0800
LV Status available
# open 0
LV Size 49.00 GiB
Current LE 12544
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 252:1
## Format the logical volume with ext4
[ root@runsdata-test-0004~]# mkfs.ext4 /dev/vgdata/docker
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
3276800 inodes,13107200 blocks
655360 blocks(5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=2162163712400 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,4096000,7962624,11239424
Allocating group tables: done
Writing inode tables: done
Creating journal(32768 blocks): done
Writing superblocks and filesystem accounting information: done
## Format the logical volume with ext4
[ root@runsdata-test-0004~]# mkfs.ext4 /dev/vgdata/data
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
3211264 inodes,12845056 blocks
642252 blocks(5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=2162163712392 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,4096000,7962624,11239424
Allocating group tables: done
Writing inode tables: done
Creating journal(32768 blocks): done
Writing superblocks and filesystem accounting information: done
[ root@runsdata-test-0004~]#
[ root@runsdata-test-0004~]# cd /[root@runsdata-test-0004/]# ls
bin dev home lib64 media opt root sbin sys usr
boot etc lib lost+found mnt proc run srv tmp var[root@runsdata-test-0004/]# mkdir docker
[ root@runsdata-test-0004/]#
[ root@runsdata-test-0004/]# mount /dev/vgdata/docker docker
[ root@runsdata-test-0004/]#
[ root@runsdata-test-0004/]# mkdir data
[ root@runsdata-test-0004/]#
[ root@runsdata-test-0004/]# mount /dev/vgdata/data data
[ root@runsdata-test-0004/]#
[ root@runsdata-test-0004/]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/vda1 40G 1.5G 36G 4% /
devtmpfs 7.8G 07.8G 0%/dev
tmpfs 7.8G 07.8G 0%/dev/shm
tmpfs 7.8G 352K 7.8G 1%/run
tmpfs 7.8G 07.8G 0%/sys/fs/cgroup
tmpfs 1.6G 01.6G 0%/run/user/0/dev/mapper/vgdata-docker 50G 53M 47G 1%/docker
/dev/mapper/vgdata-data 49G 53M 46G 1%/data
[ root@runsdata-test-0004/]#
You can see that the /docker and /data directories are already in use.
Test write files to see.
[ root@runsdata-test-0004/]# cd /docker/[root@runsdata-test-0004 docker]# pwd
/docker
[ root@runsdata-test-0004 docker]# ls
lost+found
[ root@runsdata-test-0004 docker]#
[ root@runsdata-test-0004 docker]# echo 123>1.txt
[ root@runsdata-test-0004 docker]# ls
1. txt lost+found
[ root@runsdata-test-0004 docker]# cat 1.txt
123[ root@runsdata-test-0004 docker]#
Okay, it's basically ok to write here, but if the server restarts, the /docker and /data directories cannot be automatically mounted.
Let's set up the automatic mounting of the disk at boot.
The system needs to be automatically mounted at boot, and the mount information needs to be written into the /etc/fstab
file.
[ root@runsdata-test-0004 docker]# vim /etc/fstab
#
# /etc/fstab
# Created by anaconda on Sun Oct 1515:19:002017
#
# 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=eb448ab..Omit...6a31 / ext4 defaults 11/dev/mapper/vgdata-docker /docker ext4 defaults 11/dev/mapper/vgdata-data /data ext4 defaults 11
Then perform a restart reboot
test.
reboot test
The advantage of LVM management is that it is flexible to expand and manage disks, and you can write a chapter on expanding disks in the future.