Linux操作系统系列之“载入点”


和 Windows 将分区当做一个个 "磁盘" 不同,Linux 将所有的分区和设备都整合在同一个文件目录规范当中,所有分区都必须 "挂载" 到某个目录才能使用。

1. 挂载演示

我们试着将一个新分区挂载到文件目录。

(1) 创建分区

yuhen@yuhen-desktop:~$ sudo fdisk /dev/sdb

The number of cylinders for this disk is set to 1044.
There is nothing wrong with that, but this is larger than 1024,
and could in certain setups cause problems with:
1) software that runs at boot time (e.g., old versions of LILO)
2) booting and partitioning software from other OSs
(e.g., DOS FDISK, OS/2 FDISK)

Command (m for help): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-1044, default 1):
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-1044, default 1044):
Using default value 1044

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.

yuhen@yuhen-desktop:~$ sudo fdisk -l /dev/sdb

Disk /dev/sdb: 8589 MB, 8589934592 bytes
255 heads, 63 sectors/track, 1044 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Disk identifier: 0x90947a1b

Device Boot Start End Blocks Id System
/dev/sdb1 1 1044 8385898+ 83 Linux

(2) 格式化分区

yuhen@yuhen-desktop:~$ sudo mke2fs -j /dev/sdb1
mke2fs 1.41.4 (27-Jan-2009)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
524288 inodes, 2096474 blocks
104823 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=2147483648
64 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632

Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done

This filesystem will be automatically checked every 21 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.

(3) 创建要挂载的目录

yuhen@yuhen-desktop:~$ sudo mkdir /backup
yuhen@yuhen-desktop:~$ cd /backup

yuhen@yuhen-desktop:/backup$ sudo touch a.txt
yuhen@yuhen-desktop:/backup$ ls -l
total 0
-rw-r--r-- 1 root root 0 2009-08-03 11:12 a.txt

# 创建一个空文件用于演示。

(4) 挂载分区

yuhen@yuhen-desktop:~$ sudo mount /dev/sdb1 /backup
yuhen@yuhen-desktop:~$ cd /backup
yuhen@yuhen-desktop:/backup$ ls -l
total 16
drwx------ 2 root root 16384 2009-08-03 11:11 lost+found

我们会发现挂载分区后,原目录中的文件和目录被 "隐藏"。

(5) 取消挂载

yuhen@yuhen-desktop:/backup$ sudo umount /backup
umount: /backup: device is busy.
(In some cases useful info about processes that use
the device is found by lsof(8) or fuser(1))

# 取消挂载前需要离开挂载目录。

yuhen@yuhen-desktop:/backup$ cd ..
yuhen@yuhen-desktop:/$ sudo umount /backup
yuhen@yuhen-desktop:/$ ls -l /backup
total 0
-rw-r--r-- 1 root root 0 2009-08-03 11:12 a.txt

取消挂载后,原目录内容 "恢复"。

2. 自动载入

将挂载信息写入 "/etc/fstab" 就可以在每次启动 Linux 时自动挂载设备。

yuhen@yuhen-desktop:/$ sudo cat /etc/fstab

# /etc/fstab: static file system information.
#
# Use 'vol_id --uuid' to print the universally unique identifier for a
# device; this may be used with UUID= as a more robust way to name devices
# that works even if disks are added and removed. See fstab(5).
#
# <file system> <mount point> <type> <options> <dump> <pass>

proc /proc proc defaults 0 0
UUID=d5b76e3b-de44-4fab-b9d3-741e8b81df11 / ext3 relatime,errors=remount-ro 0 1 # / was on /dev/sda1 during installation
UUID=61b0fcee-b18b-4c2b-8e17-6ee23d953322 none swap sw 0 0 # swap was on /dev/sda5 during installation
/dev/scd0 /media/cdrom0 udf,iso9660 user,noauto,exec,utf8 0 0
/dev/fd0 /media/floppy0 auto rw,user,noauto,exec,utf8 0 0

我们可以看到 "/"、"swap" 等挂载设置,我们将 /dev/sdb1 也加进去。

yuhen@yuhen-desktop:/$ sudo nano /etc/fstab

# <file system> <mount point> <type> <options> <dump> <pass>
...
/dev/scd0 /media/cdrom0 udf,iso9660 user,noauto,exec,utf8 0 0
/dev/fd0 /media/floppy0 auto rw,user,noauto,exec,utf8 0 0
/dev/sdb1 /backup ext3 defaults 0 0
....

文件类型是 ext3,参数通常用 defaults 即可。dump 表示是否做系统备份,pass 表示启动的时候是否做分区检查。

yuhen@yuhen-desktop:/$ sudo reboot

重启系统后 /dev/sdb1 会自动挂载。我们还可以执行 "sudo mount -a" 重新载入 /etc/fstab 中的挂载内容。

yuhen@yuhen-desktop:~$ sudo mount

/dev/sda1 on / type ext3 (rw,relatime,errors=remount-ro)
tmpfs on /lib/init/rw type tmpfs (rw,nosuid,mode=0755)
proc on /proc type proc (rw,noexec,nosuid,nodev)
sysfs on /sys type sysfs (rw,noexec,nosuid,nodev)
varrun on /var/run type tmpfs (rw,nosuid,mode=0755)
varlock on /var/lock type tmpfs (rw,noexec,nosuid,nodev,mode=1777)
udev on /dev type tmpfs (rw,mode=0755)
tmpfs on /dev/shm type tmpfs (rw,nosuid,nodev)
devpts on /dev/pts type devpts (rw,noexec,nosuid,gid=5,mode=620)
fusectl on /sys/fs/fuse/connections type fusectl (rw)
lrm on /lib/modules/2.6.28-11-generic/volatile type tmpfs (rw,mode=755)
/dev/sdb1 on /backup type ext3 (rw)
securityfs on /sys/kernel/security type securityfs (rw)
binfmt_misc on /proc/sys/fs/binfmt_misc type binfmt_misc (rw,noexec,nosuid,nodev)
gvfs-fuse-daemon on /home/yuhen/.gvfs type fuse.gvfs-fuse-daemon (rw,nosuid,nodev,user=yuhen)

相关内容