linux磁盘命令-gdisk,fdisk,mkfs,xfs_info,dumpe2fs

2018-05-28 18:39:06

gdisk是用来对gpt磁盘分区格式进行分区的。对磁盘格式不了解的可以阅读《磁盘分区格式mbr、gpt》

[root@192 ~]# lsblk
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda      8:0    0   20G  0 disk 
├─sda1   8:1    0    1G  0 part /boot
├─sda2   8:2    0    2G  0 part [SWAP]
└─sda3   8:3    0   17G  0 part /
sdb      8:16   0   20G  0 disk 
sr0     11:0    1 1024M  0 rom

我们对 /dev/sdb 来进行分区,分区完成之后可以使用如下命令来查看分区信息。

gdisk -l /dev/sdb
[root@192 ~]# gdisk /dev/sdb
GPT fdisk (gdisk) version 0.8.6

Partition table scan:
  MBR: not present
  BSD: not present
  APM: not present
  GPT: not present

Creating new GPT entries.

Command (? for help): ?     
b	back up GPT data to a file
c	change a partition's name
d	删除分区
i	show detailed information on a partition
l	list known partition types
n	新增分区
o	create a new empty GUID partition table (GPT)
p	打印分区表信息
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

输入gdisk /dev/sdb后我们输入?就会有各种命令提示。

假设我们要分出
4GB 的 xfs 文件系统 (Linux)
4GB 的 vfat 文件系统 (Windows)
1GB 的 swap (Linux swap) (这个分区用来测试删除删除)

Command (? for help): n
Partition number (1-128, default 1): 
First sector (34-41943006, default = 2048) or {+-}size{KMGTP}: 
Last sector (2048-41943006, default = 41943006) or {+-}size{KMGTP}: +4G
Current type is 'Linux filesystem'
Hex code or GUID (L to show codes, Enter = 8300): L     这里用来显示所有支持的类型
0700 Microsoft basic data  0c01 Microsoft reserved    2700 Windows RE          
4200 Windows LDM data      4201 Windows LDM metadata  7501 IBM GPFS            
7f00 ChromeOS kernel       7f01 ChromeOS root         7f02 ChromeOS reserved   
8200 Linux swap            8300 Linux filesystem      8301 Linux reserved      
8e00 Linux LVM             a500 FreeBSD disklabel     a501 FreeBSD boot        
a502 FreeBSD swap          a503 FreeBSD UFS           a504 FreeBSD ZFS         
a505 FreeBSD Vinum/RAID    a580 Midnight BSD data     a581 Midnight BSD boot   
a582 Midnight BSD swap     a583 Midnight BSD UFS      a584 Midnight BSD ZFS    
a585 Midnight BSD Vinum    a800 Apple UFS             a901 NetBSD swap         
a902 NetBSD FFS            a903 NetBSD LFS            a904 NetBSD concatenated 
a905 NetBSD encrypted      a906 NetBSD RAID           ab00 Apple boot          
af00 Apple HFS/HFS+        af01 Apple RAID            af02 Apple RAID offline  
af03 Apple label           af04 AppleTV recovery      af05 Apple Core Storage  
be00 Solaris boot          bf00 Solaris root          bf01 Solaris /usr & Mac Z
bf02 Solaris swap          bf03 Solaris backup        bf04 Solaris /var        
bf05 Solaris /home         bf06 Solaris alternate se  bf07 Solaris Reserved 1  
bf08 Solaris Reserved 2    bf09 Solaris Reserved 3    bf0a Solaris Reserved 4  
bf0b Solaris Reserved 5    c001 HP-UX data            c002 HP-UX service       
ed00 Sony system partitio  ef00 EFI System            ef01 MBR partition scheme
ef02 BIOS boot partition   fb00 VMWare VMFS           fb01 VMWare reserved     
fc00 VMWare kcore crash p  fd00 Linux RAID            
Hex code or GUID (L to show codes, Enter = 8300): 
Changed type of partition to 'Linux filesystem'

Command (? for help): p
Disk /dev/sdb: 41943040 sectors, 20.0 GiB
Logical sector size: 512 bytes
Disk identifier (GUID): 3CFD6949-953A-4397-8665-2E10DAD8D269
Partition table holds up to 128 entries
First usable sector is 34, last usable sector is 41943006
Partitions will be aligned on 2048-sector boundaries
Total free space is 33554365 sectors (16.0 GiB)

Number  Start (sector)    End (sector)  Size       Code  Name
   1            2048         8390655   4.0 GiB     8300  Linux filesystem

Command (? for help): n
Partition number (2-128, default 2): 
First sector (34-41943006, default = 8390656) or {+-}size{KMGTP}: 
Last sector (8390656-41943006, default = 41943006) or {+-}size{KMGTP}: +4G
Current type is 'Linux filesystem'
Hex code or GUID (L to show codes, Enter = 8300): 0700
Changed type of partition to 'Microsoft basic data'

Command (? for help): p
Disk /dev/sdb: 41943040 sectors, 20.0 GiB
Logical sector size: 512 bytes
Disk identifier (GUID): 3CFD6949-953A-4397-8665-2E10DAD8D269
Partition table holds up to 128 entries
First usable sector is 34, last usable sector is 41943006
Partitions will be aligned on 2048-sector boundaries
Total free space is 25165757 sectors (12.0 GiB)

Number  Start (sector)    End (sector)  Size       Code  Name
   1            2048         8390655   4.0 GiB     8300  Linux filesystem
   2         8390656        16779263   4.0 GiB     0700  Microsoft basic data

Command (? for help): n
Partition number (3-128, default 3): 
First sector (34-41943006, default = 16779264) or {+-}size{KMGTP}: 
Last sector (16779264-41943006, default = 41943006) or {+-}size{KMGTP}: +1G  
Current type is 'Linux filesystem'
Hex code or GUID (L to show codes, Enter = 8300): 8200
Changed type of partition to 'Linux swap'

Command (? for help): w    如果输入q代表,不保存。

Final checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING
PARTITIONS!!

Do you want to proceed? (Y/N): y
OK; writing new GUID partition table (GPT) to /dev/sdb.
The operation has completed successfully

分区完毕,我们看看结果

[root@192 ~]# lsblk
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda      8:0    0   20G  0 disk 
├─sda1   8:1    0    1G  0 part /boot
├─sda2   8:2    0    2G  0 part [SWAP]
└─sda3   8:3    0   17G  0 part /
sdb      8:16   0   20G  0 disk 
├─sdb1   8:17   0    4G  0 part 
├─sdb2   8:18   0    4G  0 part 
└─sdb3   8:19   0    1G  0 part 
sr0     11:0    1 1024M  0 rom

有时候,如果我们在当前使用中的磁盘进行,分区,那么分区完毕后,并没有更新到内核,此时可以用partprobe -s来更新内核分区表。此处不需要执行。

接下来我们删除掉一个分区

[root@192 ~]# gdisk /dev/sdb
GPT fdisk (gdisk) version 0.8.6

Partition table scan:
  MBR: protective
  BSD: not present
  APM: not present
  GPT: present

Found valid GPT with protective MBR; using GPT.

Command (? for help): p
Disk /dev/sdb: 41943040 sectors, 20.0 GiB
Logical sector size: 512 bytes
Disk identifier (GUID): 3CFD6949-953A-4397-8665-2E10DAD8D269
Partition table holds up to 128 entries
First usable sector is 34, last usable sector is 41943006
Partitions will be aligned on 2048-sector boundaries
Total free space is 23068605 sectors (11.0 GiB)

Number  Start (sector)    End (sector)  Size       Code  Name
   1            2048         8390655   4.0 GiB     8300  Linux filesystem
   2         8390656        16779263   4.0 GiB     0700  Microsoft basic data
   3        16779264        18876415   1024.0 MiB  8200  Linux swap

Command (? for help): d
Partition number (1-3): 3

Command (? for help): w

Final checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING
PARTITIONS!!

Do you want to proceed? (Y/N): y
OK; writing new GUID partition table (GPT) to /dev/sdb.
The operation has completed successfully

这样,/dev/sdb3就被删除了。

fdisk是用来做传统的mbr格式的分区,现在新版本也能进行gpt分区,但是还处于测试阶段,不建议用来做gpt分区。用法跟gdisk差不多,这里就不讲解了。

fdisk /dev/sdb
WARNING: fdisk GPT support is currently new, and therefore in an experimental phase. Use at your own discretion.
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
   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
   q   quit without saving changes
   s   create a new empty Sun disklabel
   w   write table to disk and exit


磁盘格式化

分区完成之后肯定需要对分区进行格式化后才能使用,格式化指令集mkfs,这个指令其实是个综合的指令,他会去调用正确的文件系统格式化工具软件。CentOS 7 使用 xfs 作为默认文件系统, 下面我们会先介绍 mkfs.xfs

[root@study ~]# mkfs.xfs [-b bsize] [-d parms] [-i parms] [-l parms] [-L label] [-f] \
[-r parms] 设备名称
选项与参数:
关於单位:下面只要谈到“数值”时,没有加单位则为 Bytes 值,可以用 k,m,g,t,p (小写) 等来解释
比较特殊的是 s 这个单位,它指的是 sector 的“个数”喔!
-b :后面接的是 block 容量,可由 512 到 64k,不过最大容量限制为 Linux 的 4k 喔!
-d :后面接的是重要的 data section 的相关参数值,主要的值有:
agcount=数值 :设置需要几个储存群组的意思(AG) ,通常与 CPU 有关
agsize=数值 :每个 AG 设置为多少容量的意思,通常 agcount/agsize 只选一个设置即可
file :指的是“格式化的设备是个文件而不是个设备”的意思!(例如虚拟磁盘)
size=数值 :data section 的容量,亦即你可以不将全部的设备容量用完的意思
su=数值 :当有 RAID 时,那个 stripe 数值的意思,与下面的 sw 搭配使用
sw=数值 :当有 RAID 时,用于储存数据的磁盘数量(须扣除备份碟与备用碟)
sunit=数值 :与 su 相当,不过单位使用的是“几个 sector(512Bytes大小) ”的意思
swidth=数值 :就是 su*sw 的数值,但是以“几个 sector(512Bytes大小) ”来设置
-f :如果设备内已经有文件系统,则需要使用这个 -f 来强制格式化才行!
-i :与 inode 有较相关的设置,主要的设置值有:
size=数值 :最小是 256Bytes 最大是 2k,一般保留 256 就足够使用了!
internal=[0|1]:log 设备是否为内置?默认为 1 内置,如果要用外部设备,使用下面设置
logdev=device :log 设备为后面接的那个设备上头的意思,需设置 internal=0 才可!
size=数值 :指定这块登录区的容量,通常最小得要有 512 个 block,大约 2M 以上才行!
-L :后面接这个文件系统的标头名称 Label name 的意思!
-r :指定 realtime section 的相关设置值,常见的有:
extsize=数值 :就是那个重要的 extent 数值,一般不须设置,但有 RAID 时,
最好设置与 swidth 的数值相同较佳!最小为 4K 最大为 1G 。

一般情况下,我们默认格式化磁盘就行了。

[root@192 ~]# mkfs.xfs /dev/sdb1
meta-data=/dev/sdb1              isize=512    agcount=4, agsize=262144 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0, sparse=0
data     =                       bsize=4096   blocks=1048576, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
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

有其他额外想要处理的项目,才需要加上一堆设置值!举例来说,因为 xfs 可以使用多个数据流来读写系统,以增加速度,因此那个 agcount 可以跟 CPU 的核心数来做搭配!举例来说,如果我的服务器仅有一 颗 4 核心,但是有启动 Intel 超线程功能,则系统会仿真出 8 颗 CPU 时,那个 agcount 就可 以设置为 8

mkfs.xfs -f -d agcount=2 /dev/sdb1

磁盘阵列相关的请参考 linux软件磁盘阵列命令-mdadm

xfs_info查看superblock 内容,注意,必须先挂载才能查看。

[root@192 ~]# xfs_info /dev/sdb1
meta-data=/dev/sdb1              isize=512    agcount=4, agsize=262144 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0 spinodes=0
data     =                       bsize=4096   blocks=1048576, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal               bsize=4096   blocks=2560, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0



mkfs.ext4

[root@study ~]# mkfs.ext4 [-b size] [-L label] 设备名称
选项与参数:
-b :设置 block 的大小,有 1K, 2K, 4K 的容量,
-L :后面接这个设备的标头名称。
mkfs.ext4 /dev/sdb1
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
262144 inodes, 1048576 blocks
52428 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=1073741824
32 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks: 
	32768, 98304, 163840, 229376, 294912, 819200, 884736

Allocating group tables: done                            
Writing inode tables: done                            
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done
[root@192 ~]# dumpe2fs -h /dev/sdb1
dumpe2fs 1.42.9 (28-Dec-2013)
Filesystem volume name:   <none>
Last mounted on:          <not available>
Filesystem UUID:          c2c1994f-6983-4aaa-a908-4a10d99177a3
...
Free blocks:              995250
Free inodes:              262133
...
Journal backup:           inode blocks
Journal features:         (none)
Journal size:             128M
Journal length:           32768
Journal sequence:         0x00000001
Journal start:            0

dumpe2fs -h是用来列出superblock相关的信息。 mkfs 其实是个综合指令而已,当我们使用 mkfs -t xfs 时,它就会跑去找 mkfs.xfs 相关的参数 给我们使用,我们可以看看,系统支持哪些文件系统格式。

[root@192 ~]# mkfs[tab][tab]
mkfs         mkfs.cramfs  mkfs.ext3    mkfs.fat     mkfs.msdos   mkfs.xfs     
mkfs.btrfs   mkfs.ext2    mkfs.ext4    mkfs.minix   mkfs.vfat

格式化为vfat

[root@192 ~]# mkfs -t vfat /dev/sdb2
mkfs.fat 3.0.20 (12 Jun 2013)


备注:
1.本系列命令都在centos7里测试,其他发行版如ubuntu、debian、fedora、opensuse等可能略微不同
2.觉得本文有帮助的小伙伴可以关注笔者
3.原文地址http://www.freecls.com/a/2712/3c  

©著作权归作者所有
收藏
推荐阅读
简介
天降大任于斯人也,必先苦其心志。