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

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM