Linux系统中lvm是什么?有什么作用?

Linux系统中lvm是什么?有什么作用?,第1张

在学习Linux知识的过程中,大家都会接触到很多专业术语,让人琢磨不透,完全不知道是什么意思,比如:LVM。那么Linux系统中lvm是什么?LVM中文意思为逻辑卷管理,是Linux环境下对磁盘分区进行管理的一种机制,接下来我们来看看详细的内容介绍。

Linux系统中lvm是什么?

LVM,全称Logical Volume

Manager,即逻辑卷管理,是Linux环境下对磁盘分区进行管理的一种机制,LVM是建立在磁盘和分区之上的一个逻辑层,来提高磁盘分区管理的灵活性。通过LVM系统管理员可以轻松管理磁盘分区,如:将若干个磁盘分区连接为一个整块的卷组,形成一个存储池。管理员可以在卷组上随意创建逻辑卷组,并进一步在逻辑卷组上创建文件系统。管理员通过LVM可以方便的调整存储卷组的大小,并且可以对磁盘存储按照组的方式进行命名、管理和分配。当系统添加了新的磁盘,通过LVM管理员就不必将磁盘的文件移动到新的磁盘上以充分利用新的存储空间,而是直接扩展文件系统跨越磁盘即可。

一般来说,物理磁盘或分区之间是分隔的,数据无法跨盘或分区,而各磁盘或分区的大小固定,重新调整比较麻烦。LVM可以将这些底层的物理磁盘或分区整合起来,抽象成容量资源池,以划分成逻辑卷的方式供上层使用,其最主要的功能即是可以在无需关机无需重新格式化的情况下d性调整逻辑卷的大小。

LVM的写入模式

LVM有两种写入模式:线性模式和条带模式

线性模式即写完一个设备后再写另一个设备

条带模式就有点类似于RAID0,即数据是被分散写入到LVM各成员设备上的。

因为条带模式的数据不具有安全性,且LVM并不强调读写性能,故LVM默认为线性模式,这样即使一个设备坏了,其它设备上的数据还在。

一、磁盘管理

步骤:

1. 物理磁盘被格式化为PV,空间被划分成一个个PE

2. 不同的PV加入同一个VG中,不同PV的PE全部进入VG的PE池;

3. 在VG中基于PE创建LV,组成LV的PE可能来自于不同的物理磁盘;

4. LV直接格式化后挂在使用;

5. LV的增加或减少其实就是增加或减少该组LV的PE数量,其过程不会丢失原始数据。

*** 作步骤:

①查看待分配的磁盘

[root@cen7 ~]# fdisk -l

Disk /dev/sda: 214.7 GB, 214748364800 bytes, 419430400 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: 0x000a39f5

   Device Boot      Start         End      Blocks   Id  System

/dev/sda1   *        2048      452607      225280   83  Linux

/dev/sda2          452608   415227903   207387648   83  Linux

/dev/sda3       415227904   419422207     2097152   82  Linux swap / Solaris

Disk /dev/sdb: 53.7 GB, 53687091200 bytes, 104857600 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: 0x99bab3b4

   Device Boot      Start         End      Blocks   Id  System

/dev/sdb1            2048    10487807     5242880   8e  Linux LVM

②创建PV

[root@cen7 ~]# pvcreate /dev/sdb1

WARNING: dos signature detected on /dev/sdb1 at offset 510. Wipe it? [y/n]: y

  Wiping dos signature on /dev/sdb1.

  Physical volume "/dev/sdb1" successfully created.

③查看PV

[root@cen7 ~]# pvdisplay /dev/sdb1

  "/dev/sdb1" is a new physical volume of "5.00 GiB"

  --- NEW Physical volume ---

  PV Name               /dev/sdb1

  VG Name

  PV Size               5.00 GiB

  Allocatable           NO

  PE Size               0

  Total PE              0

  Free PE               0

  Allocated PE          0

  PV UUID               yxWtAD-9Pye-9hVt-H7sZ-h44x-3MyP-NpadQ8

④创建VG

[root@cen7 ~]# vgcreate vg01 /dev/sdb1

  Volume group "vg01" successfully created

⑤查看VG

[root@cen7 ~]# vgdisplay

  --- Volume group ---

  VG Name               vg01

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

  PE Size               4.00 MiB

  Total PE              1279

  Alloc PE / Size       0 / 0

  Free  PE / Size       1279 / <5.00 GiB

  VG UUID               fVnLdP-mVtf-e1HI-33pR-MOcS-PLs5-l6nDck

⑥创建LV

[root@cen7 ~]# lvcreate -n lv01 -L 4.996G vg01#空间大小=1279*4M/1024

  Rounding up size to full physical extent <5.00 GiB

  Logical volume "lv01" created.

或者

[root@cen7 ~]# lvcreate -n lv01 -l 1279 vg01

  Logical volume "lv01" created.

⑦查看LV

[root@cen7 ~]# lvdisplay

  --- Logical volume ---

  LV Path                /dev/vg01/lv01

  LV Name                lv01

  VG Name                vg01

  LV UUID                dq0d77-sVP2-F3CX-baQC-5fvy-MeTU-YE710s

  LV Write Access        read/write

  LV Creation host, time cen7, 2022-02-22 10:12:04 +0800

  LV Status              available

  # open                 0

  LV Size                <5.00 GiB

  Current LE             1279

  Segments               1

  Allocation             inherit

  Read ahead sectors     auto

  - currently set to     8192

  Block device           253:0

⑧格式化文件系统

[root@cen7 ~]# mkfs.xfs /dev/vg01/lv01

meta-data=/dev/vg01/lv01         isize=512    agcount=4, agsize=327424 blks

         =                       sectsz=512   attr=2, projid32bit=1

         =                       crc=1        finobt=0, sparse=0

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

         =                       sunit=0      swidth=0 blks

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

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

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

realtime =none                   extsz=4096   b

⑨挂载文件分区

[root@cen7 ~]# mkdir /mnt/lv01

[root@cen7 ~]# mount /dev/vg01/lv01 /mnt/lv01

⑩查看是否挂在成功

[root@cen7 ~]# df -Th

Filesystem            Type      Size  Used Avail Use% Mounted on

devtmpfs              devtmpfs  2.0G     0  2.0G   0% /dev

tmpfs                 tmpfs     2.0G     0  2.0G   0% /dev/shm

tmpfs                 tmpfs     2.0G   13M  2.0G   1% /run

tmpfs                 tmpfs     2.0G     0  2.0G   0% /sys/fs/cgroup

/dev/sda2             xfs       198G   27G  172G  14% /

/dev/sda1             xfs       217M  147M   71M  68% /boot

tmpfs                 tmpfs     394M  4.0K  394M   1% /run/user/42

tmpfs                 tmpfs     394M   28K  394M   1% /run/user/0

/dev/sr0              iso9660   4.4G  4.4G     0 100% /run/media/root/CentOS 7 x86_64

/dev/mapper/vg01-lv01  xfs       5.0G   33M  5.0G   1% /mnt/lv01

⑧移除LV

[root@cen7 ~]# lvremove /dev/vg01/lv01

Do you really want to remove active logical volume vg01/lv01? [y/n]: y

  Logical volume "lv01" successfully removed

LVM将一个或多个硬盘的分区在逻辑上集合,相当于一个大硬盘来使用,当硬盘的空间不够使用的时候,可以继续将 其它 的硬盘的分区加入其中,这样可以实现磁盘空间的动态管理,相对于普通的磁盘分区有很大的灵活性。那么你知道linux系统硬盘怎么格式化吗?我带来了linux系统硬盘格式化的具 体 *** 作过程,下面大家跟着我一起来学习一下吧。

linux系统硬盘怎么格式化

分区与格式化

先用fdisk分区,分区完成后再用mkfs格式化并创建文件系统,挂载,磁盘就能使用啦。

分区的原理:

MBR:主引导扇区

主分区表:64bytes,最多只能分四个主分区,每个主分区的记录(相关信息,比如分区大小,位置)在主分区表里占14bytes。

如果要建多于四个的分区,就要拿出一个主分区做为扩展分区,再在扩展分区里面进行其它的分区 *** 作。在 建扩展分区的时候会建立一张对应的扩展分区表,它记录了在这个扩展分区里的分区的相关信息理论上它没有分区数量的限制,在扩展分区内部的分区叫做逻辑分区,如上图中的 /dev/hda5,/dev/hda6/,/dev/hda7

格式化原理:

在分好区后,分区里面是空的,没有任何东西。为了能让OS识别,就必须要向分区里写入相应格式的数据。比如windows的FAT32,NTFSLinux的ext2,ext3,ext4(目前ext3格式的用的比较多,ext4还在实验之中,在新的Fedora上使用的就是ext4的文件系统)。

Windows/dos常用的分区工具:fdisk/partition magic/diskpart

Linux下常用的分区工具:

fdisk/sfdisk:命令行工具,各种版本和环境都能使用,包含在软件包util-linux中

diskdruid:图形化分区工具,只能在安装REDHAT系统时使用。

下面我们开始实验:

环境/工具:Fedora 14/256M内存卡fdisk

第一步:fdisk

[root@novice ~]# fdisk -l /dev/sdb

Disk /dev/sdb: 254 MB, 254017536 bytes

8 heads, 61 sectors/track, 1016 cylinders, total 496128 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 identifier: 0x00000000

Device Boot Start End Blocks Id System

[root@novice ~]# fdisk /dev/sdb

Command (m for help): #在输入上面的命令后会出现左边的提示,输入m就会得到一个帮助菜单,如下:

Command (m for help): m

Command action

a toggle a bootable flag

b edit bsd disklabel

c toggle the dos compatibility flag

d delete a partition

l list known partition types

m print this menu

n add a new partition

o create a new empty DOS partition table

p print the partition table

q quit without saving changes

s create a new empty Sun disklabel

t change a partition's system id

u change display/entry units

v verify the partition table

w write table to disk and exit

x extra functionality (experts only)

#help虽然是英文的,可都很简单,在这里不再解释。

#现在,我们正式开始分区的 *** 作:

Command (m for help): n #新建分区

Command action

e extended

p primary partition (1-4)

#e/p分别对应扩展分区 /主分区我们先分四个主分区,每个50M然后再来增加主分区或扩展分区,看会出现怎样的状况,嘿嘿。

p #分区类型为主分区

Partition number (1-4, default 1): 1 #分区号,在这里我们依次选择1、2、3、4

First sector (2048-496127, default 2048): #指定分区的起始扇区,一般默认,按enter键即可。

Last sector, +sectors or +size{K,M,G} (2048-496127, default 496127): +50M #指定分区的终止扇区,根据前面的提示我们可以做出相应的选择+sectors 或 +size{K,M,G}

Command (m for help): p #用p打印出已建好的分区列表

Disk /dev/sdb: 254 MB, 254017536 bytes

8 heads, 61 sectors/track, 1016 cylinders, total 496128 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 identifier: 0x00000000

Device Boot Start End Blocks Id System

/dev/sdb1 2048 104447 51200 83 Linux

#剩下的三个分区的建立 *** 作同上

#分好四个主分区后的情况如下

Command (m for help): p

Disk /dev/sdb: 254 MB, 254017536 bytes

8 heads, 61 sectors/track, 1016 cylinders, total 496128 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 identifier: 0x00000000

Device Boot Start End Blocks Id System

/dev/sdb1 2048 104447 51200 83 Linux

/dev/sdb2 104448 206847 51200 83 Linux

/dev/sdb3 206848 309247 51200 83 Linux

/dev/sdb4 309248 309298 25+ 83 Linux

#已经建好四个主分区啦,现在我们来看看如果再建主分区或是扩展分区的话会出现怎样的情况:

Command (m for help): n

You must delete some partition and add an extended partition first

#看到了吧,不能再建分区啦!要再建分区的话必须删除some分区,再新建一个扩展分区才行。

#现在,我们删掉一个主分区,来新建扩展分区

Command (m for help): d #删除分区

Partition number (1-4): 4 #选择要删除分区的分区号,我们选第四个

Command (m for help): p #打印,如下,四个分区变成了三个!

Disk /dev/sdb: 254 MB, 254017536 bytes

8 heads, 61 sectors/track, 1016 cylinders, total 496128 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 identifier: 0x00000000

Device Boot Start End Blocks Id System

/dev/sdb1 2048 104447 51200 83 Linux

/dev/sdb2 104448 206847 51200 83 Linux

/dev/sdb3 206848 309247 51200 83 Linux

#新建一个扩展分区

#如果在没有建满三个主分的区的情况下建立扩展分区,相关选项会有些不同。

Command (m for help): n

Command action

e extended

p primary partition (1-4)

e

Selected partition 4

First sector (309248-496127, default 309248): #enter,默认

Using default value 309248

Last sector, +sectors or +size{K,M,G} (309248-496127, default 496127): #enter,默认,使用剩余空间

Using default value 496127

Command (m for help): p

Disk /dev/sdb: 254 MB, 254017536 bytes

8 heads, 61 sectors/track, 1016 cylinders, total 496128 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 identifier: 0x00000000

Device Boot Start End Blocks Id System

/dev/sdb1 2048 104447 51200 83 Linux

/dev/sdb2 104448 206847 51200 83 Linux

/dev/sdb3 206848 309247 51200 83 Linux

/dev/sdb4 309248 496127 93440 5 Extended

#接下来,我们在新建的扩展分区里再新建两个逻辑分区,因为已经有了三个主分区,这里不会再显示是建立逻辑分区还是主分区的提示!

Command (m for help): n

First sector (311296-496127, default 311296): #enter

Using default value 311296

Last sector, +sectors or +size{K,M,G} (311296-496127, default 496127): +50M

Command (m for help): n

First sector (415744-496127, default 415744): #enter

Using default value 415744

Last sector, +sectors or +size{K,M,G} (415744-496127, default 496127): #enter

Using default value 496127

Command (m for help): p

Disk /dev/sdb: 254 MB, 254017536 bytes

8 heads, 61 sectors/track, 1016 cylinders, total 496128 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 identifier: 0x00000000

Device Boot Start End Blocks Id System

/dev/sdb1 2048 104447 51200 83 Linux

/dev/sdb2 104448 206847 51200 83 Linux

/dev/sdb3 206848 309247 51200 83 Linux

/dev/sdb4 309248 496127 93440 5 Extended

/dev/sdb5 311296 413695 51200 83 Linux

/dev/sdb6 415744 496127 40192 83 Linux

#上面的列表,就是我们今天分区的成果啦!接下来保存退出,重启计算机,就可以进行下一步的mkfs *** 作啦!如果忘记了相关的 *** 作命令,记得按m!!!

Command (m for help): w #保存

The partition table has been altered!

Calling ioctl() to re-read partition table.

Syncing disks.

另:在建好分区后,我们还可以更改相关分区的文件系统类型

#如,我们要把第二个主分区改成Linux下的交换分区, *** 作如下

Command (m for help): t #更改文件系统类型

Partition number (1-6): 2 #选择第二个分区

Hex code (type L to list codes): L #选择要更改的文件系统编码,可以按L来查看相关编码信息。

0 Empty 24 NEC DOS 81 Minix / old Lin bf Solaris

1 FAT12 39 Plan 9 82 Linux swap / So c1 DRDOS/sec (FAT-

2 XENIX root 3c PartitionMagic 83 Linux c4 DRDOS/sec (FAT-

3 XENIX usr 40 Venix 80286 84 OS/2 hidden C: c6 DRDOS/sec (FAT-

............

16 Hidden FAT16 64 Novell Netware af HFS / HFS+ fb VMware VMFS

17 Hidden HPFS/NTF 65 Novell Netware b7 BSDI fs fc VMware VMKCORE

18 AST SmartSleep 70 DiskSecure Mult b8 BSDI swap fd Linux raid auto

1b Hidden W95 FAT3 75 PC/IX bb Boot Wizard hid fe LANstep

1c Hidden W95 FAT3 80 Old Minix be Solaris boot ff BBT

1e Hidden W95 FAT1

Hex code (type L to list codes): 82 #查找到linux swap的编码为82

Changed system type of partition 2 to 82 (Linux swap / Solaris)

Command (m for help): p

..............

Device Boot Start End Blocks Id System

/dev/sdb1 2048 104447 51200 83 Linux

/dev/sdb2 104448 206847 51200 82 Linux swap / Solaris

/dev/sdb3 206848 309247 51200 83 Linux

/dev/sdb4 309248 496127 93440 5 Extended

/dev/sdb5 311296 413695 51200 83 Linux

/dev/sdb6 415744 496127 40192 83 Linux

#最后别忘了保存!如果你须要的话!

#扩展分区不能直接使用,逻辑分区只能建立在扩展分区上!

第二步:mkfs(mkfs时分区的格式最好与fdisk设定的分区格式一致,不然.......)

mkfs支持ext2 ext3 vfa msdos jfs reiserfs等文件系统。

用法1:mkfs -t

例: mkfs -t ext3 /dev/sdb2

用法2:mkfs.

例:mkfs,vfat /dev/sdb3

mke2fs支持ext2/ext3文件系统

用法:mke2fs [-j]

例:mke2fs -j /dev/sdb5

# 更多更具体的用法请参照相关命令的man手册

下面,接着实验:

例一

[root@novice ~]# mkfs -t ext3 /dev/sdb1

mke2fs 1.41.12 (17-May-2010)

Filesystem label=

OS type: Linux

Block size=1024 (log=0)

Fragment size=1024 (log=0)

Stride=0 blocks, Stripe width=0 blocks

12824 inodes, 51200 blocks

2560 blocks (5.00%) reserved for the super user

First data block=1

Maximum filesystem blocks=52428800

7 block groups

8192 blocks per group, 8192 fragments per group

1832 inodes per group

Superblock backups stored on blocks:

8193, 24577, 40961

Writing inode tables: done

Creating journal (4096 blocks): done

Writing superblocks and filesystem accounting information: done

This filesystem will be automatically checked every 34 mounts or

180 days, whichever comes first. Use tune2fs -c or -i to override.

例二:

[root@novice ~]# fdisk /dev/sdb

Command (m for help): t

Partition number (1-6): 6

Hex code (type L to list codes): L

0 Empty 24 NEC DOS 81 Minix / old Lin bf Solaris

1 FAT12 39 Plan 9 82 Linux swap / So c1 DRDOS/sec (FAT-

2 XENIX root 3c PartitionMagic 83 Linux c4 DRDOS/sec (FAT-

3 XENIX usr 40 Venix 80286 84 OS/2 hidden C: c6 DRDOS/sec (FAT-

4 FAT16 <32M 41 PPC PReP Boot 85 Linux extended c7 Syrinx

5 Extended 42 SFS 86 NTFS volume set da Non-FS data

6 FAT16 4d QNX4.x 87 NTFS volume set db CP/M / CTOS / .

7 HPFS/NTFS 4e QNX4.x 2nd part 88 Linux plaintext de Dell Utility

.........

Hex code (type L to list codes): 7

Changed system type of partition 6 to 7 (HPFS/NTFS)

Command (m for help): p

Disk /dev/sdb: 254 MB, 254017536 bytes

8 heads, 61 sectors/track, 1016 cylinders, total 496128 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 identifier: 0x00000000

Device Boot Start End Blocks Id System

/dev/sdb1 2048 104447 51200 83 Linux

/dev/sdb2 104448 206847 51200 82 Linux swap / Solaris

/dev/sdb3 206848 309247 51200 83 Linux

/dev/sdb4 309248 496127 93440 5 Extended

/dev/sdb5 311296 413695 51200 83 Linux

/dev/sdb6 415744 496127 40192 7 HPFS/NTFS

Command (m for help): w

The partition table has been altered!

Calling ioctl() to re-read partition table.

Syncing disks.

[root@novice ~]# mkfs.ntfs /dev/sdb6

Cluster size has been automatically set to 4096 bytes.

Initializing device with zeroes: 100% - Done.

Creating NTFS volume structures.

mkntfs completed successfully. Have a nice day.


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/yw/8385525.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-15
下一篇2023-04-15

发表评论

登录后才能评论

评论列表(0条)

    保存