磁盘

磁盘管理

磁盘的作用:存储数据的大小 视频......
磁盘分类:
1)机械硬盘     台式机  重 大 慢
2)固态硬盘     轻 小 快
3)NVme固态硬盘 轻 小 快
外观大小:
3.5英寸  存储大
2.5英寸  存储小

磁盘接口:
IED接口 淘汰
SCSI    淘汰 
SATA    常用     笔记本 台式机 家用
SAS     企业标配 

转速
SATA 转速慢 存储高 1T 2T 4T 6T ... 1万转/每分钟
SAS  转速快 存储小 300G 600G 900G 每分钟15000转最快 网站的数据 用户可以访问到的业务
企业:是SAS和SATA结合使用 热点存储到SAS(用户经常访问的数据),冷数据存储到SATA(用户不经常访问的数据)

内部
磁盘-硬盘(Disk)
磁头(Head)
盘面数量==磁头数量
作用:用来写入和读取数据的
径向运动
磁道(Track)
从外面到最里面 最外面是0磁道
扇区(Sector)
磁道上面最小的单位
默认大小512字节
柱面(Cylinder)
不同盘面上面的相同的磁道组成的圆柱体
不同盘面上的相同的呼啦圈组成的圆柱体
磁盘默认是按照煮面进行读写
磁头之间的切换速度 接近于光速
磁头径向运动-寻道 机械运动 慢

磁盘阵列
RAID几    几块磁盘         安全冗余                    可用容量    性能          使用场景                 举例
RAID0     最少一块磁盘     不安全(损坏一块都不行)      磁盘总和  读写最快   只要求速度、不要求安全      数据库从节点
RAID1     只能有两块磁盘    100%冗余                    一半      最慢      要求安全、速度一般          系统、企业内部服务
RAID5     最少需要3块      可以损坏一块盘               n-1块     读写还ok   业务较稳定发展             业务量、数据库不高
RAID10    最少需要4块      可以损坏一半                  一半      速度快     高并发业务场景             数据库主库业务

磁盘的名称 分区的名称
sda 第一块磁盘
sad1
adb 第二块磁盘
adb1

磁盘分区

fdisk分区 MBR 最高支持4个主分区  ctrl + u 清空所写内容
mount -a #让/etc/fstab执行一遍 如果挂载了不提示 如果没有挂载则自动挂载
小于2TB的磁盘 使用fdisk分区  MBR
大于2TB的磁盘 私用parted分区 GPT
逻辑分区基于拓展分区去分
对于小于2TB的磁盘分区 fdisk
总结:
第一步:添加一块新的硬盘
第二步:查看是否可以识别到第二块磁盘
fdisk -l 重启生效 reboot
第三步:磁盘分区
第四步:格式化
mkfs.xfs /dev/sdb1格式化第一个分区
第五步:挂载使用
第六步:开机自动挂载
/etc/fstab
/etc/rc.local





第一步:添加一块新的硬盘
虚拟机:右键 --> 设置 --> 添加 --> 硬盘 --> 下一步
物理机:拿到新的磁盘直接插入到面板处即可 支持热插拔
云服务器:阿里云 亚马逊云 支持在线磁盘扩容




第二步:查看是否可以识别到第二块磁盘
虚拟机:fdisk -l 重启生效 reboot
Disk /dev/sdb: 500 GiB, 536870912000 bytes, 1048576000 sectors
Disk model: VMware Virtual S
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes




第三步:磁盘分区
虚拟机:fdisk /dev/sdb
[root@Sovs1108:~]# fdisk /dev/sdb

Welcome to fdisk (util-linux 2.35.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.
Created a new DOS disklabel with disk identifier 0xae5f3f81.

Command (m for help): m 
使用m使用帮助

d 删除分区
n 创建分区
p 打印分区
m 显示菜单
w 保存并退出
q 退出不保存

第一次分区:
Command (m for help): n #创建分区
Partition type
   p   primary (0 primary, 0 extended, 4 free)
   e   extended (container for logical partitions)
Select (default p): p  #创建主分区
Partition number (1-4, default 1):   #分区的编号 默认回车 sdb1
First sector (2048-1048575999, default 2048): #开始的扇区  默认回车 
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-1048575999, default 1048575999): +100G   #分区100G

Created a new partition 1 of type 'Linux' and of size 100 GiB.

第二次分区:
Command (m for help): n
Partition type
   p   primary (1 primary, 0 extended, 3 free)
   e   extended (container for logical partitions)
Select (default p): 

Using default response p.
Partition number (2-4, default 2): 
First sector (209717248-1048575999, default 209717248): 
Last sector, +/-sectors or +/-size{K,M,G,T,P} (209717248-1048575999, default 1048575999): +100G

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


第三次分区:
Command (m for help):  n
Partition type
   p   primary (2 primary, 0 extended, 2 free)
   e   extended (container for logical partitions)
Select (default p): 

Using default response p.
Partition number (3,4, default 3): 
First sector (419432448-1048575999, default 419432448): 
Last sector, +/-sectors or +/-size{K,M,G,T,P} (419432448-1048575999, default 1048575999): +100G

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


第四次分区:
Command (m for help): n
Partition type
   p   primary (3 primary, 0 extended, 1 free)
   e   extended (container for logical partitions)
Select (default e): p

Selected partition 4
First sector (629147648-1048575999, default 629147648): 
Last sector, +/-sectors or +/-size{K,M,G,T,P} (629147648-1048575999, default 1048575999): +100G

Created a new partition 4 of type 'Linux' and of size 100 GiB.

报错: #最高支持四个主分区
Command (m for help): n
To create more partitions, first replace a primary with an extended partition.

删除分区:d #删除分区 默认删除最后面的
Command (m for help): d
Partition number (1-4, default 4): 4

Partition 4 has been deleted.





创建拓展分区:#逻辑分区基于拓展分区去分#不能用拓展分区
Command (m for help): n
Partition type
   p   primary (3 primary, 0 extended, 1 free)
   e   extended (container for logical partitions)
Select (default e): e #创建拓展分区

Selected partition 4
First sector (629147648-1048575999, default 629147648): 
Last sector, +/-sectors or +/-size{K,M,G,T,P} (629147648-1048575999, default 1048575999):  #m默认全部给予

Created a new partition 4 of type 'Extended' and of size 200 GiB.

分逻辑分区1:
Command (m for help): n
All primary partitions are in use.
Adding logical partition 5


First sector (629149696-1048575999, default 629149696): 
Last sector, +/-sectors or +/-size{K,M,G,T,P} (629149696-1048575999, default 1048575999): +100G

Created a new partition 5 of type 'Linux' and of size 100 GiB.

分逻辑分区2:#一共500G 最后默认给予100G
Command (m for help): n
All primary partitions are in use.
Adding logical partition 6
First sector (838866944-1048575999, default 838866944): 

Last sector, +/-sectors or +/-size{K,M,G,T,P} (838866944-1048575999, default 1048575999): 

Created a new partition 6 of type 'Linux' and of size 100 GiB.

查看分区:p


Command (m for help): p
Disk /dev/sdb: 500 GiB, 536870912000 bytes, 1048576000 sectors
Disk model: VMware Virtual S
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: 0xae5f3f81

Device     Boot     Start        End   Sectors  Size Id Type
/dev/sdb1            2048  209717247 209715200  100G 83 Linux
/dev/sdb2       209717248  419432447 209715200  100G 83 Linux
/dev/sdb3       419432448  629147647 209715200  100G 83 Linux
/dev/sdb4       629147648 1048575999 419428352  200G  5 Extended
/dev/sdb5       629149696  838864895 209715200  100G 83 Linux
/dev/sdb6       838866944 1048575999 209709056  100G 83 Linux
#一共五个分区 sdb4为拓展分区 逻辑分区基于拓展分区创建 所以sdb4 逻辑分区用不了


退出:w保存并且退出
Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.


查看磁盘分区信息:fdisk -l
Disk /dev/sdb: 500 GiB, 536870912000 bytes, 1048576000 sectors
Disk model: VMware Virtual S
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: 0xae5f3f81

Device     Boot     Start        End   Sectors  Size Id Type
/dev/sdb1            2048  209717247 209715200  100G 83 Linu
/dev/sdb2       209717248  419432447 209715200  100G 83 Linu
/dev/sdb3       419432448  629147647 209715200  100G 83 Linu
/dev/sdb4       629147648 1048575999 419428352  200G  5 Exte
/dev/sdb5       629149696  838864895 209715200  100G 83 Linu
/dev/sdb6       838866944 1048575999 209709056  100G 83 Linu







第四步:格式化
mkfs.xfs /dev/sdb1格式化第一个分区
磁盘文件类型表示房间装修不同风格 决定了业务的快慢
[root@Sovs1108:~]# mkfs.xfs /dev/sdb1
meta-data=/dev/sdb1              isize=512    agcount=4, agsize=6553600 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=1, sparse=1, rmapbt=0
         =                       reflink=1
data     =                       bsize=4096   blocks=26214400, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0, ftype=1
log      =internal log           bsize=4096   blocks=12800, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
以此类推.....



第五步:挂载使用
[root@Sovs1108:data]# mount /dev/sdb1 /data/test01
[root@Sovs1108:data]# df -h
Filesystem             Size  Used Avail Use% Mounted on
devtmpfs               451M     0  451M   0% /dev
tmpfs                  467M     0  467M   0% /dev/shm
tmpfs                  467M  6.8M  460M   2% /run
tmpfs                  467M     0  467M   0% /sys/fs/cgroup
/dev/mapper/klas-root   47G  3.8G   44G   8% /
tmpfs                  467M     0  467M   0% /tmp
/dev/sda1             1014M  169M  846M  17% /boot
tmpfs                   94M     0   94M   0% /run/user/0
/dev/sdb1              100G  746M  100G   1% /data/test01


卸载磁盘:
[root@Sovs1108:~]# umount /data/test01 
[root@Sovs1108:~]# df -h
Filesystem             Size  Used Avail Use% Mounted on
devtmpfs               451M     0  451M   0% /dev
tmpfs                  467M     0  467M   0% /dev/shm
tmpfs                  467M  6.8M  460M   2% /run
tmpfs                  467M     0  467M   0% /sys/fs/cgroup
/dev/mapper/klas-root   47G  3.8G   44G   8% /
tmpfs                  467M     0  467M   0% /tmp
/dev/sda1             1014M  169M  846M  17% /boot
tmpfs                   94M     0   94M   0% /run/user/0








第六步:开机自动挂载 
/etc/fstab
/etc/rc.local

/dev/sdb1     /date/test01   xfs         defaults           0               0
设备的名称      挂载点      文件类型   挂载的参数defaults  是否备份   是否开机自动检查
mount -a #让/etc/fstab执行一遍 如果挂在了不提示 如果没有挂载则自动挂载

parted分区 GPT

对于大于2TB的磁盘分区:格式GPT
mklabel 修改磁盘格式
mkpart   创建分区
print    查看分区表
rm       删除分区
quit      退出



第一步:插入磁盘
Disk /dev/sdb: 4.88 TiB, 5368709120000 bytes, 10485760000 sectors
Disk model: VMware Virtual S
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes



第二步:修改磁盘格式为GPT
(parted) mklabel  #修改磁盘格式
New disk label type? gpt #修改为gpt


第三步:进行分区
(parted) mkpart     #创建分区
Partition name?  []? primary #名称自定义
File system type?  [ext2]? xfs #分区类型xfs
Start? 0        #开始的大小    
End? 500G       #结束的大小
Warning: The resulting partition is not properly
aligned for best performance: 34s % 2048s != 0s
Ignore/Cancel? i     #忽略

 
非交互式:
mkpart premary xfs 1000G 1500G
       名称    类型 开始  结束
命令行:
parted /dev/sdb mkpart primary xfs 1000G 1500G





删除:  # rm 1 
(parted) rm 1
(parted) print
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 5369GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: 

Number  Start  End  Size  File system  Name  Flags



第四步:格式化
mkfs.xfs /dev/sdb1
   

第五步:挂载

相关案例

案例1:
内存不够用到执行系统出现oom内存溢出
Linux系统如果内存满了 系统为了自保,会自动杀死占用内存做搞得进程 导致系统故障
什么情况下会导致内存不够?
a 业务流量过高    增加内存使用
b 代码问题java    找开发解决
c 流量波动业务    增加swap解决  偶尔的内部测试服务器
d 受攻击导致oom   防火墙软件

swap:内存不够用时,swap临时充当内存使用 swap是磁盘的分区空间 磁盘比内存慢
free -h 查看

创建swap分区:
第一种:安装系统 磁盘分区 3个分区
/boot
swap
/


第二种:进入系统后创建swap
第一步:生成1G的文件
经常用来做测试 生成大文件使用 #使用dd命令
[root@shang:~]# dd if=/dev/zero of=a.txt  bs=1M count=10
10+0 records in
10+0 records out
10485760 bytes (10 MB, 10 MiB) copied, 0.00320965 s, 3.3 GB/s
[root@shang:~]# ll -h a.txt 
-rw-r--r-- 1 root root 10M Jul  9 17:11 a.txt
dd 命令
if input file 要读取 /etc/zero
of output file 输出到当前的 a.txt
bs 一次要输出多大
count 总共要输出的次数


第二步:格式化文件为swap分区
mkswap /data/a.txt


第三步:挂载激活swap分区
swapon /date/a.txt

查看swap的组成方式:
[root@Sovs1108:~]# swapon -s
Filename				Type	     Size	Used	Priority
/dev/dm-1                              	partition    2138108	0	-2


卸载swap:
swapoff /date/a.txt
案例2:磁盘满导致无法写入
inode满  df -i  大量的小文件    查找:find / -type d -size +1M目录比较大 里面的文件多
block满         大文件          查找: find / -size +200M
du -sh 统计整个目录的大小
案例3:解决磁盘不够用的问题
需求:第一块磁盘满了,并且日志文件的位置不能发生变化
导致原因:服务持续追加内容到/var/log/test.log

第一步:添加一个磁盘
第二步:格式化磁盘
第三步:挂载磁盘
第四步:开机自动启动
将文件移动到挂载磁盘的目录 做软连接
案例4:删除大文件 磁盘空间不释放
真正的删除:inode和block同时为0 并且没有被进程占用
解决办法:
lsof 查看当前命令占用
查找到后杀死进程 kill -9 加上lsof上的数字
孩儿立志出乡关,学不成名誓不还。埋骨何须桑梓地,人生无处不青山。

评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇