Linux磁碟和檔案系統管理(1)_分區 格式化
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 ~]# dfFilesystem 1K-blocks Used Available Use% Mounted on/dev/sda3 9947976 551432 8868160 6% /devtmpfs 493016 0 493016 0% /devtmpfs 501580 0 501580 0% /dev/shmtmpfs 501580 6792 494788 2% /runtmpfs 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% /devtmpfs tmpfs 501580 0 501580 0% /dev/shmtmpfs tmpfs 501580 6792 494788 2% /runtmpfs 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 -ThFilesystem Type Size Used Avail Use% Mounted on/dev/sda3 ext4 9.5G 539M 8.5G 6% /devtmpfs devtmpfs 482M 0 482M 0% /devtmpfs tmpfs 490M 0 490M 0% /dev/shmtmpfs tmpfs 490M 6.7M 484M 2% /runtmpfs 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]# du4 ./testbash28 .[root@localhost test]# du -a16 ./functions4 ./testbash4 ./test_vim28 .[root@localhost test]# du -h4.0K ./testbash28K .[root@localhost test]# du -sh /etc/22M /etc/[root@localhost test]# du -S4 ./testbash24 .
分區:分隔儲存空間為多個小的空間,每個空間可獨立使用檔案系統;分區工具: 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 Distributed File System: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 sectorsUnits = sectors of 1 * 512 = 512 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk label type: dosDisk 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 LinuxPartition table entries are not in disk orderDisk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 sectorsUnits = sectors of 1 * 512 = 512 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytes#在新磁碟上建立新分區[root@localhost ~]# fdisk /dev/sdbWelcome 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 tableBuilding 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): 1First sector (2048-41943039, default 2048): ----->選擇開始柱面 預設2048開始Using default value 2048Last sector, +sectors or +size{K,M,G} (2048-41943039, default 41943039): +1024MPartition 1 of type Linux and of size 1 GiB is setCommand (m for help): p ----------------------> 列印分區表資訊# 說明 上面建立分區大小有兩種方式# 1 輸入開始柱面和結束柱面,決定分區大小# 2 直接輸入 +xx單位 分配分區大小Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 sectorsUnits = sectors of 1 * 512 = 512 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk label type: dosDisk identifier: 0x3bb13419 Device Boot Start End Blocks Id System/dev/sdb1 2048 2099199 1048576 83 LinuxCommand (m for help): nPartition type: p primary (1 primary, 0 extended, 3 free) e extendedSelect (default p): e -------------------> 建立擴充分區(最多建立一個擴充分區)Partition number (2-4, default 2): 2First sector (2099200-41943039, default 2099200): Using default value 2099200Last sector, +sectors or +size{K,M,G} (2099200-41943039, default 41943039): Using default value 41943039Partition 2 of type Extended and of size 19 GiB is setCommand (m for help): nPartition type: p primary (1 primary, 1 extended, 2 free) l logical (numbered from 5) --------->因為前面建立了擴充分區,所以有了邏輯分區Select (default p): lAdding logical partition 5First sector (2101248-41943039, default 2101248): 512Value out of range.First sector (2101248-41943039, default 2101248): Using default value 2101248Last sector, +sectors or +size{K,M,G} (2101248-41943039, default 41943039): +512MPartition 5 of type Linux and of size 512 MiB is setCommand (m for help): pDisk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 sectorsUnits = sectors of 1 * 512 = 512 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk label type: dosDisk 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 LinuxCommand (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/sdbNR 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/sdb1meta-data=/dev/sdb1 isize=256 agcount=4, agsize=65536 blks = sectsz=512 attr=2, projid32bit=1 = crc=0 finobt=0data = bsize=4096 blocks=262144, imaxpct=25 = sunit=0 swidth=0 blksnaming =version 2 bsize=4096 ascii-ci=0 ftype=0log =internal log bsize=4096 blocks=2560, version=2 = sectsz=512 sunit=0 blks, lazy-count=1realtime =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/sdb5mke2fs 1.42.9 (28-Dec-2013)Filesystem label=OS type: LinuxBlock size=4096 (log=2)Fragment size=4096 (log=2)Stride=0 blocks, Stripe width=0 blocks32768 inodes, 131072 blocks6553 blocks (5.00%) reserved for the super userFirst data block=0Maximum filesystem blocks=1342177284 block groups32768 blocks per group, 32768 fragments per group8192 inodes per groupSuperblock backups stored on blocks: 32768, 98304Allocating group tables: done Writing inode tables: done Creating journal (4096 blocks): doneWriting 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/sdb6mke2fs 1.42.9 (28-Dec-2013)Filesystem label=test_mk2fsOS type: LinuxBlock size=4096 (log=2)Fragment size=4096 (log=2)Stride=0 blocks, Stripe width=0 blocks32768 inodes, 131072 blocks6553 blocks (5.00%) reserved for the super userFirst data block=0Maximum filesystem blocks=1342177284 block groups32768 blocks per group, 32768 fragments per group8192 inodes per groupSuperblock backups stored on blocks: 32768, 98304Allocating group tables: done Writing inode tables: done Creating journal (4096 blocks): doneWriting superblocks and filesystem accounting information: done