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 GPLdrwxr-xr-x 4 root root 2048 06-19 23:22 imagesdrwxr-xr-x 2 root root 2048 06-19 23:05 isolinux (以下省略)

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

[root@xx ~]# ls /dev |grep ^looploop0loop1loop2loop3loop4loop5loop6loop7

一般一个系统中就有这么多,当然你也可以自行增加哈, 在看看刚刚挂的个镜象用的是哪个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=1000010000+0 records in10000+0 records out10240000 bytes (10 MB) copied, 0.0853896 seconds, 120 MB/s[root@xx ~]# file hd.imghd.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/loop1Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabelBuilding a new DOS disklabel. Changes will remain in memory only,until you decide to write them. After that, of course, the previouscontent 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/loop1mke2fs 1.39 (29-May-2006)Filesystem label=OS type: LinuxBlock size=1024 (log=0)Fragment size=1024 (log=0)2512 inodes, 10000 blocks500 blocks (5.00%) reserved for the super userFirst data block=1Maximum filesystem blocks=104857602 block groups8192 blocks per group, 8192 fragments per group1256 inodes per groupSuperblock backups stored on blocks:8193

Writing inode tables: doneWriting superblocks and filesystem accounting information: done

This filesystem will be automatically checked every 33 mounts or180 days, whichever comes first. Use tune2fs -c or -i to override.[root@xx ~]# file hd.imghd.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.cmain(){}

接着把其重定向到/dev/null,发现没有什么内容会出现在SHELL窗口[root@xx ~]# cat c.c > /dev/null[root@xx ~]#

看自家总在期待,不知将来好歹,新乐吧总在不断等待,

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

相关文章:

你感兴趣的文章:

标签云: