Linux 磁盤管理
Linux磁盤管理好壞直接關(guān)系到整個(gè)系統(tǒng)的性能問題涝桅。
Linux磁盤管理常用三個(gè)命令為df茁裙、du和fdisk喻鳄。
- df:列出文件系統(tǒng)的整體磁盤使用量
- du:檢查磁盤空間使用量
- fdisk:用于磁盤分區(qū)
df
df命令參數(shù)功能:檢查文件系統(tǒng)的磁盤空間占用情況郊酒〔鹜穑可以利用該命令來獲取硬盤被占用了多少空間嗓奢,目前還剩下多少空間等信息。
語法:
df [-ahikHTm] [目錄或文件名]
選項(xiàng)與參數(shù):
- -a :列出所有的文件系統(tǒng)浑厚,包括系統(tǒng)特有的 /proc 等文件系統(tǒng)股耽;
- -k :以 KBytes 的容量顯示各文件系統(tǒng);
- -m :以 MBytes 的容量顯示各文件系統(tǒng)瞻颂;
- -h :以人們較易閱讀的 GBytes, MBytes, KBytes 等格式自行顯示豺谈;
- -H :以 M=1000K 取代 M=1024K 的進(jìn)位方式;
- -T :顯示文件系統(tǒng)類型, 連同該 partition 的 filesystem 名稱 (例如 ext3) 也列出贡这;
- -i :不用硬盤容量茬末,而以 inode 的數(shù)量來顯示
實(shí)例1
將系統(tǒng)內(nèi)所有的文件系統(tǒng)列出來
[root@localhost ~]# df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/mapper/VolGroup-lv_root 18134344 1349912 15863244 8% /
tmpfs 502204 0 502204 0% /dev/shm
/dev/sda1 495844 33466 436778 8% /boot
實(shí)例2
將容量結(jié)果以易讀的容量格式顯示出來
[root@localhost ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root 18G 1.3G 16G 8% /
tmpfs 491M 0 491M 0% /dev/shm
/dev/sda1 485M 33M 427M 8% /boot
實(shí)例3
將系統(tǒng)內(nèi)的所有特殊文件格式及名稱都列出來
[root@localhost ~]# df -aT
Filesystem Type 1K-blocks Used Available Use% Mounted on
/dev/mapper/VolGroup-lv_root ext4 18134344 1349912 15863244 8% /
proc proc 0 0 0 - /proc
sysfs sysfs 0 0 0 - /sys
devpts devpts 0 0 0 - /dev/pts
tmpfs tmpfs 502204 0 502204 0% /dev/shm
/dev/sda1 ext4 495844 33466 436778 8% /boot
none binfmt_misc 0 0 0 - /proc/sys/f
實(shí)例 4
將 /etc 底下的可用的磁盤容量以易讀的容量格式顯示
[root@localhost ~]# df -h /etc
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root 18G 1.3G 16G 8% /
du
Linux du命令也是查看使用空間的,但是與df命令不同的是Linux du命令是對文件和目錄磁盤使用的空間的查看盖矫,還是和df命令有一些區(qū)別的丽惭,這里介紹Linux du命令。
語法:
du [-ahskm] 文件或目錄名稱
選項(xiàng)與參數(shù):
- -a :列出所有的文件與目錄容量辈双,因?yàn)槟J(rèn)僅統(tǒng)計(jì)目錄底下的文件量而已责掏。
- -h :以人們較易讀的容量格式 (G/M) 顯示;
- -s :列出總量而已湃望,而不列出每個(gè)各別的目錄占用容量换衬;
- -S :不包括子目錄下的總計(jì)痰驱,與 -s 有點(diǎn)差別。
- -k :以 KBytes 列出容量顯示瞳浦;
- -m :以 MBytes 列出容量顯示担映;
實(shí)例1
列出目前目錄下的所有文件容量
[root@localhost ~]# du
8 ./test4 <==每個(gè)目錄都會(huì)列出來
8 ./test2
....中間省略....
12 ./.gconfd <==包括隱藏文件的目錄
220 . <==這個(gè)目錄(.)所占用的總量
直接輸入 du 沒有加任何選項(xiàng)時(shí),則 du 會(huì)分析當(dāng)前所在目錄的文件與目錄所占用的硬盤空間叫潦。
實(shí)例2
將文件的容量也列出來
[root@localhost ~]# du -a
12 ./install.log.syslog <==有文件的列表了
8 ./.bash_logout
8 ./test4
8 ./test2
....中間省略....
12 ./.gconfd
220 .
實(shí)例3
檢查根目錄底下每個(gè)目錄所占用的容量
[root@www ~]# du -sm /*
7 /bin
6 /boot
.....中間省略....
0 /proc
.....中間省略....
1 /tmp
3859 /usr <==系統(tǒng)初期最大就是他了啦蝇完!
77 /var
通配符 * 來代表每個(gè)目錄。
與 df 不一樣的是矗蕊,du 這個(gè)命令其實(shí)會(huì)直接到文件系統(tǒng)內(nèi)去搜尋所有的文件數(shù)據(jù)短蜕。
fdisk
fdisk 是 Linux 的磁盤分區(qū)表操作工具。
語法:
fdisk [-l] 裝置名稱
選項(xiàng)與參數(shù):
- -l :輸出后面接的裝置所有的分區(qū)內(nèi)容傻咖。若僅有 fdisk -l 時(shí)朋魔, 則系統(tǒng)將會(huì)把整個(gè)系統(tǒng)內(nèi)能夠搜尋到的裝置的分區(qū)均列出來。
實(shí)例 1
列出所有分區(qū)信息
[root@localhost /]# fdisk -l
Disk /dev/sda: 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: 0x000d4e4a
Device Boot Start End Blocks Id System
/dev/sda1 * 1 64 512000 83 Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2 64 2611 20458496 8e Linux LVM
Disk /dev/mapper/VolGroup-lv_root: 18.9 GB, 18865979392 bytes
255 heads, 63 sectors/track, 2293 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
Disk /dev/mapper/VolGroup-lv_swap: 2080 MB, 2080374784 bytes
255 heads, 63 sectors/track, 252 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
實(shí)例 2
找出你系統(tǒng)中的根目錄所在磁盤卿操,并查閱該硬盤內(nèi)的相關(guān)信息
[root@localhost /]# df / <==注意:重點(diǎn)在找出磁盤文件名而已
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/hdc2 9920624 3823168 5585388 41% /
[root@localhost /]# fdisk /dev/hdc <==仔細(xì)看铺厨,不要加上數(shù)字喔!
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 后,就會(huì)看到底下這些命令介紹
Command (m for help): m <== 輸入 m 后赃磨,就會(huì)看到底下這些命令介紹
Command action
a toggle a bootable flag
b edit bsd disklabel
c toggle the dos compatibility flag
d delete a partition <==刪除一個(gè)partition
l list known partition types
m print this menu
n add a new partition <==新增一個(gè)partition
o create a new empty DOS partition table
p print the partition table <==在屏幕上顯示分割表
q quit without saving changes <==不儲(chǔ)存離開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 <==將剛剛的動(dòng)作寫入分割表
x extra functionality (experts only)
離開 fdisk 時(shí)按下 q筝家,那么所有的動(dòng)作都不會(huì)生效!相反的邻辉, 按下w就是動(dòng)作生效的意思溪王。
Command (m for help): p <== 這里可以輸出目前磁盤的狀態(tài)
Disk /dev/hdc: 41.1 GB, 41174138880 bytes <==這個(gè)磁盤的文件名與容量
255 heads, 63 sectors/track, 5005 cylinders <==磁頭、扇區(qū)與磁柱大小
Units = cylinders of 16065 * 512 = 8225280 bytes <==每個(gè)磁柱的大小
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
# 裝置文件名 啟動(dòng)區(qū)否 開始磁柱 結(jié)束磁柱 1K大小容量 磁盤分區(qū)槽內(nèi)的系統(tǒng)
Command (m for help): q
想要不儲(chǔ)存離開嗎值骇?按下 q 就對了莹菱!不要隨便按 w 啊吱瘩!
使用 p 可以列出目前這顆磁盤的分割表信息道伟,這個(gè)信息的上半部在顯示整體磁盤的狀態(tài)。
磁盤格式化
磁盤分割完畢后自然就是要進(jìn)行文件系統(tǒng)的格式化使碾,格式化的命令非常的簡單蜜徽,使用 mkfs(make filesystem) 命令。
語法:
mkfs [-t 文件系統(tǒng)格式] 裝置文件名
選項(xiàng)與參數(shù):
- -t :可以接文件系統(tǒng)格式票摇,例如 ext3, ext2, vfat 等(系統(tǒng)有支持才會(huì)生效)
實(shí)例1
查看 mkfs 支持的文件格式
[root@localhost /]# mkfs [Tab][Tab]
mkfs mkfs.cramfs mkfs.ext2 mkfs.ext3 mkfs.ext4 mkfs.ext4dev
按下兩個(gè)[tab]拘鞋,會(huì)發(fā)現(xiàn) mkfs 支持的文件格式如上所示。
實(shí)例2
將分區(qū) /dev/hdc6(可指定你自己的分區(qū)) 格式化為 ext3 文件系統(tǒng):
[root@localhost /]# 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數(shù)量
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.
# 這樣就創(chuàng)建起來我們所需要的 Ext3 文件系統(tǒng)了矢门!簡單明了盆色!
硬盤檢驗(yàn)
fsck(file system check)用來檢查和維護(hù)不一致的文件系統(tǒng)灰蛙。
若系統(tǒng)掉電或磁盤發(fā)生問題,可利用fsck命令對文件系統(tǒng)進(jìn)行檢查隔躲。
語法:
fsck [-t 文件系統(tǒng)] [-ACay] 裝置名稱
選項(xiàng)與參數(shù):
- -t : 給定檔案系統(tǒng)的型式摩梧,若在 /etc/fstab 中已有定義或 kernel 本身已支援的則不需加上此參數(shù)
- -s : 依序一個(gè)一個(gè)地執(zhí)行 fsck 的指令來檢查
- -A : 對/etc/fstab 中所有列出來的 分區(qū)(partition)做檢查
- -C : 顯示完整的檢查進(jìn)度
- -d : 打印出 e2fsck 的 debug 結(jié)果
- -p : 同時(shí)有 -A 條件時(shí),同時(shí)有多個(gè) fsck 的檢查一起執(zhí)行
- -R : 同時(shí)有 -A 條件時(shí)蹭越,省略 / 不檢查
- -V : 詳細(xì)顯示模式
- -a : 如果檢查有錯(cuò)則自動(dòng)修復(fù)
- -r : 如果檢查有錯(cuò)則由使用者回答是否修復(fù)
- -y : 選項(xiàng)指定檢測每個(gè)文件是自動(dòng)輸入yes障本,在不確定那些是不正常的時(shí)候,可以執(zhí)行 # fsck -y 全部檢查修復(fù)响鹃。
實(shí)例1
查看系統(tǒng)有多少文件系統(tǒng)支持的 fsck 命令:
[root@localhost /]# fsck
fsck fsck.cramfs fsck.ext2 fsck.ext3 fsck.ext4 fsck.ext4dev
實(shí)例2
強(qiáng)制檢測 /dev/hdc6 分區(qū):
[root@localhost /]# 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 的選項(xiàng)驾霜,則由于這個(gè)文件系統(tǒng)不曾出現(xiàn)問題,檢查的經(jīng)過非陈蛑茫快速粪糙!若加上 -f 強(qiáng)制檢查,才會(huì)一項(xiàng)一項(xiàng)的顯示過程忿项。
磁盤掛載與卸載
Linux 的磁盤掛載使用 mount 命令蓉冈,卸載使用 umount 命令。
磁盤掛載語法:
mount [-t 文件系統(tǒng)] [-L Label名] [-o 額外選項(xiàng)] [-n] 裝置文件名 掛載點(diǎn)
實(shí)例
用默認(rèn)的方式轩触,將剛剛創(chuàng)建的 /dev/hdc6 掛載到 /mnt/hdc6 上面寞酿!
[root@localhost /]# mkdir /dev/hdc6
[root@localhost /]# mount /dev/hdc6 /mnt/hdc6
[root@localhost /]# df
Filesystem 1K-blocks Used Available Use% Mounted on
.....中間省略.....
/dev/hdc6 1976312 42072 1833836 3% /mnt/hdc6
磁盤卸載命令 umount 語法:
umount [-fn] 裝置文件名或掛載點(diǎn)
選項(xiàng)與參數(shù):
-f :強(qiáng)制卸除!可用在類似網(wǎng)絡(luò)文件系統(tǒng) (NFS) 無法讀取到的情況下脱柱;
-n :不升級(jí) /etc/mtab 情況下卸除伐弹。
卸載/dev/hdc6
[root@localhost /]# umount /dev/hdc6