1. VM虚拟机添加磁盘,识别磁盘将其添加到分区
注意:reboot之后系统才能识别新加硬盘。
2. fdisk命令:fdisk -l 查看系统的分区详细信息
[root@localhost ~]# fdisk -l
Disk /dev/sda: 21.5 GB, 21474836480 bytes, 41943040 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: 0x000c2da4
Device Boot Start End Blocks Id System
/dev/sda1 * 2048 976895 487424 83 Linux
/dev/sda2 976896 41943039 20483072 8e Linux LVM
Disk /dev/sdb: 1073 MB, 1073741824 bytes, 2097152 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/sda1 后面有个*,表示该磁盘用于引导系统进行启动。
blocks:表示块的信息,一个块里面有多个扇区。
Id:表示磁盘编号。
system:表示Linux系统分区的一个分区信息。
3. fdisk 对磁盘进行分区:
[root@localhost ~]# fdisk /dev/sdb
Welcome to fdisk (util-linux 2.23.2).
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
Building a new DOS disklabel with disk identifier 0xbf249344.
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 删除一个分区
g create a new empty GPT partition table
G create an IRIX (SGI) partition table
l list known partition types 列出分区类型
m print this menu 输出帮助信息
n add a new partition 建立一个新的分区
o create a new empty DOS partition table 建立一个新的空白DOS分区表
p print the partition table 打印分区表
q quit without saving changes 退出不保存设置
s create a new empty Sun disklabel
t change a partition's system id 改变分区的ID
u change display/entry units 改变显示的单位
v verify the partition table 检查验证分区表
w write table to disk and exit 保存分区表
x extra functionality (experts only)
Command (m for help): n
Partition type:
p primary (0 primary, 0 extended, 4 free) p是主分区
e extended e是扩展分区
Select (default p): p
Partition number (1-4, default 1): 1 定义分区号(主分区最多只能有四个)
First sector (2048-2097151, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-2097151, default 2097151): 大小
Using default value 2097151
Partition 1 of type Linux and of size 1023 MiB is set
Command (m for help): p
Disk /dev/sdb: 1073 MB, 1073741824 bytes, 2097152 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: 0xbf249344
Device Boot Start End Blocks Id System
/dev/sdb1 2048 2097151 1047552 83 Linux
Command (m for help): w 保存刚才的配置信息
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
4. 对磁盘进行更新:partprobe 或者是:partx /dev/sdb1
[root@localhost ~]# partx /dev/sdb1
NR START END SECTORS SIZE NAME UUID
1 2048 2097151 2095104 1023M
5. 磁盘的格式化
格式化:指将分区格式化成不同的文件系统。
Linux系统下的文件类型:[root@localhost ~]# mkfs.
mkfs.btrfs mkfs.cramfs mkfs.ext2 mkfs.ext3 mkfs.ext4 mkfs.fat mkfs.minix mkfs.msdos mkfs.vfat mkfs.xfs
现在对刚刚创建的主分区1进行格式化,类型为xfs:
[root@localhost ~]# mkfs.xfs /dev/sdb1
meta-data=/dev/sdb1 isize=512 agcount=4, agsize=65472 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0, sparse=0
data = bsize=4096 blocks=261888, 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
查看分区情况可使用 lsblk -f 命令:
[root@localhost ~]# lsblk -f
NAME FSTYPE LABEL UUID MOUNTPOINT
sda
├─sda1 xfs 9be4187c-0d1e-40e9-b858-9e19a6b93445 /boot
└─sda2 LVM2_member jFYd2k-mpJ3-vfX3-NCnp-GGiB-KfEt-p5oyNO
├─centos-root xfs 929eb6b2-1a4b-4271-83f5-962f573954a7 /
├─centos-swap swap 7ca240cd-fc22-4efe-9d4d-2bbce981a464 [SWAP]
└─centos-home xfs bce8fb0d-6907-433d-b682-5f67626ca380 /home
sdb
└─sdb1 xfs 23aceabe-183b-47eb-9af5-de0eb01a5c42
sr0 iso9660 CentOS 7 x86_64 2020-11-04-11-36-43-00
6. 磁盘的挂载
(1)文件系统的使用:首先要挂载:mount命令和umount命令
根文件系统以外的其他文件系统想要能够被访问,都必须通过“关联”至根文件系统上的某个目录来实现,此关联操作即为“挂载”;此目录即为“挂载点”。
挂载点:mount_point 用于作为另一个文件系统的访问入口:1)事先存在;2)应该未被使用或不被其他进程使用到的目录;3)挂载点下原有的文件将会被隐藏;
(2)挂载:mount 设备名称 挂载目录
卸载:umount 设备名称 或者是 umount 挂载目录
挂载:
[root@localhost ~]# cd /
[root@localhost /]# mkdir newdisk
[root@localhost /]# mount /dev/sdb1 /newdisk/
[root@localhost /]# lsblk -f
NAME FSTYPE LABEL UUID MOUNTPOINT
sda
├─sda1 xfs 9be4187c-0d1e-40e9-b858-9e19a6b93445 /boot
└─sda2 LVM2_member jFYd2k-mpJ3-vfX3-NCnp-GGiB-KfEt-p5oyNO
├─centos-root xfs 929eb6b2-1a4b-4271-83f5-962f573954a7 /
├─centos-swap swap 7ca240cd-fc22-4efe-9d4d-2bbce981a464 [SWAP]
└─centos-home xfs bce8fb0d-6907-433d-b682-5f67626ca380 /home
sdb
└─sdb1 xfs 23aceabe-183b-47eb-9af5-de0eb01a5c42 /newdisk
sr0 iso9660 CentOS 7 x86_64 2020-11-04-11-36-43-00
卸载:
[root@localhost /]# umount /dev/sdb1
[root@localhost /]# lsblk -f
NAME FSTYPE LABEL UUID MOUNTPOINT
sda
├─sda1 xfs 9be4187c-0d1e-40e9-b858-9e19a6b93445 /boot
└─sda2 LVM2_member jFYd2k-mpJ3-vfX3-NCnp-GGiB-KfEt-p5oyNO
├─centos-root xfs 929eb6b2-1a4b-4271-83f5-962f573954a7 /
├─centos-swap swap 7ca240cd-fc22-4efe-9d4d-2bbce981a464 [SWAP]
└─centos-home xfs bce8fb0d-6907-433d-b682-5f67626ca380 /home
sdb
└─sdb1 xfs 23aceabe-183b-47eb-9af5-de0eb01a5c42
sr0 iso9660 CentOS 7 x86_64 2020-11-04-11-36-43-00
7. 永久挂载:使用永久挂载就意味着开机自动挂载。
编辑 /etc/fstab
#
# /etc/fstab
# Created by anaconda on Mon Aug 1 21:17:23 2022
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/centos-root / xfs defaults 0 0
UUID=9be4187c-0d1e-40e9-b858-9e19a6b93445 /boot xfs defaults 0 0
/dev/mapper/centos-home /home xfs defaults 0 0
/dev/mapper/centos-swap swap swap defaults 0 0
/dev/sdb1 /newdisk xfs defaults 0 0
在命令模式下按o键进入插入模式,同时换行。/dev/sdb1指挂载点目录,/指挂载点,xfs指文件系统类型,defaults指默认权限,第一个0表示不备份,第二个0表示不检查。注意格式一定要正确,可以不用对齐,最好的判断依据是依照颜色。写完之后保存退出。
8. 卸载:
umount 磁盘分区
[root@192 ~]# umount /dev/sdb1
[root@192 ~]# lsblk -f
NAME FSTYPE LABEL UUID MOUNTPO INT
sda
├─sda1 xfs 43279e56-aa33-4f2a-a591-67a1a5c59271 /boot
└─sda2 LVM2_mem Gtq6Wy-KaEL-AV76-NhPr-R3dP-WzI1-rcG0HJ
├─centos-root xfs 4b3e49a6-c242-4c30-97a9-eaaddff7f1ff /
├─centos-swap swap 060bfbbd-42c0-437e-b63e-61d52b2a2d0d [SWAP]
└─centos-home xfs 1cd3a22a-4fef-4202-9e9c-d1b290370246 /home
sdb
└─sdb1 xfs 020320b8-4f29-4774-9e0c-21dcee604512
注意:再次查看时,此时已经没有挂载点了。
9. 删除分区:
删除分区命令:fdisk 磁盘
fdisk /dev/vdb
d 删除一个分区;
Enter
p 打印分区表信息,查看分区是否已被删除
w 保存分区表
[root@192 ~]# fdisk /dev/sdb
Welcome to fdisk (util-linux 2.23.2).
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/sdb: 1073 MB, 1073741824 bytes, 2097152 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: 0x29c83098
Device Boot Start End Blocks Id System
/dev/sdb1 2048 2097151 1047552 83 Linux
Command (m for help): d
Selected partition 1
Partition 1 is deleted
Command (m for help):
Command (m for help): p
Disk /dev/sdb: 1073 MB, 1073741824 bytes, 2097152 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: 0x29c83098
Device Boot Start End Blocks Id System
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
此时查看分区时,已无原分区:
[root@192 ~]# lsblk -f
NAME FSTYPE LABEL UUID MOUNTPO INT
sda
├─sda1 xfs 43279e56-aa33-4f2a-a591-67a1a5c59271 /boot
└─sda2 LVM2_mem Gtq6Wy-KaEL-AV76-NhPr-R3dP-WzI1-rcG0HJ
├─centos-root xfs 4b3e49a6-c242-4c30-97a9-eaaddff7f1ff /
├─centos-swap swap 060bfbbd-42c0-437e-b63e-61d52b2a2d0d [SWAP]
└─centos-home xfs 1cd3a22a-4fef-4202-9e9c-d1b290370246 /home
sdb
sr0 iso9660 CentOS 7 x86_64
2020-11-04-11-36-43-00