從CentOS7默認安裝的/home中轉移空間到根目錄/ - LVM操做簡明教程

轉自 http://blog.csdn.net/evandeng2009/article/details/49814097node

1、基礎概念

Cent0S 7默認啓用LVM2(Logical Volume Manager),把機器的一塊硬盤分爲兩個區sda1和sda2,其中分區sda1做爲系統盤/boot掛載,少許空間;sda2做爲一個物理卷而且徹底做爲邏輯卷組VG(Volume Group)centos,在這個邏輯卷組centos中創建三個邏輯卷LV(Logical Volume)root和home還有swap,分別掛載到根目錄/和/home以及swap。而兩個分區sda1和sda2上都創建了文件系統XFS,文件系統XFS做爲RedHat的默認文件系統也有它的考慮,成爲繼ext3,ext4以後的主流文件系統。centos

幾個概念的關係:M個物理硬盤HD或者物理硬盤中的分區一塊兒組建爲一個邏輯卷組VG及存儲池,在卷組VG中建立N個邏輯卷LV,在一個邏輯卷LV中建立文件系統好比xfs。物理硬盤/分區、邏輯卷有最小基本尋址單元,CentOS7默認的大小爲4MB,兩者一一對應,相似於連接或者變量引用,可是一個兩者關係並不是一直不變,由於物理硬盤可能發生變化而邏輯卷自動調整。建立卷組和邏輯卷,會相似於建立分區同樣在磁盤開始位置寫入卷的信息VGDA(卷組描述符區域,Volume Group Descriptor Area)用於識別。邏輯卷的好處在於屏蔽物理底層支撐,可自由擴展變動,而不用擔憂硬盤或者分區的物理空間侷限,也就不會存在爲了擴展分區大小而去備份/擴展分區從新格式化硬盤等問題。app

HD/分區--經過pvcreate->PV--經過vgcreate(vgchange)/vgextend-->VG--經過lvcreate/lvextend-->LV--經過mkfs-->FS--經過xfs_growfs等-->df磁盤生效ssh

可是關鍵點在於,CentOS 7默認安裝時/home佔用太多空間,根目錄相較而言就小得多(只有50G),而OpenStack安裝以及存儲的東西都在根目錄下。上傳幾個鏡像說不定就把你的根目錄空間耗盡。不像其餘文件系統ext3,ext4或者reiserfs等,有命令(resize2fs,resize_reiserfs)直接支持縮小文件系統的大小,默認安裝的xfs支持擴展增大可是不支持縮小空間ui

 

2、步驟概覽

因此下面要作的步驟大概爲:(直接root用戶登陸系統,本機或者ssh root過去,若是使用當前普通用戶會遇到點沒必要要的麻煩)spa

1. 備份/home/用戶文件,要是沒啥內容則忽略這步(爲何非要這個/home,刪掉直接用root?仍是保留它,它存在也有道理的,再說生產環境仍是不要只用root).net

2. umount /home 卸載並lvremove刪除這個home邏輯卷,釋放它的空間,vgdisplay查看卷組可用空間大小rest

3. lvcreate新建一個新的home卷,並在其上mkfs創建xfs文件系統,(分配掛載到/home - 不用更改/etc/fstab,重啓便可,) 拷貝回來以前的內容code

(這個時候空餘的空間隨便你分配,能夠再創建別的邏輯卷,或者直接空閒下來之後使用,也能夠直奔主題的走下面的第四步)orm

4. 把以前的home邏輯卷釋放並分配新卷home以後剩下的空間,lvextend分配給root卷,並用命令xfs_growfs擴展它的文件系統空間

 

3、相關命令

看看大概的命令有哪些

# pv

pvchange   pvck       pvcreate   pvdisplay  pvmove     pvremove   pvresize   pvs        pvscan  

# vb

vgcfgbackup    vgchange       vgconvert      vgdisplay      vgextend       vgimportclone  vgmknodes      vgremove       vgs  
vgsplit        vgcfgrestore   vgck           vgcreate       vgexport       vgimport       vgmerge        vgreduce       vgrename  
vgscan  

# lv

lvchange     lvcreate     lvextend     lvmchange    lvmdiskscan  lvmetad      lvmsar       lvremove     lvresize     lvscan  
lvconvert    lvdisplay    lvm  

4、默認安裝狀況

# df -h //查看磁盤使用狀況,邏輯卷的掛載格式爲/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 //查看邏輯卷組狀況

複製代碼
--- 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 //查看邏輯卷狀況,默認三個,root、home和交換空間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、操做步驟

1. 備份/home中的用戶數據

 

[root@localhost /]# mkdir /backup && mv /home/* /backup
[root@localhost /]# ls /home/

 

2. 卸載這個/home並刪除邏輯卷home

# umount /home

# df -h //查看磁盤狀況

複製代碼
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 //刪除邏輯卷home

Do you really want to remove active logical volume home? [y/n]: y  
  Logical volume "home" successfully removed  

# vgdisplay //查看卷組可用空間

複製代碼
--- 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  
複製代碼

這裏注意,Free PE / Size 中顯示的空間爲卷組的空閒空間873G,以前爲64M,也就是剛纔刪除的邏輯卷home所佔用的空間,咱們就從這個當中拿出一部分好比50G做爲home

 

3. 新建一個卷home,fdisk格式化爲8e格式,文件系統仍是搞爲xfs(一樣掛載到/home)

# lvcreate -L 50G -n home centos //L表示大小,默認單位爲M;n表示卷名;這裏的centos是CentOS7安裝系統的時候就默認創建好的卷組名

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 //查看邏輯卷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 //再次查看卷組空間大小

複製代碼
--- 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  //可選步驟:激活卷組centos,使得這個新建的home邏輯卷生效(用vgchange而不用lvchange)

3 logical volume(s) in volume group "centos" now active  

 mkfs -t xfs /dev/centos/home //在新建的邏輯卷home上創建xfs文件系統

複製代碼
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/ //把這個新邏輯卷home掛到以前的文件夾/home中去,直接重啓用fstab來掛載也行

 

# df -h //如今來查看磁盤使用狀況

複製代碼
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/ //再把以前拷出來的東西拷回新建的/home中,拷貝完成就能夠直接用這個普通用戶來桌面登陸系統了,不用重啓

 

4. 最後再把釋放出來多餘的空間分配給root卷並xfs_growfs擴展文件系統
# lvextend -L +823G /dev/centos/root //把剩下的823G如今分配給root卷,剩下那點渣渣空間讓它閒着;+號表示在原來的基礎上額外增長,不要加好則設定爲具體額度

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 //查看邏輯卷和卷組狀況,這下root的空間就多達到873G,以前的是50G

複製代碼
--- 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 //可選步驟:再次激活下卷組centos]

 

# df -h //不使用xfs_growfs擴展文件系統,磁盤是不認得多的空間的

複製代碼
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 //擴展root卷

複製代碼
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 //再看root大小已經生效,800多G了

複製代碼
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、命令彙總

簡單總結下,就是下面幾條命令:

複製代碼
# 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  
複製代碼
相關文章
相關標籤/搜索