使用 sgdisk 管理分区
分区 sgdisk
![](/data/attachment/album/201904/24/113422nzzt1t62htuzh2rb.png)


[Roderick W. Smith](https://www.rodsbooks.com/) 的 `sgdisk` 命令可在命令行中管理硬盘的分区。下面将介绍使用它所需的基础知识。


使用 sgdisk 的大多数基本功能只需要了解以下六个参数：


1、`-p` *打印* 分区表：



```
# sgdisk -p /dev/sda
```

2、 `-d x` *删除* 分区 x：



```
# sgdisk -d 1 /dev/sda
```

3、 `-n x:y:z` 创建一个编号 x 的*新*分区，从 y 开始，从 z 结束：



```
# sgdisk -n 1:1MiB:2MiB /dev/sda
```

4、`-c x:y` *更改*分区 x 的名称为 y：



```
# sgdisk -c 1:grub /dev/sda
```

5、`-t x:y` 将分区 x 的*类型*更改为 y：



```
# sgdisk -t 1:ef02 /dev/sda
```

6、`–list-types` 列出分区类型代码：



```
# sgdisk --list-types
```

![](/data/attachment/album/201904/24/113433s5yyg91yc1999o99.jpg)


如你在上面的例子中所见，大多数命令都要求将要操作的硬盘的[设备文件名](https://en.wikipedia.org/wiki/Device_file)指定为最后一个参数。


可以组合上面的参数，这样你可以一次定义所有分区：



```
# sgdisk -n 1:1MiB:2MiB -t 1:ef02 -c 1:grub /dev/sda
```

在值的前面加上 `+` 或 `–` 符号，可以为某些字段指定相对值。如果你使用相对值，`sgdisk` 会为你做数学运算。例如，上面的例子可以写成：



```
# sgdisk -n 1:1MiB:+1MiB -t 1:ef02 -c 1:grub /dev/sda
```

`0` 值对于以下几个字段有特殊意义：


* 对于*分区号*字段，0 表示应使用下一个可用编号（编号从 1 开始）。
* 对于*起始地址*字段，0 表示使用最大可用空闲块的头。硬盘开头的一些空间始终保留给分区表本身。
* 对于*结束地址*字段，0 表示使用最大可用空闲块的末尾。


通过在适当的字段中使用 `0` 和相对值，你可以创建一系列分区，而无需预先计算任何绝对值。例如，如果在一块空白硬盘中，以下 `sgdisk` 命令序列将创建典型 Linux 安装所需的所有基本分区：



```
# sgdisk -n 0:0:+1MiB -t 0:ef02 -c 0:grub /dev/sda
# sgdisk -n 0:0:+1GiB -t 0:ea00 -c 0:boot /dev/sda
# sgdisk -n 0:0:+4GiB -t 0:8200 -c 0:swap /dev/sda
# sgdisk -n 0:0:0 -t 0:8300 -c 0:root /dev/sda
```

上面的例子展示了如何为基于 BIOS 的计算机分区硬盘。基于 UEFI 的计算机上不需要 [grub 分区](https://en.wikipedia.org/wiki/BIOS_boot_partition)。由于 `sgdisk` 在上面的示例中为你计算了所有绝对值，因此你可以在基于 UEFI 的计算机上跳过第一个命令，并且可以无需修改即可运行其余命令。同样，你可以跳过创建交换分区，并且不需要修改其余命令。


还有使用一个命令删除硬盘上所有分区的快捷方式：



```
# sgdisk --zap-all /dev/sda
```

关于最新和详细信息，请查看手册页：



```
$ man sgdisk
```



---


via: <https://fedoramagazine.org/managing-partitions-with-sgdisk/>


作者：[Gregory Bartholomew](https://fedoramagazine.org/author/glb/) 选题：[lujun9972](https://github.com/lujun9972) 译者：[geekpi](https://github.com/geekpi) 校对：[wxy](https://github.com/wxy)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译，[Linux中国](https://linux.cn/) 荣誉推出


OK
[Roderick W. Smith](https://www.rodsbooks.com/)‘s *sgdisk* command can be used to manage the partitioning of your hard disk drive from the command line. The basics that you need to get started with it are demonstrated below.

The following six parameters are all that you need to know to make use of sgdisk’s most basic features:

**-p**the partition table:__P__rint

# sgdisk -p /dev/sda**-d x**partition x:__D__elete

# sgdisk -d 1 /dev/sda**-n x:y:z**

Create apartition numbered x, starting at y and ending at z:__n__ew

# sgdisk -n 1:1MiB:2MiB /dev/sda**-c x:y**the name of partition x to y:__C__hange

# sgdisk -c 1:grub /dev/sda**-t x:y**

Change theof partition x to y:__t__ype

# sgdisk -t 1:ef02 /dev/sda**–list-types**

List the partition type codes:

# sgdisk --list-types

![](https://fedoramagazine.org/wp-content/uploads/2019/04/sgdisk.jpg)

As you can see in the above examples, most of the commands require that the [device file name](https://en.wikipedia.org/wiki/Device_file) of the hard disk drive to operate on be specified as the last parameter.

The parameters shown above can be combined so that you can completely define a partition with a single run of the sgdisk command:


Relative values can be specified for some fields by prefixing the value with a **+** or **–** symbol. If you use a relative value, sgdisk will do the math for you. For example, the above example could be written as:


The value **0** has a special-case meaning for several of the fields:

- In the
*partition number*field, 0 indicates that the next available number should be used (numbering starts at 1). - In the
*starting address*field, 0 indicates that the start of the largest available block of free space should be used. Some space at the start of the hard drive is always reserved for the partition table itself. - In the
*ending address*field, 0 indicates that the end of the largest available block of free space should be used.

By using **0** and relative values in the appropriate fields, you can create a series of partitions without having to pre-calculate any absolute values. For example, the following sequence of sgdisk commands would create all the basic partitions that are needed for a typical Linux installation if in run sequence against a blank hard drive:


# sgdisk -n 0:0:+1GiB -t 0:ea00 -c 0:boot /dev/sda

# sgdisk -n 0:0:+4GiB -t 0:8200 -c 0:swap /dev/sda

# sgdisk -n 0:0:0 -t 0:8300 -c 0:root /dev/sda

The above example shows how to partition a hard disk for a BIOS-based computer. The [grub partition](https://en.wikipedia.org/wiki/BIOS_boot_partition) is not needed on a UEFI-based computer. Because sgdisk is calculating all the absolute values for you in the above example, you can just skip running the first command on a UEFI-based computer and the remaining commands can be run without modification. Likewise, you could skip creating the swap partition and the remaining commands would not need to be modified.

There is also a short-cut for deleting all the partitions from a hard disk with a single command:


For the most up-to-date and detailed information, check the man page:


## Leslie Satenstein

Clear explanation. Thank you.

## Stuart D Gathman

The man page gives a good acount of using sgdisk to convert MBR to GPT. It involves a risk of making the system unbootable – but nothing a live USB stick can’t fix. A backup is probably a good idea, though.

## Gregory Bartholomew

Thanks for pointing that out Stuart. I probably should have mentioned the dangers of converting an existing MBR partition table in the article. Thankfully, it looks like the sgdisk command includes a bit of a safty:

-g, –mbrtogpt

Convert an MBR or BSD disklabel disk to a GPT disk. As a safety measure, use of this option is required on MBR or BSD disklabel disks if you intend to save your changes, in order to prevent accidentally damaging such disks.

The following would probably be a good idea for anyone thinking about converting an MBR disk to GPT to run:

$ sudo sfdisk -d /dev/sda > partition-table-backup.dat

That could then be restored from a rescue disk with:

# cat partition-table-backup.dat | sfdisk /dev/sda

## Stuart D Gathman

When I try converting to GPT on a VM, sgdisk complains that the last partition overlaps secondary partition table by 33 sectors. The same complaint is given on

anyMBR disk I’ve looked at with sgdisk. I’m guessing that a second copy of the GPT is kept at the end of the disk, and the last partition (usually a PV) needs to be made 33 sectors smaller. Is this correct?## Gregory Bartholomew

You are exactly correct. The backup copy is referred to as the “second-header” in the man page. It is just a backup and is only used in case the original gets corrupted somehow. So you could safely ignore the error if you want. The older MBR never kept a backup copy of its data.

I’m not sure that I would recommend people convert their partition tables though. Its not something that I ever do. That said, I think you

shouldbe able to get away with it on UEFI systems because UEFIshouldfall back to using the /efi/boot/bootx64.efi (more accurately /efi/boot/boot{machine type short-name}.efi) when the partition IDs change.If you want to tell Anaconda to use GPT by default when you first install your OS, you can pass the

inst.gptoption on the kernel command line when you boot your installation disk: Anaconda Boot OptionsSee also: An Introduction to Disk Partitions

P.S. I would always recommend rounding your partition alignments to the nearest 1MiB to reduce the changes of hitting the double-write problem: Disk Partition Alignment Is Still Important

## ignition

Cool way of explaining the ideas. Loved it.

## Anil

Why you used sfdisk instead of sgdisk to take the backup?

You can use sgdisk itself to take backup.

sgdisk –backup=/root/sda-part.bkp /dev/sda

To restore

sgdisk –load-backup=/root/sda-part.bkp /dev/sda

## Gregory Bartholomew

As a general rule, sfdisk is used to manage MBR partition tables and sgdisk is used to manage GPT partition tables. Admittedly though, sgdisks backup option is probably an exception to that rule and it

probablybacks up and restores MBR data (I’m not 100% certain of that though).Even better, of course, would be to keep a backup copy of your entire hard drive. In fact, I’m working on an article to demonstrate how to do that right now.

## commenter

what are the pros when compared to fdisk/gdisk?

## Gregory Bartholomew

The only real advantage of sgdisk over gdisk is that it can be scripted. The reason that I wrote this post is because I am using sgdisk in an upcoming article on how to convert your system drive to use software RAID and the editors requested that I write individual posts for several of the commands I used in that article — dracut, mdadm, and sgdisk (Fedora Magazine Meeting 2019-03-27 time index 20:27:09 if you are curious). The upcoming article will link back to this one for further details.