page counter next up previous contents
Next: 邏輯捲軸管理員 Up: 檔案系統的掛載 Previous: 磁碟掛載與卸載   Contents

新增硬碟或分割槽


		fdisk 		 對硬碟進行分割 (partition)﹔

mkfs 產生檔案系統 (format)﹔
mke2fs 產生 ext2/ext3 檔案系統 (format)﹔
e2label 更改磁碟的表頭資料﹔
fsck, badblocks 對檔案系統進行檢驗(可不做)﹔
mkdir 在 Linux 系統上建立掛載點,即建立要掛載的目錄﹔
mount 進行掛載。
  1. fdisk:對硬碟進行分割。
    [root@dywOffice ~]# fdisk [option] [裝置名稱]  
    option  
     -l        : 直接列出硬碟裝置的 partition table 
     -c        : 開閉DOS相容模式
     -h        : print help
     -u <size> : 給予sectors大小取代cylinders
    
    範例:
    範例一:列出硬碟的 partition table 
    [root@dywOffice ~]# fdisk -l
    
    Disk /dev/hda: 80.0 GB, 80032038912 bytes
    255 heads, 63 sectors/track, 9730 cylinders
    Units = cylinders of 16065 * 512 = 8225280 bytes
    
       Device Boot      Start         End      Blocks   Id  System
    /dev/hda1   *           1         254     2040223+  83  Linux
    /dev/hda2             255        9730    76115970    5  Extended
    /dev/hda5             255         457     1630566   82  Linux swap / Solaris
    /dev/hda6             458         584     1020096   83  Linux
    /dev/hda7             585        1603     8185086   83  Linux
    /dev/hda8            1604        2240     5116671   83  Linux
    /dev/hda9            2241        5427    25599546   83  Linux
    /dev/hda10           5428        9730    34563816   83  Linux
    
    範例二:分割 hda 磁碟
    [root@dywOffice ~]# fdisk /dev/hda <== 不可加編號
    
    The number of cylinders for this disk is set to 9730.
    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): m
    Command action
       a   toggle a bootable flag
       b   edit bsd disklabel
       c   toggle the dos compatibility flag
       d   delete a partition <== 刪除 partition
       l   list known partition types <== 列出已知的檔案系統型態
       m   print this menu <== 顯示此手冊 
       n   add a new partition <== 加入新的 partition
       o   create a new empty DOS partition table
       p   print the partition table <== 列出目前的磁碟分割狀態 
       q   quit without saving changes <== 不儲存離開 fdisk 
       s   create a new empty Sun disklabel
       t   change a partition's system id <== 改變檔案系統 id 
       u   change display/entry units
       v   verify the partition table <== 確認 partition table
       w   write table to disk and exit <== 寫入 partition table 並退出 (危險)
       x   extra functionality (experts only)
    
    ### fdisk 注意事項:
    # fdisk 最主要的工作為修改 partition tables。
    # 硬碟分割錯誤時,只要在 format 前將 partition tables 復原就可將資料救回。
    # fdisk 是針對整顆硬碟,所以其後接裝置名稱而非某個 partition (例如 hda 而非 hda1)。
    # fdisk 後悔時直接按下 q 取消剛剛的動作。
    # 一個硬碟最大的邏輯磁區可以達到 63 號(包含 1 至 4 的 primary partition)。
    

  2. mkfs:產生檔案系統。
    [root @test /root ]# mkfs [-V] [-t fstype] [fs-options] filesys [blocks]
    mkfs 是各種格式化指令(mkfs.fstype,例如:mkfs.ext2)的前端指令。
    參數 :
    -V : 詳細顯示模式。
    -t : 檔案系統的型式,預設為 ext2。 
    -c : 製作檔案系統前,檢查該 partition 是否有壞軌。
    -l bad_blocks_file : 將壞軌的 block 資料加到檔案 bad_blocks_file。
    blocks : 設定 block 的大小。 
    
    範例: 
    範例一:在 /dev/hda5 上建一個 msdos 的檔案系統,同時檢查是否有壞軌存在,並且將過程詳細列出來:
    [root @test /root]# mkfs -V -t msdos -c /dev/hda5  
    
    範例二:在 /dev/hda7 上建一個 ext3 的檔案系統,同時指定 blocks 為 1024 bytes,並且將過程詳細列出來:
    [root @test /root]# mkfs -V -t ext3 /dev/hda7 1024
    
  3. mke2fs:產生 ext2/etx3 檔案系統。
    [root @test /root ]# mke2fs [-b block-size][-c][-i bytes-per-inode][-j][-L volume-label]
    參數: 
    -b :設定 block 的大小,目前支援 1k, 2k, 4k bytes。 
    -c :檢查錯誤磁碟 
    -i :設定 inode 值 
    -j :預設為 ext2 檔案格式,若加入 -j 選項時格式化為 ext3。
    -L :設定磁區的 label (表頭名稱) 
    
    範例: 
    範例一:以 mke2fs 預設的條件( ext2 )格式化 /dev/hda5,且名稱為 csie。
    [root @test /root]# mke2fs -L "csie" /dev/hda5  
    
    範例二:以 mke2fs 預設的條件( ext2 )格式化 /dev/hda5,且過程中檢查硬碟。
    [root @test /root]# mke2fs -c /dev/hda5 
    
    範例三:以 mke2fs 將 /dev/hda5 格式化為 ext3,且 block 為 2048 bytes, inode 為 256 bytes。
    [root @test /root]# mke2fs –j -b 2048 -i 256 /dev/hda5
    
  4. e2label:更改磁碟的表頭資料。
    [root @test /root ]# e2label [/dev/hd...] [label_name]  
    
    範例:將檔案系統 /dev/hda1 表頭設定為 CsieHome  
    [root @test /root]# e2label /dev/hda1 CsieHome
    

  5. fsck:磁碟檢驗。
    [root@linux ~]# fsck [-AtCary] 裝置名稱
    參數:
    -t  :指定檢查 filesystem 的 type。
    -A  :依據 /etc/fstab 的內容,將所有的裝置都掃瞄一次
    -a  :自動修復檢查到的有問題的磁區,不用一直按 y 
    -r  :要讓使用者決定是否需要修復,與 -a 剛好相反
    -y  :與 -a 類似,但某些 filesystem 僅支援 -y 這個參數
    -C  :在檢驗過程中,使用長條圖顯示進度
    
    範例:
    範例一:顯示個別的 filesystem 的檢驗程式
    [root@dywOffice ~]# ll /sbin/fsck*
    -rwxr-xr-x 1 root root  18616 Aug 22  2006 /sbin/fsck*
    -rwxr-xr-x 1 root root  11320 Jun  2 04:48 /sbin/fsck.cramfs*
    -rwxr-xr-x 1 root root 147908 Aug 22  2006 /sbin/fsck.ext2*
    -rwxr-xr-x 1 root root 147908 Aug 22  2006 /sbin/fsck.ext3*
    -rwxr-xr-x 1 root root  23364 Jun  2 04:48 /sbin/fsck.minix*
    
    範例二: 檢驗裝置 /dev/hdb5
    [root@linux ~]# fsck -C -t ext3 /dev/hdb5 
    fsck 1.37 (21-Mar-2005)
    e2fsck 1.37 (21-Mar-2005)
    logical: clean, 11/181056 files, 21706/361446 blocks
    
    # 每個掛載點都會有一個預設目錄 lost+found。
    # fsck 若發現任何錯誤的檔案,就會將該檔案的資料存到目錄 lost+found。
    # 通常只有 root 且系統有問題的時候才使用 fsck,在正常狀況下使用,可能會造成對檔案的危害。
    # 執行 fsck 時被檢查的 partiton 必須在卸載狀態。
    
  6. badblocks:尋找磁碟壞的區塊。
    [root@linux ~]# badblocks -[sv] 裝置名稱
    參數:
    -s  :在螢幕上列出進度
    -v  :可以在螢幕上看到進度
    
    範例:
    [root@dywOffice ~]# badblocks -sv /dev/hda6
    Checking blocks 0 to 1020096
    Checking for bad blocks (read-only test): done
    Pass completed, 0 bad blocks found.
    
  7. 例題:增加一個以 10MiB 為大小的交換分區( swap )。此分區於系統啟動時應一併自動掛載。
    [root@kvm8 ~]# fdisk -uc /dev/vda
    
    Command (m for help): p
    
    Disk /dev/vda: 1503 MB, 1503238144 bytes
    16 heads, 63 sectors/track, 2912 cylinders, total 2936012 sectors
    Units = sectors of 1 * 512 = 512 bytes
    Sector size (logical/physical): 512 bytes / 512 bytes
    I/O size (minimum/optimal): 512 bytes / 512 bytes
    Disk identifier: 0x0005e0d8
    
       Device Boot      Start         End      Blocks   Id  System
    /dev/vda1   *        2048      198655       98304   83  Linux
    /dev/vda2          198656     2820095     1310720   8e  Linux LVM
    
    Command (m for help): n
    Command action
       e   extended
       p   primary partition (1-4)
    e
    Partition number (1-4): 4
    First sector (2820096-2936011, default 2820096): 
    Using default value 2820096
    Last sector, +sectors or +size{K,M,G} (2820096-2936011, default 2936011): 
    Using default value 2936011
    
    Command (m for help): n
    Command action
       l   logical (5 or over)
       p   primary partition (1-4)
    l
    First sector (2822144-2936011, default 2822144): 
    Using default value 2822144
    Last sector, +sectors or +size{K,M,G} (2822144-2936011, default 2936011): +20M
    
    Command (m for help): p
    
    Disk /dev/vda: 1503 MB, 1503238144 bytes
    16 heads, 63 sectors/track, 2912 cylinders, total 2936012 sectors
    Units = sectors of 1 * 512 = 512 bytes
    Sector size (logical/physical): 512 bytes / 512 bytes
    I/O size (minimum/optimal): 512 bytes / 512 bytes
    Disk identifier: 0x0005e0d8
    
       Device Boot      Start         End      Blocks   Id  System
    /dev/vda1   *        2048      198655       98304   83  Linux
    /dev/vda2          198656     2820095     1310720   8e  Linux LVM
    /dev/vda4         2820096     2936011       57958    5  Extended
    /dev/vda5         2822144     2863103       20480   83  Linux
    
    Command (m for help): t
    Partition number (1-5): 5
    Hex code (type L to list codes): 82
    Changed system type of partition 5 to 82 (Linux swap / Solaris)
    
    Command (m for help): p       
    
    Disk /dev/vda: 1503 MB, 1503238144 bytes
    16 heads, 63 sectors/track, 2912 cylinders, total 2936012 sectors
    Units = sectors of 1 * 512 = 512 bytes
    Sector size (logical/physical): 512 bytes / 512 bytes
    I/O size (minimum/optimal): 512 bytes / 512 bytes
    Disk identifier: 0x0005e0d8
    
       Device Boot      Start         End      Blocks   Id  System
    /dev/vda1   *        2048      198655       98304   83  Linux
    /dev/vda2          198656     2820095     1310720   8e  Linux LVM
    /dev/vda4         2820096     2936011       57958    5  Extended
    /dev/vda5         2822144     2863103       20480   82  Linux swap / Solaris
    
    Command (m for help): w
    The partition table has been altered!
    
    Calling ioctl() to re-read partition table.
    
    WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
    The kernel still uses the old table. The new table will be used at
    the next reboot or after you run partprobe(8) or kpartx(8)
    Syncing disks.
    
    [root@kvm8 ~]# partprobe 
    Warning: WARNING: the kernel failed to re-read the partition table on /dev/vda (Device or resource busy).  As a result, it may not reflect all of your changes until after reboot.
    [root@kvm8 ~]# reboot
    [root@kvm8 ~]# mkswap /dev/vda5
    Setting up swapspace version 1, size = 20476 KiB
    no label, UUID=0276d17b-c8b2-4df8-b2d1-ed185de40ee9
    [root@kvm8 ~]# vim /etc/fstab
    /dev/vda5       swap               swap    defaults        0 0
    [root@kvm8 ~]# swapon -a
    swapon: /dev/mapper/vg_kvmusb-swap: swapon failed: Device or resource busy
    
    [root@kvm8 ~]# swapon -s
    Filename				Type		Size	Used	Priority
    /dev/dm-0                               partition	49144	0	-1
    /dev/vda5                               partition	20472	0	-2
    


2015-04-13