每塊硬盤的 MBR 最多只能存儲 4 個分區信息,也就是說每塊物理硬盤我們最多分成4個主分區(P + P + P + P),或者3個主分區外加一個擴展分區(P + P + P + E)。如果我們希望劃分更多的分區,可以將擴展分區劃分更多更小的邏輯分區。
Linux 將編號 1 ~ 4 固定分配給 4 個主要分區。也就是說即便我們只有 1 個主分區和 1 個擴展分區,那第一個邏輯分區也只能是 hda5 (或 sda5)。
談論 Linux 分區時和 Windows 有很大的不同,因為這些分區通常以某個目錄方式存在 (或者說掛載點)。
/: 默認掛載點,未指定的目錄都將存放在分區中。
/home: 用戶主目錄,相當于 Windows 的 Documents and Settings,存放各用戶的相關數據文件,適合單獨分區。
/usr: 各種執行程序安裝目錄,相當于 Windows 的 Program files 和 Winnt 目錄。建議單獨分區。
/var: 通常用于存放各種臨時文件和緩存文件,容易產生磁盤碎片,建議單獨分區。
當然,別忘了充當虛擬內存 swap 分區。
以下是我個人習慣的分區方式。
/: 1GB ~ 2GB。
/usr: 1GB ~ 2GB,依據需要安裝的服務和軟件而定。
swap: 通常是物理內存的 2 倍,但似乎沒必要超過 1GB。
/home: 所有剩余空間
/var: 1GB 左右
我們用 Fdisk 對一個 8GB 的硬盤做一個分區演示。
yuhen@yuhen-desktop:~$ sudo fdisk /dev/sdb
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0x416ba19a.
Changes will remain in memory only, until you decide to write them.
After that, of course, the previous content won't be recoverable.
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)
Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)
Command (m for help): p # 查看分區信息
Disk /dev/sdb: 8589 MB, 8589934592 bytes
255 heads, 63 sectors/track, 1044 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Disk identifier: 0x416ba19a
Device Boot Start End Blocks Id System
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): +1024M # 分區大小 1GB。
Command (m for help): n # 新建主分區 /usr
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 2
First cylinder (133-1044, default 133):
Using default value 133
Last cylinder, +cylinders or +size{K,M,G} (133-1044, default 1044): +1024M
Command (m for help): n # 新建主分區 swap
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 3
First cylinder (265-1044, default 265):
Using default value 265
Last cylinder, +cylinders or +size{K,M,G} (265-1044, default 1044): +512M
Command (m for help): n # 新建擴展分區
Command action
e extended
p primary partition (1-4)
e
Selected partition 4
First cylinder (331-1044, default 331):
Using default value 331
Last cylinder, +cylinders or +size{K,M,G} (331-1044, default 1044): # 全部剩余空間
Using default value 1044
Command (m for help): p # 查看分區
Disk /dev/sdb: 8589 MB, 8589934592 bytes
255 heads, 63 sectors/track, 1044 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Disk identifier: 0x416ba19a
Device Boot Start End Blocks Id System
/dev/sdb1 1 132 1060258+ 83 Linux
/dev/sdb2 133 264 1060290 83 Linux
/dev/sdb3 265 330 530145 83 Linux
/dev/sdb4 331 1044 5735205 5 Extended
Command (m for help):
至此我們完成了 4 個主分區的劃分,接下來為創建邏輯分區。
Command (m for help): n # 創建 /home 邏輯分區
First cylinder (331-1044, default 331):
Using default value 331
Last cylinder, +cylinders or +size{K,M,G} (331-1044, default 1044): +4096M
Command (m for help): n # 創建 /var 邏輯分區
First cylinder (854-1044, default 854):
Using default value 854
Last cylinder, +cylinders or +size{K,M,G} (854-1044, default 1044):
Using default value 1044
Command (m for help): p # 顯示分區
Disk /dev/sdb: 8589 MB, 8589934592 bytes
255 heads, 63 sectors/track, 1044 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Disk identifier: 0xe1bd2899
Device Boot Start End Blocks Id System
/dev/sdb1 1 132 1060258+ 83 Linux
/dev/sdb2 133 264 1060290 83 Linux
/dev/sdb3 265 330 530145 83 Linux
/dev/sdb4 331 1044 5735205 5 Extended
/dev/sdb5 331 853 4200966 83 Linux
/dev/sdb6 854 1044 1534176 83 Linux
Command (m for help):
別忘了保存分區表,否則上面的工作就白做了。
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: 0x71f06c9b
Device Boot Start End Blocks Id System
/dev/sdb1 1 132 1060258+ 83 Linux
/dev/sdb2 133 264 1060290 83 Linux
/dev/sdb3 265 330 530145 83 Linux
/dev/sdb4 331 1044 5735205 5 Extended
/dev/sdb5 331 853 4200966 83 Linux
/dev/sdb6 854 1044 1534176 83 Linux
接下來就是格式化分區了。
yuhen@yuhen-desktop:~$ sudo mke2fs -j -L "root" /dev/sdb1
mke2fs 1.41.4 (27-Jan-2009)
Filesystem label=root
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
66384 inodes, 265064 blocks
13253 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=272629760
9 block groups
32768 blocks per group, 32768 fragments per group
7376 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376
Writing inode tables: done
Creating journal (8192 blocks): done
Writing superblocks and filesystem accounting information: done
This filesystem will be automatically checked every 27 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.
給分區設置一個 Label,便于以后管理。
有關分區掛載點,可查看后續章節。
--------------------- 分割線 ----------------------
上圖是我在 VMware 上安裝 Ubuntu Desktop 9.04 時的分區設置,由于 Desktop 默認集成了一些軟件,因此對 "/" 和 "/usr" 有最小尺寸限制。