企业案例:常用磁盘挂载方式
- 插入磁盘(虚拟机添加磁盘)
- 磁盘格式化,一般不需要分区 不能格式化用-f 强制格式化
[root@VMware-koten ~]#mkfs.xfs /dev/sdc meta-data=/dev/sdc isize=512 agcount=4, agsize=131072000 blks = sectsz=512 attr=2, projid32bit=1 = crc=1 finobt=0, sparse=0 data = bsize=4096 blocks=524288000, imaxpct=5 = sunit=0 swidth=0 blks naming =version 2 bsize=4096 ascii-ci=0 ftype=1 log =internal log bsize=4096 blocks=256000, version=2 = sectsz=512 sunit=0 blks, lazy-count=1 realtime =none extsz=4096 blocks=0, rtextents=0
- 磁盘挂载
创建空目录,挂载,df -h查看
[root@VMware-koten ~]#mkdir /data [root@VMware-koten ~]#mount /dev/sdc /data/ [root@VMware-koten ~]#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 1.9G 17G 10% / /dev/sda1 197M 110M 88M 56% /boot tmpfs 199M 0 199M 0% /run/user/0 /dev/sdb1 97M 5.3M 92M 6% /mnt /dev/sdc 2.0T 33M 2.0T 1% /data [root@VMware-koten ~]#
开机自动挂载
加入/etc/fstab 开机自动挂载硬件
[root@VMware-koten ~]#blkid /dev/sda1: UUID="d668ff1c-a7da-4b2e-b534-8582e7261529" TYPE="xfs" /dev/sda2: UUID="b2193101-867d-415d-b905-491d531af8e0" TYPE="swap" /dev/sda3: UUID="43e725b7-4493-4c94-9646-7e90df49ec1e" TYPE="xfs" /dev/sdb1: UUID="ef6f9c31-de6c-40a0-b7a7-a12206c85ba9" TYPE="xfs" /dev/sdc: UUID="00a5d78a-f5c0-491a-9ccf-2a93f625b918" TYPE="xfs" [root@VMware-koten ~]# #根据blkid信息填写fstab [root@VMware-koten ~]# cat /etc/fstab # # /etc/fstab # Created by anaconda on Wed Feb 22 21:44:17 2023 # # 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 # UUID=43e725b7-4493-4c94-9646-7e90df49ec1e / xfs defaults 0 0 UUID=d668ff1c-a7da-4b2e-b534-8582e7261529 /boot xfs defaults 0 0 UUID=b2193101-867d-415d-b905-491d531af8e0 swap swap defaults 0 0 /dev/sdb /mnt xfs defaults 0 0 [root@VMware-koten ~]#
/dev/sdb /data xfs defaults 0 0
硬件名称 挂载点 挂载类型 挂载参数 是否备份 是否检查
也可以写入/etc/rc.local
vim /etc/rc.local mount /dev/sdb /data
大于2T的磁盘进行分区
大于2T的磁盘需要将格式修改为GPT格式
使用命令parted进行分区 直接生效
第一步:在虚拟机添加一块2T的磁盘
第二步:重启虚拟机
第三步:查看是否添加成功
fdisk -l
第四步:parted进行分区
交互式
[root@VMware-koten ~]#parted /dev/sdc GNU Parted 3.1 Using /dev/sdc Welcome to GNU Parted! Type 'help' to view a list of commands. (parted) help align-check TYPE N check partition N for TYPE(min|opt) alignment help [COMMAND] print general help, or help on COMMAND mklabel,mktable LABEL-TYPE create a new disklabel (partition table) #创建新的格式 mkpart PART-TYPE [FS-TYPE] START END make a partition #创建分区 name NUMBER NAME name partition NUMBER as NAME print [devices|free|list,all|NUMBER] display #显示分区表 the partition table, available devices, free space, all found partitions, or a particular partition quit exit #退出 program rescue START END rescue a lost partition near START and END resizepart NUMBER END resize partition NUMBER rm NUMBER delete #删除分区 partition NUMBER select DEVICE choose the device to edit disk_set FLAG STATE change the FLAG on selected device disk_toggle [FLAG] toggle the state of FLAG on selected device set NUMBER FLAG STATE change the FLAG on partition NUMBER toggle [NUMBER [FLAG]] toggle the state of FLAG on partition NUMBER unit UNIT set the default unit to UNIT version display the version number and copyright information of GNU Parted (parted)3 (parted) mklabel gpt #将分区表格式更改为gpt Warning: Partition(s) on /dev/sdc are being used. Ignore/Cancel? I Warning: The existing disk label on /dev/sdc will be destroyed and all data on this disk will be lost. Do you want to continue? Yes/No? y (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 (parted)
非交互式
(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 (parted) 不需要进mkpartd命令进行分区
不进入mkpartd命令进行分区
[root@VMware-koten ~]#parted /dev/sdc mkpart primary xfs 200G 500G Information: You may need to update /etc/fstab. [root@VMware-koten ~]#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 [root@VMware-koten ~]# #删除分区 [root@VMware-koten ~]#parted /dev/sdc rm 3 Information: You may need to update /etc/fstab. [root@VMware-koten ~]#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 [root@VMware-koten ~]#
第五步:格式化分区
[root@VMware-koten ~]#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@VMware-koten ~]#mount /dev/sdc1 /data [root@VMware-koten ~]#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 1.9G 17G 11% / /dev/sda1 197M 110M 88M 56% /boot tmpfs 199M 0 199M 0% /run/user/0 /dev/sdb1 97M 5.3M 92M 6% /mnt /dev/sdc1 94G 33M 94G 1% /data [root@VMware-koten ~]#
磁盘案例
案例1:企业中程序导致内存不够用
如果内存满则系统会自动杀死占用内存最高的进程,来保护系统正常运行 什么原因导致内存满 1、大量的用户访问我们的服务器(正常情况)需要我们添加内存 2、由于程序导致内存满,而不是大量用户访问导致(找开发解决问题) 3、由于网络的波动导致内存满,需要使用swap解决 4、学习中,可能需要swap临时充当内存使用 5、公司内部业务(测试服务器),需要使用swap临时解决 swap作用:当内存不够用时,磁盘分区临时充当内存使用 查看swap分区,使用free -h [root@VMware-koten ~]#free -h total used free shared buff/cache available Mem: 1.9G 218M 1.6G 9.5M 108M 1.6G Swap: 1.0G 0B 1.0G [root@VMware-koten ~]# 创建swap分区: 1、安装系统划分的分区 2、可以安装系统后创建 第一步:生成一个200M的空间,把200M的空间当作swap空间 dd if=/dev/zero of=/tmp/200m bs=1M count=200 [root@VMware-koten ~]#dd if=/dev/zero of=/tmp/200m bs=1M count=200 200+0 records in 200+0 records out 209715200 bytes (210 MB) copied, 1.13911 s, 184 MB/s [root@VMware-koten ~]# 第二步:格式化为swap分区格式 mkswap /tmp/200m [root@VMware-koten ~]#mkswap /tmp/200m Setting up swapspace version 1, size = 204796 KiB no label, UUID=9429e531-a4c5-4e34-b238-57a1efcfc2c2 [root@VMware-koten ~]# 第三步:挂载使用swapon /tmp/200m回车 [root@VMware-koten ~]#swapon /tmp/200m swapon: /tmp/200m: insecure permissions 0644, 0600 suggested. 第四步:free -h [root@VMware-koten ~]#free -h total used free shared buff/cache available Mem: 1.9G 265M 1.0G 9.5M 678M 1.5G Swap: 1.2G 0B 1.2G 查看swap分区的组成 swspon -s [root@VMware-koten ~]#swapon -s Filename Type Size Used Priority /dev/sda2 partition 1048572 0 -2 /tmp/200m file 204796 0 -3 重启会掉,需要开机自动挂载 卸载swap [root@VMware-koten ~]#swapoff /tmp/200m [root@VMware-koten ~]#free -h total used free shared buff/cache available Mem: 1.9G 265M 1.0G 9.5M 678M 1.5G Swap: 1.0G 0B 1.0G [root@VMware-koten ~]#
案例2:磁盘不够用找出大文件备份或删除
找出系统中的大文件移动或删除
[root@VMware-koten ~]#du -h /var/log/*|awk '$1~ /^.*G/'|xargs -i mv {} /data [root@VMware-koten ~]#find / -type f -size +1G|xargs rm
案例3:找出系统中小文件多的目录备份或删除
df -i 查看inode使用信息 [root@VMware-koten ~]#df -i Filesystem Inodes IUsed IFree IUse% Mounted on devtmpfs 250790 410 250380 1% /dev tmpfs 253483 1 253482 1% /dev/shm tmpfs 253483 738 252745 1% /run tmpfs 253483 16 253467 1% /sys/fs/cgroup /dev/sda3 9858560 60425 9798135 1% / /dev/sda1 102400 326 102074 1% /boot tmpfs 253483 1 253482 1% /run/user/0 /dev/sdb1 51200 3 51197 1% /mnt /dev/sdc1 48828096 3 48828093 1% /data [root@VMware-koten ~]#find /-type d -size +70k
案例4:磁盘使用软连接解决磁盘不够用
1、添加磁盘
2、格式化
3、挂载
mount /dev/sdc /data
4、将大文件移动到新磁盘(服务日志没有输出,选择业务的低谷期,禁止用户写入内容(DB)锁库锁表)
[root@VMware-koten ~]#ln -s /data/10G /var/log/10g #将/data/10G做软链接到/var/log/10g
磁盘不够用:
- 虚拟机可以通过增加磁盘解决
- 虚拟机可以做LVM逻辑卷管理动态调整磁盘大小,互联网公司不用
- 云服务器可以动态扩缩容磁盘大小
案例5:删除文件后,磁盘空间不释放
真正的删除一个文件硬链接和软连接数都为0,且不被进程占用
1、模拟文件被进程调用 tail /data/10G 2、删除文件后磁盘空间不释放 rm -rf 10G df -h 3、查看进程调用lsof yum -y install lsof lsof |grep 10G tail 3407 root 3r REG 8,32 10485760000 67 /data/10G (deleted) 4、结束进程或者加载服务 kill -9 3407 df -h #查看磁盘是否被释放
我是koten,10年运维经验,持续分享运维干货,感谢大家的阅读和关注!