首先,我们来说一下为什么要进行磁盘分区?有以下两点:
- 数据的安全性
- 系统的功能考虑(读取数据的速度)
那么,磁盘的类型有哪些呢?
BIOS:是计算机系统中用于初始化硬件、启动操作系统和提供基本输入/输出功能的固件
MBR:用于在硬盘驱动器上存储引导加载程序和分区表的特殊区域,MBR通常位于硬盘的第一个扇区(通常是扇区0),占据约512个字节的空间。
- 主分区
- 拓展分区(又能变成逻辑分区)
- 逻辑分区
GPT:是一种用于在硬盘驱动器上存储分区信息的分区表格式。GPT是一种较新的、更先进的分区方案,相对于传统的MBR来说,GPT具有更多的优势和功能,有128个分区
命令
lsblk //查看分区
swap //交换分区
管理逻辑卷(LVM)
[root@localhost ~]# xfs_growfs /mnt //扩容之后格式化
[root@localhost ~]# df -h //查看
[root@localhost ~]# umount /mnt //取消/mnt目录下挂载的文件系统
[root@localhost ~]# lvremove /dev/vg1/lv1 //删除逻辑卷
Do you really want to remove active logical volume vg1/lv1? [y/n]: y
Logical volume "lv1" successfully removed.
[root@localhost ~]# fdisk /dev/nvme0n2 //对/dev/nvme0n2设备进行分区管理删除分区
d //是在fdisk命令的交互式界面中输入的一个命令,用于删除分区
下面我们来具体实操一下:
重要步骤:
- 创建
- 格式化
- 挂载
[root@localhost ~]# lsblk //显示块设备的层次结构
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
sr0 11:0 1 8.9G 0 rom /mnt
nvme0n1 259:0 0 20G 0 disk
├─nvme0n1p1 259:1 0 1G 0 part /boot
└─nvme0n1p2 259:2 0 19G 0 part
├─rhel-root 253:0 0 17G 0 lvm /
└─rhel-swap 253:1 0 2G 0 lvm [SWAP]
nvme0n2 259:3 0 10G 0 disk
├─nvme0n2p1 259:4 0 2G 0 part
├─nvme0n2p2 259:5 0 2G 0 part /root/test2
├─nvme0n2p3 259:6 0 2G 0 part
├─nvme0n2p4 259:7 0 1K 0 part
└─nvme0n2p5 259:8 0 4G 0 part [SWAP]
[root@localhost ~]# swapon /dev/nvme0n2 //将指定的设备或文件作为交换空间启用
/dev/nvme0n2 /dev/nvme0n2p2 /dev/nvme0n2p4
/dev/nvme0n2p1 /dev/nvme0n2p3 /dev/nvme0n2p5
[root@localhost ~]# blkid //查看每个块设备的详细信息
/dev/mapper/rhel-swap: UUID="8c92af1a-ddcd-4823-8fc8-828acbfa2bc1" TYPE="swap"
/dev/nvme0n1p1: UUID="d3e8f281-eaa5-4a21-afc2-93f153b30d50" TYPE="xfs" PARTUUID="c7a68f2c-01"
/dev/nvme0n1p2: UUID="ZNeKc9-C1qJ-y2ea-2VKD-R30C-vqP0-pcD0xt" TYPE="LVM2_member" PARTUUID="c7a68f2c-02"
/dev/sr0: UUID="2023-04-13-16-58-02-00" LABEL="RHEL-9-2-0-BaseOS-x86_64" TYPE="iso9660" PTUUID="d3d1f9a5" PTTYPE="dos"
/dev/nvme0n2p3: UUID="24ced1c3-ebde-403d-ad73-c706da608eed" TYPE="xfs" PARTUUID="9d205cd2-03"
/dev/nvme0n2p1: UUID="f29e8e89-5129-4e02-b46e-31d07cc2deb9" TYPE="xfs" PARTUUID="9d205cd2-01"
/dev/nvme0n2p2: UUID="984b2cad-287f-4c47-9575-7e4b8599b061" TYPE="xfs" PARTUUID="9d205cd2-02"
/dev/nvme0n2p5: UUID="c98a1259-55fd-48bf-8cdd-50a2d9b09d23" TYPE="swap" PARTUUID="9d205cd2-05"
/dev/mapper/rhel-root: UUID="96ddd388-7e98-4e53-9380-d5a1c8455dc5" TYPE="xfs"
[root@localhost ~]# swapoff -a //关闭系统中当前所有的交换空间
[root@localhost ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
sr0 11:0 1 8.9G 0 rom /mnt
nvme0n1 259:0 0 20G 0 disk
├─nvme0n1p1 259:1 0 1G 0 part /boot
└─nvme0n1p2 259:2 0 19G 0 part
├─rhel-root 253:0 0 17G 0 lvm /
└─rhel-swap 253:1 0 2G 0 lvm
nvme0n2 259:3 0 10G 0 disk
├─nvme0n2p1 259:4 0 2G 0 part
├─nvme0n2p2 259:5 0 2G 0 part /root/test2
├─nvme0n2p3 259:6 0 2G 0 part
├─nvme0n2p4 259:7 0 1K 0 part
└─nvme0n2p5 259:8 0 4G 0 part
[root@localhost ~]# vim /etc/fstab //用vim打开需要挂载的文件系统以及挂载选项
[root@localhost ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
sr0 11:0 1 8.9G 0 rom /mnt
nvme0n1 259:0 0 20G 0 disk
├─nvme0n1p1 259:1 0 1G 0 part /boot
└─nvme0n1p2 259:2 0 19G 0 part
├─rhel-root 253:0 0 17G 0 lvm /
└─rhel-swap 253:1 0 2G 0 lvm
nvme0n2 259:3 0 10G 0 disk
├─nvme0n2p1 259:4 0 2G 0 part
├─nvme0n2p2 259:5 0 2G 0 part /root/test2
├─nvme0n2p3 259:6 0 2G 0 part
├─nvme0n2p4 259:7 0 1K 0 part
└─nvme0n2p5 259:8 0 4G 0 part
[root@localhost ~]# yum -i httpd //用yum下载httpd
Updating Subscription Management repositories.
Unable to read consumer identity
This system is not registered with an entitlement server. You can use subscription-manager to register.
No such command: httpd. Please use /usr/bin/yum --help
It could be a YUM plugin command, try: "yum install 'dnf-command(httpd)'"
[root@localhost ~]# cd /mnt
[root@localhost mnt]# ll
total 44
drwxr-xr-x. 1 lisi systemd-oom 2048 Apr 14 2023 AppStream
drwxr-xr-x. 1 lisi systemd-oom 2048 Apr 14 2023 BaseOS
drwxrwxr-x. 1 root root 2048 Apr 14 2023 EFI
-rw-r--r--. 1 lisi systemd-oom 8154 Apr 14 2023 EULA
-rw-r--r--. 1 lisi systemd-oom 1455 Apr 14 2023 extra_files.json
-rw-r--r--. 1 lisi systemd-oom 18092 Apr 14 2023 GPL
drwxrwxr-x. 1 root root 2048 Apr 14 2023 images
drwxrwxr-x. 1 root root 2048 Apr 14 2023 isolinux
-rw-r--r--. 1 lisi systemd-oom 103 Apr 14 2023 media.repo
-rw-r--r--. 1 lisi systemd-oom 1669 Apr 14 2023 RPM-GPG-KEY-redhat-beta
-rw-r--r--. 1 lisi systemd-oom 3682 Apr 14 2023 RPM-GPG-KEY-redhat-release
[root@localhost mnt]# vim /etc/fstab
[root@localhost mnt]# ll
total 44
drwxr-xr-x. 1 lisi systemd-oom 2048 Apr 14 2023 AppStream
drwxr-xr-x. 1 lisi systemd-oom 2048 Apr 14 2023 BaseOS
drwxrwxr-x. 1 root root 2048 Apr 14 2023 EFI
-rw-r--r--. 1 lisi systemd-oom 8154 Apr 14 2023 EULA
-rw-r--r--. 1 lisi systemd-oom 1455 Apr 14 2023 extra_files.json
-rw-r--r--. 1 lisi systemd-oom 18092 Apr 14 2023 GPL
drwxrwxr-x. 1 root root 2048 Apr 14 2023 images
drwxrwxr-x. 1 root root 2048 Apr 14 2023 isolinux
-rw-r--r--. 1 lisi systemd-oom 103 Apr 14 2023 media.repo
-rw-r--r--. 1 lisi systemd-oom 1669 Apr 14 2023 RPM-GPG-KEY-redhat-beta
-rw-r--r--. 1 lisi systemd-oom 3682 Apr 14 2023 RPM-GPG-KEY-redhat-release
[root@localhost mnt]# exit
logout
Connection closed.
...
There was 1 failed login attempt since the last successful login.
Last login: Tue Nov 21 20:40:33 2023
[root@localhost ~]# fdisk /dev/nvme0n3
Welcome to fdisk (util-linux 2.37.4).
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.
Created a new DOS disklabel with disk identifier 0x5278d3f8.
Command (m for help): n //新建分区
Partition type
p primary (0 primary, 0 extended, 4 free)
e extended (container for logical partitions)
Select (default p):
Using default response p.
Partition number (1-4, default 1):
First sector (2048-20971519, default 2048):
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-20971519, default 20971519):
Created a new partition 1 of type 'Linux' and of size 10 GiB.
Command (m for help): p //显示分区表
Disk /dev/nvme0n3: 10 GiB, 10737418240 bytes, 20971520 sectors
Disk model: VMware Virtual NVMe Disk
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: dos
Disk identifier: 0x5278d3f8
Device Boot Start End Sectors Size Id Type
/dev/nvme0n3p1 2048 20971519 20969472 10G 83 Linux
Command (m for help): w //保存更改
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.
[root@localhost ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
sr0 11:0 1 8.9G 0 rom /mnt
nvme0n1 259:0 0 20G 0 disk
├─nvme0n1p1 259:1 0 1G 0 part /boot
└─nvme0n1p2 259:2 0 19G 0 part
├─rhel-root 253:0 0 17G 0 lvm /
└─rhel-swap 253:1 0 2G 0 lvm
nvme0n2 259:3 0 10G 0 disk
├─nvme0n2p1 259:4 0 2G 0 part /test
├─nvme0n2p2 259:5 0 2G 0 part /test2
├─nvme0n2p3 259:6 0 2G 0 part
├─nvme0n2p4 259:7 0 1K 0 part
└─nvme0n2p5 259:8 0 4G 0 part
nvme0n3 259:9 0 10G 0 disk
└─nvme0n3p1 259:11 0 10G 0 part
[root@localhost ~]# fdisk /dev/nvm0n2
Welcome to fdisk (util-linux 2.37.4).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
fdisk: cannot open /dev/nvm0n2: No such file or directory
[root@localhost ~]# fdisk /dev/nvme0n2
Welcome to fdisk (util-linux 2.37.4).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
This disk is currently in use - repartitioning is probably a bad idea.
It's recommended to umount all file systems, and swapoff all swap
partitions on this disk.
Command (m for help): d //删除分区
Partition number (1-5, default 5): 1
Command (m for help): w
The partition table has been altered.
Failed to remove partition 2 from system: Device or resource busy
Failed to update system information about partition 1: Device or resource busy
The kernel still uses the old partitions. The new table will be used at the next reboot.
Syncing disks.
[root@localhost ~]# mkfs.xfs /dev/nvme0n2p3 //格式化nvme0n2p3
meta-data=/dev/nvme0n2p3 isize=512 agcount=4, agsize=131072 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=1, sparse=1, rmapbt=0
= reflink=1 bigtime=1 inobtcount=1
data = bsize=4096 blocks=524288, 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 blocks=0, rtextents=0
[root@localhost ~]# mkdir test3 //创建一个名为test3的文件
[root@localhost ~]# mount /dev/nvme0n2p3 /root/test3 //将nvme0n2p3挂载到test3上
[root@localhost ~]# rpm -qa | grep lvm //列出系统上已安装的所有RPM包,并筛选出包含"lvm"关键字的包
llvm-libs-15.0.7-1.el9.x86_64
lvm2-libs-2.03.17-7.el9.x86_64
lvm2-2.03.17-7.el9.x86_64
[root@localhost ~]# pv
pvchange pvcreate pvmove pvresize pvscan
pvck pvdisplay pvremove pvs
[root@localhost ~]# pvcreate /dev/nvme0n2p1 /devnvme0n3p1 将/dev/nvme0n2p1和/dev/nvme0n3p1这两个设备初始化为LVM中的物理卷
No device found for /devnvme0n3p1.
Can't open /dev/nvme0n2p1 exclusively. Mounted filesystem?
Can't open /dev/nvme0n2p1 exclusively. Mounted filesystem?
Error opening device /dev/nvme0n2p1 for reading at 0 length 4096.
Cannot use /dev/nvme0n2p1: device has a signature
[root@localhost ~]# lv
lvchange lvm lvm_import_vdo lvremove
lvconvert lvmconfig lvmpolld lvrename
lvcreate lvmdevices lvmsadc lvresize
lvdisplay lvmdiskscan lvmsar lvs
lvextend lvmdump lvreduce lvscan
[root@localhost ~]# lvcreate -n lv1 -l 100%FREE vg1 //在卷组vg1中创建一个名为lv1的逻辑卷,并且使用卷组中的所有可用空间
Logical volume "lv1" created.
[root@localhost ~]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
root rhel -wi-ao---- <17.00g
swap rhel -wi-a----- 2.00g
lv1 vg1 -wi-a----- 11.99g
[root@localhost ~]# mkfs.xfs /dev/vg1/lv1 //在逻辑卷/dev/vg1/lv1上创建一个XFS文件系统
meta-data=/dev/vg1/lv1 isize=512 agcount=4, agsize=785920 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=1, sparse=1, rmapbt=0
= reflink=1 bigtime=1 inobtcount=1
data = bsize=4096 blocks=3143680, 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 blocks=0, rtextents=0
[root@localhost ~]# lvremove /dev/vg1/lv1
Do you really want to remove active logical volume vg1/lv1? [y/n]: y
Logical volume "lv1" successfully removed.
[root@localhost ~]# lvs //显示逻辑卷的信息
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
root rhel -wi-ao---- <17.00g
swap rhel -wi-a----- 2.00g
[root@localhost ~]# lvcreate -n lv1 -L 8G vg1 //创建一个8GB大小的逻辑卷
WARNING: xfs signature detected on /dev/vg1/lv1 at offset 0. Wipe it? [y/n]: y
Wiping xfs signature on /dev/vg1/lv1.
Logical volume "lv1" created.
[root@localhost ~]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
root rhel -wi-ao---- <17.00g
swap rhel -wi-a----- 2.00g
lv1 vg1 -wi-a----- 8.00g
[root@localhost ~]# cd /mnt
[root@localhost mnt]# ll
total 44
drwxr-xr-x. 1 lisi systemd-oom 2048 Apr 14 2023 AppStream
drwxr-xr-x. 1 lisi systemd-oom 2048 Apr 14 2023 BaseOS
drwxrwxr-x. 1 root root 2048 Apr 14 2023 EFI
-rw-r--r--. 1 lisi systemd-oom 8154 Apr 14 2023 EULA
-rw-r--r--. 1 lisi systemd-oom 1455 Apr 14 2023 extra_files.json
-rw-r--r--. 1 lisi systemd-oom 18092 Apr 14 2023 GPL
drwxrwxr-x. 1 root root 2048 Apr 14 2023 images
drwxrwxr-x. 1 root root 2048 Apr 14 2023 isolinux
-rw-r--r--. 1 lisi systemd-oom 103 Apr 14 2023 media.repo
-rw-r--r--. 1 lisi systemd-oom 1669 Apr 14 2023 RPM-GPG-KEY-redhat-beta
-rw-r--r--. 1 lisi systemd-oom 3682 Apr 14 2023 RPM-GPG-KEY-redhat-release
[root@localhost mnt]# cd
[root@localhost ~]# umount /mnt
[root@localhost ~]# mount /dev/vg1/lv1 /mnt
[root@localhost ~]# mount | grep /mnt //显示系统上所有已挂载的文件系统,并且过滤出挂载点为/mnt的文件系统
/dev/mapper/vg1-lv1 on /mnt type xfs (rw,relatime,seclabel,attr2,inode64,logbufs=8,logbsize=32k,noquota)
[root@localhost ~]# lvextend /dev/vg1/lv1 -L +2g //将这个逻辑卷的大小扩展2GB
Size of logical volume vg1/lv1 changed from 8.00 GiB (2048 extents) to 10.00 GiB (2560 extents).
Logical volume vg1/lv1 successfully resized.
[root@localhost ~]# xfs_growfs /mnt //在XFS文件系统上扩展已有的文件系统
Filesystem Size Used Avail Use% Mounted on
devtmpfs 4.0M 0 4.0M 0% /dev
tmpfs 876M 0 876M 0% /dev/shm
tmpfs 351M 8.6M 342M 3% /run
/dev/mapper/rhel-root 17G 5.4G 12G 32% /
/dev/nvme0n1p1 1014M 293M 722M 29% /boot
tmpfs 176M 88K 175M 1% /run/user/0
/dev/mapper/vg1-lv1 10G 104M 9.9G 2% /mnt
[root@localhost ~]# umount /mnt
[root@localhost ~]# lvremove /dev/vg1/lv1 //移除逻辑卷/dev/vg1/lv1
Do you really want to remove active logical volume vg1/lv1? [y/n]: y
Logical volume "lv1" successfully removed.
[root@localhost ~]# vgremove /dev/vg1 //移除名为vg1的卷组
Volume group "vg1" successfully removed
[root@localhost ~]# pvremove /dev/nvme0n2p1 /dev/nvme0n3p1 //移除物理卷/dev/nvme0n2p1和/dev/nvme0n3p1
Labels on physical volume "/dev/nvme0n2p1" successfully wiped.
Labels on physical volume "/dev/nvme0n3p1" successfully wiped.
[root@localhost ~]# pvs //显示物理卷的信息
PV VG Fmt Attr PSize PFree
/dev/nvme0n1p2 rhel lvm2 a-- <19.00g 0
[root@localhost ~]# vgs //显示系统上所有卷组的信息
VG #PV #LV #SN Attr VSize VFree
rhel 1 2 0 wz--n- <19.00g 0