UNIX 檔案系統與管理
首頁

檔案系統與管理
 

bullet

UNIX File System
bullet

File System是在UNIX作業系統中管理檔案、資料設或設備的一種方式。

bullet

UNIX中,檔案也可以是一個裝置(磁碟、光碟、Modem ...)。

bullet

不同的檔案系統間的差異在於儲存資料方法與格式的不同。

bullet

UNIX中所謂的檔案系統一般指的是一個具有獨立檔案表或inode Table的檔案管理單元,可以獨立運作(format),一個硬碟分割區可構成一個獨立的檔案系統。

bullet

Linux支援各式各樣不同的檔案系統,常見的例子:

bullet

檔案系統

形態名稱

用途

Second Extended File System

ext2

Linux上通用的檔案系統

Network File System

nfs

遠端主機檔案存取

UMSDOS File System

umsdos

用於將Linux安裝在MSDOS分割區上

DOS-FAT File System

msdos

MS-DOS檔案存取

VFAT File System

vfat

95/98檔案存取(中文及長檔名)

NT File System

ntfs

NT檔案存取

/proc File System

proc

為ps指令提供process資訊

ISO 9660 File System

iso9660

CDROM檔案存取

UFS File System

ufs

存取UFS檔案格式(Solaris, HP ...)

SMB File System

smbfs

For Samba (SMB)

 

bullet

檔案系統掛載(File System Mount)
bullet

File System Mount - The process that makes a disk's (partition) content available to the system, and merge it into the system directory tree.

bullet

A disk partition can be mounted anywhere in the system directory tree. After a disk partition is mounted, its top-level directory take the name of the directory on which it is mounted. For Example, when /dev/hdc2 was mounted on the mount point /home, then we can access the root of partition /dev/hdc2 from mount point /home.

bullet

A mount point is the place( directory) where the disk is connected to the directory tree. Usually, mount point is an existed empty directory in original directory tree.

bullet

If the mount point is not empty, any file in that directory is not accessible when the new file system is mounted.

bullet

A mounted disk partition is always called a "filesystem". For example, when /dev/hdc2 in mounted at /home, we called it "the /home1 filesystem".

bullet

A filesystem can only be mounted in one place in one time.

bullet

A filesystem is unmounted (manually ) when it's not used anymore.
Unmount a filesystem will do a disk synchronization before it really unmount this filesystem. To ensure a disk write back from RAM, you can also do disk sync manually. The command is "sync".  Do this always when you are going to shutdown the system ! 
Another reason for doing an unmounting is to promote system performance !(If you really do not need this file system anymore !)

bullet

UNIX Read-Ahead & Write-Behind mechnism makes the disk sync important for data integrity !

bullet

取出floopyf前切記先行unmount floopy, 否則先前寫入之資料極可能被遺失。

bullet

Format of Command "mount":
mount [-t filetype] [-o options]  device  mount-point
若未指定檔案格式,Linux將會嘗試猜測用何種檔案系統格式掛載該裝置(It  works for ext、ext2、minix and iso9660 etc.)

bullet

Eamples
bullet

Mount the floopy disk to /floopy for read-only
# mkdir /floopy
create the mount point, or make sure the mount point is there
# mount -t msdos -r /dev/fd0 /floopy
mount the floopy as read-only

bullet

Mount another partition (/dev/hdc5) in Disk /dev/hdc to /home1 for read/write
#mkdir /home1
#mount -t ext2 /dev/hdc5 /home1

bullet

Unmount the floopy disk
#unmount /floopy
or
#umount /dev/fd0
Notes: 卸載檔案系統的指令是umount, NOT unmount

bullet

/etc/fstab
bullet

/etc/fstab記錄系統啟動時應自動掛載(Mount)的檔案系統

bullet

/etc/mtab 動態記錄目前系統已掛載的檔案系統("df" will read this file)

bullet

/etc/fstab format
Device    Mount-point    File-type    Options    Dump    Check Order

Device  Device file to mount
Mount-Point Mount Point (Directory Noame)
File-type File Type of this file system
Options  mount -o 之後之選項,常用選項及意義如下
auto This File System can be mount by "-a" option. 
Defaults 相當於options rw+suid+dev+exec+auto+nouser+async
exec Permit execution of binaries
noauto Can only be mounted explicitly
noexec Do not allow execution of any binaries on the mounted file system
nosuid Do not allow set-user-identifier or set-group-identifier to take effect
nouser Forbid an ordianry user to mount this this file system
ro Mount this file system as read only
rw Mount this file system as read/write
suiduser 與nosuid、nouser選項意義相反
owner 與user選項類似,不同點為使用者必須是Device File的Owner,方可將此一File System掛上
Dump  used for these filesystemsby the dump(8) command to determine which filesystems needto be dumped.
Check Order is used by the fsck(8) program to determine the order in which filesystem checks are done at reboot time. The root filesystem should be specified with a fs_passno of 1, and other filesystems shouldhave a fs_passno of 2.


各欄位意義、File Type、選項種類參考mount man page ( By "
man mount")

An Example:

/dev/hda1 / ext2 defaults 1 1
/dev/hda2 /data ext2 defaults 1 2
/dev/hda5 /home ext2 defaults 1 2
/dev/cdrom /mnt/cdrom iso9660 noauto,owner,ro 0 0
/dev/hda6 /nmc ext2 defaults 1 2
/dev/hda3 swap swap defaults 0 0
/dev/fd0 /mnt/floppy ext2 noauto,owner 0 0
none /proc proc defaults 0 0
none /dev/pts devpts gid=5,mode=620 0 0

 

bullet

"mount -a"  will check /etc/fstab, and mount every filesystem (if not mounted)

bullet

記錄在/etc/fstab的檔案系統可以直接以mount point掛載
以上面/etc/fstab內容為例:
mount  /cdrom  or mount /dev/cdrom 都相當於 mount /dev/cdrom   /cdrom

bullet

由於Linux的root file system 掛載在 / 下,因此 / 相對的device位置必須被編寫進核心image file中(通常是/boot/vmlinuz)。

假設學員A的Linux裝在/dev/hdc1,學員B的Linux裝在/dev/hdc5,你會發現A做的開機片無法開啟B的Linux,其原因是編寫入A開機片的"/"位置(is in  /dev/hdc1)不同於B的"/"(is in /dev/hdc5),檢視或改變kernel image file的"/"位置資訊,你可以用指令"rdev":

檢視開機片中kernel image file "/"位置:
rdev  /dev/fd0

將開機片中kernel image file "/"位置改變至新位置/dev/hdc5:
rdev  /dev/hdc5   /dev/fd0
上面這個步驟之後,你會發現學員A的開機片可以順利的啟動學員B的Linux了。

bullet/proc
bullet

/proc並不是一個真正的檔案系統,不佔據任何硬碟空間,但是你可以"好像"使用真正的檔案系統一般,瀏覽該目錄下的檔案內容。

bullet

/proc記錄UNIX系統程序(process)執行狀況,真正內容並非存在於硬碟,而是由核心根據系統及process狀況,透過這個虛擬的檔案系統,回應相關的檔案及目錄的存取。

bullet

系統相關的運作資訊幾乎都可以透過/proc系的"檔案"得到答案。
例如/proc/net下記錄的是系統目前網路運作的狀況,你會發現/proc/net/arp記錄的內容和指令"arp -a"所得到的結果是一樣的,這是系統的arp table cache (IP address與MAC address的對應)。又如,你可以由/proc/1234目錄下,得到pid 為1234的rpocess相關訊息。/proc/meminfo記錄的是記憶體的相關資訊(same as result from command "free")。而/proc/ioports及/proc/interrupts則分別提供你目前系統io address及interrupt使用的狀況。

bullet

/proc的好處是提供一個簡易且統一的系統資訊擷取管道,管理者及程式設計者都可由此獲得必要且重要的系統即時資訊。

bullet

remove file or directory under /proc is nor permitted.

bulletDevice File
bullet

Device File代表的是一個系統裝置的對應檔案,透過kernel,使用者可以使用系統的裝置。

bullet

Device File提供一個使用系統資源的便利方式,程式設計師不須了解裝置內部狀況及驅動程式細節,即可直接對各項設備進行存取。

bullet

就本質而言,裝置檔並非一個真正檔案,而是一個指向某個裝置的檔案。("touch file_name可以產生一個一般檔案,device file則無法以這種方式產生。)

bullet

Device File擺放的位置在目錄/dev下,每一個裝置都應有一個device對應,而真正決定裝置檔與裝置關係的為device file 的major number與minor number兩個參數,並非檔案名稱
[woody@nmc /dev]$ ls -l /dev/hdc*
brw-rw---- 1 root disk 22, 0 May 6 1998 /dev/hdc
brw-rw---- 1 root disk 22, 1 May 6 1998 /dev/hdc1
brw-rw---- 1 root disk 22, 2 May 6 1998 /dev/hdc2
brw-rw---- 1 root disk 22, 3 May 6 1998 /dev/hdc3
brw-rw---- 1 root disk 22, 4 May 6 1998 /dev/hdc4
brw-rw---- 1 root disk 22, 5 May 6 1998 /dev/hdc5
brw-rw---- 1 root disk 22, 6 May 6 1998 /dev/hdc6
brw-rw---- 1 root disk 22, 7 May 6 1998 /dev/hdc7
brw-rw---- 1 root disk 22, 8 May 6 1998 /dev/hdc8
brw-rw---- 1 root disk 22, 9 May 6 1998 /dev/hdc9
....
/dev/hdc代表IDE2的第一顆硬碟(master),上例中,device file /dev/hdc的major number為22,minor number為 0,/dev/hdc1代表/dev/hdc的第一個primary 分割區,major number為22,minor number為1。

bullet

/dev/hdc1對應到IDE2的第一顆硬碟(master)的primary partition 1原因在於其(major,minor)=(22,1),與/dev/hdc1的檔案名稱無關。你可以用下面這個指令產生一個指向同一位置及裝置的device file,至於檔名叫什麼,甚或是否在目錄/dev下,就不是那麼重要了!

mknod  /my_dev b 22 1

b代表這是個形態為block的device file (Hard disk 唯一block device)

bullet

Device File的驅動程式屬於kernel的一部份,Major Number代表對應到核心的特定系統,minor number則是指定由特定驅動程式管理的特定程式。

bullet

Device File代表系統裝置,應特別注意檔案權限的設定(通常是644)。