lvm分區
試驗環境 centos 6.5
借圖如下
一、LVM原理
傳統文件系統,比如這個盤只有300G,那么建立在這個300G上面的文件系統最多只能用到300G,但是有了LVM這個功能后,我們建立文件系統的盤就不是建立在物理盤上,而是建立在一個叫LV邏輯卷上面,這個卷是一個邏輯概念不是物理盤,空間可能大於一個物理盤,也可能小於一個物理盤。而且這個LV邏輯卷的空間可以擴展和縮小,這樣就給上層的文件系統提供了更好的支持。
需要理解的幾個概念:
PV(Physical Volume):物理空間的意思,其實就是指一個分區(如/dev/sdb1 )或者是一個盤(如/dev/sdb)
VG(Volume Group):相當於一個Pool,由多個PV組成的pool
LV(Logical Volume):用來建立一個文件系統的空間,這個空間來源於VG,大小隨意,可以擴展。(比如/dev/mapper/rhel-root這個目錄其實是一個文件系統掛載點,這個點就是承載在一個LV上,這個文件系統的大小就是這個LV的大小。 )
二、實際操作
1、使用fdisk -l查看磁盤情況,可以看到我有兩塊未分區的磁盤,分別是/dev/sdb /dev/sdc,我們先用一塊磁盤/dev/sdb進行測試。
[root@test_iptables ~]# fdisk -l Disk /dev/sdb: 1073 MB, 1073741824 bytes 255 heads, 63 sectors/track, 130 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 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: 0x0005b552 Device Boot Start End Blocks Id System /dev/sda1 * 1 39 307200 83 Linux Partition 1 does not end on cylinder boundary. /dev/sda2 39 2350 18566144 83 Linux /dev/sda3 2350 2611 2097152 82 Linux swap / Solaris Disk /dev/sdc: 1073 MB, 1073741824 bytes 255 heads, 63 sectors/track, 130 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、對/dev/sdb進行分區
(1)先進行標准分區
[root@test_iptables ~]# fdisk /dev/sdb Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel Building a new DOS disklabel with disk identifier 0x34c351e7. Changes will remain in memory only, until you decide to write them. After that, of course, the previous content won't be recoverable. 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 Command action e extended p primary partition (1-4) p Partition number (1-4): 1 First cylinder (1-130, default 1): Using default value 1 Last cylinder, +cylinders or +size{K,M,G} (1-130, default 130): Using default value 130 Command (m for help): w The partition table has been altered! Calling ioctl() to re-read partition table. Syncing disks.
(2)查看使用vgdisplay查看已經存在的卷組名稱
[root@test_iptables ~]# vgdisplay
No volume groups found
(3)開始進行lvm處理
創建物理卷(PV)
[root@test_iptables ~]# pvcreate /dev/sdb1 dev_is_mpath: failed to get device for 8:17 Physical volume "/dev/sdb1" successfully created
創建卷組(VG)
[root@test_iptables ~]# vgcreate vg01 /dev/sdb1 Volume group "vg01" successfully created
查看創建的物理卷及卷組
在卷組vg01上創建名為date,大小為1000M的邏輯卷
[root@test_iptables ~]# lvcreate -L 1000M -n data vg01 Logical volume "data" created
3、格式化分區
[root@test_iptables ~]# mkfs.ext4 /dev/vg01/data mke2fs 1.41.12 (17-May-2010) Filesystem label= OS type: Linux Block size=4096 (log=2) Fragment size=4096 (log=2) Stride=0 blocks, Stripe width=0 blocks 64000 inodes, 256000 blocks 12800 blocks (5.00%) reserved for the super user First data block=0 Maximum filesystem blocks=264241152 8 block groups 32768 blocks per group, 32768 fragments per group 8000 inodes per group Superblock backups stored on blocks: 32768, 98304, 163840, 229376 Writing inode tables: done Creating journal (4096 blocks): done Writing superblocks and filesystem accounting information: done This filesystem will be automatically checked every 33 mounts or 180 days, whichever comes first. Use tune2fs -c or -i to override.
4、掛載分區
[root@test_iptables ~]# mkdir /u01 [root@test_iptables ~]# mount /dev/vg01/data /u01/ [root@test_iptables ~]# df -h Filesystem Size Used Avail Use% Mounted on /dev/sda2 18G 9.2G 7.4G 56% / tmpfs 931M 72K 931M 1% /dev/shm /dev/sda1 291M 39M 238M 14% /boot /dev/vg01/data 985M 18M 918M 2% /u01
5、設置開機自動掛載
[root@test_iptables ~]# cat /etc/fstab # # /etc/fstab # Created by anaconda on Wed Sep 11 09:19:25 2019 # # Accessible filesystems, by reference, are maintained under '/dev/disk' # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info # UUID=fe5d2642-dc89-45a2-963a-2a5e0f7e5a3a / ext4 defaults 1 1 UUID=e356222c-cde8-4f30-a38c-3fcf905b65c8 /boot ext4 defaults 1 2 UUID=4d5bbee9-6557-4b6b-a275-4c43b14dce9d swap swap defaults 0 0 tmpfs /dev/shm tmpfs defaults 0 0 devpts /dev/pts devpts gid=5,mode=620 0 0 sysfs /sys sysfs defaults 0 0 proc /proc proc defaults 0 0 /dev/vg01/data /u01 ext4 defaults 0 0
至此,一個lvm分區設置完畢
三、PV擴容
1、首先進行標准分區
root@test_iptables ~]# fdisk /dev/sdc Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel Building a new DOS disklabel with disk identifier 0x114bf00d. Changes will remain in memory only, until you decide to write them. After that, of course, the previous content won't be recoverable. 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 Command action e extended p primary partition (1-4) p Partition number (1-4): 1 First cylinder (1-130, default 1): Using default value 1 Last cylinder, +cylinders or +size{K,M,G} (1-130, default 130): Using default value 130 Command (m for help): w The partition table has been altered! Calling ioctl() to re-read partition table. Syncing disks.
2、創建物理卷
[root@test_iptables ~]# pvcreate /dev/sdc1 Physical volume "/dev/sdc1" successfully created
3、VG擴容
將新的物理卷加入卷組
[root@test_iptables ~]# vgextend vg01 /dev/sdc1 Volume group "vg01" successfully extended
擴展邏輯卷 並resize2fs加載邏輯卷
[root@test_iptables ~]# lvextend -l +100%FREE /dev/vg01/data Extending logical volume data to 1.98 GiB Logical volume data successfully resized [root@test_iptables ~]# resize2fs /dev/vg01/data resize2fs 1.41.12 (17-May-2010) Filesystem at /dev/vg01/data is mounted on /u01; on-line resizing required old desc_blocks = 1, new_desc_blocks = 1 Performing an on-line resize of /dev/vg01/data to 520192 (4k) blocks. The filesystem on /dev/vg01/data is now 520192 blocks long.
至此,以上就是整個流程,成功擴展為2G。