Linux---磁盘管理

1.磁盘外部结构

存储数据的地方
磁盘分类:
第一类: 机械磁盘 通过机械运动读写数据 台式机 3.5英寸
第二类: 固态磁盘 芯片
第三类: NVME磁盘 PCI-E 类似显卡
接口类型及转速: 决定了读写速度
IDE 接口 淘汰
SCSI接口 不常用
SATA接口 笔记本 台式机 7.2K转/10K
SAS接口 服务器标配 7.2K 10K 15K
15K/每分钟最大存储 900G 600G 300G 6008 6004块磁盘
比如: 做备份服务器 只管空间大 2T 4T*8
磁盘大小: 3.5英寸 2.5英寸

2.磁盘分区格式

MBR格式:
最多支持4个主分区
支持3个主分区+1个扩展分区
基于扩展分区划分逻辑分区(逻辑分区理论上没有限制)
磁盘默认为MBR格式
GPT格式:
最多支持128个主分区
MBR格式和GPT格式分区区别:
MBR格式对于小于2T的磁盘进行分区 使用fdisk分区
GPT格式对于大于2T的磁盘进行分区 使用parted分区
磁盘分区的设备名称:
第一块磁盘: sda
第二块磁盘: sdb
...
第一块磁盘的第一个主分区: sda1
第一块磁盘的第二个主分区: sda2
第一块磁盘的第三个主分区: sda3
第一块磁盘的第一个扩展分区: sda4
第一块磁盘的第一个逻辑分区: ada5

3.磁盘挂载

3.1.挂载cdrom

Linux系统中的硬件必须通过挂载的方式才可以访问
默认无法读取硬件(类似于没有窗户没有门的监狱 进不去也出不来)



案例1: 挂载cdrom



第一步: 将镜像文件插入到DVD(装系统已经插进去了)



第二步: 查看dev下的cdrom



第三步: 直接将cdrom挂载到临时的挂载点/mnt
[root@centos ~]#mount /dev/cdrom /mnt
mount: /dev/sr0 is write-protected, mounting read-only
写保护 cdrom只读



查看挂载信息:
[root@centos ~]#df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 980M 0 980M 0% /dev
tmpfs 991M 0 991M 0% /dev/shm
tmpfs 991M 26M 965M 3% /run
tmpfs 991M 0 991M 0% /sys/fs/cgroup
/dev/sda3 19G 2.2G 17G 12% /
/dev/sda1 197M 110M 88M 56% /boot
tmpfs 199M 0 199M 0% /run/user/0
/dev/sr0 4.5G 4.5G 0 100% /mnt



第四步: 通过大门/mnt进去cdrom中查看里面的内容
cd /mnt
ls



第五步: 卸载cdrom 注意退出/mnt目录才可以卸载
[root@centos ~]#umount /mnt
[root@centos ~]#df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 980M 0 980M 0% /dev
tmpfs 991M 0 991M 0% /dev/shm
tmpfs 991M 26M 965M 3% /run
tmpfs 991M 0 991M 0% /sys/fs/cgroup
/dev/sda3 19G 2.2G 17G 12% /
/dev/sda1 197M 110M 88M 56% /boot
tmpfs 199M 0 199M 0% /run/user/0



注意: 挂载的目录下面有内容会被隐藏,卸载后1.txt会显示
/data/1.txt
mount /dev/cdrom /data

3.2.磁盘挂载

磁盘挂载:
第一步: 虚拟机右键设置添加一块500M的磁盘



第二步: 添加完成后需要重启系统
注意:
1.vmware添加磁盘需要重启 
2.企业中使用硬件服务器插入磁盘不需要重启 支持热插拔
3.云服务器不需要重启
[root@centos ~]#reboot



第三步: 使用fdisk -l命令查看磁盘分区信息


第一块磁盘: sda
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: 0x000230aa
Device Boot Start End Blocks Id System
/dev/sda1 * 2048 411647 204800 83 Linux
/dev/sda2 411648 2508799 1048576 82 Linux swap / Solaris
/dev/sda3 2508800 41943039 19717120 83 Linux



第二块磁盘: sdb
Disk /dev/sdb: 536 MB, 536870912 bytes, 1048576 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



第四步: 对磁盘进行分区(类似房子打隔断)
[root@centos ~]#fdisk /dev/sdb
Command (m for help): m
d delete a partition # 删除分区
l list known partition types # 列出分区类型
m print this menu # 显示帮助信息 菜单信息
n add a new partition # 创建分区
p print the partition table # 打印分区信息
q quit without saving changes # 退出不保存
w write table to disk and exit # 保存并退出




Command (m for help): n # n 创建分区
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended



Select (default p): p # 选择创建分区类似 p默认为主分区
Partition number (1-4, default 1): 1 # 主分区的编号
First sector (2048-1048575, default 2048): # 开始的扇区默认回车
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-1048575, default 1048575): +100M # 大小


Partition 1 of type Linux and of size 100 MiB is set
Command (m for help): p # 查看分区表
Disk /dev/sdb: 536 MB, 536870912 bytes, 1048576 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: 0x5f63be54
Device Boot Start End Blocks Id System
/dev/sdb1 2048 206847 102400 83 Linux
省略创建剩下的三个主分区........





删除第四个主分区:
Command (m for help): d
Partition number (1-4, default 4): 4
Partition 4 is deleted




划分扩展分区:
Command (m for help): n
Partition type:
p primary (3 primary, 0 extended, 1 free)
e extended
Select (default e):
Using default response e
Selected partition 4
First sector (616448-1048575, default 616448):
Using default value 616448
Last sector, +sectors or +size{K,M,G} (616448-1048575, default 1048575):
Using default value 1048575
Partition 4 of type Extended and of size 211 MiB is set
Command (m for help): p
/dev/sdb1 2048 206847 102400 83 Linux
/dev/sdb2 206848 411647 102400 83 Linux
/dev/sdb3 411648 616447 102400 83 Linux
/dev/sdb4 616448 1048575 216064 5 Extended




第五步: 格式化磁盘(打扫卫生)
1.[root@centos ~]#mkfs.xfs /dev/sdb1
meta-data=/dev/sdb1 isize=512 agcount=4, agsize=6400 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0, sparse=0
data = bsize=4096 blocks=25600, 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




2.[root@centos ~]#mkfs.xfs /dev/sdb5
meta-data=/dev/sdb5 isize=512 agcount=4, agsize=6400 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0, sparse=0
data = bsize=4096 blocks=25600, 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




第六步: 挂载使用磁盘
[root@centos ~]#mount /dev/sdb1 /mnt
[root@centos ~]#df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 980M 0 980M 0% /dev
tmpfs 991M 0 991M 0% /dev/shm
tmpfs 991M 9.6M 981M 1% /run
tmpfs 991M 0 991M 0% /sys/fs/cgroup
/dev/sda3 19G 2.2G 17G 12% /
/dev/sda1 197M 110M 88M 56% /boot
tmpfs 199M 0 199M 0% /run/user/0
/dev/sdb1 97M 5.3M 92M 6% /mnt

3.3.企业常用的挂载磁盘的方式

1.插入磁盘 :虚拟机添加磁盘



2.格式化磁盘
[root@centos ~]#mkfs.xfs -f /dev/sdb
meta-data=/dev/sdb isize=512 agcount=4, agsize=32768 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0, sparse=0
data = bsize=4096 blocks=131072, 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



3.挂载磁盘
[root@centos ~]#mkdir /data
[root@centos ~]#mount /dev/sdb /data
[root@centos ~]#df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 980M 0 980M 0% /dev
tmpfs 991M 0 991M 0% /dev/shm
tmpfs 991M 9.5M 981M 1% /run
tmpfs 991M 0 991M 0% /sys/fs/cgroup
/dev/sda3 19G 2.2G 17G 12% /
/dev/sda1 197M 110M 88M 56% /boot
tmpfs 199M 0 199M 0% /run/user/0
/dev/sdb 509M 26M 483M 6% /data

4.开机自动挂载磁盘

vim /etc/fstab
加入/etc/fstab 开机自动挂载硬件
[root@centos ~]#tail -1 /etc/fstab
/dev/sdb /data xfs defaults 0 0
硬件名称 挂载点 挂载类型 挂载参数 是否备份 是否检查
写入/etc/rc.local
vim /etc/rc.local
mount /dev/sdb /data

5.大于2T的磁盘进行分区

5.1.挂载步骤

大于2T的磁盘需要将格式修改为GPT
使用命令parted进行分区 直接生效

第一步: 虚拟机添加一块2T的磁盘

第二步: 重启虚拟机
[root@centos ~]#reboot


第三步: 查看是否添加成功
fdisk -l


第四步: parted进行分区
[root@centos ~]#parted /dev/sdc
mklabel,mktable LABEL-TYPE create a new disklabel (partition table)
# 创建新的格式
mkpart PART-TYPE [FS-TYPE] START END make a partition
# 创建分区
print # 显示分区表
quit # 退出
rm NUMBER # 删除分区
(parted) mklabel gpt # 将分区表格式更改为gpt



第五步: 格式化分区
[root@centos ~]#mkfs.xfs /dev/sdc1
meta-data=/dev/sdc1 isize=512 agcount=4, agsize=6103515 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0, sparse=0
data = bsize=4096 blocks=24414058, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
log =internal log bsize=4096 blocks=11920, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0



第六步: 挂载使用
[root@centos ~]#mount /dev/sdc1 /data
[root@centos ~]#df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 980M 0 980M 0% /dev
tmpfs 991M 0 991M 0% /dev/shm
tmpfs 991M 9.6M 981M 1% /run
tmpfs 991M 0 991M 0% /sys/fs/cgroup
/dev/sda3 19G 2.2G 17G 12% /
/dev/sdc1 94G 33M 94G 1% /data

5.2.分区方式:交互式分区

(parted) mkpart # 分区命令
Partition name? []? primary # 分区名称
File system type? [ext2]? xfs # 分区类型
Start? 0 #从哪里开始
End? 100G # 大小
Warning: The resulting partition is not properly aligned for best performance.
Ignore/Cancel? I # 输入I 忽略警告
(parted) print # 打印分区表
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdc: 2147GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:
Number Start   End    Size  File system Name Flags
1    17.4kB    100GB  100GB            primary

5.3.分区方式:非交互式分区

(parted) mkpart primary xfs 100G 200G
(parted) print
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdc: 2147GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:
Number Start   End    Size File system Name Flags
 1    17.4kB   100GB  100GB           primary
 2     100GB   200GB   100GB          primary


不进去mkpartd命令进行分区:
[root@centos ~]#parted /dev/sdc mkpart primary xfs 200G 500G
Information: You may need to update /etc/fstab.
[root@centos ~]#parted /dev/sdc print
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdc: 2147GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:
Number Start End Size File system Name Flags
1 17.4kB 100GB 100GB primary
2 100GB 200GB 100GB primary
3 200GB 500GB 300GB primary

5.4.删除分区

[root@centos ~]#parted /dev/sdc rm 3
Information: You may need to update /etc/fstab.
[root@centos ~]#parted /dev/sdc print
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdc: 2147GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:
Number Start End Size File system Name Flags
1 17.4kB 100GB 100GB primary
2 100GB 200GB 100GB primary
posted @ 2024-01-20 22:40  只为心情愉悦  阅读(29)  评论(0编辑  收藏  举报