lvm磁盘挂载和扩容

lvm磁盘挂载

先查看磁盘情况,这台服务器我加了两块空磁盘,sdc-50G,sdd-100G

root@percona-node1 ~]# fdisk -l

Disk /dev/sdb: 145.0 GB, 144955146240 bytes
255 heads, 63 sectors/track, 17623 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: 0x04a4264e

Device Boot Start End Blocks Id System
/dev/sdb1 * 1 17623 141555712 83 Linux

Disk /dev/sda: 32.2 GB, 32212254720 bytes
255 heads, 63 sectors/track, 3916 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: 0x000c23d3

Device Boot Start End Blocks Id System
/dev/sda1 * 1 3789 30432256 83 Linux
/dev/sda2 3789 3917 1024000 82 Linux swap / Solaris

Disk /dev/sdc: 53.7 GB, 53687091200 bytes
255 heads, 63 sectors/track, 6527 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/sdd: 107.4 GB, 107374182400 bytes
255 heads, 63 sectors/track, 13054 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

创建lvm分区

[root@percona-node1 ~]# 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 0xa9173f24.
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): 4
First cylinder (1-6527, default 1):
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-6527, default 6527): +5000M

Command (m for help): t
Selected partition 4
Hex code (type L to list codes): 8e
Changed system type of partition 4 to 8e (Linux LVM)

Command (m for help): p

Disk /dev/sdc: 53.7 GB, 53687091200 bytes
255 heads, 63 sectors/track, 6527 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: 0xa9173f24

Device Boot Start End Blocks Id System
/dev/sdc4 1 638 5124703+ 8e Linux LVM

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

Calling ioctl() to re-read partition table.
Syncing disks.

这个分区创建完成我才知道我只分了5000M,就是差不多5个G,sdc还有45个G没有分掉,就这样吧,下次接着增加。

创建pv

[root@percona-node1 dev]# pvcreate sdc4

Physical volume “/dev/sdc4” successfully created

显示pv情况

[root@percona-node1 dev]# pvdisplay

“/dev/sdc4” is a new physical volume of “4.89 GiB”
— NEW Physical volume —
PV Name /dev/sdc4
VG Name
PV Size 4.89 GiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID n1xsHK-njVq-YUg3-m0GB-SbBp-jLH4-1m9doO

创建vg

[root@percona-node1 dev]# vgcreate VolGroup00 /dev/sdc4

Volume group “VolGroup00” successfully created

查看vg情况

[root@percona-node1 dev]# vgdisplay

— Volume group —
VG Name VolGroup00
System ID
Format lvm2
Metadata Areas 1
Metadata Sequence No 1
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 0
Open LV 0
Max PV 0
Cur PV 1
Act PV 1
VG Size 4.88 GiB
PE Size 4.00 MiB
Total PE 1250
Alloc PE / Size 0 / 0
Free PE / Size 1250 / 4.88 GiB
VG UUID jQ2D29-0BXa-FQiQ-U12S-iSwg-3e9Y-hEPaPP

创建lv

[root@percona-node1 dev]# lvcreate -L 5000M -n lvData VolGroup00

Logical volume “lvData” created

格式化分区

[root@percona-node1 dev]# mkfs -t ext4 /dev/VolGroup00/lvData

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
320000 inodes, 1280000 blocks
64000 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=1312817152
40 block groups
32768 blocks per group, 32768 fragments per group
8000 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736

Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information:
done

This filesystem will be automatically checked every 22 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.

将格式化后的LV分区挂载到指定的目录下,就可以像普通目录一样存储数据了.

[root@percona-node1 /]# mkdir /u01
[root@percona-node1 /]# mount /dev/VolGroup00/lvData /u01/
[root@percona-node1 /]# df -h

Filesystem Size Used Avail Use% Mounted on
/dev/sda1 29G 5.4G 22G 20% /
tmpfs 1.7G 0 1.7G 0% /dev/shm
/dev/sdb1 133G 60M 126G 1% /mnt/resource
/dev/mapper/VolGroup00-lvData
4.7G 9.8M 4.5G 1% /u01

如果要在系统启动的时候启动LV,最好是将lvData写入fstable 文件中.

[root@percona-node1 /]# echo /dev/VolGroup00/lvData /u01 ext4 defaults 1 2 >>/etc/fstab

这样就算一块磁盘lvm挂载完成了,接下来要扩展分区。

扩容当前分区

[root@percona-node1 /]# fdisk /dev/sdd

Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0x304922b8.
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-13054, default 1):
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-13054, default 13054): +100G
Value out of range.
Last cylinder, +cylinders or +size{K,M,G} (1-13054, default 13054): 13054

Command (m for help): t
Selected partition 1
Hex code (type L to list codes): 8e
Changed system type of partition 1 to 8e (Linux LVM)

Command (m for help): p

Disk /dev/sdd: 107.4 GB, 107374182400 bytes
255 heads, 63 sectors/track, 13054 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: 0x304922b8

Device Boot Start End Blocks Id System
/dev/sdd1 1 13054 104856223+ 8e Linux LVM

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

Calling ioctl() to re-read partition table.
Syncing disks.

[root@percona-node1 /]# pvcreate /dev/sdd1

Physical volume “/dev/sdd1” successfully created

[root@percona-node1 /]# pvdisplay

— Physical volume —
PV Name /dev/sdc4
VG Name VolGroup00
PV Size 4.89 GiB / not usable 4.59 MiB
Allocatable yes (but full)
PE Size 4.00 MiB
Total PE 1250
Free PE 0
Allocated PE 1250
PV UUID n1xsHK-njVq-YUg3-m0GB-SbBp-jLH4-1m9doO

“/dev/sdd1” is a new physical volume of “100.00 GiB”
— NEW Physical volume —
PV Name /dev/sdd1
VG Name
PV Size 100.00 GiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID zwNLeb-dyTr-ERrZ-t7Gz-OLT2-Chty-A5SxRo

增加pv

[root@percona-node1 /]# vgextend VolGroup00 /dev/sdd1

Volume group “VolGroup00” successfully extended

[root@percona-node1 /]# lvdisplay

— Logical volume —
LV Path /dev/VolGroup00/lvData
LV Name lvData
VG Name VolGroup00
LV UUID Gkx5aQ-r7Jd-GZQ1-Sd0F-P4kD-aBDp-OImEd4
LV Write Access read/write
LV Creation host, time percona-node1, 2015-05-07 06:44:39 +0000
LV Status available
# open 1
LV Size 4.88 GiB
Current LE 1250
Segments 1
Allocation inherit
Read ahead sectors auto
– currently set to 256
Block device 253:0

扩展LV

[root@percona-node1 /]# lvextend -L 100G /dev/VolGroup00/lvData

Size of logical volume VolGroup00/lvData changed from 4.88 GiB (1250 extents) to 100.00 GiB (25600 extents).
Logical volume lvData successfully resized
注:这里100G是总的大小,不是增加的大小。

[root@percona-node1 /]# resize2fs /dev/VolGroup00/lvData

resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/VolGroup00/lvData is mounted on /u01; on-line resizing required
old desc_blocks = 1, new_desc_blocks = 7
Performing an on-line resize of /dev/VolGroup00/lvData to 26214400 (4k) blocks.

The filesystem on /dev/VolGroup00/lvData is now 26214400 blocks long.

等待重载完成后磁盘大小就增加了。

[root@percona-node1 /]# df -h

Filesystem Size Used Avail Use% Mounted on
/dev/sda1 29G 5.4G 22G 20% /
tmpfs 1.7G 0 1.7G 0% /dev/shm
/dev/sdb1 133G 60M 126G 1% /mnt/resource
/dev/mapper/VolGroup00-lvData
99G 18M 94G 1% /u01