方便扩容、缩容磁盘空间,文件系统发生损坏数据找不到,恢复系统比较麻烦
创建物理分区--物理卷--卷组--划分逻辑卷--格式化--挂载--使用
准备磁盘分区:
·1 fdisk /dev/sdb
·2 n 创建三个新分区,分别为1G
·3 t 改变分区类型为8e
[root@localhost ~]# fdisk /dev/sdb 欢迎使用 fdisk (util-linux 2.23.2)。 更改将停留在内存中,直到您决定将更改写入磁盘。 使用写入命令前请三思。 Device does not contain a recognized partition table 使用磁盘标识符 0xfc57b572 创建新的 DOS 磁盘标签。 命令(输入 m 获取帮助):n Partition type: p primary (0 primary, 0 extended, 4 free) e extended Select (default p): p 分区号 (1-4,默认 1): 起始 扇区 (2048-20971519,默认为 2048): 将使用默认值 2048 Last 扇区, +扇区 or +size{K,M,G} (2048-20971519,默认为 20971519):+1G 分区 1 已设置为 Linux 类型,大小设为 1 GiB 命令(输入 m 获取帮助):p 磁盘 /dev/sdb:10.7 GB, 10737418240 字节,20971520 个扇区 Units = 扇区 of 1 * 512 = 512 bytes 扇区大小(逻辑/物理):512 字节 / 512 字节 I/O 大小(最小/最佳):512 字节 / 512 字节 磁盘标签类型:dos 磁盘标识符:0xfc57b572 设备 Boot Start End Blocks Id System /dev/sdb1 2048 2099199 1048576 83 Linux 命令(输入 m 获取帮助):t## t改变分区格式 已选择分区 1 Hex 代码(输入 L 列出所有代码):8e 已将分区“Linux”的类型更改为“Linux LVM” 命令(输入 m 获取帮助):n Partition type: p primary (1 primary, 0 extended, 3 free) e extended Select (default p): p 分区号 (2-4,默认 2): 起始 扇区 (2099200-20971519,默认为 2099200): 将使用默认值 2099200 Last 扇区, +扇区 or +size{K,M,G} (2099200-20971519,默认为 20971519):+1G 分区 2 已设置为 Linux 类型,大小设为 1 GiB 命令(输入 m 获取帮助):t 分区号 (1,2,默认 2): Hex 代码(输入 L 列出所有代码):8e 已将分区“Linux”的类型更改为“Linux LVM” 命令(输入 m 获取帮助):n Partition type: p primary (2 primary, 0 extended, 2 free) e extended Select (default p): p 分区号 (3,4,默认 3): 起始 扇区 (4196352-20971519,默认为 4196352): 将使用默认值 4196352 Last 扇区, +扇区 or +size{K,M,G} (4196352-20971519,默认为 20971519):+1G 分区 3 已设置为 Linux 类型,大小设为 1 GiB 命令(输入 m 获取帮助):t 分区号 (1-3,默认 3): Hex 代码(输入 L 列出所有代码):8e 已将分区“Linux”的类型更改为“Linux LVM” 命令(输入 m 获取帮助):p 磁盘 /dev/sdb:10.7 GB, 10737418240 字节,20971520 个扇区 Units = 扇区 of 1 * 512 = 512 bytes 扇区大小(逻辑/物理):512 字节 / 512 字节 I/O 大小(最小/最佳):512 字节 / 512 字节 磁盘标签类型:dos 磁盘标识符:0xfc57b572 设备 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 命令(输入 m 获取帮助):w The partition table has been altered! Calling ioctl() to re-read partition table. WARNING: Re-reading the partition table failed with error 16: 设备或资源忙. 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) 正在同步磁盘。 [root@localhost ~]# fdisk -l 磁盘 /dev/sda:21.5 GB, 21474836480 字节,41943040 个扇区 Units = 扇区 of 1 * 512 = 512 bytes 扇区大小(逻辑/物理):512 字节 / 512 字节 I/O 大小(最小/最佳):512 字节 / 512 字节 磁盘标签类型:dos 磁盘标识符:0x0007f9b9 设备 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 磁盘 /dev/sdb:10.7 GB, 10737418240 字节,20971520 个扇区 Units = 扇区 of 1 * 512 = 512 bytes 扇区大小(逻辑/物理):512 字节 / 512 字节 I/O 大小(最小/最佳):512 字节 / 512 字节 磁盘标签类型:dos 磁盘标识符:0xfc57b572 设备 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
·pvcreate /dev/sdb2
·pvcreate /dev/sdb3
·pvdisplay 列出当前的物理卷
·pvremove /dev/sdb3 删除物理卷
pvcreate命令需要安装lvm2来实现。如果不知道某个命令所需要的软件包,则可以使用yum provides '/*/pvcreate'来查找,在结果中可以看到具体的名称
之后安装lvm2包:
[root@localhost ~]# yum install -y lvm2 过程省略…… 已安装: lvm2.x86_64 7:2.02.171-8.el7 作为依赖被安装: device-mapper-event.x86_64 7:1.02.140-8.el7 device-mapper-event-libs.x86_64 7:1.02.140-8.el7 device-mapper-persistent-data.x86_64 0:0.7.0-0.1.rc6.el7 libaio.x86_64 0:0.3.109-13.el7 lvm2-libs.x86_64 7:2.02.171-8.el7 完毕! [root@localhost ~]# pvcreate /dev/sdb1 Device /dev/sdb1 not found (or ignored by filtering). [root@localhost ~]# partprobe ##如果无法使用,则可以执行partprob命令 [root@localhost ~]# ls /dev/sdb1 /dev/sdb1 [root@localhost ~]# umount /mnt ##需要先卸载磁盘,否则会报错 [root@localhost ~]# !pv pvcreate /dev/sdb1 WARNING: xfs signature detected on /dev/sdb1 at offset 0. Wipe it? [y/n]:y Wiping xfs signature on /dev/sdb2. WARNING: xfs_external_log signature detected on /dev/sdb2 at offset 16384. Wipe it? [y/n]: y Wiping xfs_external_log signature on /dev/sdb2. WARNING: xfs_external_log signature detected on /dev/sdb2 at offset 17408. Wipe it? [y/n]: y Wiping xfs_external_log signature on /dev/sdb2. WARNING: xfs_external_log signature detected on /dev/sdb2 at offset 18432. Wipe it? [y/n]: y Wiping xfs_external_log signature on /dev/sdb2. Physical volume "/dev/sdb2" successfully created. [root@localhost ~]# pvcreate /dev/sdb3 WARNING: xfs signature detected on /dev/sdb3 at offset 0. Wipe it? [y/n]: y Wiping xfs signature on /dev/sdb3. Physical volume "/dev/sdb3" successfully created. [root@localhost ~]# pvdisplay "/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 E3iCXq-nh8L-fOrY-XHhg-0Lv3-FjRF-M6rDrS "/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 c3nT8H-DNhT-jSwu-YRkF-rAQr-RpDL-IEwlE9 "/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 qwWtVZ-48nE-iShp-ZvGB-qEpm-Buih-cE03Oz
扩展:http://man.linuxde.net/partprobe
·除了用pvdisplay查看物理卷以外,还可以用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
·创建卷组:vgcreate 卷组名 物理卷1名称 物理卷2名称
[root@localhost ~]# vgcreate vg1 /dev/sdb1 /dev/sdb2 Volume group "vg1" successfully created
·查看卷组信息:vgdisplay 或者 vgs
[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 1hdLGe-O8B0-wSvV-sFNe-KQDd-OY1M-0w1FxZ [root@localhost ~]# vgs VG #PV #LV #SN Attr VSize VFree vg1 2 0 0 wz--n- 1.99g 1.99g
·删除卷组:vgremove
[root@localhost ~]# vgremove vg1 Volume group "vg1" successfully removed
·创建逻辑卷:lvcreate
-L 指定大小
-n 指定名称
[root@localhost ~]# lvcreate -L 100M -n lv1 vg1 Logical volume "lv1" created.
·格式化逻辑卷 (格式化成xfs格式,扩容缩容操作会有影响):
[root@localhost ~]# mkfs.ext4 /dev/vg1/lv1 mke2fs 1.42.9 (28-Dec-2013) 文件系统标签= OS type: Linux 块大小=1024 (log=0) 分块大小=1024 (log=0) Stride=0 blocks, Stripe width=0 blocks 25688 inodes, 102400 blocks 5120 blocks (5.00%) reserved for the super user 第一个数据块=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: 完成 正在写入inode表: 完成 Creating journal (4096 blocks): 完成 Writing superblocks and filesystem accounting information: 完成
·挂载逻辑卷:
[root@localhost ~]# mount /dev/vg1/lv1 /mnt [root@localhost ~]# df -h 文件系统 容量 已用 可用 已用% 挂载点 /dev/sda3 18G 1.1G 17G 6% / devtmpfs 479M 0 479M 0% /dev tmpfs 489M 0 489M 0% /dev/shm tmpfs 489M 6.7M 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 12月 31 17:22 /dev/vg1/lv1 -> ../dm-0 [root@localhost ~]# ll /dev/mapper/vg1-lv1 lrwxrwxrwx. 1 root root 7 12月 31 17:22 /dev/mapper/vg1-lv1 -> ../dm-0
(名称虽然不一样,其实指向同一个文件)
扩容逻辑卷:
重新设置卷大小 lvresize -L 300M /dev/vg1/lv1
检查磁盘错误(ext4执行)e2fsck -f /dev/vg1/lv1
更新逻辑卷信息(ext4执行)resize2fs /dev/vg1/lv1
xfs文件系统需要执行 xfs_growfs /dev/vg1/lv1
·重新设置卷大小 lvresize -L 300M /dev/vg1/lv1
-L 重新制定新的大小
[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 ~]# lvs LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert lv1 vg1 -wi-a----- 200.00m
·检查磁盘错误(ext4执行)e2fsck -f /dev/vg1/lv1
[root@localhost ~]# e2fsck -f /dev/vg1/lv1 e2fsck 1.42.9 (28-Dec-2013) 第一步: 检查inode,块,和大小 第二步: 检查目录结构 第3步: 检查目录连接性 Pass 4: Checking reference counts 第5步: 检查簇概要信息 /dev/vg1/lv1: 11/25688 files (9.1% non-contiguous), 8896/102400 blocks
·更新逻辑卷信息(ext4执行)resize2fs /dev/vg1/lv1
[root@localhost ~]# e2fsck -f /dev/vg1/lv1 ##如果不更新信息,挂载的时候讲不会识别新的大小 e2fsck 1.42.9 (28-Dec-2013) 第一步: 检查inode,块,和大小 第二步: 检查目录结构 第3步: 检查目录连接性 Pass 4: Checking reference counts 第5步: 检查簇概要信息 /dev/vg1/lv1: 11/25688 files (9.1% non-contiguous), 8896/102400 blocks [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 ~]# mount /dev/vg1/lv1 /mnt [root@localhost ~]# df -h 文件系统 容量 已用 可用 已用% 挂载点 /dev/sda3 18G 1.1G 17G 6% / devtmpfs 479M 0 479M 0% /dev tmpfs 489M 0 489M 0% /dev/shm tmpfs 489M 6.7M 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
先umount
检查磁盘错误(ext)e2fsck -f /dev/vg1/lv1
更新逻辑卷信息(ext)resize2fs /dev/vg1/lv1 100M
重新设置卷大小 lvresize -L 100M /dev/vg1/lv1
[root@localhost ~]# umount /dev/vg1/lv1 [root@localhost ~]# e2fsck -f /dev/vg1/lv1 e2fsck 1.42.9 (28-Dec-2013) 第一步: 检查inode,块,和大小 第二步: 检查目录结构 第3步: 检查目录连接性 Pass 4: Checking reference counts 第5步: 检查簇概要信息 /dev/vg1/lv1: 11/49400 files (9.1% non-contiguous), 11884/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 100M /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 ~]# !mount mount /dev/vg1/lv1 /mnt
(缩容的空间需要比内部存储的文件总大小要大,不然会出错)
·xfs如何扩容:
[root@localhost ~]# umount /mnt [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 ~]# mount /dev/vg1/lv1 /mnt [root@localhost ~]# touch /mnt/122.txt [root@localhost ~]# echo "aaa" > !$ ##在分区中创建文件,并在文件中写入数据 echo "aaa" > /mnt/122.txt [root@localhost ~]# lvs LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert lv1 vg1 -wi-ao---- 100.00m [root@localhost ~]# lvresize -L 300M /dev/vg1/lv1 ##扩容 操作xfs时无需卸载 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 ~]# lvs LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert lv1 vg1 -wi-ao---- 300.00m [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 文件系统 容量 已用 可用 已用% 挂载点 /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
·扩展卷组:(如果有剩余空间,可以之家扩展,之前只是sdb1和sdb1组成卷组)
[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 ~]# lvresize -L 333M /dev/vg1/lv1 ##大小必须必之前大,也就是大于300M Rounding size to boundary between physical extents: 336.00 MiB. Size of logical volume vg1/lv1 changed from 200.00 MiB (50 extents) to 336.00 MiB (84 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-ao---- 336.00m [root@localhost ~]# xfs_growfs /dev/vg1/lv1 meta-data=/dev/mapper/vg1-lv1 isize=512 agcount=14, agsize=6400 blks = sectsz=512 attr=2, projid32bit=1 = crc=1 finobt=0 spinodes=0 data = bsize=4096 blocks=86016, 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 [root@localhost ~]# df -h 文件系统 容量 已用 可用 已用% 挂载点 /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 333M 5.5M 328M 2% /mnt
4.13 磁盘故障小案例
重启进入不了系统,出现以下状况
一般都是磁盘挂载出现问题
输入root密码,进入 /etc/fstab 下,把编辑的行删除
保存退出,重启系统
问题解决
原文:http://blog.51cto.com/11530642/2056803