用parted调整分区大小

本文详细介绍了如何使用Parted命令进行硬盘分区选择、显示所有分区、调整分区大小等操作,提供了完整的命令示例和注意事项。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Select the hard disk to be parted

When you execute parted command without any argument, by default it selects the first hard disk drive that is available on your system.

In the following example, it picked /dev/sda automatically as it is the first hard drive in this system.

# parted
GNU Parted 2.3
Using /dev/sda
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted)

To choose a different hard disk, use the select command as shown below.

(parted) select /dev/sdb

It will throw the following error message when it doesn’t find the given hard disk name.

Error: Error opening /dev/sdb: No medium found
Retry/Cancel? y


Display all Partitions Using print

Using the print command, you can view all the available partitions in the selected hard disk. The print command also displays hard disk properties such as model, size, sector size and partition table as shown below.

(parted) print
Model: ATA WDC WD5000BPVT-7 (scsi)
Disk /dev/sda: 500GB
Sector size (logical/physical): 512B/4096B
Partition Table: msdos

Number  Start   End     Size    Type      File system     Flags
 1      1049kB  106MB   105MB   primary   fat16           diag
 2      106MB   15.8GB  15.7GB  primary   ntfs            boot
 3      15.8GB  266GB   251GB   primary   ntfs
 4      266GB   500GB   234GB   extended
 5      266GB   269GB   2682MB  logical   ext4
 7      269GB   270GB   524MB   logical   ext4
 8      270GB   366GB   96.8GB  logical                   lvm
 6      366GB   370GB   3999MB  logical   linux-swap(v1)
 9      370GB   500GB   130GB   logical   ext4


Resize Partition from One Size to Another Using resize

Using resize parted command, you can increase or decrease the partition size of a partition as shown in the example below.

(parted) resize 9
WARNING: you are attempting to use parted to operate on (resize) a file system.
parted's file system manipulation code is not as robust as what you'll find in
dedicated, file-system-specific packages like e2fsprogs.  We recommend
you use parted only to manipulate partition tables, whenever possible.
Support for performing most operations on most types of file systems
will be removed in an upcoming release.
Start?  [373GB]? 373GB
End?  [500GB]? 450GB

As shown above, parted command will always warn whenever you are attempting to do something dangerous (i.e : rm, resize, mkfs).

The size of partition 9 is actually reduced from 127GB to 77GB. Verify that the partition is resized properly using the print command as shown below.

(parted) print
Model: ATA WDC WD5000BPVT-7 (scsi)
Disk /dev/sda: 500GB
Sector size (logical/physical): 512B/4096B
Partition Table: msdos

Number  Start   End     Size    Type      File system     Flags
 1      1049kB  106MB   105MB   primary   fat16           diag
 2      106MB   15.8GB  15.7GB  primary   ntfs            boot
 3      15.8GB  266GB   251GB   primary   ntfs
 4      266GB   500GB   234GB   extended
 5      266GB   316GB   50.0GB  logical   ext4
 6      316GB   324GB   7999MB  logical   linux-swap(v1)
 7      324GB   344GB   20.0GB  logical   ext4
 8      344GB   364GB   20.0GB  logical
 9      373GB   450GB   77.3GB  logical   fat32           lba

Parted allows you to type unambiguous abbreviation for commands like “p” for print, “sel” for select,etc.



<think>好的,用户想了解parted命令的用法以及如何调整分区大小。首先,我需要回顾一下parted的基本功能和与fdisk的区别。根据引用[1]和[3],parted支持GPT分区,适用于大于2TB的磁盘,而fdisk只支持MBR。这点很重要,用户可能需要处理大容量磁盘。接下来,用户需要具体的分区步骤。引用[4]提供了示例命令,比如使用mkpart创建分区,并指定起始和结束位置。我应该分步骤说明如何进入parted,选择磁盘,创建分区表,然后分区。同时,要注意单位的选择,比如MB、GB或者百分比,这会影响分区的准确性。调整分区大小可能涉及扩展或缩小分区。但根据引用[3],partedresize功能可能有限,需要先卸载分区,或者使用其他工具如resize2fs。这里需要强调备份数据的重要性,避免操作失误导致数据丢失。另外,调整分区前可能需要先调整文件系统,这步骤不能遗漏。用户可能还关心实际案例,比如如何将分区从10G扩展到15G。引用[4]中的示例命令可以用百分比或具体单位,但调整大小可能需要先删除分区再重新创建,或者使用resize命令。这里要详细说明每一步的命令和注意事项,比如确保分区未挂载,使用正确的文件系统工具。 还需要注意parted的交互模式和脚本模式,引用[4]展示了如何在脚本中使用-s参数一次性执行多条命令,这对自动化部署有帮助。用户可能有脚本化需求,这部分可以提及。 最后,生成相关问题时要覆盖常见问题,比如与其他工具的比较、分区表转换、数据恢复等,帮助用户进一步学习。</think>### Parted命令分区调整分区大小教程 #### 一、Parted命令核心用法 1. **进入交互模式** ```bash parted /dev/sdX # 选择目标磁盘(如/dev/sdb) ``` 2. **创建GPT/MBR分区表** ```bash (parted) mklabel gpt # 创建GPT分区表(支持>2TB) (parted) mklabel msdos # 创建MBR分区表(传统格式) ``` 3. **创建分区** ```bash (parted) mkpart primary ext4 0% 50% # 分配前50%空间给主分区(ext4文件系统) (parted) mkpart logical xfs 50% 100% # 剩余空间分配给逻辑分区(xfs文件系统) ``` - 单位支持:`MB`、`GB`、`%`(如`2000MB`或`20%`) - 文件系统类型需与后续格式化操作一致[^4] 4. **查看分区信息** ```bash (parted) print # 显示当前磁盘分区布局 ``` #### 二、调整分区大小操作步骤 1. **卸载分区** ```bash umount /dev/sdXn # 必须确保分区未被挂载 ``` 2. **调整分区边界** ```bash (parted) resizepart 2 # 选择目标分区编号 新结束点? 15GB # 将原10GB分区扩展到15GB ``` 3. **扩展文件系统**(以ext4为例) ```bash resize2fs /dev/sdXn # 调整文件系统以匹配新分区大小 ``` **注意事项**: - **数据备份**:调整前必须备份关键数据(分区操作可能导致数据丢失)[^3] - **顺序限制**:只能调整分区结束位置,且后续分区需无数据冲突 - **文件系统支持**:ext4/xfs等支持在线调整,NTFS需借助`ntfsresize` #### 三、脚本化分区示例(一步到位) ```bash parted -s /dev/sdb mklabel gpt \ mkpart primary 0% 20% \ mkpart logical 20% 100% ``` - `-s`参数启用静默模式,适合自动化部署[^4] #### 四、MBR与GPT转换场景 ```bash (parted) mklabel gpt # 将MBR转为GPT(会清除所有分区) ``` - 转换前需确认磁盘数据可清空[^1]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值