一、添加硬盘
二、添加完成之后,如果想要能够识别,需要重启虚拟机,不过这里也可以通过命令的方式识别
[root@localhost ~]# echo "- - -" >> /sys/class/scsi_host/host0/scan
[root@localhost ~]# echo "- - -" >> /sys/class/scsi_host/host1/scan
[root@localhost ~]# echo "- - -" >> /sys/class/scsi_host/host2/scan
/sys/class/scsi_host/ 下面有几个host 就扫描几次 。
三、查看所有分区情况
[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: 0x0009d78a
Device Boot Start End Blocks Id System
/dev/sda1 * 2048 2099199 1048576 83 Linux
/dev/sda2 2099200 41943039 19921920 8e Linux LVM
Disk /dev/mapper/centos-root: 18.2 GB, 18249416704 bytes, 35643392 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/mapper/centos-swap: 2147 MB, 2147483648 bytes, 4194304 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: 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
/dev/sdb就是你刚刚添加的磁盘。
四、给磁盘添加分区
[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 0xb8805589.
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
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
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)
Command (m for help):
输入n并回车,创建一个分区。
Command (m for help): n
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
Select (default p):
选择分区类型。p为主分区,e为扩展分区
Select (default p): p
Partition number (1-4, default 1):
选择区号。
Partition number (1-4, default 1): 1
First sector (2048-41943039, default 2048): 2048
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-41943039, default 41943039): +5G
Partition 1 of type Linux and of size 5 GiB is set
选择起始位置与结束位置,也可用 +5G的这种方式,到这里,这个分区也就创建完成了。
输入w并回车,意思是对刚才的结果进行保存。
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
再次使用fdisk -l查看分区的结果,如下分的新区为/dev/sdb1
[root@localhost ~]# fdisk -l
...
Disk /dev/sdb: 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: 0x6f942ad1
Device Boot Start End Blocks Id System
/dev/sdb1 2048 10487807 5242880 83 Linux
五、格式化分区
要使用这些分区,还要进行格式化操作(给分区配置一个文件系统),不同的磁盘分区,可以做不同的 文件系统。Linux7版本的默认文件系统类型为:xfs.
[root@localhost ~]# mkfs.xfs /dev/sdb1
meta-data=/dev/sdb1 isize=512 agcount=4, agsize=327680 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0, sparse=0
data = bsize=4096 blocks=1310720, 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 /mnt/xuexi
[root@localhost ~]# mount /dev/sdb1 /mnt/xuexi
查看挂载情况。查看内存情况。
[root@localhost ~]# mount | tail -1
/dev/sdb1 on /mnt/xuexi type xfs (rw,relatime,seclabel,attr2,inode64,noquota)
[root@localhost ~]# df -hT
Filesystem Type Size Used Avail Use% Mounted on
/dev/mapper/centos-root xfs 17G 3.7G 14G 22% /
devtmpfs devtmpfs 1.9G 0 1.9G 0% /dev
tmpfs tmpfs 1.9G 0 1.9G 0% /dev/shm
tmpfs tmpfs 1.9G 13M 1.9G 1% /run
tmpfs tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup
/dev/sda1 xfs 1014M 179M 836M 18% /boot
tmpfs tmpfs 378M 4.0K 378M 1% /run/user/42
tmpfs tmpfs 378M 52K 378M 1% /run/user/1000
tmpfs tmpfs 378M 0 378M 0% /run/user/0
/dev/sdb1 xfs 5.0G 33M 5.0G 1% /mnt/xuexi
六、给分区永久挂载,也就是开机自动挂载
编辑/etc/fstab文件
[root@localhost ~]# vim /etc/fstab
#
# /etc/fstab
# Created by anaconda on Sat Apr 2 15:59:04 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=9a896cd1-b64f-4f33-ae0e-4b60b7597176 /boot xfs defaults 0 0
/dev/mapper/centos-swap swap swap defaults 0 0
/dev/sdb1 /mnt/xuexi xfs defaults 0 0
[root@localhost ~]# mount -a
mount -a 的意思是将/etc/fstab的所有内容重新加载。
到这里,分区的整个流程就结束了。