一、磁盘分区知识点
1.MBR
Master Boot Record 主引导记录
位置
磁盘的开头
0磁头 0磁道 1扇区512字节前446字节
0头 0道 1扇区512字节前446字节
2.分区类型
(1)主分区(primary)
1个主分区占用16字节分区表
在一个磁盘中 最多4个主分区
一般必须要有
(2)扩展分区(extended)
在一个磁盘中最多只能有4个主分区
扩展分区无法直接使用
1个磁盘中只能创建1个扩展分区
数据必须放在扩展分区中的逻辑分区
(3)逻辑分区(logical)
需要先创建扩展分区
存放数据
11个最多
3.分区命名规则
(1)硬盘名字
sata/sas/scsi
/dev/sd
第1块硬盘 --> /dev/sda
第4块硬盘 --> /dev/sdd
云服务器(阿里云)
/dev/vda
/dev/vdb
(2)分区名字
主分区或扩展分区
分区编号:1-4
/dev/vda1 /dev/vda4
逻辑分区
分区编号:从5开始(>=5)
/dev/vda5
习题
第3块sata磁盘的第2个主分区
/dev/sdc2
第6块sas磁盘的第2个逻辑分区
/dev/sdf6
4.分区实战
(1)分区命令
fdisk
-l 查看磁盘分区信息(小写字母L)
fdisk内部命令
p
print显示磁盘分区信息
n
new 创建分区
d
delete 删除磁盘分区
w
write 保存并退出
q或Ctrl+c
退出不保存
t
改变分区类型
parted
(2)创建2个分区 1个10MB的主分区 创建1个使用所有剩余空间的扩展 分区 1个20MB的逻辑分区
[root@wolf1 ~]# 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.
Command (m for help): p
Disk /dev/sdb: 106 MB, 106954752 bytes, 208896 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: 0x6df46197
Device Boot Start End Blocks Id System
#创建10M主分区:
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):
分区号 默认
First sector (2048-208895, default 2048):
磁盘分区中第1个扇区(从哪里开始) 默认
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-208895, default 208895):
磁盘分区中最后1个扇区
Last sector, +sectors or +size{K,M,G} (2048-208895, default 208895): +10M
Partition 1 of type Linux and of size 10 MiB is set
创建了分区号是1的 10MB的分区
Command (m for help): p
Disk /dev/sdb: 106 MB, 106954752 bytes, 208896 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: 0x6df46197
Device Boot Start End Blocks Id System
/dev/sdb1 2048 22527 10240 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):
First sector (22528-208895, default 22528):
Using default value 22528
Last sector, +sectors or +size{K,M,G} (22528-208895, default 208895):
Using default value 208895
Partition 2 of type Extended and of size 91 MiB is set
Command (m for help): p
Disk /dev/sdb: 106 MB, 106954752 bytes, 208896 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: 0xdbb0296d
Device Boot Start End Blocks Id System
/dev/sdb1 2048 22527 10240 83 Linux
/dev/sdb2 22528 208895 93184 5 Extended
#创建逻辑分区
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 (24576-208895, default 24576):
Using default value 24576
Last sector, +sectors or +size{K,M,G} (24576-208895, default 208895): +20M
Partition 5 of type Linux and of size 20 MiB is set
Command (m for help): P
Disk /dev/sdb: 106 MB, 106954752 bytes, 208896 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: 0xdbb0296d
Device Boot Start End Blocks Id System
/dev/sdb1 2048 22527 10240 83 Linux
/dev/sdb2 22528 208895 93184 5 Extended
/dev/sdb5 24576 65535 20480 83 Linux
Command (m for help): W
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
(3)创建1个使用磁盘所有容量的分区 并挂载到/mnt上
第1个里程碑-创建分区
fdisk
保存
第2里程碑-格式化 创建文件系统
mkfs.xfs
第3个里程碑-挂载
卸载umount /mnt
临时挂载
永久挂载
方法1 == /etc/rc.local
方法2 == /etc/fstab 开机自动挂载
- 实施过程
第1个里程碑-创建分区
Command (m for help): p
Disk /dev/sdb: 106 MB, 106954752 bytes, 208896 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: 0x6df46197
Device Boot Start End Blocks Id System
Command (m for help): n
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
Select (default p):
Using default response p
Partition number (1-4, default 1):
First sector (2048-208895, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-208895, default 208895):
Using default value 208895
Partition 1 of type Linux and of size 101 MiB is set
第2个里程碑-格式化 创建文件系统 mkfs make filesystem
[root@wolf ~]# mkfs.xfs /dev/sdb1
meta-data=/dev/sdb1 isize=512 agcount=4, agsize=6464 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0, sparse=0
data = bsize=4096 blocks=25856, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
log =internal log bsize=4096 blocks=855, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
第3个里程碑-挂载
[root@wolf ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda3 99G 1.8G 97G 2% /
devtmpfs 980M 0 980M 0% /dev
tmpfs 991M 0 991M 0% /dev/shm
tmpfs 991M 9.6M 981M 1% /run
tmpfs 991M 0 991M 0% /sys/fs/cgroup
/dev/sda1 197M 105M 93M 54% /boot
tmpfs 199M 0 199M 0% /run/user/0
[root@wolf ~]# mount /dev/sdb1 /mnt/
[root@wolf ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda3 99G 1.8G 97G 2% /
devtmpfs 980M 0 980M 0% /dev
tmpfs 991M 0 991M 0% /dev/shm
tmpfs 991M 9.6M 981M 1% /run
tmpfs 991M 0 991M 0% /sys/fs/cgroup
/dev/sda1 197M 105M 93M 54% /boot
tmpfs 199M 0 199M 0% /run/user/0
/dev/sdb1 98M 5.3M 93M 6% /mnt
第4个里程碑-永久挂载
1./etc/rc.local
2./etc/fstab
[root@wolf ~]# cat /etc/fstab
UUID=1e827748-cc43-4b1d-8bbc-9a85c239782d / xfs defaults 0 0
UUID=29a4ef75-7502-48df-b31c-c7c10264a7c8 /boot xfs defaults 0 0
UUID=60c45f5e-c78d-41fe-aa92-93d10de6def3 swap swap defaults 0 0
第1列:UUID或设备名称 第2列 第3列 第4列 第5列 第6列
查询系统的磁盘与对应的uuid 挂载点(入口) 文件系统类型 挂载参数 是否进行备份 是否开机自动检查
(4)parted(实时生效)
p print 显示磁盘信息
mktable make partition table 创建磁盘分区表 MBR GPT
mkpart make partition 创建磁盘分区
rm 删除磁盘分区
q quit 退出磁盘分区
例题:创建1个主分区 10M
(parted) p
Error: /dev/sdc: unrecognised disk label #无法识别的
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdc: 107MB
Sector size (logical/physical): 512B/512B
Partition Table: unknown #未知
Disk Flags:
(parted) mktable gpt
(parted) p
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdc: 107MB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:
Number Start End Size File system Name Flags
(parted) mkpart primary 0 10
Warning: The resulting partition is not properly aligned for best performance.
Ignore/Cancel? i
(parted) p
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdc: 107MB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:
Number Start End Size File system Name Flags
1 17.4kB 10.0MB 9983kB primary
(5)fdisk与parted的区别:
fdisk | parted | |
---|---|---|
共同 | 磁盘分区 | 磁盘分区 |
区别 | 支持2TB以内硬盘 | 大于2TB磁盘 |
支持MBR分区表 | MBR GPT(支持更大的硬盘和更多的主分区) |
二、分区中常见的命令提示
1.
Changes will remain in memory only, until you decide to write them.
你进行的修改默认存放在内存中,直到你决定保存这些修改的时候.
:wq
Be careful before using the write command.
保存之前小心点!
Device does not contain a recognized partition table
设备 没有 包含 磁盘分区表。
Building a new DOS disklabel with disk identifier 0x6df46197.
DPT= disk partition table 磁盘分区表
2.
Invalid partition type `e' 无效参数类型e
3.
mkfs.xfs /dev/sdb1
mkfs.xfs: /dev/sdb1 appears to contain an existing filesystem (ext4).
这个磁盘分区中 已经包含了1个文件系统(已经格式化过了)
mkfs.xfs: Use the -f option to force overwrite.
如果强制格式化使用-f
4.
[root@wolf ~]# mount /dev/sdb1 /mnt
mount: /dev/sdb1 is write-protected, mounting read-only
mount: unknown filesystem type '(null)'
未知的文件系统(没有格式化)
5.
[root@wolf ~]# mount /dev/sdb1 /mnt/
mount: special device /dev/sdb1 does not exist
特殊文件(设备文件) 不存在
6.
[root@wolf ~]# mkfs.xfs /dev/sdb1
mkfs.xfs: /dev/sdb1 contains a mounted filesystem
sdb1磁盘分区 包含 1个已经挂载的文件系统 (磁盘分区已经挂载)
7. [root@wolf ~]# mkfs.xfs /dev/sdb1 -f
agsize (2560 blocks) too small, need at least 4096 blocks
磁盘分区太小了 无法格式化
Usage: mkfs.xfs
8.
[root@wolf ~]# mount /dev/sdb1 /mnt/
mount: /dev/sdb1 is already mounted or /mnt busy
磁盘分区 已经 挂载或/mnt正在使用中