磁盘分区和格式化操作是比较接近硬件底层的操作,这两个操作也是至关重要的,决定了磁盘的使用效率和磁盘的数据安全。(相信现在应该没有什么特别的需要,一般都已经抛弃了ext2,ext4文件格式了吧~~~~,ext2,ext3,ext4,xfs ,等等文件系统格式的优缺点不在本文讨论范围,有想了解的百度即可啦)

本文也不讨论Windows的文件系统格式,比如什么DOS,fat16,fat32,ntfs这些啦,主要的是想说一下Linux下的分区表格式,那就是MBR和GPT和loop。

有些同学会奇怪,怎么还有一个loop,这里需要申明,loop是表示整盘分区的,意思就是整个硬盘作为一个独立的分区,比如,一个新的硬盘插到主板上后,Linux系统开机后,如果不出意外,将会lsblk命令可以看到,此时,不使用fdisk或者parted,gparted等分区格式化工具,直接 格式化整个硬盘,格式化完成后,此时的硬盘分区就是loop了。

假设我有一块新的硬盘被Linux系统识别为了sde, 那么,我只执行 mkfs.xfs /dev/sde 这个格式化命令,此时,这个硬盘就是loop分区格式啦!!!!!!!!~~~~~

下面将仔细讲解。




分区所使用的命令主要为fdisk和parted,查看磁盘的详细情况的命令主要为lsblk,blkid,df   这三个命令。

  1. lsblk

盲猜这个命令是 list status block的缩写,当然, 这么理解也是会更记忆深刻:列出所有块设备的状态。用于列出所有可用块设备的信息,而且还能显示他们之间的依赖关系,但是它不会列出RAM盘的信息。块设备有硬盘,闪存盘,cd-ROM等等。lsblk命令包含在util-linux-ng包中,现在该包改名为util-linux。这个包带了几个其它工具,如dmesg。要安装lsblk,请在此处下载util-linux包。如果没有此命令, yum install util-linux-ng来安装该包即可。比如,我的虚拟机的所有磁盘信息是这样的:

[root@hdp-1 ~]# lsblk
NAME            MAJ:MIN RM    SIZE RO TYPE MOUNTPOINT
sda               8:0    0    100G  0 disk 
├─sda1            8:1    0    500M  0 part /boot
├─sda2            8:2    0   99.5G  0 part 
│ ├─centos-root 253:0    0     65G  0 lvm  /
│ ├─centos-swap 253:1    0    4.5G  0 lvm  [SWAP]
│ ├─centos-var  253:2    0     20G  0 lvm  /var
│ └─centos-home 253:3    0     10G  0 lvm  /home
├─sda3            8:3    0 1023.5K  0 part 
└─sda4            8:4    0    512B  0 part 
sdb               8:16   0     22G  0 disk /mnt/newdata
sdc               8:32   0    3.7T  0 disk /opt/data
sr0              11:0    1    4.4G  0 rom  

这里,表示有三个硬盘,sda,sdb,sdc,容量分别为100G,22G,3.7T,sda这个硬盘划分了两个分区,分别为sda1和sda2,sda2又是使用的lvm格式,并且每个分区的挂载点也列出来了,sda3和sda4容量基本可以忽略,并且没有进行挂载。sr0表示光驱,也没有挂载。

    2. blkid

该命令显示所有已经格式化的分区详细信息,其中有UUID,该ID为系统随机分配的固定的唯一标识,因此,我们挂载的时候应该使用UUID写入/etc/fstab ,即使硬盘挂载顺序有变,依然保证挂载的有效(比如,我有很多硬盘,然后我把一个新的硬盘插到了某个旧的原有的硬盘位,而旧有的硬盘换位置啦,这个时候,可能sdb 就会变成sdd或者别的名字了,如果/etc/fstab 里写的是 诸如 /dev/sdb 这么固定的,那么,挂载必定会出错啦~~!!~~~~!!~~~~!!)

[root@hdp-1 ~]# blkid
/dev/sda1: UUID="e58de036-fbe2-4992-a1bc-00a7376febf0" TYPE="xfs" 
/dev/sda2: UUID="oYYZPi-rGCZ-3ujF-CtkU-TB6h-qU03-kvkQjL" TYPE="LVM2_member" 
/dev/sdb: UUID="0fe31ddd-9388-4b5f-ab21-28d6190eed94" TYPE="ext4" 
/dev/sr0: UUID="2019-09-11-18-50-31-00" LABEL="CentOS 7 x86_64" TYPE="iso9660" PTTYPE="dos" 
/dev/sdc: UUID="42cf4e54-fd20-4956-9883-3688d27001d0" TYPE="ext4" 
/dev/mapper/centos-root: UUID="05415307-263e-4430-9a13-88ae9fc9f0d9" TYPE="xfs" 
/dev/mapper/centos-swap: UUID="9f28e7d2-5799-499c-98e1-f9cd1aeccd56" TYPE="swap" 
/dev/mapper/centos-var: UUID="afd83464-82b4-4693-a86b-19d24dc5d4af" TYPE="xfs" 
/dev/mapper/centos-home: UUID="7ef6f1af-c6a1-445e-93a8-560da49274f1" TYPE="xfs" 

以上显示我所有的格式化过的分区,每个分区的文件系统类型也标识了出来,比如,/dev/sdb: UUID="0fe31ddd-9388-4b5f-ab21-28d6190eed94" TYPE="ext4" 表示 sdb这个硬盘格式化成了ext4文件系统,这个命令的用处就是查看并校验我们是否格式化成功的命令

    3. df

df 是一个古老的命令,该命令会显示所有已挂载到系统内的分区的使用情况,

[root@hdp-1 ~]# df -ah
Filesystem               Size  Used Avail Use% Mounted on
sysfs                       0     0     0    - /sys
proc                        0     0     0    - /proc
devtmpfs                 3.9G     0  3.9G   0% /dev
securityfs                  0     0     0    - /sys/kernel/security
tmpfs                    3.9G     0  3.9G   0% /dev/shm
devpts                      0     0     0    - /dev/pts
tmpfs                    3.9G  9.0M  3.9G   1% /run
tmpfs                    3.9G     0  3.9G   0% /sys/fs/cgroup
cgroup                      0     0     0    - /sys/fs/cgroup/systemd
pstore                      0     0     0    - /sys/fs/pstore
cgroup                      0     0     0    - /sys/fs/cgroup/net_cls,net_prio
cgroup                      0     0     0    - /sys/fs/cgroup/blkio
cgroup                      0     0     0    - /sys/fs/cgroup/devices
cgroup                      0     0     0    - /sys/fs/cgroup/pids
cgroup                      0     0     0    - /sys/fs/cgroup/cpuset
cgroup                      0     0     0    - /sys/fs/cgroup/memory
cgroup                      0     0     0    - /sys/fs/cgroup/freezer
cgroup                      0     0     0    - /sys/fs/cgroup/hugetlb
cgroup                      0     0     0    - /sys/fs/cgroup/cpu,cpuacct
cgroup                      0     0     0    - /sys/fs/cgroup/perf_event
configfs                    0     0     0    - /sys/kernel/config
/dev/mapper/centos-root   65G  1.5G   64G   3% /
systemd-1                   0     0     0    - /proc/sys/fs/binfmt_misc
mqueue                      0     0     0    - /dev/mqueue
debugfs                     0     0     0    - /sys/kernel/debug
hugetlbfs                   0     0     0    - /dev/hugepages
/dev/sdb                  22G   45M   21G   1% /mnt/newdata
/dev/sdc                 3.7T   89M  3.5T   1% /opt/data
/dev/mapper/centos-home   10G   33M   10G   1% /home
/dev/sda1                497M  130M  368M  26% /boot
/dev/mapper/centos-var    20G  4.6G   16G  23% /var
tmpfs                    799M     0  799M   0% /run/user/0

这里比较特殊的是cgroup文件系统,它的定义大概是这样的(docker和cgroup是深度绑定的,因此,在这里稍微提一下。):

Linux CGroup全称Linux Control Group, 是Linux内核的一个功能,用来限制,控制与分离一个进程组群的资源(如CPU、内存、磁盘输入输出等)。这个项目最早是由Google的工程师在2006年发起(主要是Paul Menage和Rohit Seth),最早的名称为进程容器(process containers)。在2007年时,因为在Linux内核中,容器(container)这个名词太过广泛,为避免混乱,被重命名为cgroup,并且被合并到2.6.24版的内核中去。然后,其它开始了他的发展。 

   Linux CGroupCgroup 可​​​让​​​您​​​为​​​系​​​统​​​中​​​所​​​运​​​行​​​任​​​务​​​(进​​​程​​​)的​​​用​​​户​​​定​​​义​​​组​​​群​​​分​​​配​​​资​​​源​​​ — 比​​​如​​​ CPU 时​​​间​​​、​​​系​​​统​​​内​​​存​​​、​​​网​​​络​​​带​​​宽​​​或​​​者​​​这​​​些​​​资​​​源​​​的​​​组​​​合​​​。​​​您​​​可​​​以​​​监​​​控​​​您​​​配​​​置​​​的​​​ cgroup,拒​​​绝​​​ cgroup 访​​​问​​​某​​​些​​​资​​​源​​​,甚​​​至​​​在​​​运​​​行​​​的​​​系​​​统​​​中​​​动​​​态​​​配​​​置​​​您​​​的​​​ cgroup。

示例

这里这个示例我是使用的vm虚拟机,操作系统是centos7.4,总共挂载了三块硬盘,硬盘的情况如下:

[root@hdp-1 ~]# lsblk
NAME            MAJ:MIN RM    SIZE RO TYPE MOUNTPOINT
sda               8:0    0    100G  0 disk 
├─sda1            8:1    0    500M  0 part /boot
├─sda2            8:2    0   99.5G  0 part 
│ ├─centos-root 253:0    0     65G  0 lvm  /
│ ├─centos-swap 253:1    0    4.5G  0 lvm  [SWAP]
│ ├─centos-var  253:2    0     20G  0 lvm  /var
│ └─centos-home 253:3    0     10G  0 lvm  /home
├─sda3            8:3    0 1023.5K  0 part 
└─sda4            8:4    0    512B  0 part 
sdb               8:16   0     22G  0 disk /mnt/newdata
sdc               8:32   0    3.7T  0 disk /opt/data
sr0              11:0    1    4.4G  0 rom  

这里提前说一下,我这个操作系统内有三种,sda是MBR分区表形式,sdb和c是loop分区表形式(也就是整盘挂载),GPT分区表后面在实现。

先容我卖一个关子,讲讲怎么看 MBR,GPT和loop的分区表信息吧~~~~~~~




A:查看MBR分区表信息:

dd if=/dev/sda of=mbr.hex bs=512 count=1

 前面已经说了sda是MBR分区表,以上命令是复制备份分区表信息,将信息存放到名为mbr.hex这个文件内的,查看该文件的内容是如下命令:

hexdump -C mbr.hex

文件内容如下:

[root@hdp-1 ~]# !80
hexdump -C mbr.hex 
00000000  eb 63 90 10 8e d0 bc 00  b0 b8 00 00 8e d8 8e c0  |.c..............|
00000010  fb be 00 7c bf 00 06 b9  00 02 f3 a4 ea 21 06 00  |...|.........!..|
00000020  00 be be 07 38 04 75 0b  83 c6 10 81 fe fe 07 75  |....8.u........u|
00000030  f3 eb 16 b4 02 b0 01 bb  00 7c b2 80 8a 74 01 8b  |.........|...t..|
00000040  4c 02 cd 13 ea 00 7c 00  00 eb fe 00 00 00 00 00  |L.....|.........|
00000050  00 00 00 00 00 00 00 00  00 00 00 80 01 00 00 00  |................|
00000060  00 00 00 00 ff fa 90 90  f6 c2 80 74 05 f6 c2 70  |...........t...p|
00000070  74 02 b2 80 ea 79 7c 00  00 31 c0 8e d8 8e d0 bc  |t....y|..1......|
00000080  00 20 fb a0 64 7c 3c ff  74 02 88 c2 52 be 05 7c  |. ..d|<.t...R..||
00000090  b4 41 bb aa 55 cd 13 5a  52 72 3d 81 fb 55 aa 75  |.A..U..ZRr=..U.u|
000000a0  37 83 e1 01 74 32 31 c0  89 44 04 40 88 44 ff 89  |7...t21..D.@.D..|
000000b0  44 02 c7 04 10 00 66 8b  1e 5c 7c 66 89 5c 08 66  |D.....f..\|f.\.f|
000000c0  8b 1e 60 7c 66 89 5c 0c  c7 44 06 00 70 b4 42 cd  |..`|f.\..D..p.B.|
000000d0  13 72 05 bb 00 70 eb 76  b4 08 cd 13 73 0d 5a 84  |.r...p.v....s.Z.|
000000e0  d2 0f 83 de 00 be 85 7d  e9 82 00 66 0f b6 c6 88  |.......}...f....|
000000f0  64 ff 40 66 89 44 04 0f  b6 d1 c1 e2 02 88 e8 88  |d.@f.D..........|
00000100  f4 40 89 44 08 0f b6 c2  c0 e8 02 66 89 04 66 a1  |.@.D.......f..f.|
00000110  60 7c 66 09 c0 75 4e 66  a1 5c 7c 66 31 d2 66 f7  |`|f..uNf.\|f1.f.|
00000120  34 88 d1 31 d2 66 f7 74  04 3b 44 08 7d 37 fe c1  |4..1.f.t.;D.}7..|
00000130  88 c5 30 c0 c1 e8 02 08  c1 88 d0 5a 88 c6 bb 00  |..0........Z....|
00000140  70 8e c3 31 db b8 01 02  cd 13 72 1e 8c c3 60 1e  |p..1......r...`.|
00000150  b9 00 01 8e db 31 f6 bf  00 80 8e c6 fc f3 a5 1f  |.....1..........|
00000160  61 ff 26 5a 7c be 80 7d  eb 03 be 8f 7d e8 34 00  |a.&Z|..}....}.4.|
00000170  be 94 7d e8 2e 00 cd 18  eb fe 47 52 55 42 20 00  |..}.......GRUB .|
00000180  47 65 6f 6d 00 48 61 72  64 20 44 69 73 6b 00 52  |Geom.Hard Disk.R|
00000190  65 61 64 00 20 45 72 72  6f 72 0d 0a 00 bb 01 00  |ead. Error......|
000001a0  b4 0e cd 10 ac 3c 00 75  f4 c3 00 00 00 00 00 00  |.....<.u........|
000001b0  00 00 00 00 00 00 00 00  e3 42 0b 00 00 00 80 20  |.........B..... |
000001c0  21 00 83 dd 1e 3f 00 08  00 00 00 a0 0f 00 00 dd  |!....?..........|
000001d0  1f 3f 8e fe ff ff 00 a8  0f 00 00 40 70 0c 00 00  |.?.........@p...|
000001e0  02 00 83 20 20 00 01 00  00 00 ff 07 00 00 00 fe  |...  ...........|
000001f0  ff ff 83 fe ff ff 00 e8  7f 0c 01 00 00 00 55 aa  |..............U.|
00000200

 在末尾,可以看到mbr的标志:55aa,也就是说只要看到55aa,我们就应该可以确定,这个硬盘是MBR分区表形式啦。55aa是硬盘有效标志,若被篡改则系统认为硬盘损坏.早期一些dos下的病毒喜欢篡改这个标志,造成硬盘损坏,但是分区还在。比较出名的此类病毒比如熊猫烧香就是更改55aa标志啦。

B:

GPT分区表的查看

dd if=/dev/sdc of=gpt.hex bs=512 count=32

gpt标准的结构是32个扇区,所以这里要操作32次,即依次读取第一块硬盘的0~31扇区。

读取二进制文件的内容命令为:

hexdump -C gpt.hex

GPT暂时没有,后面补充该文件的内容

首先开头的是一个保护性的MBR,为的是兼容,和早期的MBR类似,以55AA作为结束。

接下来是GPT头,以UEFI PART作为头标识字符。

C:

loop分区表的查看

dd if=/dev/sdb of=loop.hex bs=512 count=1

文件内容如下: 

[root@hdp-1 ~]# !82
hexdump -C loop.hex 
00000000  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00000200

在查看sdc的分区表,发现内容和sdb是一样的:

[root@hdp-1 ~]# dd if=/dev/sdc of=sdc.hex bs=512 count=1 
1+0 records in
1+0 records out
512 bytes (512 B) copied, 9.6962e-05 s, 5.3 MB/s
[root@hdp-1 ~]# hexdump -C sdc.hex 
00000000  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00000200

 有一个有意思的事情,我们如果查看光驱,也就是sr0的分区表 ,将会看到MBR分区表的标识,但是,你可以发现,后面的几行全是0

[root@hdp-1 ~]# dd if=/dev/sr0 of=sr0.hex bs=512 count=1 
1+0 records in
1+0 records out
512 bytes (512 B) copied, 0.000509761 s, 1.0 MB/s
[root@hdp-1 ~]# hexdump -C sr0.hex 
00000000  33 ed 90 90 90 90 90 90  90 90 90 90 90 90 90 90  |3...............|
00000010  90 90 90 90 90 90 90 90  90 90 90 90 90 90 90 90  |................|
00000020  33 ed fa 8e d5 bc 00 7c  fb fc 66 31 db 66 31 c9  |3......|..f1.f1.|
00000030  66 53 66 51 06 57 8e dd  8e c5 52 be 00 7c bf 00  |fSfQ.W....R..|..|
00000040  06 b9 00 01 f3 a5 ea 4b  06 00 00 52 b4 41 bb aa  |.......K...R.A..|
00000050  55 31 c9 30 f6 f9 cd 13  72 16 81 fb 55 aa 75 10  |U1.0....r...U.u.|
00000060  83 e1 01 74 0b 66 c7 06  f1 06 b4 42 eb 15 eb 00  |...t.f.....B....|
00000070  5a 51 b4 08 cd 13 83 e1  3f 5b 51 0f b6 c6 40 50  |ZQ......?[Q...@P|
00000080  f7 e1 53 52 50 bb 00 7c  b9 04 00 66 a1 b0 07 e8  |..SRP..|...f....|
00000090  44 00 0f 82 80 00 66 40  80 c7 02 e2 f2 66 81 3e  |D.....f@.....f.>|
000000a0  40 7c fb c0 78 70 75 09  fa bc ec 7b ea 44 7c 00  |@|..xpu....{.D|.|
000000b0  00 e8 83 00 69 73 6f 6c  69 6e 75 78 2e 62 69 6e  |....isolinux.bin|
000000c0  20 6d 69 73 73 69 6e 67  20 6f 72 20 63 6f 72 72  | missing or corr|
000000d0  75 70 74 2e 0d 0a 66 60  66 31 d2 66 03 06 f8 7b  |upt...f`f1.f...{|
000000e0  66 13 16 fc 7b 66 52 66  50 06 53 6a 01 6a 10 89  |f...{fRfP.Sj.j..|
000000f0  e6 66 f7 36 e8 7b c0 e4  06 88 e1 88 c5 92 f6 36  |.f.6.{.........6|
00000100  ee 7b 88 c6 08 e1 41 b8  01 02 8a 16 f2 7b cd 13  |.{....A......{..|
00000110  8d 64 10 66 61 c3 e8 1e  00 4f 70 65 72 61 74 69  |.d.fa....Operati|
00000120  6e 67 20 73 79 73 74 65  6d 20 6c 6f 61 64 20 65  |ng system load e|
00000130  72 72 6f 72 2e 0d 0a 5e  ac b4 0e 8a 3e 62 04 b3  |rror...^....>b..|
00000140  07 cd 10 3c 0a 75 f1 cd  18 f4 eb fd 00 00 00 00  |...<.u..........|
00000150  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
000001b0  74 4d 00 00 00 00 00 00  00 33 53 08 00 00 80 00  |tM.......3S.....|
000001c0  01 00 00 3f e0 ff 00 00  00 00 00 00 8b 00 00 fe  |...?............|
000001d0  ff ff ef fe ff ff 74 09  00 00 00 44 00 00 00 00  |......t....D....|
000001e0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
000001f0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 55 aa  |..............U.|
00000200



上面我们是通过复制备份硬盘的分区表,根据它自己的特点,可以看出是哪一类的分区,那有没有更快速有效的方法呢?当然有了,这时候就需要 fdisk和parted命令啦;

一,fdisk

古老的硬盘分区程序,Linux自带的,只是这个程序只支持MBR分区,并且该命令是修改内存的,也就是说不按w键,将不会对任何改动生效

例如,我将sdc这个硬盘转换成GPT后,使用命令 fdisk /dev/sdc  将会出现如下输出,

[root@hdp-1 ~]# fdisk /dev/sdc
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): 

 如果是MBR,fdisk将不会提示:

[root@hdp-1 ~]# 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):

二,parted命令 

这个命令相比fdisk更新,因此,该程序向下兼容fdisk,也就是说可以分区MBR,也可以分区GPT,此命令是即时生效,这里需要注意,不同于fdisk哦,因此,该命令需要谨慎使用。查看分区类型的命令是 :parted 磁盘名称 print,比如看sdb和sdc

[root@hdp-1 ~]# parted /dev/sdb print
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 23.6GB
Sector size (logical/physical): 512B/512B
Partition Table: loop
Disk Flags: 

Number  Start  End     Size    File system  Flags
 1      0.00B  23.6GB  23.6GB  ext4

[root@hdp-1 ~]# parted /dev/sdc print
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdc: 4080GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: 

Number  Start  End  Size  File system  Name  Flags

sdb是整盘分区,loop,sdc就是GPT啦。

下面的命令是将硬盘分区为GPT : parted /dev/sdc mklabel gpt

其中的Warning: Partition(s) on /dev/sdc are being used.说的是硬盘在使用,如果强行更改分区表,数据将会全部丢失,选择当然是忽略Ignore啦,第二个是问要不要继续,当然是Yes啦。更改完后parted提示你要修改挂载配置文件 /etc/fstab。
Ignore/Cancel? Ignore 
Warning: The existing disk label on /dev/sdc will be destroyed and all data on this disk will be lost. Do you want to continue?
Yes/No? Yes                                                               
Information: You may need to update /etc/fstab

[root@hdp-1 ~]# parted /dev/sdc mklabel gpt
Warning: Partition(s) on /dev/sdc are being used.
Ignore/Cancel? Ignore 
Warning: The existing disk label on /dev/sdc will be destroyed and all data on this disk will be lost. Do you want to continue?
Yes/No? Yes                                                               
Information: You may need to update /etc/fstab.

[root@hdp-1 ~]# parted /dev/sdc
GNU Parted 3.1
Using /dev/sdc
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) print                                                            
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdc: 4080GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: 

Number  Start  End  Size  File system  Name  Flags

以上仅仅是将磁盘sdc的分区表改为了GPT,还需要继续分区,命令是: mkpart mydata xfs

[root@hdp-1 ~]# parted /dev/sdc
GNU Parted 3.1
Using /dev/sdc
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) mkpart mydata xfs
Start? 0                                                                  
End? 20000                                                                
Warning: The resulting partition is not properly aligned for best performance.
Ignore/Cancel? Ignore                                                     
(parted) print
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdc: 4080GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: 

Number  Start   End     Size    File system  Name    Flags
 1      17.4kB  20.0GB  20.0GB               mydata

(parted) quit                                                             
Information: You may need to update /etc/fstab.

通过命令parted mkpart mydata xfs  输入20000(单位是M,mydata是一个自定义的名称,可以随意,但最好有一定的意义,自己清楚比较好哦),我们分了一个20G的区,现在格式化后(命令为mkfs.xfs /dev/sdc1)可以通过命令 blkid看到我们新分出来的这个区啦

为什么是sdc1呢?因为,我们通过lsblk命令可以看到啦!!!!!!!!!

[root@hdp-1 ~]# lsblk
NAME            MAJ:MIN RM    SIZE RO TYPE MOUNTPOINT
sda               8:0    0    100G  0 disk 
├─sda1            8:1    0    500M  0 part /boot
├─sda2            8:2    0   99.5G  0 part 
│ ├─centos-root 253:0    0     65G  0 lvm  /
│ ├─centos-swap 253:1    0    4.5G  0 lvm  [SWAP]
│ ├─centos-var  253:2    0     20G  0 lvm  /var
│ └─centos-home 253:3    0     10G  0 lvm  /home
├─sda3            8:3    0 1023.5K  0 part 
└─sda4            8:4    0    512B  0 part 
sdb               8:16   0     22G  0 disk 
sdc               8:32   0    3.7T  0 disk 
└─sdc1            8:33   0   18.6G  0 part 
sr0              11:0    1    4.4G  0 rom  
[root@hdp-1 ~]# mkfs.xfs /dev/sdc1
meta-data=/dev/sdc1              isize=512    agcount=4, agsize=1220702 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0, sparse=0
data     =                       bsize=4096   blocks=4882808, 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
[root@hdp-1 ~]# blkid
/dev/sda1: UUID="e58de036-fbe2-4992-a1bc-00a7376febf0" TYPE="xfs" 
/dev/sda2: UUID="oYYZPi-rGCZ-3ujF-CtkU-TB6h-qU03-kvkQjL" TYPE="LVM2_member" 
/dev/sdb: UUID="0fe31ddd-9388-4b5f-ab21-28d6190eed94" TYPE="ext4" 
/dev/sr0: UUID="2019-09-11-18-50-31-00" LABEL="CentOS 7 x86_64" TYPE="iso9660" PTTYPE="dos" 
/dev/mapper/centos-root: UUID="05415307-263e-4430-9a13-88ae9fc9f0d9" TYPE="xfs" 
/dev/mapper/centos-swap: UUID="9f28e7d2-5799-499c-98e1-f9cd1aeccd56" TYPE="swap" 
/dev/mapper/centos-var: UUID="afd83464-82b4-4693-a86b-19d24dc5d4af" TYPE="xfs" 
/dev/mapper/centos-home: UUID="7ef6f1af-c6a1-445e-93a8-560da49274f1" TYPE="xfs" 
/dev/sdc1: UUID="fbb45648-2580-43f1-8ea9-f03f1c911787" TYPE="xfs" PARTLABEL="mydata" PARTUUID="2f4a584a-4f87-490d-9ddf-76f407c2c190"

那,现在并不想只有一个分区怎么办呢?光一个sdc1太孤单啦,在分一个区吧!!! 命令是:parted /dev/sdc mkpart mydata2


[root@hdp-1 ~]# parted /dev/sdc mkpart mydata2
File system type?  [ext2]? xfs                                            
Start? 20000                                                              
End? 200000                                                               
Warning: The resulting partition is not properly aligned for best performance.
Ignore/Cancel? Ignore                                                     
Information: You may need to update /etc/fstab.

[root@hdp-1 ~]# parted /dev/sdc print
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdc: 4080GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: 

Number  Start   End     Size    File system  Name     Flags
 1      17.4kB  20.0GB  20.0GB  xfs          mydata
 2      20.0GB  200GB   180GB                mydata2

好啦,现在我们在通过lsblk和blkid看看sdc这个硬盘什么样了吧!!!

[root@hdp-1 ~]# blkid
/dev/sda1: UUID="e58de036-fbe2-4992-a1bc-00a7376febf0" TYPE="xfs" 
/dev/sda2: UUID="oYYZPi-rGCZ-3ujF-CtkU-TB6h-qU03-kvkQjL" TYPE="LVM2_member" 
/dev/sdb: UUID="0fe31ddd-9388-4b5f-ab21-28d6190eed94" TYPE="ext4" 
/dev/sr0: UUID="2019-09-11-18-50-31-00" LABEL="CentOS 7 x86_64" TYPE="iso9660" PTTYPE="dos" 
/dev/mapper/centos-root: UUID="05415307-263e-4430-9a13-88ae9fc9f0d9" TYPE="xfs" 
/dev/mapper/centos-swap: UUID="9f28e7d2-5799-499c-98e1-f9cd1aeccd56" TYPE="swap" 
/dev/mapper/centos-var: UUID="afd83464-82b4-4693-a86b-19d24dc5d4af" TYPE="xfs" 
/dev/mapper/centos-home: UUID="7ef6f1af-c6a1-445e-93a8-560da49274f1" TYPE="xfs" 
/dev/sdc1: UUID="fbb45648-2580-43f1-8ea9-f03f1c911787" TYPE="xfs" PARTLABEL="mydata" PARTUUID="2f4a584a-4f87-490d-9ddf-76f407c2c190" 
/dev/sdc2: PARTLABEL="mydata2" PARTUUID="26b2660d-ef22-4d6c-86f4-f54740828971" 
[root@hdp-1 ~]# lsblk
NAME            MAJ:MIN RM    SIZE RO TYPE MOUNTPOINT
sda               8:0    0    100G  0 disk 
├─sda1            8:1    0    500M  0 part /boot
├─sda2            8:2    0   99.5G  0 part 
│ ├─centos-root 253:0    0     65G  0 lvm  /
│ ├─centos-swap 253:1    0    4.5G  0 lvm  [SWAP]
│ ├─centos-var  253:2    0     20G  0 lvm  /var
│ └─centos-home 253:3    0     10G  0 lvm  /home
├─sda3            8:3    0 1023.5K  0 part 
└─sda4            8:4    0    512B  0 part 
sdb               8:16   0     22G  0 disk 
sdc               8:32   0    3.7T  0 disk 
├─sdc1            8:33   0   18.6G  0 part 
└─sdc2            8:34   0  167.7G  0 part 
sr0              11:0    1    4.4G  0 rom  

可以看到,又分了一个200G的名字叫mydata2的分区。以上分区操作,我们会发现使用M这个单位有点眼花啦,因为4t基本等于4000000M,光数0就很头疼啦,怎么办?可以指定单位是G啦,好了,现在就把所有分的区清理掉,命令是:parted /dev/sdc rm 1   parted   /dev/sdc   rm 2

[root@hdp-1 ~]# parted /dev/sdc rm 1                                      
Information: You may need to update /etc/fstab.

[root@hdp-1 ~]# parted /dev/sdc print
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdc: 4080GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: 

Number  Start  End  Size  File system  Name  Flags

如果你瞎写一个 不存在的分区序号,那么会简单报个错(比如,上面的分区已经删除完了,在执行一次 parted /dev/sdc rm 1):

[root@hdp-1 ~]# parted /dev/sdc rm 1
Error: Partition doesn't exist.

现在开始正式的分区啦,当然,首先我们要看看sdc到底是多大的硬盘,命令如下:

[root@hdp-1 ~]# parted /dev/sdc print
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdc: 4080GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: 

Number  Start  End  Size  File system  Name  Flags

可以看到,是4080G,第一个分区我们要分4t大小,那么,命令是这样的(非交互式的哦,效果是和上面的一样的):

[root@hdp-1 ~]# parted /dev/sdc mkpart mydata xfs 0 4T
Warning: The resulting partition is not properly aligned for best performance.
Ignore/Cancel? Ignore                                                     
Information: You may need to update /etc/fstab.

[root@hdp-1 ~]# parted /dev/sdc print
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdc: 4080GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: 

Number  Start   End     Size    File system  Name    Flags
 1      17.4kB  4080GB  4080GB  xfs          mydata

[root@hdp-1 ~]# blkid
/dev/sda1: UUID="e58de036-fbe2-4992-a1bc-00a7376febf0" TYPE="xfs" 
/dev/sda2: UUID="oYYZPi-rGCZ-3ujF-CtkU-TB6h-qU03-kvkQjL" TYPE="LVM2_member" 
/dev/sdb: UUID="0fe31ddd-9388-4b5f-ab21-28d6190eed94" TYPE="ext4" 
/dev/sr0: UUID="2019-09-11-18-50-31-00" LABEL="CentOS 7 x86_64" TYPE="iso9660" PTTYPE="dos" 
/dev/mapper/centos-root: UUID="05415307-263e-4430-9a13-88ae9fc9f0d9" TYPE="xfs" 
/dev/mapper/centos-swap: UUID="9f28e7d2-5799-499c-98e1-f9cd1aeccd56" TYPE="swap" 
/dev/mapper/centos-var: UUID="afd83464-82b4-4693-a86b-19d24dc5d4af" TYPE="xfs" 
/dev/mapper/centos-home: UUID="7ef6f1af-c6a1-445e-93a8-560da49274f1" TYPE="xfs" 
/dev/sdc1: UUID="fbb45648-2580-43f1-8ea9-f03f1c911787" TYPE="xfs" PARTLABEL="mydata" PARTUUID="07e89474-b2b8-4259-8f99-4233c34dcd2d" 
[root@hdp-1 ~]# lsblk
NAME            MAJ:MIN RM    SIZE RO TYPE MOUNTPOINT
sda               8:0    0    100G  0 disk 
├─sda1            8:1    0    500M  0 part /boot
├─sda2            8:2    0   99.5G  0 part 
│ ├─centos-root 253:0    0     65G  0 lvm  /
│ ├─centos-swap 253:1    0    4.5G  0 lvm  [SWAP]
│ ├─centos-var  253:2    0     20G  0 lvm  /var
│ └─centos-home 253:3    0     10G  0 lvm  /home
├─sda3            8:3    0 1023.5K  0 part 
└─sda4            8:4    0    512B  0 part 
sdb               8:16   0     22G  0 disk 
sdc               8:32   0    3.7T  0 disk 
└─sdc1            8:33   0    3.7T  0 part 
sr0              11:0    1    4.4G  0 rom  

可以看到,非交互式可以直接格式化好分区,交互式并不会。因为4T大于4080G,因此,是全部分完的哦。

再次删除,重新划分分区,这次是3000G和1000G两个分区,那么,命令应该是这样的(这里有技巧的,下一个分区的开始是上一个分区的结束,这里请仔细理解):

[root@hdp-1 ~]# parted /dev/sdc mkpart mydata xfs 0 3000G
Warning: The resulting partition is not properly aligned for best performance.
Ignore/Cancel? Ignore                                                     
Information: You may need to update /etc/fstab.

[root@hdp-1 ~]# parted /dev/sdc print
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdc: 4080GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: 

Number  Start   End     Size    File system  Name    Flags
 1      17.4kB  3000GB  3000GB  xfs          mydata

[root@hdp-1 ~]# parted /dev/sdc mkpart mydata2 xfs 3000G 4000G
Information: You may need to update /etc/fstab.

[root@hdp-1 ~]# parted /dev/sdc print
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdc: 4080GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: 

Number  Start   End     Size    File system  Name     Flags
 1      17.4kB  3000GB  3000GB  xfs          mydata
 2      3000GB  4000GB  1000GB               mydata2

这里需要注意,第二个分区因为分区的时候输入了Ignore,因此,指定的xfs并没有显示,我们需要专门对此分区格式化一下,也就是命令 :

特别注意,这样指定的xfs其实并没有什么卵用,还是需要格式化一下的,因为,你如果不格式化,直接挂载是可以成功的,但df命令显示的容量是不对的。

Number  Start   End     Size    File system  Name     Flags
 1      17.4kB  3000GB  3000GB  xfs          mydata

mkfs.xfs /dev/sdc2   当然,也可以格式化成你想要的其它文件系统,比如ext4,这些都是没有什么问题的。

[root@hdp-1 ~]# mkfs.xfs /dev/sdc2
meta-data=/dev/sdc2              isize=512    agcount=4, agsize=61035136 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0, sparse=0
data     =                       bsize=4096   blocks=244140544, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal log           bsize=4096   blocks=119209, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
[root@hdp-1 ~]# parted /dev/sdc print
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdc: 4080GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: 

Number  Start   End     Size    File system  Name     Flags
 1      17.4kB  3000GB  3000GB  xfs          mydata
 2      3000GB  4000GB  1000GB  xfs          mydata2

通过blkid我们查出sdc1的UUID,然后将这个UUID写入/etc/fstab 进行挂载就可以使用啦,当然,把这个写入/etc/fstab 文件内也是可以接受的---PARTUUID=4618b8db-269e-4255-ab88-e6c9e3c0ad9a

[root@hdp-1 ~]# blkid
/dev/sda1: UUID="e58de036-fbe2-4992-a1bc-00a7376febf0" TYPE="xfs" 
/dev/sda2: UUID="oYYZPi-rGCZ-3ujF-CtkU-TB6h-qU03-kvkQjL" TYPE="LVM2_member" 
/dev/sdb: UUID="0fe31ddd-9388-4b5f-ab21-28d6190eed94" TYPE="ext4" 
/dev/sr0: UUID="2019-09-11-18-50-31-00" LABEL="CentOS 7 x86_64" TYPE="iso9660" PTTYPE="dos" 
/dev/mapper/centos-root: UUID="05415307-263e-4430-9a13-88ae9fc9f0d9" TYPE="xfs" 
/dev/mapper/centos-swap: UUID="9f28e7d2-5799-499c-98e1-f9cd1aeccd56" TYPE="swap" 
/dev/mapper/centos-var: UUID="afd83464-82b4-4693-a86b-19d24dc5d4af" TYPE="xfs" 
/dev/mapper/centos-home: UUID="7ef6f1af-c6a1-445e-93a8-560da49274f1" TYPE="xfs" 
/dev/sdc1: UUID="fbb45648-2580-43f1-8ea9-f03f1c911787" TYPE="xfs" PARTLABEL="mydata" PARTUUID="12843991-0208-4615-9fdc-55d4548fe964" 
/dev/sdc2: UUID="b8ff9af1-49ee-4994-8232-6f61cc0a5291" TYPE="xfs" PARTLABEL="mydata2" PARTUUID="4618b8db-269e-4255-ab88-e6c9e3c0ad9a"


[root@hdp-1 ~]# cat /etc/fstab

#
# /etc/fstab
# Created by anaconda on Wed Jul  7 04:05:33 2021
#
# 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
#
/dev/mapper/centos-root /                       xfs     defaults        0 0
UUID=e58de036-fbe2-4992-a1bc-00a7376febf0 /boot                   xfs     defaults        0 0
/dev/mapper/centos-home /home                   xfs     defaults        0 0
/dev/mapper/centos-var  /var                    xfs     defaults        0 0
/dev/mapper/centos-swap swap                    swap    defaults        0 0
/dev/sdb /mnt/newdata ext4 defaults 0  0
UUID=fbb45648-2580-43f1-8ea9-f03f1c911787 /opt/data xfs defaults 0 0 

格式化后,挂载成功后,df命令将显示实际的准确的容量(文件系统毕竟也是要算容量的啊,当然会小很多啦。):

[root@hdp-1 ~]# df -ah
Filesystem               Size  Used Avail Use% Mounted on

/dev/mapper/centos-root   65G  1.5G   64G   3% /
systemd-1                   -     -     -    - /proc/sys/fs/binfmt_misc
mqueue                      0     0     0    - /dev/mqueue
hugetlbfs                   0     0     0    - /dev/hugepages
debugfs                     0     0     0    - /sys/kernel/debug
/dev/sdb                  22G   45M   21G   1% /mnt/newdata
/dev/sda1                497M  130M  368M  26% /boot
/dev/mapper/centos-home   10G   33M   10G   1% /home
/dev/mapper/centos-var    20G  4.6G   16G  23% /var
tmpfs                    799M     0  799M   0% /run/user/0
binfmt_misc                 0     0     0    - /proc/sys/fs/binfmt_misc
/dev/sdc2                931G   33M  931G   1% /mnt/mydata2
/dev/sdc1                2.7T   89M  2.6T   1% /opt/data



整盘分区也就是loop模式就是直接执行硬盘格式化命令,系统会认为这个硬盘是loop分区表啦,例子如下:

[root@hdp-1 ~]# mkfs.xfs -f /dev/sdc
meta-data=/dev/sdc               isize=512    agcount=4, agsize=249036800 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0, sparse=0
data     =                       bsize=4096   blocks=996147200, imaxpct=5
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal log           bsize=4096   blocks=486400, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
[root@hdp-1 ~]# parted /dev/sdc print
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdc: 4080GB
Sector size (logical/physical): 512B/512B
Partition Table: loop
Disk Flags: 

Number  Start  End     Size    File system  Flags
 1      0.00B  4080GB  4080GB  xfs

Logo

开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!

更多推荐