龙空技术网

新增和扩展硬盘(基于LVM)

Wins5188 193

前言:

此时各位老铁们对“centos repair filesystem”大致比较关注,看官们都想要知道一些“centos repair filesystem”的相关文章。那么小编也在网络上搜集了一些对于“centos repair filesystem””的相关文章,希望我们能喜欢,同学们快快来学习一下吧!

【适用于Linux/Ubuntu】

安装扩充硬盘,如果是独立硬盘,可以用LVM逻辑分区进行管理分配,便于后期扩展扩充。

注意!在安装系统时,若未使用LVM分区,系统默认不安装lvm2工具,本次操作基于lvm2工具进行,需要提前准备安装:# apt install lvm2 -y

检查磁盘

1、查看当前磁盘

# fdisk -l

Disk /dev/loop0: 55.45 MiB, 58130432 bytes, 113536 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 /dev/loop1: 70.32 MiB, 73728000 bytes, 144000 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 /dev/loop2: 32.3 MiB, 33865728 bytes, 66144 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 /dev/sdb: 100 GiB, 107374182400 bytes, 209715200 sectors #第二块硬盘

Disk model: VMware Virtual S

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 /dev/sda: 100 GiB, 107374182400 bytes, 209715200 sectors #第一块硬盘

Disk model: VMware Virtual S

Units: sectors of 1 * 512 = 512 bytes

Sector size (logical/physical): 512 bytes / 512 bytes

I/O size (minimum/optimal): 512 bytes / 512 bytes

Disklabel type: gpt

Disk identifier: E0AAAE47-980E-426F-872D-10C00817C200

Device Start End Sectors Size Type

/dev/sda1 2048 4095 2048 1M BIOS boot

/dev/sda2 4096 2101247 2097152 1G Linux filesystem

/dev/sda3 2101248 209713151 207611904 99G Linux filesystem

#文件系统挂载分区

Disk /dev/mapper/ubuntu--vg-ubuntu--lv: 49.51 GiB, 53150220288 bytes, 103809024 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

/dev/sda: 100 GiB为当前正在使用的第一块磁盘;

/dev/loop*为当前分区信息;

/dev/sdb: 100 GiB为新添加的第二块磁盘;

2、查看磁盘分区

# df -h

Filesystem Size Used Avail Use% Mounted on

udev 936M 0 936M 0% /dev

tmpfs 196M 1.3M 195M 1% /run

/dev/mapper/ubuntu--vg-ubuntu--lv 49G 7.2G 39G 16% /

tmpfs 980M 0 980M 0% /dev/shm

tmpfs 5.0M 0 5.0M 0% /run/lock

tmpfs 980M 0 980M 0% /sys/fs/cgroup

/dev/loop0 56M 56M 0 100% /snap/core18/2128

/dev/loop1 71M 71M 0 100% /snap/lxd/21029

/dev/loop2 33M 33M 0 100% /snap/snapd/12704

/dev/sda2 976M 107M 803M 12% /boot

tmpfs 196M 0 196M 0% /run/user/1000

删除分区

注意!如果硬盘是全新的,可跳过【删除分区】的步骤,按照【创建PV】操作;

1)根据“磁盘分区”的检查结果,确保sdb的分区未被挂载;

2)如果已挂载需先umount,然后在/etc/fstab里删除相关配置;

3)取消sdb挂载:# vi /etc/fstab 后删除对应的条目;

4)若删除挂载并重启后,出现如下信息:

*** An error occurred during the file system check.

*** Dropping you to a shell; the system will reboot

Give root password for maintenance

(or type Control D to continue):

5)在修复模式下(Repair filesystem)文件是被保护的,不能被修改 。因此编辑 # vi /etc/fstab 后并不能保存;

6)可以运行 # mount -o remount, rw /,把系统文件权限修改为可读写(rw),然后编辑/etc/fstab并删除挂载点信息,重启服务器即可解决问题;

7)进入sdb的磁盘编辑界面:

注意!若不熟悉磁盘管理命令,可输入“m或help”查看命令说明;

# sudo fdisk /dev/sdb

8)输入“d”,若单个磁盘下存在多个分区存在时,会提示选择删除的分区(也会有默认选项),根据实际清空就连续输入“d+Enter”即可,直至提示已无分区可删,最后输入“w”,确认执行操作并退出;

9)至此,磁盘格式化重置完毕,可以用 # sudo fdisk -l 命令再次查看,应该与新盘一致。

创建PV

PV(physical volume物理卷),将硬盘初始化为物理卷(若有多块硬盘依次执行),以便LVM使用;

注意!无论是新增磁盘、扩展磁盘,都必须先创建物理卷;

1、创建物理卷

# pvcreate /dev/sdb

Physical volume "/dev/sdb" successfully created.

注意!出现如下报错信息,则选择指定硬盘,按照【删除分区】操作;

Device /dev/sdb'excluded by a filter

删除PV:# pvremove /dev/sdd

验证PV是否删除:# pvscan

检查物理卷是否创建成功,并检查相应格式配置;

# pvs

PV VG Fmt Attr PSize PFree

/dev/sda3 ubuntu-vg lvm2 a-- <99.00g 0 #已有的物理卷信息

/dev/sdb ubuntu-vg1 lvm2 a-- <20.00g 0 #已有的物理卷信息

/dev/sdc lvm2 --- 20.00g 20.00g

/dev/sdd ubuntu-vg1 lvm2 a-- <20.00g 0 #已有的物理卷信息,并已扩展到ubuntu-vg1卷组中

2、显示物理卷信息

# pvdisplay

--- Physical volume ---

PV Name /dev/sda3 #原有的PV(物理卷)

VG Name ubuntu-vg

PV Size <99.00 GiB / not usable 0

Allocatable yes (but full)

PE Size 4.00 MiB

Total PE 25343

Free PE 0

Allocated PE 25343

PV UUID Cyjjiz-pqqq-L73R-yvG9-G1IN-Em47-MDTwvc

"/dev/sdb" is a new physical volume of "100.00 GiB"

--- NEW Physical volume ---

PV Name /dev/sdb #新增的PV(物理卷)

VG Name

PV Size 100.00 GiB

Allocatable NO

PE Size 0

Total PE 0

Free PE 0

Allocated PE 0

PV UUID E3FC40-jFuJ-ddiK-GrNF-joKb-Nig6-N3gP4Q

创建VG

VG(volume group物理卷组),独立硬盘设置独立物理卷组(即可以设置单独VG),也可以将一组物理卷组收集为一个管理单元;

注意:若准备扩展磁盘,可以跳过本节第1点,参考本节第2点(扩展VG)

1、创建VG(独立硬盘设置独立物理卷组)

# vgcreate ubuntu-vg1 /dev/sdb #创建VG并设置VG名称为ubuntu-vg1

Volume group "ubuntu-vg1" successfully created

2、显示VG信息

# vgdisplay

--- Volume group ---

VG Name ubuntu-vg1 #新增VG

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 <100.00 GiB

PE Size 4.00 MiB

Total PE 25599

Alloc PE / Size 0 / 0

Free PE / Size 25599 / <100.00 GiB

VG UUID AACe19-HDWc-s8Zs-YrjX-29sO-WPAv-C2KmRM

--- Volume group ---

VG Name ubuntu-vg #原有VG

System ID

Format lvm2

Metadata Areas 1

Metadata Sequence No 3

VG Access read/write

VG Status resizable

MAX LV 0

Cur LV 1

Open LV 1

Max PV 0

Cur PV 1

Act PV 1

VG Size <99.00 GiB

PE Size 4.00 MiB

Total PE 25343

Alloc PE / Size 25343 / <99.00 GiB

Free PE / Size 0 / 0

VG UUID CV1yAL-cUhd-VwW8-GwOY-GFpG-gJ0l-4c83wA

2、扩展VG( 多块硬盘扩展为一个物理卷组)

注意!若有多块硬盘扩展VG时,可以将多个VG收集并做为一个管理单元。如:可以先使用sdb(第1块新增磁盘)作为管理单元起点,然后用sdc(第2块新增磁盘)扩展sdb。

说明:

#vgcreate [自定义vg名称] [硬盘设备]

#vgextend [被扩展vg名称] [硬盘设备]

在本次实例中只有1块新增磁盘,因此只设置单元起点,为以后再新增磁盘时进行扩展方便。

查看VG信息:# vgscan

查看卷组ubuntu-vg1包含的PV、LV信息:# vgdisplay -v ubuntu-vg1

将已建立PV物理卷的新增磁盘/dev/sdd扩展到卷组ubuntu-vg1上

# vgextend ubuntu-vg1 /dev/sdd

Volume group "ubuntu-vg1" successfully extended

查看ubuntu-vg1扩展后的信息

# vgdisplay

--- Volume group ---

VG Name ubuntu-vg1

System ID

Format lvm2

Metadata Areas 2

Metadata Sequence No 7

VG Access read/write

VG Status resizable

MAX LV 0

Cur LV 1

Open LV 1

Max PV 0

Cur PV 2

Act PV 2

VG Size 39.99 GiB

PE Size 4.00 MiB

Total PE 10238

Alloc PE / Size 4864 / 19.00 GiB

Free PE / Size 5374 / 20.99 GiB

VG UUID udxpQ4-UUtF-QTFi-kdCl-IbHZ-1l10-eiOsdO

--- Volume group ---

VG Name ubuntu-vg

System ID

Format lvm2

Metadata Areas 1

Metadata Sequence No 3

VG Access read/write

VG Status resizable

MAX LV 0

Cur LV 1

Open LV 1

Max PV 0

Cur PV 1

Act PV 1

VG Size <99.00 GiB

PE Size 4.00 MiB

Total PE 25343

Alloc PE / Size 25343 / <99.00 GiB

Free PE / Size 0 / 0

VG UUID Y9Hjve-MngI-9rbW-jLSC-wyRd-D1GQ-QN7ev0

3、删除VG

# vgremove ubuntu-vg2

Volume group "ubuntu-vg2" successfully removed

如果采用安全的方式删除卷组VG,则必须使用 # vgchange -a n ubuntu-vg2 关闭ubuntu-vg2,然后才可以删除它。

# vgchange -a n ubuntu-vg2

Volume group "ubuntu-vg2" not found

Cannot process volume group ubuntu-vg2

验证卷组VG是否已删除:

# vgscan

Found volume group "ubuntu-vg1" using metadata type lvm2

Found volume group "ubuntu-vg" using metadata type lvm2

4、合并VG(已扩展VG合并为一个管理单元)

给逻辑卷分区VG(ubuntu-vg1)增加39.99G

# lvextend -L +20.99G /dev/ubuntu-vg1/data

Rounding size to boundary between physical extents: 20.99 GiB.

Size of logical volume ubuntu-vg1/data changed from 19.00 GiB (4864 extents) to 39.99 GiB (10238 extents).

Logical volume ubuntu-vg1/data successfully resized.

重新计算/dev/ubuntu-vg1/data,使用扩展容量生效

# resize2fs /dev/ubuntu-vg1/data

resize2fs 1.45.5 (07-Jan-2020)

Filesystem at /dev/ubuntu-vg1/data is mounted on /data; on-line resizing required

old_desc_blocks = 3, new_desc_blocks = 5

The filesystem on /dev/ubuntu-vg1/data is now 10483712 (4k) blocks long.

查看/dev/ubuntu-vg1/data的新容量

# df -h

Filesystem Size Used Avail Use% Mounted on

udev 934M 0 934M 0% /dev

tmpfs 196M 1.3M 195M 1% /run

/dev/mapper/ubuntu--vg-ubuntu--lv 98G 13G 81G 14% /

tmpfs 978M 0 978M 0% /dev/shm

tmpfs 5.0M 0 5.0M 0% /run/lock

tmpfs 978M 0 978M 0% /sys/fs/cgroup

/dev/mapper/ubuntu--vg1-data 40G 52M 38G 1% /data

/dev/loop0 71M 71M 0 100% /snap/lxd/21029

/dev/loop1 56M 56M 0 100% /snap/core18/2128

/dev/loop2 33M 33M 0 100% /snap/snapd/12704

/dev/sda2 976M 223M 687M 25% /boot

tmpfs 196M 0 196M 0% /run/user/0

创建LV

LV(logical volume逻辑卷/分区),用来管理第4步创建物理卷组,并格式化分区;

1、创建LV(占满所有磁盘)

注意!一般情况下,不能创建完整容量(如:磁盘有50G,创建LV是50G,会出现创建不成功);需要空出一点空间(如:磁盘有50G,创建LV是49.9G,即可完成);

# lvcreate -L +99G -n data ubuntu-vg1

Logical volume "data" created.

查看逻辑卷状态

# lvdisplay

--- Logical volume ---

LV Path /dev/ubuntu-vg1/data

LV Name data

VG Name ubuntu-vg1

LV UUID 22q76v-A7Xo-cI3n-n5DC-T9mU-3N3y-YJQCbf

LV Write Access read/write

LV Creation host, time fds10, 2021-12-13 11:08:43 +0800

LV Status available

# open 0

LV Size 99.00 GiB

Current LE 25344

Segments 1

Allocation inherit

Read ahead sectors auto

- currently set to 256

Block device 253:1

查看物理卷组状态

root@fds10:~# vgdisplay

--- Volume group ---

VG Name ubuntu-vg1

System ID

Format lvm2

Metadata Areas 1

Metadata Sequence No 8

VG Access read/write

VG Status resizable

MAX LV 0

Cur LV 1

Open LV 0

Max PV 0

Cur PV 1

Act PV 1

VG Size <100.00 GiB

PE Size 4.00 MiB

Total PE 25599

Alloc PE / Size 25344 / 99.00 GiB

Free PE / Size 255 / 1020.00 MiB

VG UUID AACe19-HDWc-s8Zs-YrjX-29sO-WPAv-C2KmRM

2、格式化LV(创建LV后,需要格式化LV,才能正常使用)

【适用于Ubuntu】

# mkfs.ext4 /dev/ubuntu-vg1/data

mke2fs 1.45.5 (07-Jan-2020)

Creating filesystem with 25952256 4k blocks and 6488064 inodes

Filesystem UUID: d307f78f-51f2-4b6e-af36-efac5027f055

Superblock backups stored on blocks:

32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,

4096000, 7962624, 11239424, 20480000, 23887872

Allocating group tables: done

Writing inode tables: done

Creating journal (131072 blocks): done

Writing superblocks and filesystem accounting information: done

【适用于CentOS】

# lvcreate -L +499.99G -n data centos-vg

Rounding up size to full physical extent 499.99 GiB

Logical volume "data" created.

# mkfs.xfs /dev/centos-vg/data

meta-data=/dev/centos-vg/data isize=512 agcount=4, agsize=32767488 blks

= sectsz=512 attr=2, projid32bit=1

= crc=1 finobt=0, sparse=0

data = bsize=4096 blocks=131069952, imaxpct=25

= sunit=0 swidth=0 blks

naming =version 2 bsize=4096 ascii-ci=0 ftype=1

log =internal log bsize=4096 blocks=63999, version=2

= sectsz=512 sunit=0 blks, lazy-count=1

realtime =none

注意!如果是CentOS7,需要格式化成xfs文件系统,命令行是:# mkfs.xfs /dev/centos-vg1/data

同时,在测试中由于centos-vg1中带了一个 “-” ,在挂载重启后会变成 “--” 两个中横杠的情况。

3、卸载LV

要卸载对应卷组VG的所有逻辑卷LV,从上面的信息可以看出对应的物理卷组ubuntu-vg1只有/dev/ubuntu-vg1/data这个逻辑卷LV,因此用以下方法卸载(注意:卸载不等于删除)。

# umount /dev/ubuntu-vg1/data

4、移除LV

# lvremove /dev/ubuntu-vg1/data

Do you really want to remove and DISCARD active logical volume ubuntu-vg1/data? [y/n]: y

Logical volume "data" successfully removed

验证LV是否删除:# lvdisplay | grep "dev/ubuntu-vg2/data1"

设置开机自动挂载

设置开机自动挂载至指定目录“/data”

查询分区UUID:

# blkid

/dev/sr0: UUID="2021-08-24-09-09-05-00" LABEL="Ubuntu-Server 20.04.3 LTS amd64" TYPE="iso9660" PTUUID="7fcaeaa1" PTTYPE="dos"

/dev/sda2: UUID="580224a3-b988-47e1-bd13-b8eb8d97be68" TYPE="ext4" PARTUUID="c4afd6ee-18d3-4167-83f8-175c95779cfe"

/dev/sda3: UUID="wcE0WC-QUoy-A8Ac-kCNv-2PDP-Fogb-iWyHg3" TYPE="LVM2_member" PARTUUID="72394099-2af8-4209-8758-a94987ac1154"

/dev/mapper/ubuntu--vg-ubuntu--lv: UUID="ae1119c5-887e-416f-bb03-29d0fed8d68a" TYPE="ext4"

/dev/loop0: TYPE="squashfs"

/dev/loop1: TYPE="squashfs"

/dev/loop2: TYPE="squashfs"

/dev/sdb: UUID="0lHjFo-LkpT-kVJ1-bcgg-qQc9-jEDc-OdXGq7" TYPE="LVM2_member"

/dev/sda1: PARTUUID="d761cc85-49d8-4393-92d0-66447f8f9c65"

/dev/mapper/ubuntu--vg1-data: UUID="dc2c3dfe-c0be-46c3-8727-c810af09b05b" TYPE="ext4"

编辑配置文件,加入上述分区:

# sudo vim /etc/fstab

按insert,然后加入一条:

UUID=dc2c3dfe-c0be-46c3-8727-c810af09b05b /data ext4 defaults 0 0

注意!centos7 挂载时修改 /etc/fstab文件格式上稍有些区别,如下:

/dev/centos-vg1/data /data ext4 defaults 0 0

/dev/mapper/data--vg1-data: UUID="a5f9d7a4-25cf-49e5-8435-dfba463f9935" TYPE="xfs"

标签: #centos repair filesystem