lvm磁盤掛載方法

lvm磁盤掛載方法

系統環境:centos 7.x ubuntu

一.fdisk -l 查看分區信息,找到要添加的硬盤盤符 如:/dev/vdb

   #fdisk -l
   Disk /dev/vdb: 53.7 GB, 53687091200 bytes, 104857600 sectors
   Units = sectors of 1 * 512 = 512 bytes
   Sector size (logical/physical): 512 bytes / 512 bytes
   I/O size (minimum/optimal): 512 bytes / 512 bytes

二.劃分物理分區

(一)分區沒超過2T的,使用fdisk

(1)fdisk /dev/vdb

#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 new DOS disklabel with disk identifier 0xd069b076.

Command (m for help): 

(2) n(新建)、p-1、其他的默認

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, default 1): 1
First sector (2048-104857599, default 2048):   #輸入開始分區位置,回車默認位置
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-104857599, default 104857599):  #輸入結束分區位置,回車默認全部分區
Using default value 104857599
Partition 1 of type Linux and of size 50 GiB is set

(3)將磁盤格式修改爲lvm格式, t(改格式)、8e

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'

(4)p 查看創建分區信息,w 保存

Command (m for help): p

Disk /dev/vdb: 53.7 GB, 53687091200 bytes, 104857600 sectors
Units = sectors of 1 * 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: 0xd069b076

   Device Boot      Start         End      Blocks   Id  System
/dev/vdb1            2048   104857599    52427776   8e  Linux LVM

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.
# partprobe                 ##更新分區表

(二)分區超過2T的,用parted

(1)parted /dev/vdb

#parted /dev/vdb
GNU Parted 3.1
Using /dev/vdb
Welcome to GNU Parted! Type 'help' to view a list of commands.

(2)mklabel gpt #設置格式爲gpt

(parted) p
Model: Virtio Block Device (virtblk)
Disk /dev/vdb: 53.7GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags: 

Number  Start  End  Size  Type  File system  Flags
(parted) mklabel gpt
Warning: The existing disk label on /dev/vdb will be destroyed and all data on this disk will be lost. Do you want to
continue?
Yes/No? yes                                                               
(parted) p                                                                
Model: Virtio Block Device (virtblk)
Disk /dev/vdb: 53.7GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: 

Number  Start  End  Size  File system  Name  Flags

(3)mkpart #新建分區

(parted) mkpart
Partition name?  []? vdb1            #分區名稱                                     
File system type?  [ext2]? ext4      #文件系統類型                                     
Start? 1                             #起始位置,1爲1M,一般預留1M空間                                     
End? 100%                            #結束位置,100% 爲使用全部空間                                    
(parted) p                           #顯示分區內容                                    
Model: Virtio Block Device (virtblk)
Disk /dev/vdb: 53.7GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: 

Number  Start   End     Size    File system  Name  Flags
 1      1049kB  53.7GB  53.7GB               vdb1

(parted) q                             #退出                                  
Information: You may need to update /etc/fstab.

三.將創建的vdb1分區,創建pv 物理卷

#pvcreate /dev/vdb1
  Physical volume "/dev/vdb1" successfully created.
#pvdisplay
  --- Physical volume ---
  PV Name               /dev/vda2
  VG Name               centos
  PV Size               <99.00 GiB / not usable 3.00 MiB
  Allocatable           yes 
  PE Size               4.00 MiB
  Total PE              25343
  Free PE               1
  Allocated PE          25342
  PV UUID               2I5XOD-VzbD-fCZU-gdTm-fLPD-pnpa-oUjYjg

  "/dev/vdb1" is a new physical 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               u1CAKQ-Tae9-0XwY-Y9Vs-Ub9G-rlL9-r6VXIz

四.創建vg卷組,創建lv邏輯卷

(一)擴展已有分區

(1)vgdisplay #查看要擴展的vg(卷組)的名稱 例如:centos

#vgdisplay
  --- Volume group ---
  VG Name               centos
  System ID             
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  5
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                4
  Open LV               4
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               <99.00 GiB
  PE Size               4.00 MiB
  Total PE              25343
  Alloc PE / Size       25342 / 98.99 GiB
  Free  PE / Size       1 / 4.00 MiB
  VG UUID               QHEpYY-Z6c1-dAkv-9Vl5-uWs7-Ydpa-OQNNvF

(2)vgextend centos /dev/vdb1 #把vdb1的pv空間擴展給centos的vg

#vgextend centos /dev/vdb1
  Volume group "centos" successfully extended
#vgdisplay                        #查看增加後的卷組
  --- Volume group ---
  VG Name               centos
  System ID             
  Format                lvm2
  Metadata Areas        2
  Metadata Sequence No  6
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                4
  Open LV               4
  Max PV                0
  Cur PV                2
  Act PV                2
  VG Size               148.99 GiB
  PE Size               4.00 MiB
  Total PE              38142
  Alloc PE / Size       25342 / 98.99 GiB
  Free  PE / Size       12800 / 50.00 GiB
  VG UUID               QHEpYY-Z6c1-dAkv-9Vl5-uWs7-Ydpa-OQNNvF

(3)df -hT #查看要增加空間的名稱,例要增加根目錄 / 的空間

#df -hT
Filesystem                 Type      Size  Used Avail Use% Mounted on
devtmpfs                   devtmpfs  5.8G     0  5.8G   0% /dev
tmpfs                      tmpfs     5.8G     0  5.8G   0% /dev/shm
tmpfs                      tmpfs     5.8G   27M  5.8G   1% /run
tmpfs                      tmpfs     5.8G     0  5.8G   0% /sys/fs/cgroup
/dev/mapper/centos-root    xfs        50G  8.6G   42G  18% /
/dev/vda1                  xfs      1014M  209M  806M  21% /boot
/dev/mapper/centos-home    xfs        31G   33M   31G   1% /home
/dev/mapper/centos-var_log xfs        12G   65M   11G   1% /var/log
tmpfs                      tmpfs     1.6G     0  1.6G   0% /run/user/0

(4)lvextend -l +100%FREE /dev/mapper/centos-root #將空閒的空間,都增 加給lv(邏輯卷),即增加給根目錄 /

#lvextend -l +100%FREE /dev/mapper/centos-root
  Size of logical volume centos/root changed from 50.00 GiB (12800 extents) to 100.00 GiB (25600 extents).
  Logical volume centos/root successfully resized.

(5)xfs_growfs /dev/mapper/centos-root #重新刷新識別空間(注:xfs_growfs命令,針對的是xfs文件系統,resize2fs命令 針對的是ext2、ext3、ext4文件系統)

#xfs_growfs /dev/mapper/centos-root
meta-data=/dev/mapper/centos-root isize=512    agcount=4, agsize=3276800 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0 spinodes=0
data     =                       bsize=4096   blocks=13107200, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal               bsize=4096   blocks=6400, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
data blocks changed from 13107200 to 26214400

(6)df -hT #查看確認空間增長

#df -hT
Filesystem                 Type      Size  Used Avail Use% Mounted on
devtmpfs                   devtmpfs  5.8G     0  5.8G   0% /dev
tmpfs                      tmpfs     5.8G     0  5.8G   0% /dev/shm
tmpfs                      tmpfs     5.8G   27M  5.8G   1% /run
tmpfs                      tmpfs     5.8G     0  5.8G   0% /sys/fs/cgroup
/dev/mapper/centos-root    xfs       100G  8.6G   92G   9% /
/dev/vda1                  xfs      1014M  209M  806M  21% /boot
/dev/mapper/centos-home    xfs        31G   33M   31G   1% /home
/dev/mapper/centos-var_log xfs        12G   66M   11G   1% /var/log
tmpfs                      tmpfs     1.6G     0  1.6G   0% /run/user/0

(二)掛載新的分區

(1)vgcreate myvg /dev/vdb1 #創建新的vg 卷組

(2)lvcreate -l +100%FREE myvg -n mylv #-L 100G 是隻分100G給lv

(3)mkfs.ext4 /dev/myvg/mylv #格式化lv

(4)分別用pvdisplay、vgdisplay、lvdisplay查看添加的pv、vg、lv

(5)mkdir /data #新建要掛載的文件夾

(6)vi /etc/fstab ,在末尾添加 /dev/myvg/mylv /data ext4 defaults 0 0

(7)mount -a #掛載成功

(8)df -hT #查看掛載的分區和空間

五.xfs文件系統空間減小方法(如果分區有數據,不能減小)

(一)umount /home #卸載分區

(二)減少home分區空間

# lvreduce -L -100G /dev/mapper/centos-home
  WARNING: Reducing active logical volume to 618.00 GiB.
  THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce centos/home? [y/n]: y
  Size of logical volume centos/home changed from 718.00 GiB (183808 extents) to 618.00 GiB (158208 extents).
  Logical volume centos/home successfully resized.

(三)xfs文件系統不能直接執行分區減小的調整

# xfs_growfs /dev/mapper/centos-home
xfs_growfs: /dev/mapper/centos-home is not a mounted XFS filesystem
# mount /dev/mapper/centos-home /home/
mount: /dev/mapper/centos-home:不能讀超級塊

(四)重新格式化這個分區,格式化後才能再次掛載到home下

# mkfs.xfs /dev/mapper/centos-home -f
meta-data=/dev/mapper/centos-home isize=512    agcount=4, agsize=41156608 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0, sparse=0
data     =                       bsize=4096   blocks=164626432, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal log           bsize=4096   blocks=80384, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
# mount /dev/mapper/centos-home /home/

(五)df -hT查看,空間已經減小100G了,但/home裏的文件已經被 格式化,丟失了。

# df -hT         
文件系統                 類型      容量  已用  可用 已用% 掛載點
/dev/mapper/centos-root xfs       205G  2.2G  203G    2% /
devtmpfs                devtmpfs   32G     0   32G    0% /dev
tmpfs                   tmpfs      32G     0   32G    0% /dev/shm
tmpfs                   tmpfs      32G   49M   32G    1% /run
tmpfs                   tmpfs      32G     0   32G    0% /sys/fs/cgroup
/dev/sda1               xfs       197M  139M   59M   71% /boot
tmpfs                   tmpfs     6.3G     0  6.3G    0% /run/user/0
/dev/mapper/centos-home xfs       618G   73M  578G    1% /home
# vgdisplay
  --- Volume group ---
  VG Name               centos
  System ID            
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  9
  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               930.80 GiB
  PE Size               4.00 MiB
  Total PE              238285
  Alloc PE / Size       211456 / 826.00 GiB
  Free  PE / Size       26829 / 104.80 GiB
  VG UUID               a5hiAh-LB8M-9lRv-Ps1a-z35L-J4fk-sP3KrF

注:格式化時,也可以格式化成ext4格式的,增加減少空間就比較方便,不會存在要格式化分區的情況

# mkfs.ext4 /dev/mapper/centos-home           #格式化成ext4格式
#lvextend -L 120G /dev/mapper/centos-home     #增大至120G
lvreduce -L -80G /dev/mapper/centos-home      #減小80G
resize2fs /dev/mapper/centos-home             #執行調整
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章