四周第一次课
4.10/4.11/4.12 lvm讲解
4.13 磁盘故障小案例
4.10/4.11/4.12 lvm讲解
- lvm的优缺点
- 优势:很方便的扩容和缩容磁盘空间
- 局限性:,磁盘发生损坏,不易于恢复
操作流程 : 磁盘分区–>创建物理卷–>划分为卷组–>划分成逻辑卷–>格式化、挂载–>扩容。
一,创建磁盘分区
- fdisk /dev/sdb
- n 创建三个新分区,分别是1G
- t 改变分区的类型为8e
[root@localhost ~]# fdisk /dev/sdb
Welcome to fdisk (util-linux 2.23.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Device does not contain a recognized partition table
Building a new DOS disklabel with disk identifier 0x28933be0.
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):
First sector (2048-20971519, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-20971519, default 20971519): +1G
Partition 1 of type Linux and of size 1 GiB is set
Command (m for help): t
Selected partition 1
Hex code (type L to list all codes): 8e
Changed type of partition 'Linux' to 'Linux LVM'
Command (m for help): n
Partition type:
p primary (1 primary, 0 extended, 3 free)
e extended
Select (default p): p
Partition number (2-4, default 2):
First sector (2099200-20971519, default 2099200):
Using default value 2099200
Last sector, +sectors or +size{K,M,G} (2099200-20971519, default 20971519): +1G
Partition 2 of type Linux and of size 1 GiB is set
Command (m for help): t
Partition number (1,2, default 2): 2
Hex code (type L to list all codes): 8e
Changed type of partition 'Linux' to 'Linux LVM'
Command (m for help): n
Partition type:
p primary (2 primary, 0 extended, 2 free)
e extended
Select (default p): p
Partition number (3,4, default 3):
First sector (4196352-20971519, default 4196352):
Using default value 4196352
Last sector, +sectors or +size{K,M,G} (4196352-20971519, default 20971519): +1G
Partition 3 of type Linux and of size 1 GiB is set
Command (m for help): t
Partition number (1-3, default 3):
Hex code (type L to list all codes): 8e
Changed type of partition 'Linux' to 'Linux LVM'
Command (m for help): p
Disk /dev/sdb: 10.7 GB, 10737418240 bytes, 20971520 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x28933be0
Device Boot Start End Blocks Id System
/dev/sdb1 2048 2099199 1048576 8e Linux LVM
/dev/sdb2 2099200 4196351 1048576 8e Linux LVM
/dev/sdb3 4196352 6293503 1048576 8e Linux LVM
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
[root@localhost ~]# fdisk -l
Disk /dev/sda: 21.5 GB, 21474836480 bytes, 41943040 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x000da311
Device Boot Start End Blocks Id System
/dev/sda1 * 2048 411647 204800 83 Linux
/dev/sda2 411648 4605951 2097152 82 Linux swap / Solaris
/dev/sda3 4605952 41943039 18668544 83 Linux
Disk /dev/sdb: 10.7 GB, 10737418240 bytes, 20971520 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x28933be0
Device Boot Start End Blocks Id System
/dev/sdb1 2048 2099199 1048576 8e Linux LVM
/dev/sdb2 2099200 4196351 1048576 8e Linux LVM
/dev/sdb3 4196352 6293503 1048576 8e Linux LVM
二,创建物理卷
- 创建物理卷 : pvcreate /dev/sdb1
- 列出当期的物理卷 pvdisplay
- 删除物理卷 pvremove /dev/sdb2
注意: 一般情况在创建完分区后系统会自动生成相应的分区文件“/dev/sdbn”,但是当系统未自动生成时无法完成物理卷创建任务,需要执行命令 “partprobe” 生成相应文件再执行pvcreate命令。
[root@localhost ~]# partprobe
[root@localhost ~]# ls /dev/sdb1
/dev/sdb1
[root@localhost ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda3 18G 1.1G 17G 6% /
devtmpfs 479M 0 479M 0% /dev
tmpfs 489M 0 489M 0% /dev/shm
tmpfs 489M 6.8M 482M 2% /run
tmpfs 489M 0 489M 0% /sys/fs/cgroup
/dev/sda1 197M 97M 100M 50% /boot
tmpfs 98M 0 98M 0% /run/user/0
[root@localhost ~]# pvcreate /dev/sdb1
-bash: pvcreate: command not found
[root@localhost ~]# yum install -y lvm2
[root@localhost ~]# pvcreate /dev/sdb1 //创建物理卷/dev/sdb1
WARNING: ext4 signature detected on /dev/sdb1 at offset 1080. Wipe it? [y/n]: y
Wiping ext4 signature on /dev/sdb1.
Physical volume "/dev/sdb1" successfully created.
[root@localhost ~]# pvcreate /dev/sdb2 //创建物理卷/dev/sdb2
Physical volume "/dev/sdb2" successfully created.
[root@localhost ~]# pvcreate /dev/sdb3 //创建物理卷/dev/sdb3
Physical volume "/dev/sdb3" successfully created.
[root@localhost ~]# pvdisplay //创建完物理卷之后,使用pvdisplay查看。
"/dev/sdb1" is a new physical volume of "1.00 GiB"
--- NEW Physical volume ---
PV Name /dev/sdb1
VG Name
PV Size 1.00 GiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID N0ju7i-G2dm-Ojmz-imO2-cEPg-dn7Q-zM1zUY
"/dev/sdb3" is a new physical volume of "1.00 GiB"
--- NEW Physical volume ---
PV Name /dev/sdb3
VG Name
PV Size 1.00 GiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID Q3Eg0P-SJz9-o8uo-JkkV-vJOQ-sNbI-lcimXF
"/dev/sdb2" is a new physical volume of "1.00 GiB"
--- NEW Physical volume ---
PV Name /dev/sdb2
VG Name
PV Size 1.00 GiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID YcmEJ7-keEl-wsX2-SM1p-QxlT-0JMn-LNxeZp
使用 pvs 查看物理卷
[root@localhost ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/sdb1 lvm2 --- 1.00g 1.00g
/dev/sdb2 lvm2 --- 1.00g 1.00g
/dev/sdb3 lvm2 --- 1.00g 1.00g
[root@localhost ~]#
三,创建卷组
-
命令语法
vgcreate(选项)(参数) -
命令描述
vgcreate命令用于创建LVM卷组。卷组(Volume Group)将多个物理卷组织成一个整体,屏蔽了底层物理卷细节。在卷组上创建逻辑卷时不用考虑具体的物理卷信息。 -
命令选项
-l:卷组上允许创建的最大逻辑卷数; -p:卷组中允许添加的最大物理卷数; -s:卷组上的物理卷的PE大小。
[root@localhost ~]# vgcreate vg1 /dev/sdb1 /dev/sdb2 //创建卷组
Volume group "vg1" successfully created
[root@localhost ~]# vgdisplay //查看卷组详细信息
--- Volume group ---
VG Name vg1
System ID
Format lvm2
Metadata Areas 2
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 2
Act PV 2
VG Size 1.99 GiB
PE Size 4.00 MiB
Total PE 510
Alloc PE / Size 0 / 0
Free PE / Size 510 / 1.99 GiB
VG UUID 7oe5bv-FmML-72GF-PY6b-titg-l3FQ-2Mr0wQ
[root@localhost ~]# vgs //查看卷组
VG #PV #LV #SN Attr VSize VFree
vg1 2 0 0 wz--n- 1.99g 1.99g
[root@localhost ~]#
四,创建逻辑卷
-
命令语法
lvcreate(选项)(参数) -
命令描述
lvcreate命令用于创建LVM的逻辑卷。逻辑卷是创建在卷组之上的。逻辑卷对应的设备文件保存在卷组目录下,例如:在卷组”vg1000”上创建一个逻辑卷”lvol0”,则此逻辑卷对应的设备文件为”/dev/vg1000/lvol0”。 -
命令选项
-L:指定逻辑卷的大小,单位为“kKmMgGtT”字节; -l:指定逻辑卷的大小(LE数)。
[root@localhost ~]# lvcreate -L 100M -n lv1 vg1
Logical volume "lv1" created.
[root@localhost ~]#
五,格式化和挂载
[root@localhost ~]# mkfs.ext4 /dev/vg1/lv1 //格式化
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
Stride=0 blocks, Stripe width=0 blocks
25688 inodes, 102400 blocks
5120 blocks (5.00%) reserved for the super user
First data block=1
Maximum filesystem blocks=33685504
13 block groups
8192 blocks per group, 8192 fragments per group
1976 inodes per group
Superblock backups stored on blocks:
8193, 24577, 40961, 57345, 73729
Allocating group tables: done
Writing inode tables: done
Creating journal (4096 blocks): done
Writing superblocks and filesystem accounting information: done
[root@localhost ~]# mount /dev/vg1/lv1 /mnt/ //挂载
[root@localhost ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda3 18G 1.1G 17G 6% /
devtmpfs 479M 0 479M 0% /dev
tmpfs 489M 0 489M 0% /dev/shm
tmpfs 489M 6.8M 482M 2% /run
tmpfs 489M 0 489M 0% /sys/fs/cgroup
/dev/sda1 197M 97M 100M 50% /boot
tmpfs 98M 0 98M 0% /run/user/0
/dev/mapper/vg1-lv1 93M 1.6M 85M 2% /mnt //名字发生了变化
[root@localhost ~]# ll /dev/vg1/lv1
lrwxrwxrwx. 1 root root 7 Feb 3 09:03 /dev/vg1/lv1 -> ../dm-0
[root@localhost ~]# ll /dev/mapper/vg1-lv1
lrwxrwxrwx. 1 root root 7 Feb 3 09:03 /dev/mapper/vg1-lv1 -> ../dm-0
[root@localhost ~]#
六,扩容逻辑卷
流程:更改逻辑卷信息(lvresize)–>检测磁盘错误(e2fsck -f)–>更新逻辑卷信息(resize2fs)
- 先在/mnt/目录下写入一些东西,做实验用
[root@localhost ~]# mkdir /mnt/1212
[root@localhost ~]# touch /mnt/23.txt
[root@localhost ~]# echo "1111112221121" > /mnt/23.txt
[root@localhost ~]#
-
命令语法
lvresize(选项)(参数) -
命令描述
lvresize命令用于调整LVM逻辑卷的空间大小,可以增大空间和缩小空间。使用lvresize命令增大逻辑卷空间大小和缩小空间时需要谨慎,因为它有可能导致数据丢失。 -
命令选项
-L:指定逻辑卷的大小,单位为“kKmMgGtT”字节;
-l:指定逻辑卷的大小(LE数)。 -
增大逻辑卷磁盘大小
[root@localhost ~]# umount /mnt/
[root@localhost ~]# lvresize -L 200M /dev/vg1/lv1
Size of logical volume vg1/lv1 changed from 100.00 MiB (25 extents) to 200.00 MiB (50 extents).
Logical volume vg1/lv1 successfully resized.
[root@localhost ~]#
- 检测磁盘错误
[root@localhost ~]# e2fsck -f !$
e2fsck -f /dev/vg1/lv1
e2fsck 1.42.9 (28-Dec-2013)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
/dev/vg1/lv1: 13/25688 files (7.7% non-contiguous), 8899/102400 blocks
[root@localhost ~]#
- 更新逻辑卷的信息
[root@localhost ~]# resize2fs /dev/vg1/lv1
resize2fs 1.42.9 (28-Dec-2013)
Resizing the filesystem on /dev/vg1/lv1 to 204800 (1k) blocks.
The filesystem on /dev/vg1/lv1 is now 204800 blocks long.
[root@localhost ~]#
- 更新逻辑卷信息后新分区在挂载的时候才能被识别新的大小。
挂载:
[root@localhost ~]# !mount
mount /dev/vg1/lv1 /mnt/
[root@localhost ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda3 18G 1.1G 17G 6% /
devtmpfs 479M 0 479M 0% /dev
tmpfs 489M 0 489M 0% /dev/shm
tmpfs 489M 6.8M 482M 2% /run
tmpfs 489M 0 489M 0% /sys/fs/cgroup
/dev/sda1 197M 97M 100M 50% /boot
tmpfs 98M 0 98M 0% /run/user/0
/dev/mapper/vg1-lv1 190M 1.6M 175M 1% /mnt
[root@localhost ~]#
扩容后变成190M
- 系统扩容不会使文件丢失
[root@localhost ~]# ls /mnt/
1212 23.txt lost+found
[root@localhost ~]#
流程:卸载(umount)–>检测(e2fsck -f)–>更新逻辑卷信息(大小)(resize2fs)–>重置逻辑卷大小(lvresize)
[root@localhost ~]# umount /mnt/
[root@localhost ~]# e2fsck -f /dev/vg1/lv1
e2fsck 1.42.9 (28-Dec-2013)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
/dev/vg1/lv1: 13/49400 files (7.7% non-contiguous), 11887/204800 blocks
[root@localhost ~]# resize2fs /dev/vg1/lv1 100M
resize2fs 1.42.9 (28-Dec-2013)
Resizing the filesystem on /dev/vg1/lv1 to 102400 (1k) blocks.
The filesystem on /dev/vg1/lv1 is now 102400 blocks long.
[root@localhost ~]# lvresize -L 100 /dev/vg1/lv1 //重置逻辑卷大小
WARNING: Reducing active logical volume to 100.00 MiB.
THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce vg1/lv1? [y/n]: y
Size of logical volume vg1/lv1 changed from 200.00 MiB (50 extents) to 100.00 MiB (25 extents).
Logical volume vg1/lv1 successfully resized.
[root@localhost ~]# lvs //查看大小
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
lv1 vg1 -wi-a----- 100.00m
[root@localhost ~]# ls /mnt/
1212 23.txt lost+found
[root@localhost ~]#
即,合理缩容也不会使系统内文件丢失。
格式化&挂载
格式化成xfs格式
[root@localhost ~]# mkfs.xfs -f /dev/vg1/lv1
meta-data=/dev/vg1/lv1 isize=512 agcount=4, agsize=6400 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0, sparse=0
data = bsize=4096 blocks=25600, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
log =internal log bsize=4096 blocks=855, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
[root@localhost ~]#
扩容
**流程:更改逻辑卷信息(lvresize)–>更新逻辑卷信息(xfs_growfs)
更改逻辑卷信息(lvresize)**
[root@localhost ~]# !mount
mount /dev/vg1/lv1 /mnt/
[root@localhost ~]# lvresize -L 300M /dev/vg1/lv1
Size of logical volume vg1/lv1 changed from 100.00 MiB (25 extents) to 300.00 MiB (75 extents).
Logical volume vg1/lv1 successfully resized.
[root@localhost ~]# xfs_growfs /dev/vg1/lv1
meta-data=/dev/mapper/vg1-lv1 isize=512 agcount=4, agsize=6400 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0 spinodes=0
data = bsize=4096 blocks=25600, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
log =internal bsize=4096 blocks=855, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
data blocks changed from 25600 to 76800
[root@localhost ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda3 18G 1.1G 17G 6% /
devtmpfs 479M 0 479M 0% /dev
tmpfs 489M 0 489M 0% /dev/shm
tmpfs 489M 6.8M 482M 2% /run
tmpfs 489M 0 489M 0% /sys/fs/cgroup
/dev/sda1 197M 97M 100M 50% /boot
tmpfs 98M 0 98M 0% /run/user/0
/dev/mapper/vg1-lv1 297M 5.5M 292M 2% /mnt
[root@localhost ~]#
扩容卷组
-
将/dev/sdb3增加到vg1
-
命令语法
vgextend [卷组名] [物理卷]
[root@localhost ~]# vgextend vg1 /dev/sdb3
Volume group "vg1" successfully extended
[root@localhost ~]# vgs
VG #PV #LV #SN Attr VSize VFree
vg1 3 1 0 wz--n- <2.99g <2.70g
[root@localhost ~]#
4.13 磁盘故障小案例
- 在做实验的时候,写入一行配置到/etc/fstab中去,在做完 lvm实验之后,reboot重启之后,会发现进入不了系统(如下图类似的界面)
- 本来应该是显示中文,但是在vm终端下,中文不支持,所以看到几个小方块,就是让我们输入root用户密码,
- 在生产环境出现类似问题,往往是磁盘挂载出现问题,不能正常挂载到挂载点
- 输入root密码,就可进入界面(和正常登录系统类似)
- 进入vi /etc/fstab中删除/dev/sdb这一行(快捷键dd),然后保存退出
- 再次重启,会恢复正常
友情链接:阿铭Linux