Linux磁碟和檔案系統管理(3)_磁碟掛載 卸載
磁碟的掛載與卸載 掛載:將系統額外的檔案系統通過根下的目錄與根檔案系統建立起關聯關係,將該目錄成為進入新磁碟分割(即檔案系統)的入口操作。 卸載:解除此關聯關係的過程。 掛載點 mount point:裝置掛載的目錄 注意:掛載點在掛載後,其目錄下的原有檔案和目錄將會暫時隱藏,等新分區卸載後,原有檔案和目錄會再次顯示出來。所以建議 掛載點使用空目錄。 掛載 mount: mount [-l] 通過讀取/etc/mtab檔案來顯示當前系統所有已經掛載的裝置 [root@localhost test]# mountproc on /proc type proc (rw,nosuid,nodev,noexec,relatime)/dev/sda3 on / type ext4 (rw,relatime,seclabel,data=ordered)/dev/sda2 on /usr type ext4 (rw,relatime,seclabel,data=ordered)....省略...debugfs on /sys/kernel/debug type debugfs (rw,relatime)/dev/sda1 on /boot type ext4 (rw,relatime,seclabel,data=ordered)/dev/sda5 on /home type ext4 (rw,relatime,seclabel,data=ordered) mount -a: 按照設定檔/etc/fstab的資料掛載所有沒有掛載的磁碟 mount [-fnrsvw] [-t vfstype] [-o options] device mount_point device :要掛載的裝置 1 裝置檔案: /dev/sdb5 2 卷標: -L ‘label’ 3 uuid: -U ‘uuid’ 4 偽檔案系統 5 檔案路徑:綁定掛載 mount_point :掛載點 建議空目錄 選項:-l:顯示當前掛載資訊時,增加Label列的顯示-n:不寫入/etc/mtab,預設情況下,將實際掛載情況即時寫入/etc/mtab中此時,如果想查看掛載的所有檔案系統:cat /proc/mounts-t:指定將要掛載的檔案系統類型-L : 通過指定卷標label掛載-r:以唯讀方式掛載-a:掛載在/etc/fstab中的所有檔案系統-f : 跳過系統掛載,試運行-B:綁定一個目錄到另外一個目錄上-o options 主要用來描述裝置或檔案的掛接方式。 參數: loop:用來把一個檔案當成硬碟分區掛接上系統 ro:採用唯讀方式掛接裝置 rw:採用讀寫方式掛接裝置 iocharset:指定訪問檔案系統所用字元集 async:檔案系統使用非同步寫入 sync:檔案系統使用同步寫入 auto,noauto:允許分區被使用mount -a 自動掛載 dev,nodev:是否允許此分區上建立裝置檔案 suid,nosuid:是否允許此分區上有suid/sgid的檔案格式 user,nouser:是否允許在此分區下任何使用者執行mount defaults:預設值rw, suid, dev, exec, auto, nouser, and async. remount:重新掛載,多在系統出錯時,或更新參數時使用 樣本
#按指定卷標掛載 -L [root@localhost ~]# mount -L 'mytest' /mnt/test1[root@localhost ~]# mount | grep '/dev/sdb.'30:/dev/sdb1 on /root/test_mount/test1 type ext4 (rw,relatime,seclabel,data=ordered)#唯讀方式掛載分區[root@localhost ~]# mount -r /dev/sdb5 /mnt/test2[root@localhost ~]# mount | grep '/dev/sdb.'30:/dev/sdb1 on /root/test_mount/test1 type ext4 (rw,relatime,seclabel,data=ordered)31:/dev/sdb5 on /root/test_mount/test2 type ext4 (ro,relatime,seclabel,data=ordered)[root@localhost ~]# dfFilesystem 1K-blocksUsed Available Use% Mounted on....省略.../dev/sdb1 9993202564927944 1% /mnt/test1/dev/sdb5 499656 784462176 1% /mnt/test2#將/home目錄掛載到/mnt/test3下面[root@localhost mnt]# mount -B /home/ /mnt/test3/[root@localhost mnt]# ll /home/ /mnt/test3//home/:total 32drwx------. 2 hadoophadoop 4096 Sep 9 08:04 hadoopdrwx------. 2 root root 16384 Aug 20 06:12 lost+founddrwx------. 2 roger roger 4096 Aug 20 06:34 rogerdrwx------. 3 test1 test1 4096 Sep 7 04:28 test1drwx------. 2 testuser1 testuser1 4096 Sep 13 18:03 testuser1/mnt/test3/:total 32drwx------. 2 hadoophadoop 4096 Sep 9 08:04 hadoopdrwx------. 2 root root 16384 Aug 20 06:12 lost+founddrwx------. 2 roger roger 4096 Aug 20 06:34 rogerdrwx------. 3 test1 test1 4096 Sep 7 04:28 test1drwx------. 2 testuser1 testuser1 4096 Sep 13 18:03 testuser1
umount:卸載命令umount [-fn] DEVICE DEVICE 裝置檔案名稱umount [-fn] MOUNT_POINT MOUNT_POINT 掛載點 參數:-f:強制卸載-n:不更新/etc/mtab的情況下卸載 樣本:
#裝置名稱卸載[root@localhost ~]# umount /dev/sdb1#因為掛載的是目錄,所以只能同過掛載點卸載[root@localhost ~]# umount /mnt/test3/
卸載時遇到裝置被佔用[root@localhost ~]# umount /dev/sdb1umount: /mnt/test3: target is busy. (In some cases useful info about processes that use the device is found by lsof(8) or fuser(1)) 查看正在訪問指定掛載點的進程:
# fuser -v MOUNT_POINT[root@localhost ~]# fuser -v /mnt/test3/USER PID ACCESS COMMAND/mnt/test3: root kernel mount /mnt/test3root3356 ..c.. bash
終止所有正在訪問指定的掛載點的進程:
# fuser -km MOUNT_POINT[root@localhost ~]# fuser -km /mnt/test3//mnt/test3:3356c#成功卸載[root@localhost ~]# umount /dev/sdb1
補充說明: fuser命令:顯示所有正在使用著指定的file, file system 或者 sockets的進程資訊 fuser [-fuv] [-a|-s] [-4|-6] [-c|-m|-n space] [ -k [-i] [-M] [-w] [-SIGNAL] ] 檔案名稱/檔案系統/sockets 參數:-k,--kill:殺死正在訪問的檔案的進程 -i:互動模式 殺死進程前進行確認-m:後面接的那個檔案名稱會主動上提到該檔案系統的最頂層,對umount不成功很有效-u :進程號後圓括弧中顯示進程擁有者-v:輸出進程詳細資料預設情況下,每個進程號後面都跟隨一個字母,該字母指示進程如何使用檔案。c 檔案作為目前的目錄e 檔案作為程式的可執行對象r 檔案作為根目錄m 檔案作為mmap檔案或共用庫 樣本:
[root@localhost ~]# fuser /mnt//mnt: 3427c[root@localhost ~]# fuser -u /mnt//mnt: 3427c(root)[root@localhost ~]# fuser -v /mnt/USER PID ACCESS COMMAND/mnt: root3427 ..c.. bash[root@localhost ~]# fuser -ki /mnt//mnt: 3427cKill process 3427 ? (y/N) n
掛載相關設定檔:/etc/fstab (file system table) 記錄是系統準備裝載檔案系統,用來設定 開機掛載
[root@localhost ~]# cat /etc/fstab ## /etc/fstab# Created by anaconda on Thu Aug 20 06:13:01 2015## Accessible filesystems, by reference, are maintained under '/dev/disk'# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info#UUID=8306bbdb-4c6b-4975-8596-c3721eeb6998 /ext4defaults 1 1UUID=4bfd1d98-b114-4685-84a0-d44130fb3128 /bootext4defaults 1 2UUID=86be1f1b-e7b0-4d86-a743-7cc99ba332ec /homeext4defaults 1 2UUID=cc4474b4-402e-4e99-9b04-f55327d94faa /usr ext4defaults 1 2UUID=f885b330-d561-454f-90d2-259c4a81a5f9 swap swapdefaults 0 0
/etc/fstab檔案中每一行定義一個檔案系統每行有6個欄位:要掛載的裝置或偽檔案系統掛載點檔案系統類型掛載選項轉儲頻率自檢次序要掛載的裝置 裝置名稱/dev/sda5, UUID=, Label=, 偽檔案系統名掛載點: 目錄檔案系統類型: ext2/ext3/ext4, xfs...掛載選項: defaults,rw/ro, nouser,async/sync, auto/noauto...轉儲頻率: 0代表不做備份 1代表每天進行dump操作 2代表不定期進行dump操作自檢次序:fsck檢驗扇區 0表示不自檢 1表示最早自檢 2 優先順序比1低 樣本:
#編輯 /etc/fstab[root@localhost ~]# vim /etc/fstab /dev/sdb1 /mnt/test3 ext4defaults 0 0# 掛載 [root@localhost ~]# mount -a#查看[root@localhost ~]# dfFilesystem 1K-blocksUsed Available Use% Mounted on/dev/sda3 9947976 554736 8864856 6% /devtmpfs 4930160493016 0% /dev...../dev/sdb1 9993202564927944 1% /mnt/test3
/etc/mtab檔案: 記錄當前系統已經裝載的檔案系統,包括作業系統建立的虛擬檔案等;
[root@localhost ~]# cat /etc/mtabrootfs / rootfs rw 0 0proc /proc proc rw,nosuid,nodev,noexec,relatime 0 0sysfs /sys sysfs rw,seclabel,nosuid,nodev,noexec,relatime 0 0devtmpfs /dev devtmpfs rw,seclabel,nosuid,size=493016k,nr_inodes=123254,mode=755 0 0securityfs /sys/kernel/security securityfs rw,nosuid,nodev,noexec,relatime 0 0tmpfs /dev/shm tmpfs rw,seclabel,nosuid,nodev 0 0devpts /dev/pts devpts rw,seclabel,nosuid,noexec,relatime,gid=5,mode=620,ptmxmode=000 0 0tmpfs /run tmpfs rw,seclabel,nosuid,nodev,mode=755 0 0tmpfs /sys/fs/cgroup tmpfs rw,seclabel,nosuid,nodev,noexec,mode=755 0 0....
當 mount 掛載分區、umount 卸載分區,都會即時更新 mtab,mtab 總是保持著當前系統中已掛載的分區資訊,fdisk、df 這類程式,必須要讀取 mtab 檔案,才能獲得當前系統中的分區掛載情況。還可以通過讀取/proc/mount也可以來擷取當前掛載資訊。