分析Linux磁盘管理与文件系统专题

1.关于设备文件

A 我们知道常见的设备文件有:字符设备文件(character),块设备文件(block)。


B 块设备,简写b,随机访问,比如硬盘。


C 字符设备,简写c,线性访问,比如键盘,鼠标,显示器。


D 我们的设备文件常常在/dev目录下,并且没有大小。因为设备文件只是作为设备访问   的入口。


E 设备文件,一般用major(主设备号),minor(次设备号)进行标示。主设备号标示设备类型,次设备号标示同一种类型设备下的不同设备。



2.创建设备文件实例

1
2
3
4
5
[root@localhost dev] # mknod mydev c 66 0
[root@localhost dev] # mknod -m 640 mydev2  c 66 1
[root@localhost dev] # ls -l mydev*
crw-r--r-- 1 root root 66, 0 Jun 22 21:27 mydev
crw-r----- 1 root root 66, 1 Jun 22 21:28 mydev2

也就是说:

mknod  [-m permission] yourDevName b|c major minor


设备文件主要就是作为设备的访问入口的。要保证我们的主设备号,与某类设备是关联的。内核会识别主设备号的。比如,我们插入某个设备,内核识别为66,那么我们读取/dev/mydev,就是在读取这个设备,向/dev/mydev发送数据,就是向这个设备发送数据。举个例子如下:

1
2
3
4
5
6
7
[root@localhost dev] # tty
/dev/pts/0
[root@localhost dev] # who
root     pts /0         2014-06-22 20:22 (192.168.204.1)
zhangfengzhe pts /1         2014-06-22 21:36 (192.168.204.1)
[root@localhost dev] # echo "hello , zhangfengzhe " >> /dev/pts/1
[root@localhost dev] #

那么zhangfengzhe用户的tty下,将出现消息"hello,zhangfengzhe"。


3.关于硬盘

A 硬盘设备如何标示的?

并口的IDE,ATA 通常标示为/dev/hd

串口的STA,SCSI,USB 通常标示为/dev/sd


B 对于同一种类型的硬盘下的不同设备,通常会用a,b,c...进行标示


C 比如我们计算机上IDE通常有2个口,而一个口可以接入2块盘,那么有:

    /dev/hda , /dev/hdb , /dev/hdc , /dev/hdd


D 标示完硬盘后,就应该标示分区信息了。比如:

    /dev/hda1  表示IDE,第一块硬盘,第一个primary partition


E 注意下:

    一块硬盘,最多有4个主分区,1个扩展分区,多个逻辑分区。

   【下面会进行实验,清清楚楚的看到!】


4.任务实战:对一个新硬盘进行分区

首先,利用fdisk -l查看下分区信息。


1
2
3
4
5
6
7
8
9
10
11
12
13
14
[root@localhost ~] # fdisk -l
Disk  /dev/sda : 21.4 GB, 21474836480 bytes
255 heads, 63 sectors /track , 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
    Device Boot      Start         End      Blocks   Id  System
/dev/sda1    *           1           6       48163+  83  Linux
/dev/sda2                7         515     4088542+  83  Linux
/dev/sda3              516        2356    14787832+  83  Linux
/dev/sda4             2357        2610     2040255    5  Extended
/dev/sda5             2357        2610     2040223+  82  Linux swap / Solaris
Disk  /dev/sdb : 1073 MB, 1073741824 bytes
255 heads, 63 sectors /track , 130 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Disk  /dev/sdb  doesn't contain a valid partition table



从上面的信息,可以看出,/dev/sdb为新硬盘,没有分区。



然后,使用fdisk /dev/sdb来进行分区操作。

注意利用fdisk /dev/sdb将进入到一个交互式的界面。

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@localhost ~] # fdisk /dev/sdb
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel. Changes will remain  in  memory only,
until  you decide to write them. After that, of course, the previous
content won't be recoverable.
Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)
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)


我们可以利用m来获取帮助,p来显示当前硬盘的分区信息【包括新建的没有保存的】,d来删除分区,t来修改分区类型【所谓分区类型,就是指文件系统类型】,l可以列出分区类型,w用来保存你设置的分区信息并退出,q则不保存且退出。



接下来,我们输入n,来创建新分区:

1
2
3
4
5
6
7
8
Command (m  for  help): n
Command action
    e   extended
    p   primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-130, default 1): 1
Last cylinder or +size or +sizeM or +sizeK (1-130, default 130): +50M


创建分区,需要明确是扩展分区,还是主分区。对于新硬盘,还需要指明分区编号。分区都是从一个柱面到另一个柱面,即起始柱面到结束柱面。但是,大多数时候,我们一般都是说分多大个区,可以用上面的方式来指定分区大小。需要注意的是,这并不精确,因为比如7个磁道为49.2M,8个磁道为50.2M,由于一个磁道只能属于一个分区。


然后,输入p,看下现在的分区信息:

1
2
3
4
5
6
Command (m  for  help): p
Disk  /dev/sdb : 1073 MB, 1073741824 bytes
255 heads, 63 sectors /track , 130 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
    Device Boot      Start         End      Blocks   Id  System
/dev/sdb1                1           7       56196   83  Linux


采用上面的方式,创建4个主分区,如下:

1
2
3
4
5
6
7
8
9
Command (m  for  help): p
Disk  /dev/sdb : 1073 MB, 1073741824 bytes
255 heads, 63 sectors /track , 130 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
    Device Boot      Start         End      Blocks   Id  System
/dev/sdb1                1           7       56196   83  Linux
/dev/sdb2                8          15       64260   83  Linux
/dev/sdb3               16          25       80325   83  Linux
/dev/sdb4               26         130      843412+   5  Extended


然后,我们来继续创建主分区,看看会发生什么?

You must delete some partition and add an extended partition first


接下来,我们删除第四个主分区,去创建扩展分区。

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
Command (m  for  help): d
Partition number (1-4): 4
Command (m  for  help): p
Disk  /dev/sdb : 1073 MB, 1073741824 bytes
255 heads, 63 sectors /track , 130 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
    Device Boot      Start         End      Blocks   Id  System
/dev/sdb1                1           7       56196   83  Linux
/dev/sdb2                8          15       64260   83  Linux
/dev/sdb3               16          25       80325   83  Linux
Command (m  for  help): n
Command action
    e   extended
    p   primary partition (1-4)
e
Selected partition 4
First cylinder (26-130, default 26): 
Using default value 26
Last cylinder or +size or +sizeM or +sizeK (26-130, default 130): 
Using default value 130
Command (m  for  help): p
Disk  /dev/sdb : 1073 MB, 1073741824 bytes
255 heads, 63 sectors /track , 130 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
    Device Boot      Start         End      Blocks   Id  System
/dev/sdb1                1           7       56196   83  Linux
/dev/sdb2                8          15       64260   83  Linux
/dev/sdb3               16          25       80325   83  Linux
/dev/sdb4               26         130      843412+   5  Extended


注意,扩展分区创建完毕后,并不能使用,必须创建逻辑分区。


1
2
3
4
5
6
7
8
9
10
11
12
13
14
Command (m  for  help): n
First cylinder (26-130, default 26):  
Using default value 26
Last cylinder or +size or +sizeM or +sizeK (26-130, default 130): +50M
Command (m  for  help): p
Disk  /dev/sdb : 1073 MB, 1073741824 bytes
255 heads, 63 sectors /track , 130 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
    Device Boot      Start         End      Blocks   Id  System
/dev/sdb1                1           7       56196   83  Linux
/dev/sdb2                8          15       64260   83  Linux
/dev/sdb3               16          25       80325   83  Linux
/dev/sdb4               26         130      843412+   5  Extended
/dev/sdb5               26          32       56196   83  Linux


最后,我们w退出,然后查看:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
[root@localhost ~] # fdisk -l
Disk  /dev/sda : 21.4 GB, 21474836480 bytes
255 heads, 63 sectors /track , 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
    Device Boot      Start         End      Blocks   Id  System
/dev/sda1    *           1           6       48163+  83  Linux
/dev/sda2                7         515     4088542+  83  Linux
/dev/sda3              516        2356    14787832+  83  Linux
/dev/sda4             2357        2610     2040255    5  Extended
/dev/sda5             2357        2610     2040223+  82  Linux swap / Solaris
Disk  /dev/sdb : 1073 MB, 1073741824 bytes
255 heads, 63 sectors /track , 130 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
    Device Boot      Start         End      Blocks   Id  System
/dev/sdb1                1           7       56196   83  Linux
/dev/sdb2                8          15       64260   83  Linux
/dev/sdb3               16          25       80325   83  Linux
/dev/sdb4               26         130      843412+   5  Extended
/dev/sdb5               26          32       56196   82  Linux swap / Solaris
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值