Linux下如何格式化新磁碟



磁碟空間總是有不夠的一天。不論是使用虛擬的或是實體的機器。新增硬碟就成了一個必然的任務。以下就是我安裝過程的全記錄:




在硬碟新安裝完成後,應該在/dev目錄中可以找到新的disk。像是sda, sdb... 以下統稱 sdx。
若是硬碟是全新的,硬碟上應該沒有建立任何的分割表,因此就不會出現sdx1, sdx2... 之類的磁碟分割,而只能單獨看到sdx。

因此,首要任務就是使用fdisk來建立磁碟分割。本例為將磁碟上所有的空間分割給一個磁區。

fdisk /dev/sdx

Command action
   a   toggle a bootable flag
   b   edit bsd disklabel
   c   toggle the dos compatibility flag
   d   delete a partition
   l   list known partition types
   m   print this menu
   n   add a new partition
   o   create a new empty DOS partition table
   p   print the partition table
   q   quit without saving changes
   s   create a new empty Sun disklabel
   t   change a partition's system id
   u   change display/entry units
   v   verify the partition table
   w   write table to disk and exit
   x   extra functionality (experts only)

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-13054, default 1):
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-13054, default 13054):
Using default value 13054


分割完成,去ls /dev/sdb* 觀察一下變化

warren@ubuntu:/$ ls /dev/sdb*
sdb   sdb1

磁區分割完成,接下來就是進行格式化的動作,這樣才可以真正的對磁碟進行讀寫。下面範例為將磁區格式化成ext4格式。

warren@ubuntu:/$ sudo mkfs -t ext4 /dev/sdb1
mke2fs 1.41.11 (14-Mar-2010)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
6553600 inodes, 26214055 blocks
1310702 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=4294967296
800 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, 2654208,
        4096000, 7962624, 11239424, 20480000, 23887872

Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information:
done

This filesystem will be automatically checked every 33 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.

接著要將格式化好的邏輯磁碟掛載到系統才能使用。我們使用mount指進來進行掛載。另外掛點必需為一個預先建好的空目錄。在本範例中已先在系統建立一個 /work 目錄。在mount前的sudo指令是為了取得系統管理者的權限來進行掛載。


warren@ubuntu:/$ sudo mount -t ext4 /dev/sdb1 /work

接著利用df指令來觀察檔案系統,注意最後一行就是我們掛載成功的證明:

warren@ubuntu:/$ df
Filesystem           1K-blocks      Used Available Use% Mounted on
/dev/sda1             19737268   4910064  13824608  27% /
none                   2023824       288   2023536   1% /dev
none                   2028372      1136   2027236   1% /dev/shm
none                   2028372       348   2028024   1% /var/run
none                   2028372         0   2028372   0% /var/lock
none                   2028372         0   2028372   0% /lib/init/rw
.host:/              357794812  85513132 272281680  24% /mnt/hgfs
/dev/sdb1            103210940    192116  97776016   1% /work

如果我們要每次開機時就能自動掛載該磁區的話,就需要對fstab來進行修改。
在Linux ubuntu 2.6.32上,該檔案位於 /etc/fstab。

在檔案尾新增


/dev/sdb1        /work  ext4 defaults 1       2

存檔。重開機即可發現系統已自動對該磁區進行掛載了。大功告成!

CC License picture from pobre.ch

留言

熱門文章