linux 磁盘基础
一、fdisk工具使用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 | [root@luoahong-nb ~] # fdisk -l|grep 'sd[a-c]:' Disk /dev/sda : 10.7 GB, 10737418240 bytes Disk /dev/sdb : 106 MB, 106954752 bytes Disk /dev/sdc : 106 MB, 106954752 bytes n 创建分区 d 删除分区 p 显示分区信息 q 退出不保存 w 退出并保存 100M 30M主分区 30M逻辑分区 Command (m for help): p Disk /dev/sdb : 106 MB, 106954752 bytes 64 heads, 32 sectors /track , 102 cylinders, total 208896 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 identifier: 0x6d033dc2 Device Boot Start End Blocks Id System Command (m for help): n Command action e extended p primary partition (1-4) p Partition number (1-4): 1 First sector (2048-208895, default 2048): Using default value 2048 Last sector, +sectors or +size{K,M,G} (2048-208895, default 208895): +30M Command (m for help): p Disk /dev/sdb : 106 MB, 106954752 bytes 64 heads, 32 sectors /track , 102 cylinders, total 208896 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 identifier: 0x6d033dc2 Device Boot Start End Blocks Id System /dev/sdb1 2048 63487 30720 83 Linux Command (m for help): n Command action e extended p primary partition (1-4) e Partition number (1-4): 2 First sector (63488-208895, default 63488): Using default value 63488 Last sector, +sectors or +size{K,M,G} (63488-208895, default 208895): Using default value 208895 Command (m for help): p Disk /dev/sdb : 106 MB, 106954752 bytes 64 heads, 32 sectors /track , 102 cylinders, total 208896 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 identifier: 0x6d033dc2 Device Boot Start End Blocks Id System /dev/sdb1 2048 63487 30720 83 Linux /dev/sdb2 63488 208895 72704 5 Extended Command (m for help): w The partition table has been altered! Calling ioctl() to re- read partition table. Syncing disks. |
二、给/dev/sdb硬盘只创建一个分区,把他挂载到/mnt上面
1.分区
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | Command (m for help): n Command action e extended p primary partition (1-4) p Partition number (1-4): 1 First sector (2048-208895, default 2048): Using default value 2048 Last sector, +sectors or +size{K,M,G} (2048-208895, default 208895): Using default value 208895 Command (m for help): p Disk /dev/sdb : 106 MB, 106954752 bytes 64 heads, 32 sectors /track , 102 cylinders, total 208896 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 identifier: 0x6d033dc2 Device Boot Start End Blocks Id System /dev/sdb1 2048 208895 103424 83 Linux Command (m for help): w The partition table has been altered! Calling ioctl() to re- read partition table. Syncing disks. |
2.通知系统/dev/sdb硬盘的磁盘分区表变化了
1 | partprobe /dev/sdb |
3.格式化创建文件系统
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | [root@luoahong-nb ~] # #make filesystem [root@luoahong-nb ~] # mkfs.ext4 /dev/sdb1 mke2fs 1.41.12 (17-May-2010) Filesystem label= OS type : Linux Block size=1024 (log=0) Fragment size=1024 (log=0) Stride=0 blocks, Stripe width=0 blocks 25896 inodes, 103424 blocks 5171 blocks (5.00%) reserved for the super user First data block=1 Maximum filesystem blocks=67371008 13 block groups 8192 blocks per group, 8192 fragments per group 1992 inodes per group Superblock backups stored on blocks: 8193, 24577, 40961, 57345, 73729 Writing inode tables: done Creating journal (4096 blocks): done Writing superblocks and filesystem accounting information: done This filesystem will be automatically checked every 36 mounts or 180 days, whichever comes first. Use tune2fs -c or -i to override. |
4.关闭磁盘分区的自动检查
1 2 3 4 5 6 7 | [root@luoahong-nb ~] # tune2fs -c 0 -i 0 /dev/sdb1 tune2fs 1.41.12 (17-May-2010) Setting maximal mount count to -1 Setting interval between checks to 0 seconds #-c 0 count 关闭每挂载多少次进行磁盘检查的功能 #-i 0 interval 关闭每个180天进行磁盘检查功能 |
5.进行磁盘分区挂载
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | [root@luoahong-nb ~] # df -h Filesystem Size Used Avail Use% Mounted on /dev/sda3 8.8G 8.8G 0 100% / tmpfs 1.9G 0 1.9G 0% /dev/shm /dev/sda1 190M 40M 141M 22% /boot [root@luoahong-nb ~] # [root@luoahong-nb ~] # mount /dev/sdb1 /mnt/ [root@luoahong-nb ~] # df -h Filesystem Size Used Avail Use% Mounted on /dev/sda3 8.8G 8.8G 0 100% / tmpfs 1.9G 0 1.9G 0% /dev/shm /dev/sda1 190M 40M 141M 22% /boot /dev/sdb1 94M 1.6M 88M 2% /mnt |
6.进行永久挂载(如何在开机的时候自动挂载)
方法 1./etc/rc.local
1 | /bin/mount /dev/sdb1 /mnt/ |
方法 2./etc/fstab
1 2 3 4 5 6 7 | UUID=6b8ba13d-2833-49be-8206-deb002d2d523 / ext4 defaults 1 1 UUID=e01d5cbd-b233-44b9-832d-15225cc4d8ff /boot ext4 defaults 1 2 UUID=7c5f5672-9c30-444c-a273-1350c6a06130 swap swap defaults 0 0 #/dev/sdb1 /mnt ext4 defaults 0 0 #man fstab |
三、parted
1、分区
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | p[rint] 显示磁盘分区表信息 mklabel 创建磁盘分区表 gpt msdos(mbr) mkpart 创建分区 rm 删除分区 q 退出不保存 创建30M 30M (parted) p Error: /dev/sdc : unrecognised disk label (parted) mklabel gpt (parted) p Model: VMware, VMware Virtual S (scsi) Disk /dev/sdc : 107MB Sector size (logical /physical ): 512B /512B Partition Table: gpt Number Start End Size File system Name Flags (parted) mkpart primary 0 30 Warning: The resulting partition is not properly aligned for best performance. Ignore /Cancel ? I (parted) p Model: VMware, VMware Virtual S (scsi) Disk /dev/sdc : 107MB Sector size (logical /physical ): 512B /512B Partition Table: gpt Number Start End Size File system Name Flags 1 17.4kB 30.0MB 30.0MB primary (parted) mkpart primary 30 60 (parted) p Model: VMware, VMware Virtual S (scsi) Disk /dev/sdc : 107MB Sector size (logical /physical ): 512B /512B Partition Table: gpt Number Start End Size File system Name Flags 1 17.4kB 30.0MB 30.0MB primary 2 30.4MB 59.8MB 29.4MB primary |
2、以上命令顺序
1 2 3 4 | parted /dev/sdc mklabel gpt parted /dev/sdc mkpart primary 0 30 Ignore parted /dev/sdc mkpart primary 30 60 parted /dev/sdc p |
企业案例:java环境内存不够导致swap
创建一个文件让文件成为swap
#1.创建一个文件块
1 2 3 4 5 6 | [root@luoahong-nb ~] # dd if=/dev/zero of=/tmp/100m bs=1M count=100 100+0 records in 100+0 records out 104857600 bytes (105 MB) copied, 0.935927 s, 112 MB /s [root@luoahong-nb ~] # file /tmp/100m /tmp/100m : data |
#2.变成swap
1 2 3 4 5 6 7 | [root@luoahong-nb ~] # mkswap /tmp/100m mkswap: /tmp/100m : warning: don't erase bootbits sectors on whole disk. Use -f to force. Setting up swapspace version 1, size = 102396 KiB no label, UUID=1b2aea27-a092-4345-806f-b95b8d33873a [root@luoahong-nb ~] # file /tmp/100m /tmp/100m : Linux /i386 swap file (new style) 1 (4K pages) size 25599 pages |
#3.swap生效
1 2 3 4 5 6 7 8 9 10 11 | [root@luoahong-nb ~] # swapon /tmp/100m [root@luoahong-nb ~] # free -h total used free shared buffers cached Mem: 3.9G 398M 3.5G 248K 65M 134M -/+ buffers /cache : 198M 3.7G Swap: 867M 0B 867M [root@luoahong-nb ~] # swapon -s Filename Type Size Used Priority /dev/sda2 partition 786428 0 -1 /tmp/100m file 102396 0 -2 |
#4.swap永久生效
###方法1. /etc/rc.local
1 | swapon /tmp/100m |
###方法2. /etc/fstab
1 | #/tmp/100m swap swap defaults 0 0 |
查看是否成功
1 2 3 4 5 | [root@luoahong-nb ~] # free -h total used free shared buffers cached Mem: 3.9G 398M 3.5G 248K 65M 134M -/+ buffers /cache : 199M 3.7G Swap: 867M 0B 867M |
Linux特点会把使用过的命令和数据临时存放在内存中 cache和buffer
磁盘常用命令大全
1、分区工具
1 2 3 | fdisk :磁盘分区工具 ★★★★☆ parted:磁盘分区工具 ★★★★☆ partprobe:通知系统磁盘的分区信息变化 更新内核的硬盘分区表信息 ★★★☆☆ |
2、tune2fs
1 2 3 4 | tune2fs:调整ext2 /ext3/ext4 文件系统参数 ★★☆☆☆ -c 0 count 关闭每挂载多少次进行磁盘检查 -i 0 interval 关闭每个多久进行磁盘检查 关闭磁盘分区的自动检查 |
3、mkfs
1 2 3 | mkfs: make filesystem 创建Linux文件系统 ★★★☆☆ -t 指定文件系统类型 mkfs.ext4 == mkfs -t ext4 |
4、dumpe2fs
dumpe2fs:显示文件系统信息 ★★☆☆☆
5、其他
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | resize2fs:调整ext2 /ext3/ext4 文件系统大小 ★★☆☆☆ fsck :检查并修复Linux文件系统 ★★★☆☆ -a dd :转换或复制文件 ★★★☆☆ od 查看二进制文件的内容 -xa dd if = /dev/sda of= /tmp/512 .bin bs=512 count=1 od -xa /tmp/512 .bin mount :挂载文件系统 ★★★★★ umount :卸载文件系统 ★★★☆☆ umount /mnt df :报告文件系统磁盘空间的使用情况 ★★★★★ mkswap:创建交换分区 ★★☆☆☆ swapon:激活交换分区 ★★☆☆☆ swapoff:关闭交换分区 ★★★☆☆ sync :刷新文件系统缓冲区 ★★☆☆☆ top iotop 查看系统的磁盘读写速度 显示出进程使用swap的情况 io input /output 输入/输出 读写 htop top 升级版 iftop 查询网卡流量情况 -i 指定监视网卡 |
作者:罗阿红
出处:http://www.cnblogs.com/luoahong/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
2017-03-19 成长记录 | 所学,所思,所行