linux_磁盤管理與文件系統

一、總結:

Linux系統管理


磁盤和文件系統管理;

RAID, LVM; btrfs; 

程序包管理:rpm, yum(dnf)

Linux的網絡管理; 

進程和作業管理:htop, glances, tsar;

sed和awk;

Linux系統的開機啓動流程;

內核基礎知識,內核定製編譯;

系統安裝:kickstart, pxe


Linux磁盤管理

硬盤:

機械式硬盤

SSD


機械式:

track

sector: 512bytes

cylinder: 分區的基本單位;


MBR:Master Boot Record

512bytes

446: bootloader

64: filesystem allocation table

16: 標識一個分區

2:55AA


磁盤接口類型:

IDE (ATA):133MB/s,/dev/hd

SCSI: 640MB/s

SATA:6Gbps

SAS:6Gbps

USB:480MB/s


識別硬盤設備:/dev/sd

標記不同的硬盤設備:/dev/sd[a-z]

標記同一設備上的不同分區:/dev/sd[a-z][1-]

1-4: 主或擴展分區標識

5+:邏輯分區標識


設備文件:特殊文件

設備號:

major, minor

major: 設備類型

minor: 同一類型下的不同設備


“塊”:block,隨機設備

“字符”:character,線性設備


GPT


分區:分隔存儲空間爲多個小的空間,每個空間可獨立使用文件系統;


分區工具:

fdisk, parted, sfdisk


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


Linux文件系統管理


文件系統:

VFS:Virtual File System


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


二、創建一個10G文件系統,類型爲ext4,要求開機可自動掛載至/mydata目錄;

首先查看磁盤信息:

[root@Centos6 ~]# 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: 0x0008c80d

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1               1        7650    61440000   8e  Linux LVM
/dev/sda2   *        7650        7675      204800   83  Linux

Disk /dev/mapper/vg_centos6-LogVol00: 2097 MB, 2097152000 bytes
255 heads, 63 sectors/track, 254 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/vg_centos6-LogVol03: 39.8 GB, 39841693696 bytes
255 heads, 63 sectors/track, 4843 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: 0xe8070000


Disk /dev/mapper/luks-6f00ee5a-0ab7-4164-bad4-865d6c97b01b: 39.8 GB, 39839596544 bytes
255 heads, 63 sectors/track, 4843 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/vg_centos6-LogVol01: 10.5 GB, 10485760000 bytes
255 heads, 63 sectors/track, 1274 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/vg_centos6-LogVol02: 10.5 GB, 10485760000 bytes
255 heads, 63 sectors/track, 1274 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


對/dev/sda進行操作:

[root@Centos6 ~]# fdisk /dev/sda

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):


選擇n,添加一塊新的分區

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)


選擇主分區或者擴展分區,主分區最多4個,目前還有2個可以選擇:

Command (m for help): p

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: 0x0008c80d

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1               1        7650    61440000   8e  Linux LVM
/dev/sda2   *        7650        7675      204800   83  Linux

使用擴展分區,並且創建10G的擴展分區

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
e
Partition number (1-4): 3
First cylinder (7675-15665, default 7675): 
Using default value 7675
Last cylinder, +cylinders or +size{K,M,G} (7675-15665, default 15665): +10G

按w保存創建內容:

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.

對擴展分區再次進行操作,建立10G的空間

[root@Centos6 ~]# fdisk /dev/sda

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): n
Command action
   l   logical (5 or over)
   p   primary partition (1-4)
l
First cylinder (7675-8980, default 7675): 
Using default value 7675
Last cylinder, +cylinders or +size{K,M,G} (7675-8980, default 8980): 
Using default value 8980

Command (m for help): w
The partition table has been altered!

partprobe刷新後使用cat /proc/partitions看到:

[root@Centos6 ~]# cat /proc/partitions 
major minor  #blocks  name

   8        0  125829120 sda
   8        1   61440000 sda1
   8        2     204800 sda2
   8        3          1 sda3
   8        5   10485994 sda5
 253        0    2048000 dm-0
 253        1   38907904 dm-1
 253        2   38905856 dm-2
 253        3   10240000 dm-3
 253        4   10240000 dm-4

sda5出來了。

對這10G的空間進行格式化成ext4類型。

[root@Centos6 ~]# mkfs.ext4 /dev/sda5 
mke2fs 1.41.12 (17-May-2010)
warning: 58 blocks unused.

Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
656640 inodes, 2621440 blocks
131074 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=2684354560
80 block groups
32768 blocks per group, 32768 fragments per group
8208 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: ddone

This filesystem will be automatically checked every 36 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.

建立/mydata目錄

[root@Centos6 ~]# mkdir /mydata

更改fstab使得開機能將/dev/sda5掛載到/mydata下面,掛載之後,使用df -h查看

[root@Centos6 ~]# cat /etc/fstab | tail -n1
/dev/sda5		/mydata		ext4		defaults	0 0

[root@Centos6 ~]# mount -a
[root@Centos6 ~]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/dm-2              37G  356M   35G   2% /
tmpfs                 932M     0  932M   0% /dev/shm
/dev/sda2             190M   41M  140M  23% /boot
/dev/mapper/vg_centos6-LogVol01
                      9.5G  1.6G  7.5G  17% /usr
/dev/mapper/vg_centos6-LogVol02
                      9.5G   79M  9.0G   1% /var
/dev/sda5             9.8G   23M  9.2G   1% /mydata
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章