Linux磁碟管理及檔案系統使用
- 磁碟管理基礎知識
- 分割槽
- fdisk
- parted
- gdisk
- 格式化
- 掛載
- Linux中磁碟命名:
IDE型別:/dev/hd[a-z]
SCSI型別:/dev/sd[a-z] -
一塊磁碟經過哪些步驟才能夠使用:分割槽(非必需)-->格式化-->掛載;磁碟分割槽的型別有MBR(master boot record)和GPT(GUID Partition Table),主要區別在MBR只能分4個主分割槽超過需要分為邏輯分割槽,磁碟容量最大不超過2T,超過部分無法識別。
- MBR 也就是主引導記錄,位於硬碟的 0 磁軌、0 柱面、1 扇區中,主要記錄了啟動載入程式和磁碟的分割槽表:
由於分割槽表大小固定:最多隻能分4個,超過需要使用擴充套件分割槽來劃分邏輯分割槽,即使主分割槽再分配一個,邏輯分割槽命名從/dev/sda5開始。 - 分割槽
-
fdisk:不適用於GPT分割槽。
1.檢視磁碟分割槽資訊:
用法:fdisk -l [-u] [device...]:列出指定磁碟裝置上的分割槽情況;[[email protected] ~]# fdisk -l Disk /dev/sda: 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: 0x00036ca6 Device Boot Start End Blocks Id System /dev/sda1 2048 20973567 10485760 83 Linux /dev/sda2 20973568 25167871 2097152 82 Linux swap / Solaris /dev/sda3 25167872 25692159 262144 83 Linux
2.分割槽管理,fdisk提供了互動介面來管理分割槽
用法:fdisk [options] <disk>[[email protected] ~]# fdisk /dev/sda 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. 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:刪除分割槽 g create a new empty GPT partition table G create an IRIX (SGI) partition table 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)
3.在已經分割槽並且已經掛載其中某個分割槽的磁碟裝置上建立的新分割槽,核心可能無法直接識別,通知核心重讀分割槽表
檢視:cat /proc/partitions
CentOS 5:partprobe [device]
CentOS 6,7:partx, kpartx
partx -a [device]
kpartx -af [device] - parted 、gdisk能夠使用GPT模式
1.檢視磁碟資訊
parted [options] [device][[email protected] ~]# parted -l Model: VMware, VMware Virtual S (scsi) Disk /dev/sda: 21.5GB Sector size (logical/physical): 512B/512B Partition Table: msdos Disk Flags: Number Start End Size Type File system Flags 1 1049kB 10.7GB 10.7GB primary xfs 2 10.7GB 12.9GB 2147MB primary linux-swap(v1) 3 12.9GB 13.2GB 268MB primary btrfs Error: /dev/sdb: unrecognised disk label Model: VMware, VMware Virtual S (scsi) Disk /dev/sdb: 3221GB Sector size (logical/physical): 512B/512B Partition Table: unknown Disk Flags:
2.分割槽管理
用法: parted device[[email protected] ~]# parted /dev/sdb GNU Parted 3.1 Using /dev/sdb Welcome to GNU Parted! Type 'help' to view a list of commands. (parted) help #檢視幫助 align-check TYPE N check partition N for TYPE(min|opt) alignment help [COMMAND] print general help, or help on COMMAND mklabel,mktable LABEL-TYPE create a new disklabel (partition table) mkpart PART-TYPE [FS-TYPE] START END make a partition name NUMBER NAME name partition NUMBER as NAME print [devices|free|list,all|NUMBER] display the partition table, available devices, free space, all found partitions, or a particular partition quit exit program rescue START END rescue a lost partition near START and END rm NUMBER delete partition NUMBER select DEVICE choose the device to edit disk_set FLAG STATE change the FLAG on selected device disk_toggle [FLAG] toggle the state of FLAG on selected device set NUMBER FLAG STATE change the FLAG on partition NUMBER toggle [NUMBER [FLAG]] toggle the state of FLAG on partition NUMBER unit UNIT set the default unit to UNIT version display the version number and copyright information of GNU Parted (parted) print Error: /dev/sdb: unrecognised disk label Model: VMware, VMware Virtual S (scsi) Disk /dev/sdb: 3221GB Sector size (logical/physical): 512B/512B Partition Table: unknown Disk Flags: (parted) mklabel gpt #修改磁碟結構型別:"aix", "amiga", "bsd", "dvh", "gpt", "loop", "mac", "msdos", "pc98", or "sun" Warning: The existing disk label on /dev/sdb will be destroyed and all data on this disk will be lost. Do you want to continue? Yes/No? yes (parted) mkpart #互動式分割槽 Partition name? []? primary #分割槽名稱: primary", "logical", or "extended" File system type? [ext2]? ext4 #檔案系統型別 Start? 0 End? 1000 #分配分割槽大小 Warning: The resulting partition is not properly aligned for best performance. Ignore/Cancel? Ignore (parted) print Model: VMware, VMware Virtual S (scsi) Disk /dev/sdb: 3221GB Sector size (logical/physical): 512B/512B Partition Table: gpt Disk Flags: Number Start End Size File system Name Flags 1 17.4kB 1000MB 1000MB primary (parted) rm 1 #刪除分割槽1 (parted) p Model: VMware, VMware Virtual S (scsi) Disk /dev/sdb: 3221GB Sector size (logical/physical): 512B/512B Partition Table: gpt Disk Flags: Number Start End Size File system Name Flags ============================================================== (parted) mkpart primary 0 2000G # 方法2分割槽 mkpart part-type [fs-type] start end Warning: The resulting partition is not properly aligned for best performance. Ignore/Cancel? i (parted) p Model: VMware, VMware Virtual S (scsi) Disk /dev/sdb: 3221GB Sector size (logical/physical): 512B/512B Partition Table: gpt Disk Flags: Number Start End Size File system Name Flags 1 17.4kB 2000GB 2000GB primary (parted) q Information: You may need to update /etc/fstab.
-
centos7有更簡便工具:gdisk 與fdisk類似
[[email protected] ~]# gdisk /dev/sdb GPT fdisk (gdisk) version 0.8.10 Partition table scan: MBR: protective BSD: not present APM: not present GPT: present Found valid GPT with protective MBR; using GPT. #預設是GPT模式 Command (? for help): ?#檢視幫助 b back up GPT data to a file c change a partition's name d delete a partition i show detailed information on a partition l list known partition types n add a new partition o create a new empty GUID partition table (GPT) p print the partition table q quit without saving changes r recovery and transformation options (experts only) s sort partitions t change a partition's type code v verify disk w write table to disk and exit x extra functionality (experts only) ? print this menu ================================================ Command (? for help): n 新增新分割槽 Partition number (1-128, default 1): First sector (34-6291455966, default = 2048) or {+-}size{KMGTP}: +3T First sector (34-6291455966, default = 2048) or {+-}size{KMGTP}: Last sector (2048-6291455966, default = 6291455966) or {+-}size{KMGTP}: +3T Last sector (2048-6291455966, default = 6291455966) or {+-}size{KMGTP}: Current type is 'Linux filesystem' Hex code or GUID (L to show codes, Enter = 8300): 8300 Changed type of partition to 'Linux filesystem' ======檢視分割槽資訊 Command (? for help): p Disk /dev/sdb: 6291456000 sectors, 2.9 TiB Logical sector size: 512 bytes Disk identifier (GUID): FA9BB121-FD84-4955-95C3-D4F21890A508 Partition table holds up to 128 entries First usable sector is 34, last usable sector is 6291455966 Partitions will be aligned on 2048-sector boundaries Total free space is 2014 sectors (1007.0 KiB) Number Start (sector) End (sector) Size Code Name 1 2048 6291455966 2.9 TiB 8300 Linux filesystem
-
格式化:低階格式化(分割槽之前進行,劃分磁軌)、高階格式化(分割槽之後對分割槽進行,建立檔案系統)
1.建立檔案系統的工具:[[email protected] ~]# mkfs mkfs.btrfs mkfs.ext2 mkfs.ext4 mkfs.xfs mkfs.cramfs mkfs.ext3 mkfs.minix
2.ext系列檔案系統專用管理工具:mke2fs
mke2fs [OPTIONS] device -t {ext2|ext3|ext4}:指明要建立的檔案系統型別 mkfs.ext4 = mkfs -t ext4 = mke2fs -t ext4 -b {1024|2048|4096}:指明檔案系統的塊大小; -L LABEL:指明卷標; -j:建立有日誌功能的檔案系統ext3; mke2fs -j = mke2fs -t ext3 = mkfs -t ext3 = mkfs.ext3 -i #:bytes-per-inode,指明inode與位元組的比率;即每多少位元組建立一個Indode; -N #:直接指明要給此檔案系統建立的inode的數量; -m #:指定預留的空間,百分比; -O [^]FEATURE:以指定的特性建立目標檔案系統;
3.檢測及修復檔案系統工具 fsck.type e2fsck xfs_repair,修復檔案建議離線修復
ext系列檔案系統的專用工具: e2fsck : check a Linux ext2/ext3/ext4 file system e2fsck [OPTIONS] device -y:對所有問題自動回答為yes; -f:即使檔案系統處於clean狀態,也要強制進行檢測; -b:superblock 超級快 fsck:check and repair a Linux file system -t fstype:指明檔案系統型別; fsck -t ext4 = fsck.ext4 -a:無須互動而自動修復所有錯誤; -r:互動式修復; eg: e2fsck -v -y -b 163840 /dev/sdb7 當fsck修復不了時,使用該命令修復
4.檢視檔案系統屬性
dumpe2fs, tune2fs ,xfs_info -
掛載
建立好檔案系統後要使用先得掛載:根檔案系統這外的其它檔案系統要想能夠被訪問,都必須通過“關聯”至根檔案系統上的某個目錄來實現,此關聯操作即為“掛載”;此目錄即為“掛載點”;
1.掛載點:
事先必須存在
該使用未被或不會被其它程序使用到的目錄
工作目錄非空,原有檔案會被覆蓋隱藏
2.檢視當前掛載情況
mount
cat /etc/mtab
cat /proc/mounts3.mount掛載命令使用
mount [-nrw] [-t vfstype] [-o options] device dir命令選項: -r:readonly,只讀掛載; -w:read and write, 讀寫掛載; -n:預設裝置掛載或解除安裝的操作會同步更新至/etc/mtab檔案中;-n用於禁止此特性; -a:讀取/etc/fstab中沒有掛載的裝置, mount all filesystems mentioned in fstab -t vfstype:指明要掛載的裝置上的檔案系統的型別;可省略,會通過blkid來判斷掛載裝置的檔案系統型別; -o options:掛載選項 sync/async:同步/非同步操作; atime/noatime:檔案或目錄在被訪問時是否更新其訪問時間戳; diratime/nodiratime:目錄在被訪問時是否更新其訪問時間戳; remount:重新掛載; acl:支援使用facl功能; # mount -o acl device dir # tune2fs -o acl device mount -o remount,acl /dev/sda3 /mnt ro:只讀 rw:讀寫 dev/nodev:此裝置上是否允許建立裝置檔案; exec/noexec:是否允許執行此裝置上的程式檔案; auto/noauto: user/nouser:是否允許普通使用者掛載此檔案系統; suid/nosuid:是否允許程式檔案上的suid和sgid特殊許可權生效; defaults:Use default options: rw, suid, dev, exec, auto, nouser, async, and relatime.
4.umount解除安裝命令
umount device|mount_point
注:當正在被某程序訪問時無法解除安裝
檢視被誰佔用
#lsof mount_point
#fuser -v mount_point
終止程序 訪問:
fuser -km mount_point - 如何開機自動掛載:
把裝置資訊寫入到配置檔案:/etc/fstab
mount -a:可自動掛載定義在此檔案中的所支援自動掛載的裝置# /etc/fstab # Created by anaconda on Fri Jun 1 22:10:52 2018 # # 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=9633c392-6fa0-4e3b-814d-9a2c30c60085 / xfs defaults 0 0 UUID=402d2bec-0b46-4654-8c96-6d2ca74e5668 /boot btrfs subvol=boot 0 0 UUID=9bf9acd8-1001-4eb1-b3a6-464afb90645d swap swap defaults 0 0 一共6個欄位: 1.要掛載的裝置,最好使用裝置的UUID 2.掛載點,swap型別特殊 3.檔案系統型別 4.掛載選項,如同mount中options 5.轉儲頻率 6.自檢次序:0不檢查,1首先檢查,2次級檢查一般對系統盤做檢查,業務盤不檢查即可,若檢查異常導致系統無法啟動
blkid device #檢視裝置uuid,型別
- VMware新增一塊新磁碟,不重啟,fdisk -l沒有顯示。
1.檢視主機匯流排號
[email protected] /]# ls /sys/class/scsi_host/
host0 host1 host2
2.重新掃描SCSI匯流排來新增裝置
[[email protected] /]# echo "- - -" > /sys/class/scsi_host/host0/scan
[[email protected] /]# echo "- - -" > /sys/class/scsi_host/host1/scan
[[email protected] /]# echo "- - -" > /sys/class/scsi_host/host2/scan
3.重新檢視
[[email protected] ~]# fdisk -l
Disk /dev/sda: 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: 0x00036ca6
Device Boot Start End Blocks Id System
/dev/sda1 2048 20973567 10485760 83 Linux
/dev/sda2 20973568 25167871 2097152 82 Linux swap / Solaris
/dev/sda3 25167872 25692159 262144 83 Linux
Disk /dev/sdb: 3221.2 GB, 3221225472000 bytes, 6291456000 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