歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> 關於Linux >> Linux磁盤和文件系統管理(1)_分區 格式化

Linux磁盤和文件系統管理(1)_分區 格式化

日期:2017/3/1 12:25:16   编辑:關於Linux
Linux磁盤管理 硬盤: 機械式硬盤 SSD 固態硬盤 機械式: track磁道 sector扇區: 最小的紋理存儲單位,每個扇區大小為512bytes cylinder柱面: 分區的基本單位; 硬盤第一個扇區 512bytes MBR:Master Boot Record 硬盤主引導記錄 MBR占 446 bytes: bootloader 分區表(pratition table) 占64bytes: filesystem allocation table 分區表中每16bytes 標識一個分區,所以主分區和擴展分區最多可以有4個; 2bytes:55AA 標志位,55AA表示生效,其他表示無效 磁盤接口類型: IDE(ATA):速度 133MB/s,/dev/hd SCSI: 速度 640MB/s SATA:速度 6Gbps SAS:速度 6Gbps USB:速度 480MB/s 識別硬盤設備: 各種接口的磁盤在linux中的文件命名,在centos6之後都命名為 :/dev/sd* 標記不同的硬盤設備:/dev/sd[a-z] 標記同一設備上的不同分區:/dev/sd[a-z][1-] 1-4:命名主或擴展分區標識 5+:邏輯分區標識 設備文件:特殊文件 設備號: major,minor major: 表示設備類型 minor: 同一類型下的不同設備 “塊”:block,隨機設備 “字符”:character,線性設備 GPT:GUIDPartition Table 磁盤管理 在linux系統中增加一塊新磁盤,需要 1 對磁盤進行分區 2 對分區進行格式化,創建可用文件系統 3 創建掛載點,並且將其掛載 磁盤與目錄的容量 df命令: 列出文件系統的整體磁盤使用情況 report file system disk space usage 語法: df [OPTION]... [FILE]... 選項: -a:列出所有文件系統 -k或-m:以KB,MB的單位顯示文件系統 -h:以易讀的GB,MB,KB等格式顯示 -i:不用硬盤容量,以inode的數量顯示 -T :顯示分區文件系統類型 示例:
#列出文件系統整體磁盤使用情況
[root@localhost ~]# df
Filesystem     1K-blocks    Used Available Use% Mounted on
/dev/sda3        9947976  551432   8868160   6% /
devtmpfs          493016       0    493016   0% /dev
tmpfs             501580       0    501580   0% /dev/shm
tmpfs             501580    6792    494788   2% /run
tmpfs             501580       0    501580   0% /sys/fs/cgroup
/dev/sda2       20027216 1112520  17874312   6% /usr
/dev/sda1         194235   90696     89203  51% /boot
/dev/sda5        9947976   36976   9382616   1% /home

#顯示文件系統各個分區格式
[root@localhost ~]# df -T 
Filesystem     Type     1K-blocks    Used Available Use% Mounted on
/dev/sda3      ext4       9947976  551432   8868160   6% /
devtmpfs       devtmpfs    493016       0    493016   0% /dev
tmpfs          tmpfs       501580       0    501580   0% /dev/shm
tmpfs          tmpfs       501580    6792    494788   2% /run
tmpfs          tmpfs       501580       0    501580   0% /sys/fs/cgroup
/dev/sda2      ext4      20027216 1112520  17874312   6% /usr
/dev/sda1      ext4        194235   90696     89203  51% /boot
/dev/sda5      ext4       9947976   36976   9382616   1% /home

#
[root@localhost ~]# df -Th
Filesystem     Type      Size  Used Avail Use% Mounted on
/dev/sda3      ext4      9.5G  539M  8.5G   6% /
devtmpfs       devtmpfs  482M     0  482M   0% /dev
tmpfs          tmpfs     490M     0  490M   0% /dev/shm
tmpfs          tmpfs     490M  6.7M  484M   2% /run
tmpfs          tmpfs     490M     0  490M   0% /sys/fs/cgroup
/dev/sda2      ext4       20G  1.1G   18G   6% /usr
/dev/sda1      ext4      190M   89M   88M  51% /boot
/dev/sda5      ext4      9.5G   37M  9.0G   1% /home

Filesystem:表示各個分區,列出設備名 Type:文件系統格式 1k-blocks:1k表示下面數字的單位 Used:已用空間 Avail:可用空間 Use%:磁盤使用率 Mounted on:磁盤掛載點 du命令:統計目錄所占容量 du [OPTION]... [FILE]... 參數: -a:列出所有文件與目錄容量 -h:以易讀的容量單位KB,MB,GB顯示 -s:列出總量 -S:不包含子目錄下的總計 示例:
# 後面不加目錄或文件名 默認分析當前所在目錄
[root@localhost test]# du
4    ./testbash
28    .

[root@localhost test]# du -a
16    ./functions
4    ./testbash
4    ./test_vim
28    .

[root@localhost test]# du -h
4.0K    ./testbash
28K    .

[root@localhost test]# du -sh /etc/
22M    /etc/

[root@localhost test]# du -S
4    ./testbash
24    .

分區:分隔存儲空間為多個小的空間,每個空間可獨立使用文件系統; 分區工具: fdisk,parted, sfdisk fdisk工具的使用: 最多支持在一塊硬盤上的15個分區; fdisk DEVICE:DEVICE為設備文件名,後邊不要加數字,因為分區是針對整個硬盤設備 分區管理子命令: p: 顯示當前分區表 n: 創建新分區 d: 刪除指定分區 t: 修改分區ID l: 列出所有支持分區類型及Id w: 保存退出 q: 放棄修改並退出 m: 獲取幫助 創建完成之後,查看內核是否已經識別新的分區: #cat /proc/partitions 有三個命令可以讓內核重讀磁盤分區表: CentOS5: partprobe [DEVICE] CentOS6,7下命令: partx kpartx partx命令: partx DEVICE partx -a DEVICE 登記某塊盤上的所有分區信息,如果某個分區信息已有記錄,就會報錯 如果某磁盤上的分區信息都沒有被記錄,則安靜完成。 partx -a -n M:N DEVICE 記錄第M個到N個分區信息 M 讀入第M個分區信息 M: 指定讀入分區號的最小值 :N 指定讀入 分區號的最大值 kpartx命令:從分區表建立設備映射 kpartx -af DEVICE -a:添加分區映射 Linux文件系統管理 文件系統: VFS:Virtual FileSystem 保證各種磁盤設備接口都可以使用 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 mkfs [Tab][Tab] 查看mkfs支持的文件格式 mkfs.cramfs mkfs.ext3 mkfs.minix mkfs.btrfs mkfs.ext2 mkfs.ext4 mkfs.xfs ext系列文件系統的專用管理工具: mke2fs -t {ext2|ext3|ext4} DEVICE -b BLOCK: 1024, 2048, 4096 指定塊大小;根據存儲文件的大小決定塊分配大小 -L 'LABEL': 設定卷標名稱 -i #:給多大空間給一個inode -N #:為此磁盤分區一共保留多少個inode -m #:預留磁盤空間占據多大百分比的空間為後期管理使用 -c:檢查磁盤錯誤,僅下達一次-c時,會進行快速讀取測試 -j:相當於 mk2fs -t ext3; 本來mke2fs是ext2,加入journal成為ext3 -O feature[,...] 啟用指定特性 has journal 啟用日志功能 -O ^feature[,...] 關閉指定特性 /etc/mke2fs.conf 默認特性配置文件 blkid命令:定位並顯示塊設備屬性 locate/print block device attributes blkid DEVICE 查看 LABEL,UUID, TYPE 示例:
#查看磁盤狀況 有一塊/dev/sdb新磁盤
[root@localhost ~]# fdisk -l 

Disk /dev/sda: 128.8 GB, 128849018880 bytes, 251658240 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x000c2314

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048      411647      204800   83  Linux
/dev/sda2          411648    41371647    20480000   83  Linux
/dev/sda3        41371648    61851647    10240000   83  Linux
/dev/sda4        61851648   251658239    94903296    5  Extended
/dev/sda5        61855744    82335743    10240000   83  Linux
/dev/sda6        82337792    86532095     2097152   82  Linux swap / Solaris
/dev/sda7        61853696    61855743        1024   83  Linux
/dev/sda8        86534144   107505663    10485760   83  Linux

Partition table entries are not in disk order

Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes

#在新磁盤上創建新分區
[root@localhost ~]# fdisk /dev/sdb
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Device does not contain a recognized partition table
Building a new DOS disklabel with disk identifier 0x3bb13419.

Command (m for help): n ------------------->創建新分區
Partition type:
   p   primary (0 primary, 0 extended, 4 free) ----> 選擇創建主分區
   e   extended -----------------------------------> 選擇創建擴展分區
Select (default p): p -----------------------------> 創建主分區
Partition number (1-4, default 1): 1
First sector (2048-41943039, default 2048): ----->選擇開始柱面 默認2048開始
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-41943039, default 41943039): +1024M
Partition 1 of type Linux and of size 1 GiB is set
Command (m for help): p ----------------------> 打印分區表信息
# 說明 上面創建分區大小有兩種方式
#      1 輸入開始柱面和結束柱面,決定分區大小
#      2 直接輸入 +xx單位 分配分區大小

Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x3bb13419

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048     2099199     1048576   83  Linux

Command (m for help): n
Partition type:
   p   primary (1 primary, 0 extended, 3 free)
   e   extended
Select (default p): e -------------------> 創建擴展分區(最多創建一個擴展分區)
Partition number (2-4, default 2): 2
First sector (2099200-41943039, default 2099200): 
Using default value 2099200
Last sector, +sectors or +size{K,M,G} (2099200-41943039, default 41943039): 
Using default value 41943039
Partition 2 of type Extended and of size 19 GiB is set

Command (m for help): n
Partition type:
   p   primary (1 primary, 1 extended, 2 free)
   l   logical (numbered from 5) --------->因為前面創建了擴展分區,所以有了邏輯分區
Select (default p): l
Adding logical partition 5
First sector (2101248-41943039, default 2101248): 512
Value out of range.
First sector (2101248-41943039, default 2101248): 
Using default value 2101248
Last sector, +sectors or +size{K,M,G} (2101248-41943039, default 41943039): +512M
Partition 5 of type Linux and of size 512 MiB is set

Command (m for help): p

Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x3bb13419

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048     2099199     1048576   83  Linux
/dev/sdb2         2099200    41943039    19921920    5  Extended
/dev/sdb5         2101248     3149823      524288   83  Linux

Command (m for help): w
# 輸入q 不保存操作,退出fdisk
# 輸入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   20480000 sda2
   8        3   10240000 sda3
   8        4          1 sda4
   8        5   10240000 sda5
   8        6    2097152 sda6
   8        7       1024 sda7
   8        8   10485760 sda8
   8       16   20971520 sdb
   8       17    1048576 sdb1
   8       18          1 sdb2
   8       21     524288 sdb5
  11        0     651264 sr0

   
#重讀分區信息
[root@localhost ~]# partx /dev/sdb
NR   START      END  SECTORS SIZE NAME UUID
 1    2048  2099199  2097152   1G      
 2 2099200 41943039 39843840  19G      
 5 2101248  3149823  1048576 512M  

   
 #磁盤格式化 mkfs 將剛才創建的sdb1 格式化為xfs 格式
[root@localhost ~]# mkfs -t xfs /dev/sdb1
meta-data=/dev/sdb1              isize=256    agcount=4, agsize=65536 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=0        finobt=0
data     =                       bsize=4096   blocks=262144, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=0
log      =internal log           bsize=4096   blocks=2560, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0

#查看分區格式
[root@localhost ~]# blkid /dev/sdb1
/dev/sdb1: UUID="a7712c0d-6264-496e-8f6b-1da5b7da5939" TYPE="xfs" 

#使用mkfs.ext4 DEVICE 格式化/dev/sdb5為ext4
[root@localhost ~]# mkfs.ext4 /dev/sdb5
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
32768 inodes, 131072 blocks
6553 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=134217728
4 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks: 
    32768, 98304

Allocating group tables: done                            
Writing inode tables: done                            
Creating journal (4096 blocks): done
Writing superblocks and filesystem accounting information: done

#查看分區格式
[root@localhost ~]# blkid /dev/sdb5
/dev/sdb5: UUID="7ff064f9-49c8-466d-8d86-7f4eba5e353b" TYPE="ext4"

#ext系列文件系統的專用管理工具 創建分區
[root@localhost ~]# mke2fs -t ext4 -L 'test_mk2fs' /dev/sdb6
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=test_mk2fs
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
32768 inodes, 131072 blocks
6553 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=134217728
4 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks: 
    32768, 98304

Allocating group tables: done                            
Writing inode tables: done                            
Creating journal (4096 blocks): done
Writing superblocks and filesystem accounting information: done


Copyright © Linux教程網 All Rights Reserved