一、设备文件
- Linux沿袭Unix的风格,将所有设备认成一个文件。
设备文件分为两种
块设备文件(b)
字符设备文件(c)
- 设备文件一般存放在/dev目录下
[root@51cto dev]# pwd
/dev
[root@51cto dev]# ll |more //可以查看对应的块设备和字符设备
二、常用的块设备文件
/dev/hd[a-t]:IDE设备
由于hda表示的是第一个IDE接口,因此had一般就是我们的primary master硬盘。
如果hda是我们的硬盘,hda[n]则表示该硬盘上的第n个分区。
[root@51cto ~]# fdisk -l //查看分区表
/dev/sd[a-z]:SCSI设备
/dev/fd[0-7]:标准软驱
/dev/md[0-31]:软raid设备
linux支持软raid,即用多个硬盘分区来模拟raid功能。模拟出来的软raid分区,用md[n]表示。
loop[0-15]:本地回访设备
ram[0-19]:内存
三、常用的字符设备文件
/dev/null:无限数据接收设备
/dev/null作为数据输出对象时(一般指在使用重定向时),所有输出给它的数据直接被丢弃。
/dev/zero:无限零资源
/dev/tty[0-31]:虚拟终端
/dev/ttyS[0-9]:串口
/dev/lp[0-3]:并口
/dev/lp0表示第一个并口设备,一般对应我们的打印机。
/dev/console:控制台
/dev/fb[0-31]:framebuffer
四、设备文件的使用
用户可以通过操作设备文件来完成对设备的操作。
- 在第一个tty终端上显示一个‘hello’
[root@51cto ~]# echo hello > /dev/tty1
[root@51cto ~]# echo hello 1> /dev/tty1
- 将第一个软驱中的软盘拷入第二个软驱中的软盘
[root@51cto ~]# cp /dev/fd0 /dev/fd1
- 备份第一个硬盘上的mbr为/root目录下的mbr文件
[root@51cto ~]# dd if=/dev/sda of=/root/mbr bs=512 count=1 //具体参考如下dd命令
1+0 records in
1+0 records out
512 bytes (512 B) copied, 0.000148893 seconds, 3.4 MB/s
[root@51cto ~]# pwd
/root
[root@51cto ~]# ll mbr
-rw-r--r-- 1 root root 512 09-29 22:46 mbr
- dd
dd指令是一个功能强大的copy命令
常用参数
if=文件名:指定源文件
of=文件名:指定目标文件
bs=xxx:指定块的大小
count=n:指定复制块的数量
五、硬盘结构
对于每一个硬盘:
- 主引导记录/Master Boot Record/MBR
- 硬盘分区表/Standard Partition Table/SPT
//硬盘的前512个字节(如IDE硬盘的第一个扇区)记录着硬盘的主引导记录(MBR)与
硬盘分区表(SPT)。MBR是指硬盘的前446个字节,告诉计算机如何去引导操作系统的核心。
SPT是硬盘第447字节到510字节的64个字节。其后两位则是校验码,表示之前的数据有效。
- 主分区/Primary Partition
- 扩展分区/Extended Partition
--逻辑分区/Logical Partition
六、硬盘分区
一块硬盘只能有四个主分区
用户可以也只可以将一个主分区变成扩展分区
在扩展分区上,用户可以以链表方式建立逻辑分区
Red Hat Linux对一块IDE硬盘最多支持到63个分区,SCSI硬盘支持到15个。
fdisk工具最多只能辨认出16个分区。
SPT只有64个字节,每一条分区记录需要16个字节,所以一块IDE硬盘只能有四个主分区。
分别对应/dev/hda1到/dev/hda4。逻辑分区是建立在扩展分区之上,
所以第一块IDE硬盘的第一个逻辑分区,其设备文件名是/dev/hda5。
七、为硬盘分区、挂载文件系统
硬盘分区工具
- disk druid:方便的分区工具,只能在安装时使用,不作具体介绍
- fdisk:运用广泛的字符界面下硬盘分区工具
可以通过使用fdisk –l /dev/xx可以查询硬盘的分区状况
[root@51cto ~]# fdisk -l /dev/sda
Disk /dev/sda: 8589 MB, 8589934592 bytes
255 heads, 63 sectors/track, 1044 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Device Boot Start End Blocks Id System
/dev/sda1 * 1 6 48163+ 83 Linux //Linux native分区
/dev/sda2 7 515 4088542+ 83 Linux //Linux native分区
/dev/sda3 516 949 3486105 83 Linux //Linux native分区
/dev/sda4 950 1044 763087+ 5 Extended //扩展分区
/dev/sda5 950 1044 763056 82 Linux swap / Solaris //交换分区
- 下面添加一块2G的硬盘进行分区:
- 添加完2G的硬盘后查看分区表:
[root@51cto ~]# fdisk -l
Disk /dev/sda: 8589 MB, 8589934592 bytes
255 heads, 63 sectors/track, 1044 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 949 3486105 83 Linux
/dev/sda4 950 1044 763087+ 5 Extended
/dev/sda5 950 1044 763056 82 Linux swap / Solaris
Disk /dev/sdb: 2147 MB, 2147483648 bytes //看出这块硬盘还没分区使用
255 heads, 63 sectors/track, 261 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Disk /dev/sdb doesn't contain a valid partition table
- 通过fdisk /dev/sdb进行分区:
[root@51cto ~]# 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)
- 对硬盘划分一个主分区,大小为1g
同时设置开机自动挂载:
Command (m for help): n //创建一个新分区
Command action
e extended
p primary partition (1-4)
p //选择是主分区
Partition number (1-4): 1 //第一个分区号
First cylinder (1-261, default 1): +1g //增加1G的大小
Last cylinder or +size or +sizeM or +sizeK (122-261, default 261):
Using default value 261
Command (m for help): w //分区信息保存退出
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
[root@51cto ~]# fdisk -l //查看分区信息,到此分区设置还没生效
Disk /dev/sda: 8589 MB, 8589934592 bytes
255 heads, 63 sectors/track, 1044 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 949 3486105 83 Linux
/dev/sda4 950 1044 763087+ 5 Extended
/dev/sda5 950 1044 763056 82 Linux swap / Solaris
Disk /dev/sdb: 2147 MB, 2147483648 bytes
255 heads, 63 sectors/track, 261 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Device Boot Start End Blocks Id System
/dev/sdb1 122 261 1124550 83 Linux
- 顺便利用剩下的空间划分一个扩展分区,然后分出两个逻辑分区
[root@51cto ~]# fdisk /dev/sdb
Command (m for help): p //查看分区信息,相当于fdisk -l
Disk /dev/sdb: 2147 MB, 2147483648 bytes
255 heads, 63 sectors/track, 261 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Device Boot Start End Blocks Id System
/dev/sdb1 122 261 1124550 83 Linux
Command (m for help): n //创建一个新分区
Command action
e extended
p primary partition (1-4)
e //创建一个扩展分区
Partition number (1-4): 1 //提示说第一个主分区被使用了
Partition 1 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 (1-261, default 1): //默认全部使用
Using default value 1
Last cylinder or +size or +sizeM or +sizeK (1-121, default 121):
Using default value 121
Command (m for help): p //查看分区信息
Disk /dev/sdb: 2147 MB, 2147483648 bytes
255 heads, 63 sectors/track, 261 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Device Boot Start End Blocks Id System
/dev/sdb1 122 261 1124550 83 Linux
/dev/sdb2 1 121 971901 5 Extended //扩展分区OK
Partition table entries are not in disk order
Command (m for help): n //创建一个新分区
Command action
l logical (5 or over)
p primary partition (1-4)
p //创建第二个扩展分区
Partition number (1-4): 3
No free sectors available //提示说没有空间创建了
Command (m for help): n
Command action
l logical (5 or over)
p primary partition (1-4)
l //重新选择逻辑分区
First cylinder (1-121, default 1): +512m //指定第一个逻辑分区大小
Last cylinder or +size or +sizeM or +sizeK (62-121, default 121):
Using default value 121
Command (m for help): p
Disk /dev/sdb: 2147 MB, 2147483648 bytes
255 heads, 63 sectors/track, 261 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Device Boot Start End Blocks Id System
/dev/sdb1 122 261 1124550 83 Linux
/dev/sdb2 1 121 971901 5 Extended
/dev/sdb5 62 121 481950 83 Linux
Partition table entries are not in disk order
Command (m for help): n
Command action
l logical (5 or over)
p primary partition (1-4)
l //创建第二个逻辑分区
First cylinder (1-121, default 1):
Using default value 1
Last cylinder or +size or +sizeM or +sizeK (1-61, default 61): //默认大小
Using default value 61
Command (m for help): p
Disk /dev/sdb: 2147 MB, 2147483648 bytes
255 heads, 63 sectors/track, 261 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Device Boot Start End Blocks Id System
/dev/sdb1 122 261 1124550 83 Linux
/dev/sdb2 1 121 971901 5 Extended
/dev/sdb5 62 121 481950 83 Linux //逻辑分区OK
/dev/sdb6 1 61 489919+ 83 Linux //逻辑分区OK
Partition table entries are not in disk order
Command (m for help): w //保存退出
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
- 顺便在这里把最后一个逻辑分区的文件系统类型改为82,即swap类型
[root@51cto ~]# fdisk /dev/sdb
Command (m for help): t //使用t改变分区的文件系统类型标识
Partition number (1-6): 6 //选择第六个分区号,即最后一个交换分区
Hex code (type L to list codes): 82 //82是swap类型的文件系统类型号码
Changed system type of partition 6 to 82 (Linux swap / Solaris)
Command (m for help): p //查看分区信息
Disk /dev/sdb: 2147 MB, 2147483648 bytes
255 heads, 63 sectors/track, 261 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Device Boot Start End Blocks Id System
/dev/sdb1 122 261 1124550 83 Linux
/dev/sdb2 1 121 971901 5 Extended
/dev/sdb5 62 121 481950 83 Linux
/dev/sdb6 1 61 489919+ 82 Linux swap / Solaris //交换分区ok
Partition table entries are not in disk order
Command (m for help): w //保存退出
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
- 到这里上面的分区结果已经生效,但是分区设置需要reboot系统才生效。如果不希望
reboot马上生效可以使用partprobe命令使内核更新分区表
[root@51cto ~]#[root@51cto ~]# partprobe
接下来就是格式化操作了,没格式化的分区是不能存储任何信息的。
格式化的命令有:mkfs tune2fs mkswap 三个命令
如下把第一个主分区格式化为ext3格式:
[root@51cto ~]# mkfs -t ext3 /dev/sdb1
//使用-t选项指定格式为ext3,如果直接mkfs /dev/sdb1的话就格式化为ext2类型
mke2fs 1.39 (29-May-2006)
Filesystem label= //分区卷标,这里为空
OS type: Linux
Block size=4096 (log=2) //块大小为4KB
Fragment size=4096 (log=2)
140832 inodes, 281137 blocks
14056 blocks (5.00%) reserved for the super user //保留5%的空间为root用户
First data block=0
Maximum filesystem blocks=289406976
9 block groups
32768 blocks per group, 32768 fragments per group
15648 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376
Writing inode tables: done
Creating journal (8192 blocks): done
Writing superblocks and filesystem accounting information: done
This filesystem will be automatically checked every 37 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.
//可以用tune2fs修改已经格式化的分区的参数
如果格式化的时候指定参数,把分区的块大小设为2KB,保留2%的空间给root用户,
分区卷标为sample
[root@51cto ~]# mkfs -t ext3 -b 2048 -m 2 -L sample /dev/sdb1 //具体命令
mke2fs 1.39 (29-May-2006)
Filesystem label=sample //分区卷标为sample
OS type: Linux
Block size=2048 (log=1) //分区的块大小设为2KB
Fragment size=2048 (log=1)
141120 inodes, 562274 blocks
11245 blocks (2.00%) reserved for the super user //保留2%的空间给root用户
First data block=0
Maximum filesystem blocks=537919488
35 block groups
16384 blocks per group, 16384 fragments per group
4032 inodes per group
Superblock backups stored on blocks:
16384, 49152, 81920, 114688, 147456, 409600, 442368
Writing inode tables: done
Creating journal (16384 blocks): done
Writing superblocks and filesystem accounting information: done
This filesystem will be automatically checked every 36 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.
当然,我们可以tune2fs命令来调整已经格式化的分区参数,参数项目和mkfs命令相同,
但不是所有参数都可以调整,如块大小不能调整。如果要改变分区块大小就必须重新格式化。
[root@51cto ~]# tune2fs -r 5000 -L sample24 /dev/sdb1
//使用tune2fs保留给root空间不用百分比,而是分配块,5000个,即5000*2KB,
大概10M左右。同时把卷标改为sample24
tune2fs 1.39 (29-May-2006)
Setting reserved blocks count to 5000
[root@51cto ~]# tune2fs -l /dev/sdb1
tune2fs 1.39 (29-May-2006)
Filesystem volume name: sample24
Last mounted on: <not available>
Filesystem UUID: 95c34d9d-c661-46ad-ab1c-f75a2441af80
Filesystem magic number: 0xEF53
Filesystem revision #: 1 (dynamic)
Filesystem features: has_journal resize_inode dir_index filetype sparse_super
Default mount options: (none)
Filesystem state: clean
Errors behavior: Continue
Filesystem OS type: Linux
Inode count: 141120
Block count: 562274
Reserved block count: 5000
…...
如果希望分区格式化为FAT格式,则:
[root@51cto ~]# mkfs -t vfat /dev/sdb5 //把逻辑分区格式化为fat格式
mkfs.vfat 2.11 (12 Mar 2005)
对于交换分区就不可以使用mkfs命令格式化了,而是mkswap,如下格式化/dev/sdb6
[root@51cto ~]# mkswap /dev/sdb6
Setting up swapspace version 1, size = 501669 kB
格式化话启用交换分区,首先查看启用情况:
[root@51cto ~]# free -m
total used free shared buffers cached
Mem: 408 130 277 0 44 50
-/+ buffers/cache: 35 372
Swap: 745 0 745 //发现还没启用,是属于/dev/sda5的
[root@51cto ~]# swapon /dev/sdb6 //通过swapon启用交换分区
[root@51cto ~]# free -m
total used free shared buffers cached
Mem: 408 130 277 0 44 50
-/+ buffers/cache: 35 372
Swap: 1223 0 1223 //启用了,增加了500M
如果要使得已经加载的交换分区失效,使用swapoff命令
[root@51cto ~]# swapoff /dev/sdb6
[root@51cto ~]# free -m
total used free shared buffers cached
Mem: 408 130 277 0 44 50
-/+ buffers/cache: 35 372
Swap: 745 0 745 //恢复到原来的大小了
- 如上已经对各个分区格式化完成了,接下来就是挂载文件系统使用了
对一个磁盘格式化完成后并不能直接使用分区进行存取,必须借助mount命令挂载
[root@51cto ~]# mount //直接使用mount查看挂载情况
/dev/sda2 on / type ext3 (rw)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
/dev/sda3 on /home type ext3 (rw)
/dev/sda1 on /boot type ext3 (rw)
tmpfs on /dev/shm type tmpfs (rw)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw)
nfsd on /proc/fs/nfsd type nfsd (rw)
挂载点可看成是访问文件系统的入口,以上可以看出有哪些文件系统被挂载,是否可读
首先创建目录作为挂载点:
[root@51cto ~]# mkdir /51cto //创建目录作挂载点
[root@51cto ~]# mkdir /51cto/a /51cto/b /51cto/c //创建子目录
[root@51cto ~]# touch /51cto/1 /51cto/2 /51cto/3 //创建文件
[root@51cto ~]# ls /51cto/ //查看目录和文件
1 2 3 a b c
[root@51cto ~]# mount /dev/sdb1 /51cto/ //将/dev/sdb1挂载到/51cto/
[root@51cto ~]# mount //查看挂载情况
……
/dev/sdb1 on /51cto type ext3 (rw)
[root@51cto ~]# ll /51cto/ //发现作为挂载点后,里头本来有的目录文件都没了
总计 16
drwx------ 2 root root 16384 09-30 18:48 lost+found
所以我们不要使用重要的目录作为挂载点,否则挂载后原有的内容无法访问了
[root@51cto ~]# mkdir /51cto/d //在挂载点里头创建目录
[root@51cto ~]# touch /51cto/4 //在挂载点里头创建文件
[root@51cto ~]# ls /51cto/ //发现新创建的目录文件是存在的
4 d lost+found
[root@51cto ~]# cd /51cto/ //切换到挂载点
[root@51cto 51cto]# ls //查看里头的目录文件
4 d lost+found
[root@51cto 51cto]# umount /dev/sdb1 //卸载挂载点
umount: /51cto: device is busy //提示无法卸载,必须切换到其他目录
umount: /51cto: device is busy
[root@51cto 51cto]# cd //切换到/root
[root@51cto ~]# umount /dev/sdb1 //卸载挂载点
[root@51cto ~]# ls /51cto/ //可以看到原本创建的目录,但在挂载点创建的就丢失了
1 2 3 a b c
当然,如果你重新挂载上去,还是可以看到新创建的目录文件的
[root@51cto ~]# mount /dev/sdb1 /51cto/
[root@51cto ~]# ls /51cto/
4 d lost+found
- 最后介绍mount命令的选项,-o选项是mount命令最强大的选项,如下:
[root@51cto ~]# mount -o ro /dev/sdb1 /51cto/ //以只读属性挂载
[root@51cto ~]# touch /51cto/24 //无法执行写操作
touch: 无法触碰 “/51cto/24”: 只读文件系统
[root@51cto ~]# umount /dev/sdb1 //卸载
[root@51cto ~]# mount /dev/sdb1 /51cto/ //重新普通挂载
[root@51cto ~]# touch /51cto/24 //可以执行写操作了
[root@51cto ~]# cp /bin/ls /51cto/ //copy命令ls到/dev/sdb1
[root@51cto ~]# /51cto/ls //复制过去的ls命令可以正常使用
anaconda-ks.cfg
install.log
install.log.syslog
……
[root@51cto ~]# umount /dev/sdb1 //卸载
[root@51cto ~]# mount -o noexec /dev/sdb1 /51cto/
//以noexec不可执行属性挂载文件系统
[root@51cto ~]# /51cto/ls //ls命令无法执行
-bash: /51cto/ls: 权限不够
[root@51cto ~]# umount /dev/sdb1
[root@51cto ~]# mount /dev/sdb1 /51cto/
到这里,分区、格式化、挂载使用的步骤就OK了
八、开机自动挂载文件系统
上面的操作在系统reboot后就要重新挂载,所以可以通过编辑/etc/fstab实现自动挂载
[root@51cto ~]# vim /etc/fstab
[root@51cto ~]# cat /etc/fstab
LABEL=/ / ext3 defaults 1 1
LABEL=/home /home ext3 defaults 1 2
LABEL=/boot /boot ext3 defaults 1 2
tmpfs /dev/shm tmpfs defaults 0 0
devpts /dev/pts devpts gid=5,mode=620 0 0
sysfs /sys sysfs defaults 0 0
proc /proc proc defaults 0 0
LABEL=SWAP-sda5 swap swap defaults 0 0
/dev/sdb1 /51cto ext3 defaults 0 0
添加的红色的部分,对于这一行作如下介绍:
- 第一列表示挂载的设备名 :/dev/sdb1
- 第二列表示对应的挂载点: /51cto
- 第三列表示挂载的文件系统: ext3,如果不清楚就用auto类型
- 第四列表示挂载属性: 如上面的ro、noexec等等...defaults是默认值,
- 第五列表示指明是否要备份:0表示不备份,1表示备份,一般只有根分区要备份
- 第六列表示指明自检顺序:0不自检,1或2要自检,一般根分区设1,其他分区设2
对于新增加的内容如果不reboot就生效,可以使用mount -a 命令
[root@51cto ~]# mount -a //重读/etc/fstab文件加载那些尚未生效的行
[root@51cto ~]# mount
……
/dev/sdb1 on /51cto type ext3 (rw)
如果在/etc/fstab中对已经挂载的文件系统对应的行进行了修改,可以使用mount -o remount
命令使得系统不用reboot就可以使得修改后的内容生效,比如:
[root@51cto ~]# vim /etc/fstab
[root@51cto ~]# cat /etc/fstab //红色为修改的内容
…...
/dev/sdb1 /51cto ext3 ro,noexec 0 0
[root@51cto ~]# mount
……
/dev/sdb1 on /51cto type ext3 (rw) //发现没有改变
[root@51cto ~]# mount -o remount /51cto/ //对挂载点进行操作
或者也可以对设备名进行操作:
[root@51cto ~]# mount -o remount /dev/sdb1
[root@51cto ~]# mount
/dev/sdb1 on /51cto type ext3 (ro,noexec) //发现修改成功了
http://twentyfour.blog.51cto.com/945260/399999
http://twentyfour.blog.51cto.com/945260/399999
本文深入探讨了Linux环境下硬盘分区、文件系统类型、挂载与自动挂载的详细流程,包括设备文件的使用、各类分区的创建与管理、文件系统的创建与挂载方法,以及如何配置开机自动挂载。同时介绍了如何使用mount命令调整文件系统的挂载属性,如只读、不可执行等。此外,还提供了编辑fstab文件实现自动挂载的实例,并讲解了如何通过mount命令重读fstab文件加载未生效的挂载项。
689

被折叠的 条评论
为什么被折叠?



