0
  • 聊天消息
  • 系统消息
  • 评论与回复
登录后你可以
  • 下载海量资料
  • 学习在线课程
  • 观看技术视频
  • 写文章/发帖/加入社区
创作中心

完善资料让更多小伙伴认识你,还能领取20积分哦,立即完善>

3天内不再提示

什么是磁盘分区与逻辑卷管理

汽车电子技术 来源:码农与软件时代 作者:码农与软件时代 2023-02-20 15:48 次阅读
一、磁盘分区

(一)概述

原始磁盘需要经过那些操作才能存取数据?

原始磁盘的形态是一个光滑的盘体;
需要将盘体划分出柱面和磁道,然后将磁道划分为多个扇区,称为“低级格式化”;
为了有效组织磁盘和提高I\\O效率,还需要进行“分区”;
此时,磁盘还不能存储数据,还需要对每一分区中的扇区进行逻辑编号,并建立数据结构和磁盘数据的管理方式,称为“高级格式化”,本质上是在分区上确定了文件系统,便可以进行数据存储了。

如果把原始磁盘比作待开发的地块
那么,
“低级格式化”相当于在该地块上修建了道路和房屋;
“分区”相当于根据地理区域划分不同的小区;
“高级格式化”相当于将小区内的房屋进行标记。

Windows操作系统,分区、格式化之后,就可以建目录存储文件了。
Linux操作系统则与Windows相反,需要先虚拟一个根目录,再将磁盘挂载到根目录。

(二)实践

1. 查看当前分区情况:

工具fdisk:用于Linux的磁盘分区表操作
root@linux:~# fdisk -l
Disk /dev/vda: 40 GiB, 42949672960 bytes, 83886080 sectors
# 磁盘大小为 40G
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: EC404E4B-BF45-4DEE-8D63-3D59FBB9C44E


Device      Start      End  Sectors  Size Type
/dev/vda1  227328 41943006 41715679 19.9G Linux filesystem 
/dev/vda14   2048    10239     8192    4M BIOS boot
/dev/vda15  10240   227327   217088  106M EFI System
# 磁盘分区约为20G,还有20G的空间未使用
Partition table entries are not in disk order.
2. 创建新分区
root@linux:~# fdisk /dev/vda
Welcome to fdisk (util-linux 2.31.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Command (m for help): p # 列出磁盘分区表 
Disk /dev/vda: 40 GiB, 42949672960 bytes, 83886080 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
Disklabel type: gpt
Disk identifier: EC404E4B-BF45-4DEE-8D63-3D59FBB9C44E


Device      Start      End  Sectors  Size Type
/dev/vda1  227328 41943006 41715679 19.9G Linux filesystem
/dev/vda14   2048    10239     8192    4M BIOS boot
/dev/vda15  10240   227327   217088  106M EFI System


Partition table entries are not in disk order.


Command (m for help): n # 创建新分区
Partition number (2-13,16-128, default 2): # 默认回车
First sector (41943007-83886046, default 41943040): # 默认回车
Last sector, +sectors or +size{K,M,G,T,P} (41943040-83886046, default 83886046): +10G # 使用size方式定义10G大小


Created a new partition 2 of type 'Linux filesystem' and of size 10 GiB.


Command (m for help): p
Disk /dev/vda: 40 GiB, 42949672960 bytes, 83886080 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
Disklabel type: gpt
Disk identifier: EC404E4B-BF45-4DEE-8D63-3D59FBB9C44E


Device        Start      End  Sectors  Size Type
/dev/vda1    227328 41943006 41715679 19.9G Linux filesystem
/dev/vda2  41943040 62914559 20971520   10G Linux filesystem
/dev/vda14     2048    10239     8192    4M BIOS boot
/dev/vda15    10240   227327   217088  106M EFI System


Partition table entries are not in disk order.


Command (m for help): w # 保存、退出
The partition table has been altered.
Syncing disks.
3. 查看新创建分区
root@linux:~# lsblk
NAME    MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
vda     252:0    0   40G  0 disk 
├─vda1  252:1    0 19.9G  0 part /
├─vda2  252:2    0   10G  0 part 
├─vda14 252:14   0    4M  0 part 
└─vda15 252:15   0  106M  0 part /boot/efi
4. 格式化

命令mkfs(make file system):在特定的分区上建立linux文件系统。
root@linux:~# mkfs # 两次键入Tab,则可展示支持的文件类型
mkfs         mkfs.btrfs   mkfs.ext2    mkfs.ext4    mkfs.minix   mkfs.ntfs    mkfs.xfs     
mkfs.bfs     mkfs.cramfs  mkfs.ext3    mkfs.fat     mkfs.msdos   mkfs.vfat    
root@linux:~# mkfs.ext3 /dev/vda2 # 在新分区创建ext3的文件系统
mke2fs 1.44.1 (24-Mar-2018)
Creating filesystem with 2621440 4k blocks and 655360 inodes
Filesystem UUID: 0e6a8449-acbf-4428-a5d7-b67b4535b844
Superblock backups stored on blocks: 
  32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632


Allocating group tables: done                            
Writing inode tables: done                            
Creating journal (16384 blocks): done
Writing superblocks and filesystem accounting information: done
5. 挂载

命令mount:用来挂载文件系统。
root@linux:~# mkdir /test2
root@linux:~# mount /dev/vda2 /test2/ 
root@linux:~# ls /test2/
lost+found
6. 查看分区挂载点及文件类型
root@linux:~# df -T
Filesystem     Type     1K-blocks    Used Available Use% Mounted on
udev           devtmpfs   1007416       0   1007416   0% /dev
tmpfs          tmpfs       204092     776    203316   1% /run
/dev/vda1      ext4      20145724 3279936  16849404  17% /
tmpfs          tmpfs      1020448       0   1020448   0% /dev/shm
tmpfs          tmpfs         5120       0      5120   0% /run/lock
tmpfs          tmpfs      1020448       0   1020448   0% /sys/fs/cgroup
/dev/vda15     vfat        106858    3668    103190   4% /boot/efi
tmpfs          tmpfs       204088       8    204080   1% /run/user/1001
/dev/vda2      ext3      10255672   23096   9708288   1% /test2
二、逻辑卷管理

(一)概述

逻辑卷管理也就是LVM(Logical Volume  Manager),位于磁盘和操作系统之间,将一个或多个磁盘分区(PV)虚拟为一个卷组(VG),并在VG上面划分一些逻辑卷(LV)。逻辑卷可再进行格式化、挂载,并且逻辑卷可以动态伸缩。

也就是从物理磁盘或分区开始,经过PV--VG--LV(形成逻辑卷)之后,将逻辑卷看成新的磁盘进行分区、格式化并挂载,供主机使用。


(二)实践

使用lsblk命令查看系统的磁盘使用情况(lsblk--list block);
使用fdisk命令进行分区;
使用pvcreate创建物理卷,
使用vgcreate创建卷组;
使用lvcreate创建逻辑卷。

1. 创建新分区vda3
root@linux:~# fdisk /dev/vda


Welcome to fdisk (util-linux 2.31.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.




Command (m for help): n
Partition number (3-13,16-128, default 3): 
First sector (62914560-83886046, default 62914560): 
Last sector, +sectors or +size{K,M,G,T,P} (62914560-83886046, default 83886046): +8G


Created a new partition 3 of type 'Linux filesystem' and of size 8 GiB.


Command (m for help): p
Disk /dev/vda: 40 GiB, 42949672960 bytes, 83886080 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
Disklabel type: gpt
Disk identifier: EC404E4B-BF45-4DEE-8D63-3D59FBB9C44E


Device        Start      End  Sectors  Size Type
/dev/vda1    227328 41943006 41715679 19.9G Linux filesystem
/dev/vda2  41943040 62914559 20971520   10G Linux filesystem
/dev/vda3  62914560 79691775 16777216    8G Linux filesystem
/dev/vda14     2048    10239     8192    4M BIOS boot
/dev/vda15    10240   227327   217088  106M EFI System


Partition table entries are not in disk order.


Command (m for help): w
The partition table has been altered.


Syncing disks.


2. 创建物理卷

命令pvcreate:用于将物理硬盘分区初始化为物理卷。
root@linux:~# pvcreate /dev/vda3
  Physical volume "/dev/vda3" successfully created.
root@linux:~# pvdisplay /dev/vda3
  "/dev/vda3" is a new physical volume of "8.00 GiB"
  --- NEW Physical volume ---
  PV Name               /dev/vda3
  VG Name               
  PV Size               8.00 GiB
  Allocatable           NO
  PE Size               0   
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               ke8k0X-RgZL-Fejo-1AVO-fPPO-hhEP-WVb79K
3. 创建卷组

命令vgcreate:用于创建LVM卷组(Volume Group),将多个物理卷组织成一个整体,屏蔽底层物理卷细节。
root@linux:~# vgcreate vgtest /dev/vda3
  Volume group "vgtest" successfully created
root@linux:~# vgdisplay vg0
  Volume group "vg0" not found
  Cannot process volume group vg0
root@linux:~# vgdisplay vgtest
  --- Volume group ---
  VG Name               vgtest
  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               <8.00 GiB
  PE Size               4.00 MiB
  Total PE              2047
  Alloc PE / Size       0 / 0   
  Free  PE / Size       2047 / <8.00 GiB
  VG UUID               2TM5qM-9fUR-xTTU-55Nq-nYZE-ATYg-SjQQ6y
4. 创建逻辑卷

命令lvcreate:创建LVM的逻辑卷。
root@linux:~# lvcreate -L 200M -n lv0 vgtest
  Logical volume "lv0" created.


5. 管理逻辑卷


# 查看物理卷信息
root@linux:~# pvs
  PV         VG     Fmt  Attr PSize  PFree
  /dev/vda3  vgtest lvm2 a--  <8.00g 7.80g


# 查看卷组信息
root@linux:~# vgs
  VG     #PV #LV #SN Attr   VSize  VFree
  vgtest   1   1   0 wz--n- <8.00g 7.80g


# 增加逻辑卷空间
root@linux:~# lvextend -L +100M /dev/vgtest/lv0
  Size of logical volume vgtest/lv0 changed from 200.00 MiB (50 extents) to 300.00 MiB (75 extents).
  Logical volume vgtest/lv0 successfully resized.


# 查询逻辑卷大小
root@linux:~# lvs /dev/vgtest/lv0
  LV   VG     Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  lv0  vgtest -wi-a----- 300.00m                                                    
root@linux:~#
声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉
  • 格式化
    +关注

    关注

    2

    文章

    37

    浏览量

    8984
  • 磁盘
    +关注

    关注

    1

    文章

    338

    浏览量

    24882
收藏 人收藏

    评论

    相关推荐

    一步一步学:教你在Windows下对硬盘分区

    之外的所有分区全部删除之后,原本的多个逻辑驱动器就剩下了一个“可用空间”: 这时候继续在其上面点击鼠标右键,选择“删除磁盘分区”,这时候整个扩展分区就成为了“未指派”状态: 最后,删除
    发表于 02-27 16:44

    WIN7系统里进行硬盘的创建、删除或分区设置

    员密码或进行确认,请键入该密码或提供确认)  2, 在左窗格中的“存储”下面,单击“磁盘管理”。  3, 右键单击要删除的(如分区逻辑
    发表于 03-13 14:27

    【雨林木风系统下载教程】利用XP系统磁盘分区来提升读写...

    与合并磁盘分区  如果硬盘中最好了的xp系统的某个分区容量过大,可将其拆分为两个分区。首先将该磁盘分区中的所有文件保存到其它分区中。之后在“
    发表于 07-19 10:44

    【LeMaker Guitar试用体验】之使用 gparted扩展磁盘分区

    在搭建opencv开发环境时,发现磁盘空间不够用,然后系统SD卡是16GB大小的,通过windows磁盘管理工具也可以看到,烧写系统镜像后,实际可用的为3.36G,其中有11GB的空间未被
    发表于 02-20 09:24

    linux如何查看磁盘使用情况

    fdisk 是一款强大的磁盘操作工具,来自util-linux软件包,我们在这里只说他如何查看磁盘分区表及分区结构;通过-l 参数,能获得机器中所有的硬盘的分区情况;
    发表于 07-08 08:24

    linux的三种磁盘分区

    硬盘分区有三种,主磁盘分区(83)、扩展磁盘分区(5)、逻辑分区(包括swap交换分区82)。一
    发表于 07-12 06:56

    【昉·星光 2 高性能RISC-V单板计算机体验】三:从 nvme 磁盘启动 VisionFive2 并对磁盘分区扩容

    从 nvme 磁盘启动 VisionFive2 并对磁盘分区扩容 在之前的经验贴《在 VisionFive2 上如何更快的向 nvme 硬盘安装操作系统》中介绍了一种通用的向 nvme 磁盘刷入
    发表于 09-04 02:54

    Linux下的逻辑管理

    逻辑管理(Logic Volume Manager),简称LVM,是与传统的静态分区完全不同的磁盘管理方法
    发表于 01-06 17:02 959次阅读

    Linux环境下对磁盘分区进行管理的一种机制

    物理卷就是指硬盘分区或从逻辑上与磁盘分区具有同样功能的设备(如RAID),是LVM的基本存储逻辑块,但和基本的物理存储介质(如分区
    的头像 发表于 08-10 17:58 3478次阅读

    Linux系统教程之磁盘分区和LVM系统的详细资料概述

    本文档的主要内容详细介绍的是Linux系统教程之磁盘分区和LVM系统的详细资料概述主要内容包括了:1.磁盘相关概念2.磁盘分区工具3.创建 LVM 系统4.维护 LVM 系统
    发表于 10-30 16:11 14次下载
    Linux系统教程之<b class='flag-5'>磁盘分区</b>和LVM系统的详细资料概述

    微软Windows 10研究新的方法管理现代磁盘分区

    如果要删除旧磁盘并创建新分区或扩展特定磁盘的空间,则需要依赖Windows 10的磁盘管理工具。在Windows 10上
    的头像 发表于 03-19 15:06 1904次阅读

    预装Win7系统划分更多的磁盘分区的方法

      硬盘分区有三种:主磁盘分区、扩展磁盘分区逻辑分区。一个硬盘主分区至少有1个,最多4个,扩展
    发表于 07-08 10:50 0次下载
    预装Win7系统划分更多的<b class='flag-5'>磁盘分区</b>的方法

    获取磁盘分区UUID的方法介绍

    UUID(通用唯一标识符,Universally Unique Identifiers)是磁盘分区的属性,在管理具有数百个驱动器的服务器时至关重要。
    的头像 发表于 01-31 17:45 1.1w次阅读
    获取<b class='flag-5'>磁盘分区</b>UUID的方法介绍

    Linux磁盘分区和挂载

    1.Linux 来说 wulun 有几个分区,分给哪一目录使用,他归根结底只有一个根目录,一个独立且唯一的文件结构,Linux 中每个分区都是用来组成整个文件系统的一部分。
    的头像 发表于 03-26 10:14 908次阅读

    linux系统如何进行磁盘分区

    linux系统如何进行磁盘分区磁盘分区是在Linux系统中进行硬盘划分的一种方法,它可以将一个物理硬盘划分为多个逻辑分区,每个分区可独立
    的头像 发表于 12-19 11:35 482次阅读