You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何让parted自动将下一个分区紧接在前一个分区之后创建?

Hey there! I've helped plenty of folks transition from clunky fdisk pipe scripts to cleaner parted workflows, so let's break down how to get those consecutive, dynamically-sized partitions set up automatically.

First, understand parted's non-interactive superpower

Unlike fdisk which relies on feeding keystrokes via pipes (prone to breakage if disk sizes shift), parted has a --script flag that lets you run commands directly in batch mode—perfect for your dynamic size needs. No more guessing which keystroke to pipe next!

Step 1: Initialize the partition table

First, wipe any existing partition table and create a new one (use msdos instead of gpt if you need MBR for legacy systems):

parted --script /dev/sda mklabel gpt

Double-check /dev/sda is your target disk—wrong disks mean permanent data loss!

Step 2: Calculate dynamic sizes & create consecutive partitions

Assuming you already have logic to compute A, Y, X (stick to a consistent unit like MiB/GiB), you can define exact start/end points for each partition. The core trick is setting each partition's start to the previous one's end.

Here's a full script example with dynamic size calculations:

#!/bin/bash

TARGET_DISK="/dev/sda"

# Example: Dynamically calculate sizes based on total disk space
# Adjust this logic to match your actual requirements
TOTAL_SIZE_MIB=$(blockdev --getsize64 $TARGET_DISK | awk '{print int($1 / 1048576)}')
A=$((TOTAL_SIZE_MIB * 20 / 100))  # 20% of total disk space
Y=$((TOTAL_SIZE_MIB * 30 / 100))  # 30% of total disk space
X=$((TOTAL_SIZE_MIB - A - Y - 1))  # Remaining space minus 1MiB for alignment

# Initialize partition table
parted --script $TARGET_DISK mklabel gpt

# Create first partition: starts at 1MiB (avoids SSD alignment issues), ends at 1+A MiB
parted --script $TARGET_DISK mkpart primary 1MiB $((1 + A))MiB

# Create second partition: starts right after the first, size Y MiB
parted --script $TARGET_DISK mkpart primary $((1 + A))MiB $((1 + A + Y))MiB

# Create third partition: starts right after the second
# Use this line to enforce exact X size:
# parted --script $TARGET_DISK mkpart primary $((1 + A + Y))MiB $((1 + A + Y + X))MiB
# Or use this to fill the remaining disk space:
parted --script $TARGET_DISK mkpart primary $((1 + A + Y))MiB 100%

# Verify the final partition layout
parted --script $TARGET_DISK print

Alternative: Auto-detect the last partition's end

If you don't want to track start/end values manually (great for flexible workflows), you can query parted for the end of the last created partition:

# After creating the first partition, get its end position in MiB
LAST_PART_END=$(parted --script $TARGET_DISK unit MiB print | grep -E '^ 1' | awk '{print $3}' | sed 's/MiB//')

# Create second partition starting exactly where the first ended
parted --script $TARGET_DISK mkpart primary ${LAST_PART_END}MiB $((LAST_PART_END + Y))MiB

This is ideal if you plan to add/remove partitions dynamically later on.

Key tips to avoid headaches

  • Stick to consistent units (MiB/GiB are safer than raw sectors) to prevent calculation errors.
  • Start partitions at 1MiB instead of 0% to avoid alignment issues with modern SSDs and large disks.
  • Always test the script on a disposable disk (like a USB drive) before running it on production hardware!

内容的提问来源于stack exchange,提问作者Paul D Smith

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 09:07:49