檔案系統管理(一)

要新增一個新的檔案系統 (filesystems) 至檔案系統樹 (filesystem tree),有以下六個步驟:

確認裝置 (Identify Device):
首先要先確認要增加的裝置是那一個,如 /dev/hda 或 /dev/sda

分割裝置 (Partition Device):
可使用 fdisk 或 parted 來分割磁碟裝置,將其分割成預想的區域

製作檔案系統 (Make Filesystem):
利用 mkfs -t ext3 或 mke2fs -j 來格式化為 ext3 檔案系統

標籤化檔案系統 (Label filesystem):
將檔案系統標籤化,以有意義、獨一無二名稱的方式讓自己更好辨認,也更好管理,不然當分割區多的時候,常會分不清 /dev/hda6、/dev/hda7、和 /dev/hda8 裡面是什麼,要掛載到那個分割區

修改磁碟設定表 (Create Entry in /etc/fstab):
將該檔案系統與其掛載點 (mount point) 及其掛載方式的對應新增到 /etc/fstab 中,讓 /etc/rc.d/rc.sysinit 啟動時能順利掛載新的檔案系統

掛載新檔案系統 (Mount the New Filesystem):
在重新開機前,先執行 mount -a,讓 /etc/fstab 中所有檔案系統掛載起來,並檢視有沒有錯誤,以免設定有誤時無法開機

==============================

基本概念

系統開機後會依 BIOS 指定的順序去各磁碟 (如軟碟機、光碟機、或硬碟機) 可供執行的機碼,也就是 boot loader,它是放在磁碟的第一個磁柱的第一個磁區內,也就是所謂的主要開機記錄 (Master Boot Record, MBR)。在 Linux 中,boot loader 採用的是 GNU GRUB (GRand Unified Boot loader)。

在 Linux 中,主要開機記錄 (MBR) 除了包含 boot loader 的執行程式外,同時包含了整個硬碟分割區的資訊,它包含了分割區的開始磁柱及磁柱數量 (磁柱數量 x 512 bytes = 分割區大小),另外會記錄此分割區檔案類型的代碼,常見的代碼有:
5 (或 f):Extended
82:Linux Swap
83:Linux
8e:Linux LVM
fd:Linux RAID auto

# fdisk /dev/sda
Command (m for help): l
0  Empty           1e  Hidden W95 FAT1 80  Old Minix       be  Solaris boot
1  FAT12           24  NEC DOS         81  Minix / old Lin bf  Solaris
2  XENIX root      39  Plan 9          82  Linux swap / So c1  DRDOS/sec (FAT-
3  XENIX usr       3c  PartitionMagic  83  Linux c4  DRDOS/sec (FAT-
4  FAT16 <32M      40  Venix 80286     84  OS/2 hidden C:  c6  DRDOS/sec (FAT-
5  Extended 41  PPC PReP Boot   85  Linux extended c7  Syrinx
6  FAT16           42  SFS             86  NTFS volume set da  Non-FS data
7  HPFS/NTFS       4d  QNX4.x          87  NTFS volume set db  CP/M / CTOS / .
8  AIX             4e  QNX4.x 2nd part 88  Linux plaintext de  Dell Utility
9  AIX bootable    4f  QNX4.x 3rd part 8e  Linux LVM df  BootIt
a  OS/2 Boot Manag 50  OnTrack DM      93  Amoeba          e1  DOS access
b  W95 FAT32       51  OnTrack DM6 Aux 94  Amoeba BBT      e3  DOS R/O
c  W95 FAT32 (LBA) 52  CP/M            9f  BSD/OS          e4  SpeedStor
e  W95 FAT16 (LBA) 53  OnTrack DM6 Aux a0  IBM Thinkpad hi eb  BeOS fs
f  W95 Ext’d (LBA) 54  OnTrackDM6      a5  FreeBSD         ee  EFI GPT
10  OPUS            55  EZ-Drive        a6  OpenBSD         ef  EFI (FAT-12/16/
11  Hidden FAT12    56  Golden Bow      a7  NeXTSTEP        f0  Linux/PA-RISC b
12  Compaq diagnost 5c  Priam Edisk     a8  Darwin UFS      f1  SpeedStor
14  Hidden FAT16 <3 61  SpeedStor       a9  NetBSD          f4  SpeedStor
16  Hidden FAT16    63  GNU HURD or Sys ab  Darwin boot     f2  DOS secondary
17  Hidden HPFS/NTF 64  Novell Netware  b7  BSDI fs         fd  Linux raid auto
18  AST SmartSleep  65  Novell Netware  b8  BSDI swap       fe  LANstep
1b  Hidden W95 FAT3 70  DiskSecure Mult bb  Boot Wizard hid ff  BBT
1c  Hidden W95 FAT3 75  PC/IX

如果此分割區的代碼是 0x5 或 0xf,那它代表的就是伸延磁碟區 (extended partition),它的第一個磁區 (sector) 會被拿來記錄其他分割區的資訊,這些分割區就是邏輯磁碟區 (logical partition),在 Linux 中,IDE 硬碟可分割最多 63 個分割區,SCSI 硬碟可分割最多 15 個分割區。

使用分割區的好處是:管理方便、磁碟空間獨立使用不受干擾、減少硬碟磁頭移動及搜尋時間、備份及復原方便。

==============================

管理磁碟分割

要製作磁碟分割,我們可以使用 fdisk、sfdisk、parted,而當磁碟分割完成後,要將它讀入記憶體 (可查閱 cat /proc/partitions 之內容),可使用 partprobe 指令。

fdisk

# fdisk -l (先看一下現有的磁碟分割狀況)
Disk /dev/sda: 21.4 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          13      104391   83  Linux
/dev/sda2              14        2610    20860402+  8e  Linux LVM

Disk /dev/sdb: 21.4 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot      Start         End      Blocks   Id  System (/dev/sdb 有 21.4 GB,可是沒有任何分割區)

# fdisk /dev/sdb (開始分割 /dev/sdb)
The number of cylinders for this disk is set to 2610.
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 (指令 n 是新增一個分割區,可用 m 來查詢所有的指令)
Command action
e   extended
p   primary partition (1-4)

p (選擇分割區為主要分割區或延伸分割區)
Partition number (1-4): 1 (給予編號)
First cylinder (1-2610, default 1):
Using default value 1
Last cylinder or +size or +sizeM or +sizeK (1-2610, default 2610):
+500M (設定大小)
Command (m for help): w (寫入磁碟分割表)
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
(磁碟分割完成)

# fdisk -l (查看一下分割狀況)
Disk /dev/sda: 21.4 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          13      104391   83  Linux
/dev/sda2              14        2610    20860402+  8e  Linux LVM
Disk /dev/sdb: 21.4 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1          62      497983+  83  Linux
(果然多了一個分割區)
parted

使用 GNU parted 來分割磁碟機:
# parted /dev/sdb print
Disk /dev/sdb: 21.5GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Number  Start   End    Size   Type      File system  旗標
1      32.3kB  510MB  510MB  主分割區
(剛才用 fdisk 分割的磁碟)
資訊: 如果必要,不要忘記更新 /etc/fstab。
# parted /dev/sdb mkpart primary 510 1050 (說明:用 parted 分割 /dev/sdb 使用 mkpart 指令分割磁碟,分割成主要分割區,從 510MB 處分割到 1050MB 處)
# parted /dev/sdb print
Disk /dev/sdb: 21.5GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Number  Start   End     Size   Type      File system  旗標
1      32.3kB  510MB   510MB  主分割區
2      510MB   1050MB  540MB  主分割區
(新增的磁碟區)
資訊: 如果必要,不要忘記更新 /etc/fstab。

==============================

製作檔案系統

磁碟分割完成後,可以用 mke2fs 來製作檔案系統:
# mke2fs -j /dev/sda5
mke2fs 1.39 (29-May-2006)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
1254176 inodes, 2504124 blocks
125206 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=2566914048
77 block groups
32768 blocks per group, 32768 fragments per group
16288 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 30 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.

分割區除了裝置路徑外,也可以指定標籤,用來掛載至檔案系統樹:
檢查標籤:
# e2label /dev/sda1
/boot
指定標籤:
# e2label  /dev/sda6  MyLabel (指定完成:指定 /dev/sda6 的標籤是 MyLabel)
# e2label /dev/sda6 (檢查一下)
MyLabel

掛載起來:
# mount  LABEL=MyLabel  /mnt (以標籤的方式掛載分割區,標籤名稱是 MyLabel,大小寫有差,掛載點是 /mnt)
# df -h (檢查一下)
檔案系統              容量  已用 可用 已用% 掛載點
/dev/sda1              97G   43G   50G  47% /
tmpfs                 887M     0  887M   0% /dev/shm
/dev/sda6             9.2G  150M  8.6G   2% /mnt