您的位置:首页 > 其它

LVM配置

2016-05-06 00:00 267 查看
摘要: 磁盘分区,PV创建,VG创建,LV创建,删除操作

介绍及需求:

服务器版本:Ubuntu 14.04

需求:一台服务器有多块硬盘,要将这些硬盘做成lvm,可以动态扩容

操作步骤:
创建配置--

将每块盘分区并格式化(我这里一共三块盘/dev/sdb,/dev/sdc,/dev/sdd,均为1T盘)

root@TESTLVM:~# 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 0x7ef20d7d.
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)

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):
Using default value 1
First sector (2048-2097151999, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-2097151999, default 2097151999):
Using default value 2097151999

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

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

Calling ioctl() to re-read partition table.
Syncing disks.
2. 按照上面的步骤依次把三块盘进行分区,分区完成后执行下面命令,使分区表生效
root@TESTLVM:~#partprobe

3. 创建PV,VG,LV
创建PV
root@TESTLVM:~#pvcreate /dev/sdb1
Physical volume "/dev/sdb1" successfully created

root@TESTLVM:~# pvdisplay      #查看当前PV
"/dev/sdb1" is a new physical volume of "1000.00 GiB"
--- NEW Physical volume ---
PV Name               /dev/sdb1
VG Name
PV Size               1000.00 GiB
Allocatable           NO
PE Size               0
Total PE              0
Free PE               0
Allocated PE          0
PV UUID               hFRN1p-0q7V-oWdy-fflG-qk2Z-iO8I-mIPsI6

创建VG

root@TESTLVM:~# vgcreate VolGroup01 /dev/sdb1
Volume group "VolGroup00" successfully created

#查看vg
root@TESTLVM:~# 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               1000.00 GiB
PE Size               4.00 MiB
Total PE              255999
Alloc PE / Size       0 / 0
Free  PE / Size       255999 / 1000.00 GiB
VG UUID               EMzPOw-TSdS-aT61-g1aA-paSy-Q0Gt-NflwdM

创建LV(-L 大小,-n LV名字 VG名称)

root@TESTLVM:~# lvcreate -L 999G -n lvdata VolGroup01
Logical volume "lvdata" created

#查看LV信息
root@TESTLVM:~# lvdisplay
--- Logical volume ---
LV Path                /dev/VolGroup00/lvdate
LV Name                lvdate
VG Name                VolGroup00
LV UUID                Gdxczp-QWNe-UA1m-4dZx-AUjK-6DsU-4dCOO7
LV Write Access        read/write
LV Creation host, time TESTLVM, 2016-05-06 12:17:05 +0800
LV Status              available
# open                 1
LV Size                999.00 GiB
Current LE             255744
Segments               1
Allocation             inherit
Read ahead sectors     auto
- currently set to     256
Block device           252:2

4. 格式化并挂载

root@TESTLVM:~# mkfs -t ext4 /dev/VolGroup00/lvdate
mke2fs 1.42.9 (4-Feb-2014)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
65470464 inodes, 261881856 blocks
13094092 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=4294967296
7992 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
4096000, 7962624, 11239424, 20480000, 23887872, 71663616, 78675968,
102400000, 214990848

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

root@TESTLVM:~# mount /dev/VolGroup00/lvdate test/

root@TESTLVM:~# df -h
Filesystem                     Size  Used Avail Use% Mounted on
/dev/sda1                  58G  1.3G   54G   3% /
none                           4.0K     0  4.0K   0% /sys/fs/cgroup
udev                           989M  4.0K  989M   1% /dev
tmpfs                          200M  572K  200M   1% /run
none                           5.0M     0  5.0M   0% /run/lock
none                          1000M     0 1000M   0% /run/shm
none                           100M     0  100M   0% /run/user
/dev/sda1                      236M   37M  188M  17% /boot
/dev/mapper/VolGroup00-lvdate  984G   72M  934G   1% /root/test

5. 系统启动时挂载LV
编辑/etc/fstab(将下面内容加入末尾)
/dev/VolGroup00/lvdate  /root/test      ext4    defaults        1 2

容量扩充:
root@TESTLVM:~# pvcreate /dev/sdc1
Physical volume "/dev/sdc1" successfully created

root@TESTLVM:~# vgextend VolGroup00 /dev/sdc1
Volume group "VolGroup00" successfully extended

root@TESTLVM:~# lvextend -L +999G /dev/VolGroup00/lvdate
Extending logical volume lvdate to 1.95 TiB
Logical volume lvdate successfully resized

root@TESTLVM:~# resize2fs /dev/VolGroup00/lvdate
resize2fs 1.42.9 (4-Feb-2014)
Filesystem at /dev/VolGroup00/lvdate is mounted on /root/test; on-line resizing required
old_desc_blocks = 63, new_desc_blocks = 125
The filesystem on /dev/VolGroup00/lvdate is now 523763712 blocks long.

root@TESTLVM:~# df -h
Filesystem                     Size  Used Avail Use% Mounted on
/dev/mapper/ubuntu--vg-root     58G  1.3G   54G   3% /
none                           4.0K     0  4.0K   0% /sys/fs/cgroup
udev                           989M  4.0K  989M   1% /dev
tmpfs                          200M  572K  200M   1% /run
none                           5.0M     0  5.0M   0% /run/lock
none                          1000M     0 1000M   0% /run/shm
none                           100M     0  100M   0% /run/user
/dev/sda1                      236M   37M  188M  17% /boot
/dev/mapper/VolGroup00-lvdate  2.0T   71M  1.9T   1% /root/test

#之后再添加按照此步骤添加即可

删除操作:
#取消挂载
root@TESTLVM:~# umount test/
root@TESTLVM:~# df -h
Filesystem                   Size  Used Avail Use% Mounted on
/dev/mapper/ubuntu--vg-root   58G  1.3G   54G   3% /
none                         4.0K     0  4.0K   0% /sys/fs/cgroup
udev                         989M  4.0K  989M   1% /dev
tmpfs                        200M  572K  200M   1% /run
none                         5.0M     0  5.0M   0% /run/lock
none                        1000M     0 1000M   0% /run/shm
none                         100M     0  100M   0% /run/user
/dev/sda1                    236M   37M  188M  17% /boot

#删除LV
root@TESTLVM:~# lvremove /dev/VolGroup00/lvdate
Do you really want to remove and DISCARD active logical volume lvdate? [y/n]: y
Logical volume "lvdate" successfully removed

#删除VG
root@TESTLVM:~# vgremove VolGroup00
Volume group "VolGroup00" successfully removed

#删除pv
root@TESTLVM:~# pvremove /dev/sdb1
Labels on physical volume "/dev/sdb1" successfully wiped
root@TESTLVM:~# pvremove /dev/sdc1
Labels on physical volume "/dev/sdc1" successfully wiped

补充:

当分区是出现以下错误时,需要将内核分区表更新

Command (m for help): w

The partition table has been altered.

Calling ioctl() to re-read partition table.

Re-reading the partition table failed.: Device or resource busy

The kernel still uses the old table. The new table will be used at the next reboot or after you run partprobe(8) or kpartx(8).

更新内核分区表命令

partprobe
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息