Linux操作系統系列之“載入點”
來源:程序員人生 發布時間:2013-10-17 04:29:41 閱讀次數:2581次
和 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)
生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈