Latest web development tutorials

Linux 磁盤管理

Linux磁盤管理好壞管理直接關係到整個系統的性能問題。

Linux磁盤管理常用三個命令為df、du和fdisk。

  • df:列出文件系統的整體磁盤使用量
  • du:檢查磁盤空間使用量
  • fdisk:用於磁盤分區

df

df命令參數功能:檢查文件系統的磁盤空間佔用情況。 可以利用該命令來獲取硬盤被佔用了多少空間,目前還剩下多少空間等信息。

語法:

df [-ahikHTm] [目录或文件名]

選項與參數:

  • -a :列出所有的文件系統,包括系統特有的/proc 等文件系統;
  • -k :以KBytes 的容量顯示各文件系統;
  • -m :以MBytes 的容量顯示各文件系統;
  • -h :以人們較易閱讀的GBytes, MBytes, KBytes 等格式自行顯示;
  • -H :以M=1000K 取代M=1024K 的進位方式;
  • -T :顯示文件系統類型, 連同該partition 的filesystem 名稱(例如ext3) 也列出;
  • -i :不用硬盤容量,而以inode 的數量來顯示

實例1

將系統內所有的文件系統列出來!

[root@www ~]# df
Filesystem      1K-blocks      Used Available Use% Mounted on
/dev/hdc2         9920624   3823112   5585444  41% /
/dev/hdc3         4956316    141376   4559108   4% /home
/dev/hdc1          101086     11126     84741  12% /boot
tmpfs              371332         0    371332   0% /dev/shm

在Linux 底下如果df 沒有加任何選項,那麼默認會將系統內所有的(不含特殊內存內的文件系統與swap) 都以1 Kbytes 的容量來列出來!

實例2

將容量結果以易讀的容量格式顯示出來

[root@www ~]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/hdc2             9.5G  3.7G  5.4G  41% /
/dev/hdc3             4.8G  139M  4.4G   4% /home
/dev/hdc1              99M   11M   83M  12% /boot
tmpfs                 363M     0  363M   0% /dev/shm

實例3

將系統內的所有特殊文件格式及名稱都列出來

[root@www ~]# df -aT
Filesystem    Type 1K-blocks    Used Available Use% Mounted on
/dev/hdc2     ext3   9920624 3823112   5585444  41% /
proc          proc         0       0         0   -  /proc
sysfs        sysfs         0       0         0   -  /sys
devpts      devpts         0       0         0   -  /dev/pts
/dev/hdc3     ext3   4956316  141376   4559108   4% /home
/dev/hdc1     ext3    101086   11126     84741  12% /boot
tmpfs        tmpfs    371332       0    371332   0% /dev/shm
none   binfmt_misc         0       0         0   -  /proc/sys/fs/binfmt_misc
sunrpc  rpc_pipefs         0       0         0   -  /var/lib/nfs/rpc_pipefs

實例4

將/etc 底下的可用的磁盤容量以易讀的容量格式顯示

[root@www ~]# df -h /etc
Filesystem            Size  Used Avail Use% Mounted on
/dev/hdc2             9.5G  3.7G  5.4G  41% /

du

inux du命令也是查看使用空間的,但是與df命令不同的是Linux du命令是對文件和目錄磁盤使用的空間的查看,還是和df命令有一些區別的,這裡介紹Linux du命令。

語法:

du [-ahskm] 文件或目录名称

選項與參數:

  • -a :列出所有的文件與目錄容量,因為默認僅統計目錄底下的文件量而已。
  • -h :以人們較易讀的容量格式(G/M) 顯示;
  • -s :列出總量而已,而不列出每個各別的目錄佔用容量;
  • -S :不包括子目錄下的總計,與-s 有點差別。
  • -k :以KBytes 列出容量顯示;
  • -m :以MBytes 列出容量顯示;

實例1

列出目前目錄下的所有文件容量

[root@www ~]# du
8       ./test4     <==每个目录都会列出来
8       ./test2
....中间省略....
12      ./.gconfd   <==包括隐藏文件的目录
220     .           <==这个目录(.)所占用的总量

直接輸入du 沒有加任何選項時,則du 會分析當前所在目錄的文件與目錄所佔用的硬盤空間。

實例2

將文件的容量也列出來

[root@www ~]# du -a
12      ./install.log.syslog   <==有文件的列表了
8       ./.bash_logout
8       ./test4
8       ./test2
....中间省略....
12      ./.gconfd
220     .

實例3

檢查根目錄底下每個目錄所佔用的容量

[root@www ~]# du -sm /*
7       /bin
6       /boot
.....中间省略....
0       /proc
.....中间省略....
1       /tmp
3859    /usr     <==系统初期最大就是他了啦!
77      /var

通配符* 來代表每個目錄。

與df 不一樣的是,du 這個命令其實會直接到文件系統內去搜尋所有的文件數據。


fdisk

fdisk 是Linux 的磁盤分區表操作工具。

語法:

fdisk [-l] 装置名称

選項與參數:

  • -l :輸出後面接的裝置所有的分區內容。 若僅有fdisk -l 時, 則係統將會把整個系統內能夠搜尋到的裝置的分區均列出來。

實例1

列出所有分區信息

[root@AY120919111755c246621 tmp]# fdisk -l

Disk /dev/xvda: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

    Device Boot      Start         End      Blocks   Id  System
/dev/xvda1   *           1        2550    20480000   83  Linux
/dev/xvda2            2550        2611      490496   82  Linux swap / Solaris

Disk /dev/xvdb: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x56f40944

    Device Boot      Start         End      Blocks   Id  System
/dev/xvdb2               1        2610    20964793+  83  Linux

實例2

找出你係統中的根目錄所在磁盤,並查閱該硬盤內的相關信息

[root@www ~]# df /            <==注意:重点在找出磁盘文件名而已
Filesystem           1K-blocks      Used Available Use% Mounted on
/dev/hdc2              9920624   3823168   5585388  41% /

[root@www ~]# fdisk /dev/hdc  <==仔细看,不要加上数字喔!
The number of cylinders for this disk is set to 5005.
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 (m for help): m   <== 输入 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
   u   change display/entry units
   v   verify the partition table
   w   write table to disk and exit  <==将刚刚的动作写入分割表
   x   extra functionality (experts only)

離開fdisk時按下q ,那麼所有的動作都不會生效! 相反的,按下w就是動作生效的意思。

Command (m for help): p  <== 这里可以输出目前磁盘的状态

Disk /dev/hdc: 41.1 GB, 41174138880 bytes        <==这个磁盘的文件名与容量
255 heads, 63 sectors/track, 5005 cylinders      <==磁头、扇区与磁柱大小
Units = cylinders of 16065 * 512 = 8225280 bytes <==每个磁柱的大小

   Device Boot      Start         End      Blocks   Id  System
/dev/hdc1   *           1          13      104391   83  Linux
/dev/hdc2              14        1288    10241437+  83  Linux
/dev/hdc3            1289        1925     5116702+  83  Linux
/dev/hdc4            1926        5005    24740100    5  Extended
/dev/hdc5            1926        2052     1020096   82  Linux swap / Solaris
# 装置文件名 启动区否 开始磁柱    结束磁柱  1K大小容量 磁盘分区槽内的系统

Command (m for help): q

想要不儲存離開嗎? 按下q 就對了! 不要隨便按w 啊!

使用p可以列出目前這顆磁盤的分割表信息,這個信息的上半部在顯示整體磁盤的狀態。


磁盤格式化

磁盤分割完畢後自然就是要進行文件系統的格式化,格式化的命令非常的簡單,使用mkfs (make filesystem)命令。

語法:

mkfs [-t 文件系统格式] 装置文件名

選項與參數:

  • -t :可以接文件系統格式,例如ext3, ext2, vfat 等(系統有支持才會生效)

實例1

查看mkfs 支持的文件格式

[root@www ~]# mkfs[tab][tab]
mkfs         mkfs.cramfs  mkfs.ext2    mkfs.ext3    mkfs.msdos   mkfs.vfat

按下兩個[tab],會發現mkfs 支持的文件格式如上所示。

實例2

將分區/dev/hdc6(可指定你自己的分區) 格式化為ext3 文件系統:

[root@www ~]# mkfs -t ext3 /dev/hdc6
mke2fs 1.39 (29-May-2006)
Filesystem label=                <==这里指的是分割槽的名称(label)
OS type: Linux
Block size=4096 (log=2)          <==block 的大小配置为 4K 
Fragment size=4096 (log=2)
251392 inodes, 502023 blocks     <==由此配置决定的inode/block数量
25101 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=515899392
16 block groups
32768 blocks per group, 32768 fragments per group
15712 inodes per group
Superblock backups stored on blocks:
        32768, 98304, 163840, 229376, 294912

Writing inode tables: done
Creating journal (8192 blocks): done <==有日志记录
Writing superblocks and filesystem accounting information: done

This filesystem will be automatically checked every 34 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.
# 这样就创建起来我们所需要的 Ext3 文件系统了!简单明了!

磁盤檢驗

fsck(file system check)用來檢查和維護不一致的文件系統。

若係統掉電或磁盤發生問題,可利用fsck命令對文件系統進行檢查。

語法:

fsck [-t 文件系统] [-ACay] 装置名称

選項與參數:

  • -t : 給定檔案系統的型式,若在/etc/fstab 中已有定義或kernel 本身已支援的則不需加上此參數
  • -s : 依序一個一個地執行fsck 的指令來檢查
  • -A : 對/etc/fstab 中所有列出來的分區(partition)做檢查
  • -C : 顯示完整的檢查進度
  • -d : 打印出e2fsck 的debug 結果
  • -p : 同時有-A 條件時,同時有多個fsck 的檢查一起執行
  • -R : 同時有-A 條件時,省略/ 不檢查
  • -V : 詳細顯示模式
  • -a : 如果檢查有錯則自動修復
  • -r : 如果檢查有錯則由使用者回答是否修復
  • -y : 選項指定檢測每個文件是自動輸入yes,在不確定那些是不正常的時候,可以執行# fsck -y 全部檢查修復。

實例1

查看系統有多少文件系統支持的fsck 命令:

[root@www ~]# fsck[tab][tab]
fsck         fsck.cramfs  fsck.ext2    fsck.ext3    fsck.msdos   fsck.vfat

實例2

強制檢測/dev/hdc6 分區:

[root@www ~]# fsck -C -f -t ext3 /dev/hdc6 
fsck 1.39 (29-May-2006)
e2fsck 1.39 (29-May-2006)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
vbird_logical: 11/251968 files (9.1% non-contiguous), 36926/1004046 blocks

如果沒有加上-f 的選項,則由於這個文件系統不曾出現問題,檢查的經過非常快速! 若加上-f 強制檢查,才會一項一項的顯示過程。


磁盤掛載與卸除

Linux的磁盤掛載使用mount命令,卸載使用umount命令。

磁盤掛載語法:

mount [-t 文件系统] [-L Label名] [-o 额外选项] [-n]  装置文件名  挂载点

實例1

用默認的方式,將剛剛創建的/dev/hdc6 掛載到/mnt/hdc6 上面!

[root@www ~]# mkdir /mnt/hdc6
[root@www ~]# mount /dev/hdc6 /mnt/hdc6
[root@www ~]# df
Filesystem           1K-blocks      Used Available Use% Mounted on
.....中间省略.....
/dev/hdc6              1976312     42072   1833836   3% /mnt/hdc6

磁盤卸載命令umount語法:

umount [-fn] 装置文件名或挂载点

選項與參數:

  • -f :強制卸除! 可用在類似網絡文件系統(NFS) 無法讀取到的情況下;
  • -n :不升級/etc/mtab 情況下卸除。

卸載/dev/hdc6

[root@www ~]# umount /dev/hdc6