linux 磁盘基础

一、fdisk工具使用

[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.分区

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硬盘的磁盘分区表变化了

partprobe /dev/sdb

3.格式化创建文件系统

[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.关闭磁盘分区的自动检查

[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.进行磁盘分区挂载

[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

/bin/mount /dev/sdb1 /mnt/

方法 2./etc/fstab

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、分区

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、以上命令顺序

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.创建一个文件块

[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

[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生效

[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

swapon /tmp/100m

###方法2. /etc/fstab

#/tmp/100m swap swap defaults 0 0

查看是否成功

[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、分区工具

fdisk:磁盘分区工具 ★★★★☆ 
parted:磁盘分区工具 ★★★★☆	 
partprobe:通知系统磁盘的分区信息变化 更新内核的硬盘分区表信息 ★★★☆☆

2、tune2fs

tune2fs:调整ext2/ext3/ext4文件系统参数 ★★☆☆☆	 
-c 0 count 关闭每挂载多少次进行磁盘检查 
-i 0 interval 关闭每个多久进行磁盘检查
关闭磁盘分区的自动检查

3、mkfs

mkfs:make filesystem 创建Linux文件系统 ★★★☆☆	
-t 指定文件系统类型 
mkfs.ext4 == mkfs -t ext4

4、dumpe2fs

dumpe2fs:显示文件系统信息 ★★☆☆☆

5、其他

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 指定监视网卡
posted @ 2019-03-19 16:17  活的潇洒80  阅读(204)  评论(0编辑  收藏  举报