Linux服务器LVM详细操作

1:# 查看新添加的磁盘,确认已识别新增的硬盘
	[root@localhost ~]# fdisk -l
		Disk /dev/sda: 21.5 GB, 21474836480 bytes
		255 heads, 63 sectors/track, 2610 cylinders
		Units = cylinders of 16065 * 512 = 8225280 bytes
		Sector size (logical/physical): 512 bytes / 512 bytes
		I/O size (minimum/optimal): 512 bytes / 512 bytes
		Disk identifier: 0x000433db

		   Device Boot      Start         End      Blocks   Id  System
		/dev/sda1   *           1          64      512000   83  Linux
		Partition 1 does not end on cylinder boundary.
		/dev/sda2              64        2611    20458496   8e  Linux LVM

		Disk /dev/sdb: 21.5 GB, 21474836480 bytes
		255 heads, 63 sectors/track, 2610 cylinders
		Units = cylinders of 16065 * 512 = 8225280 bytes
		Sector size (logical/physical): 512 bytes / 512 bytes
		I/O size (minimum/optimal): 512 bytes / 512 bytes
		Disk identifier: 0x00000000

2:创建分区,并将分区类型修改为8e
	[root@localhost ~]# fdisk /dev/sdb 
		Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)

		WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
		         switch off the mode (command 'c') and change display units to
		         sectors (command 'u').

		Command (m for help): n                      # 创建第1个分区
		Command action
		   e   extended
		   p   primary partition (1-4)
		p                                            # 选择创建主分区
		Partition number (1-4): 1                    # 设置第一个主分区编号 
		First cylinder (1-2610, default 1): 1        # 其实大小
		Last cylinder, +cylinders or +size{K,M,G} (1-2610, default 2610): 
		Using default value 2610

		Command (m for help): p                      # 查看

		Disk /dev/sdb: 21.5 GB, 21474836480 bytes
		255 heads, 63 sectors/track, 2610 cylinders
		Units = cylinders of 16065 * 512 = 8225280 bytes
		Sector size (logical/physical): 512 bytes / 512 bytes
		I/O size (minimum/optimal): 512 bytes / 512 bytes
		Disk identifier: 0x99783bc8

		   Device Boot      Start         End      Blocks   Id  System
		/dev/sdb1               1        2610    20964793+  83  Linux

		Command (m for help): t                    # 改变分区类型
		Selected partition 1                       # 改变第一个分区类型
		Hex code (type L to list codes): 8e        # 默认是ext格式,LVM分区为8e
		Changed system type of partition 1 to 8e (Linux LVM)

		Command (m for help): w                    # 保存退出
		The partition table has been altered!

3:将/dev/sdb1 分区转换为物理卷
	[root@localhost ~]# pvcreate /dev/sdb1
  		Physical volume "/dev/sdb1" successfully created

4:将物理卷进行整合,创建名称为”mail_strore"的卷组
	[root@localhost ~]# pvscan        # 扫描当前系统中的物理卷
	  	PV /dev/sda2   VG VolGroup        lvm2 [19.51 GiB / 0    free]
	  	PV /dev/sdb1                      lvm2 [19.99 GiB]
	  	Total: 2 [39.50 GiB] / in use: 1 [19.51 GiB] / in no VG: 1 [19.99 GiB]

  	[root@localhost ~]# vgcreate mail_strore /dev/sdb1    #使用物理卷创建卷组
  		Volume group "mail_strore" successfully created

     # 扩展知识
    [root@localhost ~]# vgcreate mail_strore /dev/sdb1  /dev/sdc1   #将多块物理卷整合,并创建mail_strore卷组
  		Volume group "mail_strore" successfully created

5:在"mail_strore"卷组中创建名为”mail“的逻辑卷,设置容量为10G
	[root@localhost ~]# vgscan | grep "mail_strore"		# 扫描当前卷组
  		Found volume group "mail_strore" using metadata type lvm2

  	[root@localhost ~]# lvcreate -L 10G -n mail mail_strore   # 在mail_strore中创建10G的逻辑卷,名称为mail
  		Logical volume "mail" created

6:进行初始化,并挂载到mail 目录下
	[root@localhost ~]# mkfs.xfs /dev/mail_strore/mail 
	meta-data=/dev/mail_strore/mail  isize=256    agcount=4, agsize=655360 blks
	         =                       sectsz=512   attr=2, projid32bit=0
	data     =                       bsize=4096   blocks=2621440, imaxpct=25
	         =                       sunit=0      swidth=0 blks
	naming   =version 2              bsize=4096   ascii-ci=0
	log      =internal log           bsize=4096   blocks=2560, version=2
	         =                       sectsz=512   sunit=0 blks, lazy-count=1
	realtime =none                   extsz=4096   blocks=0, rtextents=0

	[root@localhost ~]# mkdir /mail

	[root@localhost ~]# mount /dev/mail_strore/mail /mail

	[root@localhost ~]# df -hT
	Filesystem                   Type   Size  Used Avail Use% Mounted on
	/dev/mapper/VolGroup-lv_root ext4    18G  870M   16G   6% /
	tmpfs                        tmpfs  491M     0  491M   0% /dev/shm
	/dev/sda1                    ext4   485M   32M  428M   7% /boot
	/dev/mapper/mail_strore-mail xfs     10G   33M   10G   1% /mail

7:知识补充:可以动态进行扩容磁盘大小
	[root@localhost ~]# lvextend -L +5G /dev/mail_strore/mail 
  		Extending logical volume mail to 15.00 GiB
  		Logical volume mail successfully resized

  	[root@localhost ~]# lvdisplay /dev/mail_strore/mail      # 查看扩展后的逻辑卷大小
	  --- Logical volume ---
	  LV Path                /dev/mail_strore/mail
	  LV Name                mail
	  VG Name                mail_strore
	  LV UUID                C9IDSD-tfTB-VBge-juUA-Aod9-TjAn-1YAV2r
	  LV Write Access        read/write
	  LV Creation host, time localhost.localdomain, 2020-03-08 13:26:18 +0800
	  LV Status              available
	  # open                 1
	  LV Size                15.00 GiB
	  Current LE             3840
	  Segments               1
	  Allocation             inherit
	  Read ahead sectors     auto
	  - currently set to     256
	  Block device           253:2

	 # 验证磁盘大小并未增加
	[root@localhost ~]# df -hT
		Filesystem                   Type   Size  Used Avail Use% Mounted on
		/dev/mapper/VolGroup-lv_root ext4    18G  870M   16G   6% /
		tmpfs                        tmpfs  491M     0  491M   0% /dev/shm
		/dev/sda1                    ext4   485M   32M  428M   7% /boot
		/dev/mapper/mail_strore-mail xfs     10G   33M   10G   1% /mail

	# 执行相应的命令
	[root@localhost ~]# xfs_growfs /dev/mail_strore/mail 
		meta-data=/dev/mapper/mail_strore-mail isize=256    agcount=4, agsize=655360 blks
		         =                       sectsz=512   attr=2, projid32bit=0
		data     =                       bsize=4096   blocks=2621440, imaxpct=25
		         =                       sunit=0      swidth=0 blks
		naming   =version 2              bsize=4096   ascii-ci=0
		log      =internal               bsize=4096   blocks=2560, version=2
		         =                       sectsz=512   sunit=0 blks, lazy-count=1
		realtime =none                   extsz=4096   blocks=0, rtextents=0
		data blocks changed from 2621440 to 3932160

	# 再次验证
	[root@localhost ~]# df -hT
		Filesystem                   Type   Size  Used Avail Use% Mounted on
		/dev/mapper/VolGroup-lv_root ext4    18G  870M   16G   6% /
		tmpfs                        tmpfs  491M     0  491M   0% /dev/shm
		/dev/sda1                    ext4   485M   32M  428M   7% /boot
		/dev/mapper/mail_strore-mail xfs     15G   33M   15G   1% /mail


8:知识补充,将xfs格式转换为ext4格式
	# 卸载
	[root@localhost ~]# umount /dev/mail_strore/mail /mail/

	# 验证
	[root@localhost ~]# df -hT

	# 删除/dev/mail_strore/mail 逻辑卷
	[root@localhost ~]# lvremove /dev/mail_strore/mail 
		Do you really want to remove active logical volume mail? [y/n]: y
  		Logical volume "mail" successfully removed

  	# 扫描卷组
  	[root@localhost ~]# vgscan 
  		Reading all physical volumes.  This may take a while...
  		Found volume group "mail_strore" using metadata type lvm2
  		Found volume group "VolGroup" using metadata type lvm2

  	# 创建10G的逻辑卷,名称为:mail
    [root@localhost ~]# lvcreate -L 10G -n mail mail_strore

    # 查看
    [root@localhost ~]# lvdisplay

    # 初始化
    [root@localhost ~]# mkfs.ext4 /dev/mail_strore/mail

    # 挂载
    [root@localhost ~]# mount /dev/mail_strore/mail /mail

    # 动态扩展
    [root@localhost ~]# lvextend -L +5G /dev/mail_strore/mail

    # 重新加载
    [root@localhost ~]# resize2fs /dev/mail_strore/mail 

    # 再次验证
    [root@localhost ~]# df -hT

  

原文地址:https://www.cnblogs.com/happlyp/p/12765791.html