linux磁盘及文件系统管理
CPU,MEMORY(RAM),I/o
i/o:INPUT and output devices
磁盘:辅助存储设备
网卡:
文件接口:open(),read(),write(),close()
简化
设备类别:
块(block):随机访问
字符(character):线性,数据交换单位为字符
设备文件:FHS(lsb)
/dev/:设备
设备文件,会关联至设备驱动程序
设备号:
major:主设备号,区别设备类别,关联驱动,用于指明所需的驱动程序
minor:次设备号
mknod命令:
创建设备文件
mknod [OPTION]... NAME TYPE [MAJOR MINOR]
-m,--mode=MODE
mknod 文件名 C 1,1
设备文件的文件名:ICANN
磁盘设备文件
IDE接口:并行,133MB/S
两个IDE控制器:每个控制器可通过线缆接入两块磁盘,有主从之分
/dev/hd[a-z][数字]
/dev/hda,/dev/hdb
/dev/hdc,/dev/hdd
SCSI接口:Small computer system interface
ultraSCSI 320MB/S
SATA接口:串行,serial ATA,
6Gpbs,
USB接口:串行,
SAS接口:串行,6gpbs
设备文件统一表示为:/dev/sd[a-z][#]
[a-z]:标记设备
[#]:磁盘分区
track;磁道
sector:扇区
cylinder:柱面
寻道时间:平均寻道时间
MBR:0磁道0扇区,主引导扇区(naster boot record)
三部分:
446bytes:bootloader,程序
64bytes:分区表
16bytes:标记一个分区
4个主分区,3主,1扩展-->n逻辑分区
2bytes:55AA,当前MBR信息是否有效的标记;
fdisk
1,查看硬盘状态
fdisk -l /dev/[hs]d[a-z]
fdisk -l /dev/sd[a-z]
[root@localhost ~]# fdisk -l /dev/[hs]d[a-z]
Disk /dev/sda: 128.8 GB, 128849018880 bytes
255 heads(255个磁头), 63 (每个磁道有63个扇区)sectors/track, 15665 cylinders(15665个柱面)
Units = cylinders of 16065 * 512 = 8225280 bytes(每个存储单元有多大 )
Sector size (logical/physical): 512 bytes / 512 bytes(每一个扇区大小,物理/逻辑)
I/O size (minimum/optimal): 512 bytes / 512 bytes(I/O大小,每次实现数据存储时,交互数据存储单位512)
Disk identifier: 0x00088c28
Device(分区) Boot(存放的引导系统) Start(表示从第几个柱面开始这个分区) End (到第几个柱面结束) Blocks(有多少个数据块) Id(设备标识,分区类型表示) System(分区标识,用于那种分区系统)
/dev/sda1 *(*表示有) 1 39 307200 83 Linux
Partition 1 does not end on cylinder boundary.
/ dev/sda2 39 15413 123489280 83 Linux
/dev/sda3 15413 15666 2031616 82 Linux swap / Solaris
2,管理分区
分区标识方式:
主分区或扩展分区:1-4
逻辑分区:5+
fdisk DEVICE,最多管理15个分区
fdisk提供了一个交互式接口来管理分区,它有许多子命令,
分别用于不同的管理功能,所有操作均在内存中完成,分区后不保存,可安全退出
常用命令:
p:显示现有分区表
n:创建新分区
d:删除所有分区
t:修改分区ID
l:查看支持哪些分区id
w:保存退出
q:不保存退出
m:显示帮助信息
示例:新挂载一张硬盘,创建分区
[root@localhost ~]# fdisk /dev/sdb
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)
Command (m for help): p
Disk /dev/sdb: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 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: 0xb2f759de
Device Boot Start End Blocks Id System
/dev/sdb1 2048 2610 4522297+ 83 Linux
Command (m for help): n(创建)
Command action
e extended
p primary partition (1-4)
p(创建主分区)
Partition number (1-4): 4(选择分区编号)
First cylinder (1-2610, default 1): 1(从哪个柱面开始到哪个柱面结束)
Last cylinder, +cylinders or +size{K,M,G} (1-2047, default 2047):+2G(你要创建多大的分区)
Command (m for help):p(创建完成,使用p查看sdb4)
Disk /dev/sdb: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 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: 0xb2f759de
Device Boot Start End Blocks Id System
/dev/sdb4 1 262 2104483+ 83 Linux
Partition table entries are not in disk order
现在把剩下所有空间创建一个扩展分区,在里面添加逻辑
Command (m for help): n
Command action
e extended
p primary partition (1-4)
e
Partition number (1-4): 4
Partition 4 is already defined. Delete it before re-adding it.
Command (m for help): n
Command action
e extended
p primary partition (1-4)
e
Partition number (1-4): 2
First cylinder (263-2610, default 263):
Using default value 263
Last cylinder, +cylinders or +size{K,M,G} (263-2047, default 2047):
Using default value 2047
Command (m for help): p
Disk /dev/sdb: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 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: 0xb2f759de
Device Boot Start End Blocks Id System
/dev/sdb2 263 2047 14338012+ 5 Extended
/dev/sdb4 1 262 2104483+ 83 Linux
创建逻辑分区
Command (m for help):n
Command action
l logical (5 or over)
p primary partition (1-4)
l
First cylinder (263-2047, default 263):
Using default value 263
Last cylinder, +cylinders or +size{K,M,G} (263-2047, default 2047):+1G(创建一个1G的逻辑分区)
Command (m for help): p
Disk /dev/sdb: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 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: 0xb2f759de
Device Boot Start End Blocks Id System
/dev/sdb2 263 2047 14338012+ 5 Extended
/dev/sdb4 1 262 2104483+ 83 Linux
/dev/sdb5 263 394 1060258+ 83 Linux
Partition table entries are not in disk order
Command (m for help):w(如果创建错误,可q退出,将不会损失任何数据)
The partition table has been altered!(分区表已经修改)
Calling ioctl() to re-read partition table.(正在调用ioctl() ,让内核去重新读取分区表)
Syncing disks.
[root@localhost ~]#
查看内核是否已经识别新建分区。对于一张新加硬盘来说,他是可自动识别的
[root@localhost ~]# cat /proc/partitions
major minor #blocks name
8 0 125829120 sda
8 1 307200 sda1
8 2 123489280 sda2
8 16 20971520 sdb
8 17 4522297 sdb1
8 18 1 sdb2
8 20 2104483 sdb4
8 21 1060258 sdb5
[root@localhost ~]#
对于一个已经存在的磁盘,创建分区,内核是不会自动识别它的
示例:
[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): p
Disk /dev/sda: 128.8 GB, 128849018880 bytes
255 heads, 63 sectors/track, 15665 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: 0x00088c28
Device Boot Start End Blocks Id System
/dev/sda1 * 1 39 307200 83 Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2 39 15413 123489280 83 Linux
centos6上如果你是给本机已经在使用的硬盘剩余空间创建分区,w保存时会发送警告,重读分区表时,设备资源处于忙碌状态,因此cat /proc/partitions查看刚创建的分区是不存在的,说明内核未能识别次分区
[root@localhost ~]# cat /proc/partitions 内核并没有读取sda3
major minor #blocks name
8 0 125829120 sda
8 1 307200 sda1
8 2 123489280 sda2
8 16 20971520 sdb
8 17 4522297 sdb1
8 18 1 sdb2
8 20 2104483 sdb4
8 21 1060258 sdb5
[root@localhost ~]# partx -l /dev/sda partx能够读取到
# 1: 2048- 616447 ( 614400 sectors, 314 MB)
# 2: 616448-247595007 (246978560 sectors, 126453 MB)
# 3: 247595008-251658239 ( 4063232 sectors, 2080 MB)
# 4: 0- -1 ( 0 sectors, 0 MB)
[root@localhost ~]#
[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
[root@localhost ~]# partx -a /dev/sdb
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 4
BLKPG: Device or resource busy
error adding partition 5
[root@localhost ~]#
[root@localhost ~]# cat /proc/partitions
major minor #blocks name
8 0 125829120 sda
8 1 307200 sda1
8 2 123489280 sda2
8 3 2031616 sda3
8 16 20971520 sdb
8 17 4522297 sdb1
8 18 1 sdb2
8 20 2104483 sdb4
8 21 1060258 sdb5
[root@localhost ~]#
此外的工具: sfdisk,parted
fdisk /dev/sdb
l 查看iD标注系统的类型
Command (m for help): l
扩展分区是不支持mke2fs格式化的,
Command (m for help): m
使用t
Command (m for help): t