1、總結整理磁盤管理及文件系統管理中設計的各種命令的使用,並附註一定的示例;
磁盤管理命令:
磁盤分區工具:fdisk,parted,sfdisk
fdisk的使用:
最多支持在一塊硬盤上的15個分區;
分區管理子命令:
p: 顯示
n: 創建
d: 刪除
t: 修改分區ID
l: 列出所有支持ID類型
w: 保存退出
q: 放棄修改並退出
m: 獲取幫助
示例:
[root@localhost ~]#fdisk /dev/sda
Command (m for help): n
Command action
e extended
p primary partition (1-4)
e
Partition number(1-4): 4
First cylinder(1-15665, default 1): 1
Last cylinder,+cylinders or +size{K,M,G} (1-63, default 63): +10G
Command (m forhelp):w
# cat /proc/partitions
有三個命令可以讓內核重讀磁盤分區表:
CentOS 5: partprobe[DEVICE]
CentOS 6,7:
partx
kpartx
partx命令:
partx DEVICE
partx -a DEVICE
partx -a -n M:N DEVICE
M
M:
:N
kpartx命令:
kpartx -af DEVICE
Linux文件系統管理命令:
mkfs, mkfs -t type =mkfs.type
ext:mke2fs
創建文件系統:
mke2fs [OPTION]... DEVICE
-t {ext2|ext3|ext4}
-b {1024|2048|4096}
-L 'LABEL'
-j: mke2fs -t ext3
-i #:
-N #:
-m #: 預留磁盤空間佔據多大百分比的空間爲後期管理使用;
-O FEATURE[,...]
-O ^FEATURE:關閉此特性
文件系統屬性查看及調整工具:
e2label
e2lable DEVICE [LABEL]
tune2fs
顯示ext系列文件系統的屬性,或調整其屬性;
-l:顯示超級塊中的信息;顯示整個文件的屬性及佈局等相關信息;
-L 'LABEL':修改卷標;
-m #: 調整預留給管理員的管理空間百分比;
-j: ext2 --> ext3
-O:文件系統屬性的啓動或關閉
-o:文件系統默認掛載選項的啓用或關閉
示例:
[root@localhost ~]#tune2fs -L 'liaodijin' /dev/sda1 \\用tune2fs修改卷標名爲liaodijin
tune2fs 1.41.12 (15-Sep-2015)
dumpe2fs:
-h: 僅顯示超級塊信息;
blkid命令:
blkid DEVICE
LABEL, UUID, TYPE
[root@localhost ~]#blkid /dev/sda1 \\使用blkid查看分區UUID TYPE及卷標(即分區名)。
/dev/sda1: UUID="46d989e4-d6d6-42d0-8562-e2c1543f6881"TYPE="xfs"
文件系統檢測:
fsck: Filesystem check
fsck -t type
fsck.type
-a: 自動修復錯誤
-r: 交互式修復錯誤
-f: 強制檢測
e2fsck:ext系列文件系統專用的檢測修復工具;
-y: 自動回答爲“yes”
-f:force
文件系統的掛載和使用:
將額外文件系統與根文件系統某現存的目錄建立起關聯關係,進而使得此目錄做爲其它文件系統訪問入口的行爲,稱之爲掛載;
解除此關聯關係的過程:卸載;
掛載點:mount point, 設備掛載至目錄;
注意:掛載點在掛載在之後,其內部原有的文件會被暫時隱藏;建立使用空目錄做爲掛載點;
掛載方法:
mount:通過讀取/etc/mtab文件來顯示當前系統所有已經掛載的設備;
mount -a:掛載/etc/fstab文件中的所有支持自動掛載的文件系統;
mount [options] [-ooptions] DEVICE MOUNT_POINT
[options]:命令選項
[-o options]:掛載選項
DEVICE: 要掛載的設備
(1) 設備文件:/dev/sda5
(2) 卷標:-L 'LABEL'
(3) UUID:-U 'UUID'
(4) 僞文件系統名稱
MOUNT_POINT: 掛載點
常用選項:
-t type:文件系統類型
-r: 以“只讀”方式掛載此文件系統
-w: 以“讀寫”方式掛載此文件系統
-n:每個文件系統在掛載時都會自動更新/etc/mtab文件,-n用於禁止此功能;
此時,如果想查看掛載的所有文件系統:cat/proc/mounts
-a: 自動掛載所有支持自動掛載的設備;
-B:綁定目錄至另一個目錄上;
-o 掛載選項:
async:異步模式
sync: 同步模式
atime/noatime:是否更新訪問時間戳;
diratime/nodiratime:是否更新目錄的訪問時間戳;
auto/noauto: 是否允許此設備被自動掛載;
exec/noexec:是否允許執行此文件系統上應用程序;
dev/nodev:是否支持在此設備上使用設備文件;
suid/nosuid:
remount:重新掛載
ro
rw
user/nouser: 是否允許普通掛載此設備;
acl:是否支持此設備上使用facl;
defaults:rw, suid, dev, exec, auto, nouser, andasync
umount:卸載命令
# umount DEVICE
# umount MOUNT_POINT
查看正在訪問指定掛載點的進程:
# fuser -v MOUNT_POINT
終止所有正在訪問指定的掛載點的進程:
# fuser -kmMOUNT_POINT
交換分區:
free命令:
查看memory和swap的使用狀態
-m: 以MB爲單位
-g: 以GB爲單位
mkswap:創建交換分區
mkswap [option] DEVICE
-L 'LABEL'
swapon:啓用交換分區
swapon [option][DEVICE]
-a: 激活所有交換分區
-p PRIORITY: 設定其優先級;
swapoff:禁用交換分區
swapoff [option][DEVICE]
文件系統等空間佔用信息的查看工具:
df: disk free
-h: human-readable
-i: inode數量
-P: 以Posix兼容的格式輸出
du: disk usage
-h: human-readable
-s: summary
文件系統相關掛載配置文件:/etc/fstab
每行定義一個文件系統;
要掛載的設備或僞文件系統 掛載點 文件系統類型 掛載選項 轉儲頻率 自檢次序
要掛載的設備:
設備文件、LABEL=、UUID=、僞文件系統名稱
掛載選項:
defaults
轉儲頻率:
0: 不轉儲
1: 每天轉儲
2: 每隔一天轉儲
自檢次序:
0:不自檢
1:首先自檢,通常只有/才爲1
2:
...
符號鏈接:
鏈接文件的數據指向另一個文件路徑;
可以對目錄進行;
可以跨分區;
指向的是另一個文件路徑;而非inode;
對文件創建符號鏈接不會增加其引用計數;刪除原文件,符號鏈接文件也將失效;
ln [-s] SRC DEST
-s:symbolic link
-v:verbose
2、創建一個10G的文件系統,類型爲ext4,要求開機可自動掛載至/mydata目錄
[root@localhost ~]# fdisk /dev/sda
WARNING:DOS-compatible mode is deprecated. It's strongly recommended to
switch off the mode (command 'c') andchange display units to
sectors (command 'u').
Command (m for help):p
Disk /dev/sda: 128.8GB, 128849018880 bytes
255 heads, 63sectors/track, 15665 cylinders
Units = cylinders of16065 * 512 = 8225280 bytes
Sector size(logical/physical): 512 bytes / 512 bytes
I/O size(minimum/optimal): 512 bytes / 512 bytes
Disk identifier:0x00085376
Device Boot Start End Blocks Id System
/dev/sda1 * 1 26 204800 83 Linux
Partition 1 does notend on cylinder boundary.
/dev/sda2 26 6553 52428800 8e Linux LVM
Command (m for help):n
Command action
e extended
p primary partition (1-4)
p
Partition number(1-4): 3
First cylinder(6553-15665, default 6553):
Using default value6553
Last cylinder,+cylinders or +size{K,M,G} (6553-15665, default 15665): +10G
Command (m for help):p
Disk /dev/sda: 128.8GB, 128849018880 bytes
255 heads, 63sectors/track, 15665 cylinders
Units = cylinders of16065 * 512 = 8225280 bytes
Sector size(logical/physical): 512 bytes / 512 bytes
I/O size(minimum/optimal): 512 bytes / 512 bytes
Disk identifier:0x00085376
Device Boot Start End Blocks Id System
/dev/sda1 * 1 26 204800 83 Linux
Partition 1 does notend on cylinder boundary.
/dev/sda2 26 6553 52428800 8e Linux LVM
/dev/sda3 6553 7858 10484761 83 Linux
Command (m for help):w
The partition tablehas been altered!
Calling ioctl() tore-read partition table.
WARNING: Re-readingthe partition table failed with error 16: Device or resource busy.
The kernel still usesthe old table. The new table will be used at
the next reboot or afteryou run partprobe(8) or kpartx(8)
Syncing disks.
[root@localhost ~]#cat /proc/partitions
major minor #blocks name
8 0 125829120 sda
8 1 204800 sda1
8 2 52428800 sda2
253 0 29356032 dm-0
253 1 2097152 dm-1
253 2 20971520 dm-2
[root@localhost ~]#partx -a /dev/sda
BLKPG: Device orresource busy
error adding partition1
BLKPG: Device orresource busy
error adding partition2
[root@localhost ~]#partx -a /dev/sda3
last arg is not thewhole disk
call: partx -optsdevice wholedisk
[root@localhost ~]#cat /proc/partitions
major minor #blocks name
8 0 125829120 sda
8 1 204800 sda1
8 2 52428800 sda2
8 3 10484761 sda3
253 0 29356032 dm-0
253 1 2097152 dm-1
253 2 20971520 dm-2
[root@localhost ~]#mke2fs -t ext4 /dev/sda3
mke2fs 1.41.12(17-May-2010)
Filesystem label=
OS type: Linux
Block size=4096(log=2)
Fragment size=4096(log=2)
Stride=0 blocks,Stripe width=0 blocks
655360 inodes, 2621190blocks
131059 blocks (5.00%)reserved for the super user
First data block=0
Maximum filesystemblocks=2684354560
80 block groups
32768 blocks pergroup, 32768 fragments per group
8192 inodes per group
Superblock backupsstored on blocks:
32768, 98304, 163840, 229376, 294912,819200, 884736, 1605632
Writing inode tables:done
Creating journal(32768 blocks): done
Writing superblocksand filesystem accounting information: done
This filesystem willbe automatically checked every 24 mounts or
180 days, whichevercomes first. Use tune2fs -c or -i tooverride.
[root@localhost ~]#mkdir /mydata
[root@localhost ~]#mount /dev/sda3 /mydata
[root@localhost ~]# df-TH
Filesystem Type Size Used Avail Use% Mounted on
/dev/mapper/vol0-root
ext4 30G 397M 28G 2% /
tmpfs tmpfs 515M 78k 515M 1% /dev/shm
/dev/sda1 ext4 199M 38M 152M 20% /boot
/dev/mapper/vol0-usrext4 22G 3.2G 17G 16% /usr
/dev/sda3 ext4 11G 24M 9.9G 1% /mydata
[root@localhost ~]#vim /etc/fstab
# /etc/fstab
# Created by anacondaon Mon Sep 7 23:06:37 2015
#
# Accessiblefilesystems, by reference, are maintained under '/dev/disk'
# See man pagesfstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/vol0-root / ext4 defaults 1 1
UUID=7ae1abef-3d62-4c11-8c6d-f3b776696d61/boot ext4 defaults 1 2
/dev/mapper/vol0-usr /usr ext4 defaults 1 2
/dev/mapper/vol0-swap swap swap defaults 0 0
/dev/sda3 /mydata ext4 defaults 0 0
tmpfs /dev/shm tmpfs defaults 0 0
devpts /dev/pts devpts gid=5,mode=620 0 0
sysfs /sys sysfs defaults 0 0
proc /proc proc defaults 0 0