您的位置:首页 > 运维架构 > Linux

Linux下磁盘分区、文件管理工具详解

2015-08-25 16:38 627 查看
一、规划磁盘分区:fdisk
二、格式化磁盘分区:mkfs.xxx三、文件系统管理:blkid、e2label、tune2fs四、创建swap分区:mkswap五、文件系统的检测:fsck
一:规划磁盘分区测试环境:CentOS 6.61、查看磁盘分区 格式:fdisk -l [磁盘名]示例:

[root@localhost ~]# fdisk -l /dev/sda     //如果不指出磁盘名,会列出所有磁盘分区信息

Disk /dev/sda: 107.4 GB, 107374182400 bytes
255 heads, 63 sectors/track, 13054 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000d20cb

Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          13      102400   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              13        5113    40960000   83  Linux
/dev/sda3            5113        5368     2048000   82  Linux swap / Solaris
2、给磁盘进行分区

格式:fdisk [磁盘名]
示例:使用/dev/sda测试
[root@localhost ~]# fdisk /dev/sda

WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
switch off the mode (command 'c') and change display units to
sectors (command 'u').

Command (m for help): m
Command action
a   toggle a bootable flag
b   edit bsd disklabel
c   toggle the dos compatibility flag
d   delete a partition                //删除分区
l   list known partition types
m   print this menu         //查看帮助
n   add a new partition       //创建新分区
o   create a new empty DOS partition table
p   print the partition table     //打印现有分区表
q   quit without saving changes      //不保存退出
s   create a new empty Sun disklabel
t   change a partition's system id   //修改分区文件系统类型
u   change display/entry units
v   verify the partition table      //检验分区表
w   write table to disk and exit         //保存分区表并退出
x   extra functionality (experts only)
输入“n” 然后回车新建分区
Command (m for help): n
Command action
e   extended
p   primary partition (1-4)
选择要创建的分区类型:e-创建扩展分区;p-创建主分区,在此选择创建扩展分区输入“e”
此处需要设置分区起始柱面和结束柱面,也可指定要分区的大小,可支持k、M、G单位,都使用默认即可
这里需要说明的是:1块磁盘默认最多可以分4个分区;我测试的系统/dev/sda磁盘已经有3个主分区了,所以这里选择将剩余的磁盘空间全分给扩展分区
原因:磁盘首个512字节存放的是MBR,为446字节的BootLoader + 64字节的分区表 + 2字节分区标志。分区表中每个分区记录占16字节,所以最大的为主分区+扩展分区数为 64/16=4
e
Selected partition 4
First cylinder (5368-13054, default 5368):
Using default value 5368
Last cylinder, +cylinders or +size{K,M,G} (5368-13054, default 13054):
Using default value 13054
通过输入“p”,可以查看已有的分区表信息

Command (m for help): p

Disk /dev/sda: 107.4 GB, 107374182400 bytes
255 heads, 63 sectors/track, 13054 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000d20cb

Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          13      102400   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              13        5113    40960000   83  Linux
/dev/sda3            5113        5368     2048000   82  Linux swap / Solaris
/dev/sda4            5368       13054    61744831    5  Extended //这就是添加的扩展分区
新建一个逻辑分区,给逻辑分区划分10G空间大小
Command (m for help): n
First cylinder (5368-13054, default 5368):
Using default value 5368
Last cylinder, +cylinders or +size{K,M,G} (5368-13054, default 13054): +10G

Command (m for help): p

Disk /dev/sda: 107.4 GB, 107374182400 bytes
255 heads, 63 sectors/track, 13054 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000d20cb

Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          13      102400   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              13        5113    40960000   83  Linux
/dev/sda3            5113        5368     2048000   82  Linux swap / Solaris
/dev/sda4            5368       13054    61744831    5  Extended
/dev/sda5            5368        6673    10489417   83  Linux //10G的逻辑分区
w保存退出
Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.
至此磁盘规划完成

查看内核是否已经识别新的分区:
[root@localhost ~]# cat /proc/partitions
major minor  #blocks  name

8        0  104857600 sda
8        1     102400 sda1
8        2   40960000 sda2
8        3    2048000 sda3
通知内核重新读取硬盘分区表:
partx -a /dev/device //添加分区
-n M:N //指定分区的范围
[root@localhost ~]# partx -a /dev/sda
BLKPG: Device or resource busy
error adding partition 1
BLKPG: Device or resource busy
error adding partition 2
BLKPG: Device or resource busy
error adding partition 3
BLKPG: Device or resource busy
error adding partition 4
BLKPG: Device or resource busy
error adding partition 5
[root@localhost ~]# cat /proc/partitions major minor #blocks name 8 0 104857600 sda 8 1 102400 sda1 8 2 40960000 sda2 8 3 2048000 sda3
8 4 31 sda4
8 5 10489417 sda5


二、格式化磁盘分区:mkfs.xxx

Linux常见文件系统:

ext2, ext3, ext4, xfs, btrfs, reiserfs, jfs

swap: 交换分区
光盘:iso9660

Windows:fat32, ntfs Unix: FFS, UFS, JFS2
网络文件系统(Network File System)。是由SUN公司研制的Unix表示层协议(pressentation layer protocol),能使使用者访问网络上别处的文件就像在使用自己的计算机一样:NFS, CIFS
集群文件系统:GFS2, OCFS2
分布式文件系统(Distributed File System)是指文件系统管理的物理存储资源不一定直接连接在本地节点上,而是通过计算机网络与节点相连,分布式文件系统的设计基于客户机/服务器模式:GFS、HDFS、Lustre 、Ceph 、GridFS 、mogileFS、TFS、FastDFS等。
创建文件系统:
mkfs命令:fstype --- 文件系统类型(ext4、xfs、btrfs、vfat等)
(1) # mkfs.fstype /dev/device

(2) # mkfs -t fstype /dev/device
-L 'LABEL': 设定卷标
(3) mke2fs:ext系列文件系统专用管理工具
-t {ext2|ext3|ext4}    //设置文件系统类型
-b {1024|2048|4096}   //1k,2k,4k(bytes per block)
-L 'LABEL'      //设置卷标
-j: 相当于 -t ext3
-i #: 为数据空间中每多少个字节创建一个inode;此大小不应该小于block的大小;
-N #:为数据空间创建个多少个inode;
-m #: 为管理人员预留的空间占据的百分比;
-O FEATURE[,...]:启用指定特性  //has_journal 有日志功能
-O ^FEATURE:关闭指定特性
示例:将/dev/sda5格式化,文件系统类型为ext2,默认块大小为4k
[root@localhost ~]# mkfs.ext2 /dev/sda5


三、文件系统管理:blkid、e2label、tune2fs
blkid:块设备属性信息查看 blkid [OPTION]... [device]
-U UUID: 根据指定的UUID来查找对应的设备
-L LABEL:根据指定的LABEL来查找对应的设备
[root@localhost ~]# blkid /dev/sda5
/dev/sda5: UUID="07c5e565-83dd-45dc-a33c-23653ac32886" TYPE="ext2"
e2label:管理ext系列文件系统的LABEL
e2label device [ new-label ]
给/dev/sda5设置卷标

[root@localhost ~]# e2label /dev/sda5 mydata
[root@localhost ~]# blkid /dev/sda5 /dev/sda5: UUID="07c5e565-83dd-45dc-a33c-23653ac32886" TYPE="ext2" LABEL="mydata"
tune2fs:重新设定ext系列文件系统可调整参数的值

-l:查看指定文件系统超级块信息;super block
-L 'LABEL':修改卷标
-m #:修预留给管理员的空间百分比
-j: 将ext2升级为ext3
-O: 文件系统属性启用或禁用
-o: 调整文件系统的默认挂载选项
-U UUID: 修改UUID号;
dumpe2fs: -h:查看超级块信息
四、创建swap分区:mkswap mkswap [options] device //创建交换分区 注意前提:调整其分区的ID为82;
示例(测试):将/dev/sda5分区设置为交换分区
Command (m for help): p

Disk /dev/sda: 107.4 GB, 107374182400 bytes
255 heads, 63 sectors/track, 13054 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000d20cb

Device Boot Start End Blocks Id System
/dev/sda1 * 1 13 102400 83 Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2 13 5113 40960000 83 Linux
/dev/sda3 5113 5368 2048000 82 Linux swap / Solaris
/dev/sda4 5368 13054 61744831 5 Extended
/dev/sda5 5368 6673 10489417 83 Linux

Command (m for help): t
Partition number (1-5): 5
Hex code (type L to list codes): 82
Changed system type of partition 5 to 82 (Linux swap / Solaris)

Command (m for help): p

Disk /dev/sda: 107.4 GB, 107374182400 bytes
255 heads, 63 sectors/track, 13054 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000d20cb

Device Boot Start End Blocks Id System
/dev/sda1 * 1 13 102400 83 Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2 13 5113 40960000 83 Linux
/dev/sda3 5113 5368 2048000 82 Linux swap / Solaris
/dev/sda4 5368 13054 61744831 5 Extended
/dev/sda5 5368 6673 10489417 82 Linux swap / Solaris

Command (m for help): w The partition table has been altered! Calling ioctl() to re-read partition table. WARNING: Re-reading the partition table failed with error 16: Device or resource busy. The kernel still uses the old table. The new table will be used at the next reboot or after you run partprobe(8) or kpartx(8) Syncing disks.
[root@localhost ~]# blkid /dev/sda5 /dev/sda5: UUID="07c5e565-83dd-45dc-a33c-23653ac32886" TYPE="ext2" LABEL="mydata"
[root@localhost ~]# mkswap /dev/sda5
Setting up swapspace version 1, size = 10489412 KiB
no label, UUID=c9eb8bb6-35f3-4400-923b-d05147adcd47
[root@localhost ~]# blkid /dev/sda5
/dev/sda5: UUID="c9eb8bb6-35f3-4400-923b-d05147adcd47" TYPE="swap"

五、文件系统的检测:fsck、e2fsck

(1)fsck.fstype(2)fsck -t fstype
-a: 自动修复错误
-r: 交互式修复错误
注意: fstype一定要与分区上已经文件类型相同
[root@localhost ~]# fsck.ext2 -a /dev/sda5
mydata: clean, 11/655776 files, 46932/2622354 blocks
(3)e2fsck:ext系列文件专用的检测修复工具
-y:自动回答为yes
-f:强制修复

本文出自 “bengbengtu” 博客,请务必保留此出处http://bengbengtu.blog.51cto.com/9505633/1688107
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: