LVM ---- transfer space from / home installed by default in CentOS7 to the root directory/
1, Basic concepts
By default, LVM2 (Logical Volume Manager) is enabled in Cent0S 7. A hard disk of the machine is divided into two areas sda1 and sda2, in which sda1 is mounted as a system disk / boot, with a small amount of space; Sda2 acts as a physical volume and completely as a Logical Volume group VG(Volume Group) centos. Three logical volumes LV (Logical Volume) are established in this Logical Volume group centos Root, home and swap are mounted to the root directory / and / home and swap respectively. The two partitions sda1 and sda2 have established the file system XFS. As the default file system of red hat, the file system XFS also has its consideration and has become the mainstream file system after ext3 and ext4.
Relationship between several concepts: M physical hard disks HD or partitions in physical hard disks are formed together into a logical volume group VG and storage pool, N logical volumes LV are created in the volume group VG, and a file system, such as xfs, is created in a logical volume Lv. Physical hard disks / partitions and logical volumes have the smallest basic addressing unit, and the default size of CentOS7 is 4MB, which corresponds to one-to-one, similar to links or storage pools Variable reference, but the relationship between the two is not always the same, because the physical hard disk may change and the logical volume will be adjusted automatically. Creating a volume group and a logical volume will write the volume information VGDA (Volume Group Descriptor Area) at the beginning of the disk similar to creating a partition It is used for identification. The advantage of logical volume is that it shields the physical underlying support and can be expanded and changed freely without worrying about the physical space limitations of hard disk or partition, and there will be no problems such as backing up / expanding partition and reformatting hard disk in order to expand partition size.
HD / partition -- through pvcreate - > PV -- through vgcreate (vgchange) / vgextend -- > VG -- through lvcreate / lvextend -- > LV -- through mkfs -- > FS -- through xfs_growfs and other -- > DF disks
However, the key point is that / home takes up too much space during the default installation of CentOS 7, and the root directory is much smaller (only 50G), while the OpenStack installation and storage are in the root directory. Uploading several images may exhaust your root directory space. Unlike other file systems ext3, ext4 or ReiserFS, there are commands (resize 2fs, resize_reiserfs) It directly supports reducing the size of the file system. The default installed xfs supports expansion but does not support space reduction!
2, Step overview
Therefore, the following steps are roughly as follows: (log in to the system directly as root user, local machine or ssh root. If you use the current ordinary user, you will encounter some unnecessary trouble)
1. Back up the / home / user file. If there is nothing, ignore this step (why do you want this / home? Delete it and use root directly? Or keep it. It makes sense that it exists. Besides, don't use root only in the production environment)
2. umount /home unmount and lvremove delete the home logical volume to free its space. vgdisplay checks the available space of the volume group
3. lvcreate creates a new home volume, and mkfs establishes an xfs file system on it (allocate and mount to / home - restart without changing / etc/fstab) to copy back the contents before
(at this time, you can allocate the free space freely. You can create other logical volumes, or use them directly after being idle. You can also go straight to the topic and take the fourth step below.)
4. Release the previous home logical volume and allocate the remaining space after the new volume home, allocate lvextend to the root volume, and use the command xfs_growfs to expand its file system space
3, Related commands
Look at the general commands
[root@sunli ~]# pv pvchange pvck pvcreate pvdisplay pvmove pvremove pvresize pvs pvscan [root@sunli ~]# vg vgcfgbackup vgck vgdisplay vgimport vgmknodes vgrename vgsplit vgcfgrestore vgconvert vgexport vgimportclone vgreduce vgs vgchange vgcreate vgextend vgmerge vgremove vgscan [root@sunli ~]# lv lvchange lvdisplay lvmconf lvmdump lvmsadc lvremove lvs lvconvert lvextend lvmconfig lvmetad lvmsar lvrename lvscan lvcreate lvm lvmdiskscan lvmpolld lvreduce lvresize
4, Default installation
# df -h //Check the disk usage. The mounting format of the logical volume is/dev/mapper/VolumeGroupName-LogicalVolumeName Filesystem Size Used Avail Use% Mounted on /dev/mapper/centos-root 50G 4.8G 46G 10% / devtmpfs 3.9G 0 3.9G 0% /dev tmpfs 3.9G 140K 3.9G 1% /dev/shm tmpfs 3.9G 8.9M 3.9G 1% /run tmpfs 3.9G 0 3.9G 0% /sys/fs/cgroup /dev/mapper/centos-home 873G 12G 862G 2% /home /dev/sda1 494M 133M 362M 27% /boot
# cat /etc/fstab /dev/mapper/centos-root / xfs defaults 0 0 UUID=6abe266a-12fe-4361-9417-8d317bc25c8d /boot xfs defaults 0 0 /dev/mapper/centos-home /home xfs defaults 0 0 /dev/mapper/centos-swap swap swap defaults 0 0
# vgdisplay //Viewing logical volume groups --- Volume group --- VG Name centos System ID Format lvm2 Metadata Areas 1 Metadata Sequence No 6 VG Access read/write VG Status resizable MAX LV 0 Cur LV 3 Open LV 3 Max PV 0 Cur PV 1 Act PV 1 VG Size 931.02 GiB PE Size 4.00 MiB Total PE 238341 Alloc PE / Size 238325 / 930.96 GiB Free PE / Size 16 / 64.00 MiB VG UUID IFQnYm-fCLh-38mD-Duid-nYXQ-qktX-3F1atk
# lvdisplay //View the status of logical volumes. There are three by default, root,home And swap space swap --- Logical volume --- LV Path /dev/centos/swap LV Name swap VG Name centos LV UUID FGrPuO-2T02-1OfG-0dcz-oVTy-qcsh-IxEmd4 LV Write Access read/write LV Creation host, time localhost, 2015-11-11 04:46:00 +0800 LV Status available # open 2 LV Size 7.75 GiB Current LE 1984 Segments 1 Allocation inherit Read ahead sectors auto - currently set to 256 Block device 253:1 --- Logical volume --- LV Path /dev/centos/home LV Name home VG Name centos LV UUID x7Oh0t-6R1x-ey7G-G5OX-PnBO-VI0C-kcRuvO LV Write Access read/write LV Creation host, time localhost, 2015-11-11 04:46:01 +0800 LV Status available # open 1 LV Size 873.21 GiB Current LE 223541 Segments 1 Allocation inherit Read ahead sectors auto - currently set to 256 Block device 253:2 --- Logical volume --- LV Path /dev/centos/root LV Name root VG Name centos LV UUID PgscNL-rvyY-21Z1-tVe3-80vC-ONCw-VlvmYs LV Write Access read/write LV Creation host, time localhost, 2015-11-11 04:46:04 +0800 LV Status available # open 1 LV Size 50.00 GiB Current LE 12800 Segments 1 Allocation inherit Read ahead sectors auto - currently set to 256 Block device 253:0
5, Operation steps
1. Back up user data in / home
[root@localhost /]# mkdir /backup && mv /home/* /backup [root@localhost /]# ls /home/
2. Unmount this / home and delete the logical volume home
# umount /home # df -h //View disk status Filesystem Size Used Avail Use% Mounted on /dev/mapper/centos-root 50G 17G 34G 33% / devtmpfs 3.9G 0 3.9G 0% /dev tmpfs 3.9G 84K 3.9G 1% /dev/shm tmpfs 3.9G 9.0M 3.9G 1% /run tmpfs 3.9G 0 3.9G 0% /sys/fs/cgroup /dev/sda1 494M 133M 362M 27% /boot
# lvremove /dev/centos/home //Delete logical volume home Do you really want to remove active logical volume home? [y/n]: y Logical volume "home" successfully removed
# vgdisplay //View volume group free space --- Volume group --- VG Name centos System ID Format lvm2 Metadata Areas 1 Metadata Sequence No 7 VG Access read/write VG Status resizable MAX LV 0 Cur LV 2 Open LV 2 Max PV 0 Cur PV 1 Act PV 1 VG Size 931.02 GiB PE Size 4.00 MiB Total PE 238341 Alloc PE / Size 14784 / 57.75 GiB Free PE / Size 223557 / 873.27 GiB VG UUID IFQnYm-fCLh-38mD-Duid-nYXQ-qktX-3F1atk
Attention here , The space displayed in Free PE / Size is 873G of the free space of the volume group, which was 64M before, that is, the space occupied by the logical volume home just deleted. We will take some of this, such as 50G, as home
3. Create a new volume home, format fdisk in 8e format, and make the file system xfs (also mount to / home)
# lvcreate -L 50G -n home centos //L Indicates the size. The default unit is M;n Represents the volume name; here centos yes CentOS7 The volume group name is created by default when installing the system WARNING: xfs signature detected on /dev/centos/home at offset 0. Wipe it? [y/n]: y Wiping xfs signature on /dev/centos/home. Logical volume "home" created.
# lvdisplay //Viewing logical volumes home --- Logical volume --- LV Path /dev/centos/home LV Name home VG Name centos LV UUID b5lLlv-FQPY-dT43-SmzV-sSLA-4QqP-gdQqAP LV Write Access read/write LV Creation host, time localhost, 2015-11-13 14:05:06 +0800 LV Status available # open 0 LV Size 50.00 GiB Current LE 12800 Segments 1 Allocation inherit Read ahead sectors auto - currently set to 256 Block device 253:2
# vgdisplay //Review the volume group space size again --- Volume group --- VG Name centos System ID Format lvm2 Metadata Areas 1 Metadata Sequence No 8 VG Access read/write VG Status resizable MAX LV 0 Cur LV 3 Open LV 2 Max PV 0 Cur PV 1 Act PV 1 VG Size 931.02 GiB PE Size 4.00 MiB Total PE 238341 Alloc PE / Size 27584 / 107.75 GiB Free PE / Size 210757 / 823.27 GiB VG UUID IFQnYm-fCLh-38mD-Duid-nYXQ-qktX-3F1atk
# vgchange -ay centos //Optional step: activate the volume group centos,Make this new home Logical volume validation vgchange Instead of lvchange) 3 logical volume(s) in volume group "centos" now active
# mkfs -t xfs /dev/centos/home //Create a new logical volume home Build on xfs file system meta-data=/dev/centos/home isize=256 agcount=4, agsize=3276800 blks = sectsz=4096 attr=2, projid32bit=1 = crc=0 finobt=0 data = bsize=4096 blocks=13107200, imaxpct=25 = sunit=0 swidth=0 blks naming =version 2 bsize=4096 ascii-ci=0 ftype=0 log =internal log bsize=4096 blocks=6400, version=2 = sectsz=4096 sunit=1 blks, lazy-count=1 realtime =none extsz=4096 blocks=0, rtextents=0
# mount /dev/centos/home /home/ //Put this new logical volume home Hang to previous folder/home To go, directly restart fstab It's OK to mount it
# df -h //Now let's look at disk usage Filesystem Size Used Avail Use% Mounted on /dev/mapper/centos-root 50G 17G 34G 33% / devtmpfs 3.9G 0 3.9G 0% /dev tmpfs 3.9G 84K 3.9G 1% /dev/shm tmpfs 3.9G 9.0M 3.9G 1% /run tmpfs 3.9G 0 3.9G 0% /sys/fs/cgroup /dev/sda1 494M 133M 362M 27% /boot /dev/mapper/centos-home 50G 33M 50G 1% /home
# mv /backup/* /home/ //Then copy the previously copied things back to the new / home. After copying, you can directly use this ordinary user to log in to the system on the desktop without restarting
4. Finally, allocate the free space to the root volume and xfs_growfs expand the file system
# lvextend -L +823G /dev/centos/root //Take the rest 823 G Now assigned to root Roll, leaving a little space for it to idle;+No. indicates an additional increase on the original basis. If not, it will be set as a specific amount Size of logical volume centos/root changed from 50.00 GiB (12800 extents) to 873.00 GiB (223488 extents). Logical volume root successfully resized
# lvdisplay //Viewing logical volumes and volume groups root There is 873 more space G,The previous one was 50 G --- Logical volume --- LV Path /dev/centos/root LV Name root VG Name centos LV UUID PgscNL-rvyY-21Z1-tVe3-80vC-ONCw-VlvmYs LV Write Access read/write LV Creation host, time localhost, 2015-11-11 04:46:04 +0800 LV Status available # open 1 LV Size 873.00 GiB Current LE 223488 Segments 2 Allocation inherit Read ahead sectors auto - currently set to 256 Block device 253:0
# vgchange -ay centos //Optional step: activate the lower volume group again centos
# df -h //Not used xfs_growfs Expanding the file system, the disk does not recognize much space Filesystem Size Used Avail Use% Mounted on /dev/mapper/centos-root 50G 4.8G 46G 10% / devtmpfs 3.9G 0 3.9G 0% /dev tmpfs 3.9G 140K 3.9G 1% /dev/shm tmpfs 3.9G 9.0M 3.9G 1% /run tmpfs 3.9G 0 3.9G 0% /sys/fs/cgroup /dev/sda1 494M 133M 362M 27% /boot /dev/mapper/centos-home 50G 12G 39G 23% /home
# xfs_growfs /dev/centos/root //extend root volume meta-data=/dev/mapper/centos-root isize=256 agcount=4, agsize=3276800 blks = sectsz=4096 attr=2, projid32bit=1 = crc=0 finobt=0 data = bsize=4096 blocks=13107200, imaxpct=25 = sunit=0 swidth=0 blks naming =version 2 bsize=4096 ascii-ci=0 ftype=0 log =internal bsize=4096 blocks=6400, version=2 = sectsz=4096 sunit=1 blks, lazy-count=1 realtime =none extsz=4096 blocks=0, rtextents=0 data blocks changed from 13107200 to 228851712
# df -h //Look again root The size has come into effect, more than 800 G Yes Filesystem Size Used Avail Use% Mounted on /dev/mapper/centos-root 873G 4.8G 869G 1% / devtmpfs 3.9G 0 3.9G 0% /dev tmpfs 3.9G 140K 3.9G 1% /dev/shm tmpfs 3.9G 9.0M 3.9G 1% /run tmpfs 3.9G 0 3.9G 0% /sys/fs/cgroup /dev/sda1 494M 133M 362M 27% /boot /dev/mapper/centos-home 50G 12G 39G 23% /home
6, Command summary
To sum up, here are the following commands:
# mkdir /backup # mv /home/* /backup/ # umount /home # lvremove /dev/centos/home # lvcreate -L 50G -n home cents # mkfs -t xfs /dev/centos/home # mv /backup/* /home/ # lvextend -L +xxxG /dev/centos/root # xfs_growfs root # rm -rf /backup
New plate to do LVM, ideas --- PV --- VG --- LV
lsblk ##Create PV pvcreate /dev/sdb ##View current PV pvscan perhaps pvs ##Create VG vgcreate datevg /dev/sdb (datavg Yes VG (first name) ##View current VG vgscan ##Add new PV to VG datevg vgextend datevg /dev/sdc #Create LV lvcreate -L 200M -n lv1 datavg (-L(appoint lv Size) is specified as 200 m lv1 For LV Name from datevg Create in) ## View LV lvscan /dev/datevg/lv1 200m ##Format, create file system mount mkfs.xfs /dev/datevg/lv1 perhaps mkfs.ext4 /dev/datevg/lv1 mkdir /mnt/lv1 ##Temporary mount mount /dev/datevg/lv1 /mnt/lv1 mount -a df -h (df-Th) add TYPE type LVM complete
LVM Uninstall of [root@localhost ~]# lvremove /dev/datavg/lv1 [root@localhost ~]# vgremove datavg [root@localhost ~]# pvremove /dev/sdb