一、作業(練習)內容:
1、複習本次課程所講的內容;
2、總結整理磁盤管理及文件系統管理中設計的各種命令的使用,並附註一定的示例;
Linux磁盤管理
機械式
磁盤接口類型:
識別硬盤設備:/dev/sd
標記不同的硬盤設備:/dev/sd[a-z]
標記同一設備上的不同分區:/dev/sd[a-z][1-]
1-4:主或擴展分區標識
5+:邏輯分區標識
設備文件:特殊文件
設備號:
major, minor
major:設備類型
minor: 同一類型下的不同設備
“塊”:block,隨機設備
“字符”:character,線性設備
GPT
分區:分隔存儲空間爲多個小的空間,每個空間可獨立使用文件系統;
fdisk工具的使用:
最多支持在一塊硬盤上的15個分區;
分區管理子命令:
p:顯示
n:創建
d:刪除
t:修改分區ID
l:列出所有支持ID類型
w:保存退出
q:放棄修改並退出
m:獲取幫助
創建完成之後,查看內核是否已經識別新的分區:
# 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
文件系統:
VFS:Virtual File System
絕對路徑:/
根文件系統:rootfs
文件系統掛載:
LSB: FHS(文件系統層級結構標準)
FHS:
/boot:引導文件存放目錄,例如kernel(vmlinuz)、bootloader(grub, grub2);
/bin: binary,供用戶使用的基本的命令;OS啓動即會用到程序;
/sbin:管理類的基本命令;
/lib:基本共享庫;內核模塊;
/lib64:x86_64系統的共享庫;
/etc:配置文件目錄;
/home/USERNAME:普通用主目錄的默認位置;
/root:管理員的主目錄;
/media:便攜式移動設備;
/mnt:臨時文件系統掛載點;
/opt:可選目錄,第三方應用程序的默認安裝位置;
/dev:devices,設備文件
b:block devices,random access
c: character deivces,linear access
/proc:
僞文件系統;用於輸出內核與進程的相關信息;
/sys:
僞文件系統;
/srv:services,系統上運行服務用到中間數據;
/tmp:臨時文件目錄;
/usr:Universal Shared, Read-only data;
bin:保證系統提供基本功能;
sbin
lib
lib64
include: C程序的頭文件;header files
share:結構化的獨立數據
doc, man
local:現在常用的第三方程序的安裝位置;
bin
sbin
lib
lib64
etc
share
/var:variable data files
cache:應用程序緩存數據
lib:應用程序的狀態信息數據
local:專用於爲/usr/local下的應用程序存儲可變數據;
lock:鎖文件
log:日誌文件
opt:專用於/opt
spool:應用程序的數據池;
run:運行中的進程的相關數據;通常用守護進程 的pid文件;
/selinux
Linux:ext2, ext3, ext4, reiserfs, xfs, btrfs, swap
光盤:iso9660
Windows: fat32(vfat), ntfs
Unix: ffs, ufs, jfs, jfs2
網絡文件系統:nfs, cifs
集羣文件系統:ocfs2, gfs2
分佈式文件系統:ceph,
moosefs, mogilefs, hdfs, gfs, glusterfs
(1)日誌型文件系統
非日誌型文件系統:ext2
日誌型文件系統:ext3
(2) swap:交換分區
創建文件系統:
在分區上執行格式化(高級格式化)
要使用某種文件系統,滿足兩個條件:
內核中:支持此種文件系統
用戶空間:有文件系統管理工具
創建工具:mkfs (make filesystem)
mkfs -t type DEVICE
mkfs.type DEVICE
ext系列文件系統的專用管理工具:
mke2fs -t {ext2|ext3|ext4} DEVICE
-b BLOCK: 1024, 2048, 4096
-L 'LABEL': 設定卷標
blkid命令:
blkid DEVICE
LABEL, UUID, TYPE
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:關閉此特性
has_journal
文件系統屬性查看及調整工具:
e2label
e2lable DEVICE [LABEL]
tune2fs
顯示ext系列文件系統的屬性,或調整其屬性;
-l:顯示超級塊中的信息;顯示整個文件的屬性及佈局等相關信息;
-L 'LABEL':修改卷標;
-m #: 調整預留給管理員的管理空間百分比;
-j: ext2 --> ext3
-O:文件系統屬性的啓動或關閉
-o:文件系統默認掛載選項的啓用或關閉
dumpe2fs:
-h:僅顯示超級塊信息;
文件系統檢測:
fsck: Filesystem check
fsck -t type
fsck.type
-a:自動修復錯誤
-r:交互式修復錯誤
-f:強制檢測
e2fsck:ext系列文件系統專用的檢測修復工具;
-y:自動回答爲“yes”
-f:force
文件系統的掛載和使用:
kernel --> rootfs(分區)
rootfs分區:
bin, sbin, etc, lib, lib64, dev, tmp, proc, sys
/sbin/init
將額外文件系統與根文件系統某現存的目錄建立起關聯關係,進而使得此目錄做爲其它文件系統訪問入口的行爲,稱之爲掛載;
解除此關聯關係的過程:卸載;
掛載點:mount point, 設備掛載至目錄;
注意:掛載點在掛載在之後,其內部原有的文件會被暫時隱藏;建立使用空目錄做爲掛載點;
掛載方法:
mount:通過讀取/etc/mtab文件來顯示當前系統所有已經掛載的設備;
mount -a:掛載/etc/fstab文件中的所有支持自動掛載的文件系統;
mount [options] [-o options] 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, and async
umount:卸載命令
# umount DEVICE
# umount MOUNT_POINT
查看正在訪問指定掛載點的進程:
# fuser -v MOUNT_POINT
終止所有正在訪問指定的掛載點的進程:
# fuser -km MOUNT_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
不能對目錄進行;
不能跨分區進行
指向同一個inode的多個不同路徑; 創建文件的硬鏈接會增加inode的引用計數;刪除硬鏈接僅是刪除其一個訪問路徑,只到最後一個路徑被刪除;
符號鏈接:
鏈接文件的數據指向另一個文件路徑;
可以對目錄進行;
可以跨分區;
指向的是另一個文件路徑;而非inode;
對文件創建符號鏈接不會增加其引用計數;刪除原文件,符號鏈接文件也將無法;
ln [-s] SRC DEST
-s:symbolic link
-v:verbose
3、創建一個10G的文件系統,類型爲ext4,要求開機可自動掛載至/mydata目錄
[root@localhost ~]# fdisk -l Disk /dev/sda: 128.8 GB, 128849018880 bytes 255 heads, 63 sectors/track, 15665 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: 0x0005f700 Device Boot Start End Blocks Id System /dev/sda1 * 1 26 204800 83 Linux Partition 1 does not end on cylinder boundary. /dev/sda2 26 7675 61440000 83 Linux /dev/sda3 7675 7936 2097152 82 Linux swap / Solaris Disk /dev/sdb: 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
這裏發現有一個硬盤
[root@localhost ~]# fdisk /dev/sdb Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel Building a new DOS disklabel with disk identifier 0x88371439. Changes will remain in memory only, until you decide to write them. After that, of course, the previous content won't be recoverable. Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite) WARNING: DOS-compatible mode is deprecated. It's strongly recommended to switch off the mode (command 'c') and change display units to sectors (command 'u'). 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 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-2610, default 1): Using default value 1 Last cylinder, +cylinders or +size{K,M,G} (1-2610, default 2610): +10G Command (m for help): w The partition table has been altered! Calling ioctl() to re-read partition table. Syncing disks. [root@localhost ~]# cat /proc/partitions major minor #blocks name 8 0 125829120 sda 8 1 204800 sda1 8 2 61440000 sda2 8 3 2097152 sda3 8 16 20971520 sdb 8 17 10490413 sdb1 [root@localhost ~]#
接下來格式化,並且指定類型爲ext4
[root@localhost ~]# mkfs -t ext4 /dev/sdb1 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 655776 inodes, 2622603 blocks 131130 blocks (5.00%) reserved for the super user First data block=0 Maximum filesystem blocks=2688548864 81 block groups 32768 blocks per group, 32768 fragments per group 8096 inodes per group Superblock backups stored on blocks: 32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632 Writing inode tables: done Creating journal (32768 blocks): done Writing superblocks and filesystem accounting information: done This filesystem will be automatically checked every 35 mounts or 180 days, whichever comes first. Use tune2fs -c or -i to override.
然後在根目錄下創建mydata目錄,並使用mount命令掛載分區
[root@localhost ~]# mkdir /mydata -----創建目錄 [root@localhost ~]# [root@localhost ~]# mount -o auto /dev/sdb1 /mydata -o auto表示此分區允許自動掛載 [root@localhost ~]# df -Th Filesystem Type Size Used Avail Use% Mounted on /dev/sda2 ext4 58G 3.4G 52G 7% / tmpfs tmpfs 491M 0 491M 0% /dev/shm /dev/sda1 ext4 190M 36M 145M 20% /boot /dev/sdb1 ext4 9.8G 23M 9.3G 1% /mydata [root@localhost ~]#
最後要求開機可自動掛載
[root@localhost ~]# blkid /dev/sdb1 ---使用blkid命令查看該分去的UUD,並把該值複製下來 /dev/sdb1: UUID="c48ea605-b1d7-4585-aeb8-e6918ea4e77a" TYPE="ext4" [root@localhost ~]# [root@localhost ~]# vi /etc/fstab
-------編輯/etc/fstab配置文件,添加一行該分區的信息,下次開機後系統將會自動掛載該分區
# /etc/fstab # Created by anaconda on Wed Aug 19 18:07:26 2015 # # Accessible filesystems, by reference, are maintained under '/dev/disk' # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info # UUID=628caa97-3e69-481c-95ed-13acc4721bec / ext4 defaults 1 1 UUID=f7eb501a-f2d6-4b95-bf68-b4a616f3d992 /boot ext4 defaults 1 2 UUID=511653fb-e927-4f78-bac9-c2d349484467 swap swap defaults 0 0 UUID="c48ea605-b1d7-4585-aeb8-e6918ea4e77a" /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
最後保存退出
[root@localhost ~]# mount -a [root@localhost ~]# [root@localhost ~]# df -Th Filesystem Type Size Used Avail Use% Mounted on /dev/sda2 ext4 58G 3.4G 52G 7% / tmpfs tmpfs 491M 0 491M 0% /dev/shm /dev/sda1 ext4 190M 36M 145M 20% /boot /dev/sdb1 ext4 9.8G 23M 9.3G 1% /mydata [root@localhost ~]#