Linux 创建MBR的磁盘的扩展分区、逻辑分区相关问题(parted)

>主要参考:parted空闲空间添加分区

>参考截图:在这里插入图片描述

1、实验环境搭建:添加一块硬盘,并将该磁盘转换成MBR格式

【添加成功后查看结果】

在这里插入图片描述

2、讲该硬盘的格式转换成MBR格式(msdos)

parted /dev/sda mklabel msdos

3、使用parted先创建一个主分区

parted /dev/sda
GNU Parted 3.2
Using /dev/sda
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) mkpart		# 创建分区
Partition type?  primary/extended? primary	#选择主分区
File system type?  [ext2]?		#默认文件类型
Start? 2048s		# 指定起始大小(第一分区需从2048扇区开始)
End? 1001MB			# 指定最终大小(最终的分区大小=[起始大小-最终大小])
(parted) print		# 输出分区情况
Model: ATA VMware Virtual S (scsi)
Disk /dev/sda: 10.7GB	#磁盘的大小
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:

Number  Start   End     Size    Type     File system  Flags
 1      1049kB  1001MB  1000MB  primary  ext2         lba

4、基于主分区的基础上,创建扩展分区,给予剩余的磁盘空间

end参数填:-1
	表示:end = 磁盘大小(MB)-1MB
	#举例解释:
		你的磁盘是100MB,主分区占用了范围:2048s~10MB
		如需将扩展分区设置为磁盘的剩余空间,那么范围就是:
			11MB~(100MB-1MB)
			11MB~99MB
	>>>所以,当end = -1 的时候,就是将剩余的磁盘空间给予给该分区<<<
(parted) mkpart		# 创建分区
Partition type?  primary/extended? extended		# 创建扩展分区
Start? 1002MB		# 起始大小,基于前一个分区的大小后移1MB
End? -1				# 最终大小,-1 表示磁盘大小迁移1MB(也就是磁盘大小-1MB)
(parted) print
Model: ATA VMware Virtual S (scsi)
Disk /dev/sda: 10.7GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:

Number  Start   End     Size    Type      File system  Flags
 1      1049kB  1001MB  1000MB  primary   ext2         lba
 2      1002MB  10.7GB  9734MB  extended               lba

在这里插入图片描述

5、前面那个只是创建扩展分区而已,真正的主角应该是逻辑分区!

  5.1、创建逻辑分区

(parted) print
Model: ATA VMware Virtual S (scsi)
Disk /dev/sda: 10.7GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:

Number  Start   End     Size    Type      File system  Flags
 1      1049kB  1001MB  1000MB  primary   ext2         lba
 2      1002MB  10.7GB  9734MB  extended               lba
-------------------------------------------------------------
(parted) mkpart		# 创建分区
Partition type?  primary/logical? logical
File system type?  [ext2]?		# 默认文件类型
Start? 1003MB		# 逻辑卷的起始点很特殊,是从扩展卷的起始点后一个扇区开始
End? 2004MB			# 创建一个1000MB的逻辑分区
(parted) print
Model: ATA VMware Virtual S (scsi)
Disk /dev/sda: 10.7GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:

Number  Start   End     Size    Type      File system  Flags
 1      1049kB  1001MB  1000MB  primary   ext2         lba
 2      1002MB  10.7GB  9734MB  extended               lba
 5      1003MB  2004MB  1000MB  logical   ext2         lba

  5.2、将剩余的逻辑分区空间赋予给逻辑分区

(parted) mkpart		# 创建分区
Partition type?  primary/logical? logical
File system type?  [ext2]?		# 默认文件类型
Start? 2005MB		# 起始点:上一个逻辑分区后一个扇区开始
End? -1				>>>将整个剩余的>[扩展分区]<空间,赋予给该逻辑分区<<<
(parted) print
Model: ATA VMware Virtual S (scsi)
Disk /dev/sda: 10.7GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:

Number  Start   End     Size    Type      File system  Flags
 1      1049kB  1001MB  1000MB  primary   ext2         lba
 2      1002MB  10.7GB  9734MB  extended               lba
 5      1003MB  2004MB  1000MB  logical   ext2         lba
 6      2005MB  10.7GB  8731MB  logical   ext2         lba

在这里插入图片描述

6.梳理:磁盘空间大小、分区大小、扩展分区大小、逻辑分区大小…

>>>根据序号来观察层次关系<<<
	
	1.磁盘(硬盘)大小决定分区大小;
		2.分区大小通常多种表达方式:
			3.如果是MBR磁盘,那么分区大小这么表达:
				4.主分区大小....
				4.扩展分区大小....
					5.逻辑分区1的大小....
			3.如果是GPT磁盘,那么分区大小这么表达:
				4.某某分区大小....

7.个人拓展时间:

在这里插入图片描述

7.1 扩展分区大小对逻辑分区的影响?

通过实验案例说明:
  正常的扩展分区都是直接使用剩余的磁盘空间,那如果扩展分区的大小为1000MB的话,逻辑分区最大是多少呢?

7.1.1 创建1000MB的扩展分区
(parted) print
Model: ATA VMware Virtual S (scsi)
Disk /dev/sda: 10.7GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:

Number  Start   End     Size    Type     File system  Flags
 1      1049kB  1001MB  1000MB  primary
-------------------------------
(parted) mkpart		# 创建分区
Partition type?  primary/extended? extended
Start? 1002MB		# 紧接着着主分区的后一个扇区开始
End? 2003MB			# 创建1000MB的扩展分区
(parted) print		# 查看分区表
Model: ATA VMware Virtual S (scsi)
Disk /dev/sda: 10.7GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:

Number  Start   End     Size    Type      File system  Flags
 1      1049kB  1001MB  1000MB  primary
 2      1002MB  2003MB  1000MB  extended               lba	# 成功创建1000MB的扩展分区
7.1.2 创建2000MB的逻辑分区
(parted) mkpart		# 创建分区
Partition type?  primary/logical? logical
File system type?  [ext2]?	# 使用默认文件类型
Start? 1003MB		# 起始点,从扩展分区后一个扇区
End? 3004MB			# 创建一个2000MB的逻辑分区
Warning: You requested a partition from 1003MB to 3004MB (sectors 1958984..5867187).
The closest location we can manage is 1003MB to 2003MB (sectors 1958984..3911679).
Is this still acceptable to you?
Yes/No? yes			# 警告:最多只能给1000MB(扩展分区大小),你能接受吗?
-------------------------------
Warning: The resulting partition is not properly aligned for best performance: 
1958984s % 2048s != 0s
Ignore/Cancel? Ignore	# 提示信息,忽略
-------------------------------
(parted) print		# 查看分区表
Model: ATA VMware Virtual S (scsi)
Disk /dev/sda: 10.7GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:

Number  Start   End     Size    Type      File system  Flags
 1      1049kB  1001MB  1000MB  primary
 2      1002MB  2003MB  1000MB  extended               lba
 5      1003MB  2003MB  1000MB  logical   ext2         lba
 # 逻辑分区,最大不能超过扩展分区大小。
7.1.3 结论:
	逻辑分区 ———— 最大不能超过扩展分区大小。

7.2 如何删除、恢复分区?

通过实验案例说明:
  sda6是存放重要数据的分区(现拿镜像文件放置在其中),恰巧有一天脑子抽筋使用parted划分空间的时候将sda6分区删除了,那么该如何恢复呢?

7.2.1 实验环境搭建
环境搭建:
	1.将sda6分区格式化为任意格式:> mkfs.ext2 /dev/sda6
	2.创建[/cs]文件夹,然后将[/dev/sda6]挂在到[/cs]上面
		[root@ly ~]# mkdir /cs
		[root@ly ~]# mount /dev/sda6 /cs
		[root@ly ~]# df
		Filesystem            1K-blocks    Used Available Use% Mounted on
		/dev/sda6               8392928   18744   7947844   1% /cs
	3.将镜像拷贝到/cs文件夹中
		[root@ly ~]# cp -r /mnt/* /cs/
		cp: overwrite '/cs/EULA'? y
		cp: overwrite '/cs/extra_files.json'? y
		cp: overwrite '/cs/GPL'? y
		cp: overwrite '/cs/media.repo'? y
		cp: overwrite '/cs/RPM-GPG-KEY-redhat-beta'? y
		cp: overwrite '/cs/RPM-GPG-KEY-redhat-release'? y
		cp: overwrite '/cs/TRANS.TBL'? y
	4.查看磁盘使用情况:
	[root@ly /]# df
	Filesystem            1K-blocks    Used Available Use% Mounted on
	/dev/sda6               8392928 7785960    180628  98% /cs
7.2.2 场景一:稳重,先使用rescue恢复分区
(1)删除分区:	rm 分区序号
(parted) print
Model: ATA VMware Virtual S (scsi)
Disk /dev/sda: 10.7GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:

Number  Start   End     Size    Type      File system  Flags
 1      1049kB  1001MB  1000MB  primary
 2      1002MB  10.7GB  9734MB  extended               lba
 5      1003MB  2004MB  1000MB  logical
 6      2005MB  10.7GB  8731MB  logical   ext2
----------------------------------------------- 删除前
(parted) rm 6	# 删除分区
Warning: Partition /dev/sda6 is being used. Are you sure you want to continue?
Yes/No? yes		# 提示分区正在使用,是否继续删除
Error: Partition(s) 6 on /dev/sda have been written, but we have been unable to inform the kernel of the
change, probably because it/they are in use.  As a result, the old partition(s) will remain in use.  You
should reboot now before making further changes.
Ignore/Cancel? Ignore	# 提示重启后生效,ignore 忽视
----------------------------------------------- 删除后
(parted) print
Model: ATA VMware Virtual S (scsi)
Disk /dev/sda: 10.7GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:

Number  Start   End     Size    Type      File system  Flags
 1      1049kB  1001MB  1000MB  primary
 2      1002MB  10.7GB  9734MB  extended               lba
 5      1003MB  2004MB  1000MB  logical
(2)先恢复分区
(parted) rescue 2005MB -1
Information: 
	A ext2 logical partition was found at 2005MB -> 10.7GB.	# 发现一个ext2的逻辑分区
	Do you want to add it to the partition table?			# 是否添加回分区表中?
	Yes/No/Cancel? yes	# 是的,添加进行
-------------------------------
(parted) print			# 再次查看分区表
Model: ATA VMware Virtual S (scsi)
Disk /dev/sda: 10.7GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:

Number  Start   End     Size    Type      File system  Flags
 1      1049kB  1001MB  1000MB  primary
 2      1002MB  10.7GB  9734MB  extended               lba
 5      1003MB  2004MB  1000MB  logical
 6      2005MB  10.7GB  8731MB  logical   ext2	# 成功恢复
(3)实验结果:>>>在parted中,使用rescue命令成功恢复误删分区。
7.2.2 场景二:手忙脚乱,重启后再恢复
(1)删除分区:	rm 分区序号
(parted) print
Model: ATA VMware Virtual S (scsi)
Disk /dev/sda: 10.7GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:

Number  Start   End     Size    Type      File system  Flags
 1      1049kB  1001MB  1000MB  primary
 2      1002MB  10.7GB  9734MB  extended               lba
 5      1003MB  2004MB  1000MB  logical
 6      2005MB  10.7GB  8731MB  logical   ext2
-------------------------------
(parted) rm 6		# 删除分区
Warning: Partition /dev/sda6 is being used. Are you sure you want to continue?
Yes/No? yes			# 确认删除
-------------------------------
Error: Partition(s) 6 on /dev/sda have been written, \
but we have been unable to inform the kernel of thechange, \
probably because it/they are in use.  As a result, \
the old partition(s) will remain in use.  You \
should reboot now before making further changes.
Ignore/Cancel? Ignore	# 忽略信息
(parted) quit			# 保存退出
Information: You may need to update /etc/fstab.
(2)重启主机后,再进入parted中恢复分区
[root@ly /]# reboot		# 重启主机
-------------------------------
>>> 开机之后
>
[root@ly ~]# lsblk
NAME          MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda             8:0    0   10G  0 disk
├─sda1          8:1    0  954M  0 part
├─sda2          8:2    0    1K  0 part
└─sda5          8:5    0  954M  0 part		# 可看到sda6分区不见了
-------------------------------
>>> 尝试进入parted中恢复分区

[root@ly ~]# parted /dev/sda
GNU Parted 3.2
Using /dev/sda
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted)
-------------------------------
(parted) rescue 2005MB -1		# 恢复分区
Information: A ext2 logical partition was found at 2005MB -> 10.7GB.  Do you want to add it to the partition table?
Yes/No/Cancel? yes				# 成功找到分区,并确认恢复
-------------------------------
(parted) print					# 查看分区表
Model: ATA VMware Virtual S (scsi)
Disk /dev/sda: 10.7GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:

Number  Start   End     Size    Type      File system  Flags
 1      1049kB  1001MB  1000MB  primary
 2      1002MB  10.7GB  9734MB  extended               lba
 5      1003MB  2004MB  1000MB  logical
 6      2005MB  10.7GB  8731MB  logical   ext2			# 分区表又回来了

(parted) quit		# 退出pareted,挂载/dev/sda6分区,查看文件是否还存在
Information: You may need to update /etc/fstab.
-------------------------------
>>> 挂载恢复的分区

[root@ly ~]# mount /dev/sda6 /cs
[root@ly ~]# df
Filesystem            1K-blocks    Used Available Use% Mounted on
/dev/sda6               8392928 7785960    180628  98% /cs

在这里插入图片描述

(3)实验结果:>>> 主机重启后,再使用parted,仍然可以成功恢复!

结语:

  该笔记用于记录交流学习使用,有错误地方,望各位理解。
  如能发现错误并私信指出,更是感谢!

  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Hades_Ling

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值