Linux下几个特别有用的设备文件


正如大家所知道的那样,linux下的设备很特别,系统总是以总一的方式读取设备,而设备间的差别由设备驱动程序来负责完成。正因为如此,设备管理起来非常地容易。linux也提供了虚设备文件,即无实际物理硬件相对应的设备,它们在实际的工作学习中会经常用到,现介绍如下

(一)-首先说的当然是回环设备,loop device,这是什么呢?就是把文件模拟成设备,然后就可以像实际设备那样进行相关的操作啦,是大家所熟悉的就是挂载光盘镜象啦,比如说挂载CentOS-5.2-i386-bin-DVD.iso 这个,那么在SHELL中输入 并用ls看其内容

[root@xx ~]# mount -o loop /mnt/iso/CentOS-5.2-i386-bin-DVD/CentOS-5.2-i386-bin-DVD.iso /media

[root@xx ~]# ls /media -l
 


总计 462

drwxr-xr-x 2 root root 397312 06-19 23:23 CentOS
-rw-r–r– 7 root root 212 06-15 06:32 EULA
-rw-r–r– 7 root root 18009 06-15 06:32 GPL
drwxr-xr-x 4 root root 2048 06-19 23:22 images
drwxr-xr-x 2 root root 2048 06-19 23:05 isolinux  (以下省略)

那么一个系统中有多少个loop device呢,我们有来看一看

[root@xx ~]# ls /dev |grep ^loop
loop0
loop1
loop2
loop3
loop4
loop5
loop6
loop7


一般一个系统中就有这么多,当然你也可以自行增加哈, 在看看刚刚挂的个镜象用的是哪个loop device

[root@xx ~]# losetup -a


/dev/loop0: [0805]:1245190 (/mnt/iso/CentOS-5.2-i386-bin-DVD/CentOS-5.2-i386-bin-DVD.iso)

(二),/dev/zero,这个设备是做什么的,,每次对它读一次它就对外输出一个字节,现在来做一个10M的数据文件,然后有file命令来查看

[root@xx ~]# dd if=/dev/zero of=hd.img bs=1k count=10000
10000+0 records in
10000+0 records out
10240000 bytes (10 MB) copied, 0.0853896 seconds, 120 MB/s
[root@xx ~]# file hd.img
hd.img: data


由file命令hd.img的是数据文件,现在用loop device把它挂成一虚拟的硬盘

losetup /dev/loop1 hd.img
 


[root@xx ~]# losetup -a
/dev/loop0: [0805]:1245190 (/mnt/iso/CentOS-5.2-i386-bin-DVD/CentOS-5.2-i386-bin-DVD.iso)
/dev/loop1: [fd00]:2586037 (hd.img)


可知,hd.img 对应着loop1,而 CentOS-5.2-i386-bin-DVD/CentOS-5.2-i386-bin-DVD.iso对应着loop0,
现在我们就可以通过/dev/loop1 来操作hd.img啦,,它就象一个真实的设备一样,可以进行分区,创建文件系统等

[root@xx ~]# fdisk /dev/loop1
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 0×0000 of partition table 4 will be corrected by w(rite)
Command (m for help):


用fdisk进行分区,然后创建文件系统,并用file查看hd.img的属性

[root@xx ~]# mkfs -t ext2 /dev/loop1
mke2fs 1.39 (29-May-2006)
Filesystem label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
2512 inodes, 10000 blocks
500 blocks (5.00%) reserved for the super user
First data block=1
Maximum filesystem blocks=10485760
2 block groups
8192 blocks per group, 8192 fragments per group
1256 inodes per group
Superblock backups stored on blocks:
8193
Writing inode tables: done
Writing superblocks and filesystem accounting information: done
This filesystem will be automatically checked every 33 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.
[root@xx ~]# file hd.img
hd.img: Linux rev 1.0 ext2 filesystem data

你可借助loop device来学习LVM,RAID,借用loop device来创建多个虚拟硬盘

(三)/dev/null

这个呢,,在学SHELL编程时就会知道啦,所有发往此设备的信息全部都会变为无,借用重定向可以把不须要在SHELL窗口显示的信息全部给删除

用cat显示c.c文件的内容

[root@xx ~]# cat c.c
main()
{}

接着把其重定向到/dev/null,发现没有什么内容会出现在SHELL窗口

[root@xx ~]# cat c.c > /dev/null

[root@xx ~]#

感谢来自OwnLinux:Linux 下几个特别有用的设备文件的稿件


« 
» 
快速导航

Copyright © 2016 phpStudy | 豫ICP备2021030365号-3