04-11-2019, 12:35 AM
Managing Partitions with sgdisk
<div style="margin: 5px 5% 10px 5%;"><img src="http://www.sickgaming.net/blog/wp-content/uploads/2019/04/managing-partitions-with-sgdisk.jpg" width="720" height="540" title="" alt="" /></div><div><p><a href="https://www.rodsbooks.com/" target="_blank" rel="noreferrer noopener" aria-label=" (opens in a new tab)">Roderick W. Smith</a>‘s <em>sgdisk</em> 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.</p>
<p>The following six parameters are all that you need to know to make use of sgdisk’s most basic features:</p>
<ol>
<li><strong>-p</strong><br /><em><u>P</u>rint</em> the partition table:<br />
<div class="codecolorer-container text default" style="overflow:auto;border:1px solid #9F9F9F;width:435px">
<div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace"># sgdisk -p /dev/sda</div>
</div>
</li>
<li><strong>-d x</strong><br /><em><u>D</u>elete</em> partition x:<br />
<div class="codecolorer-container text default" style="overflow:auto;border:1px solid #9F9F9F;width:435px">
<div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace"># sgdisk -d 1 /dev/sda</div>
</div>
</li>
<li><strong>-n x:y:z</strong><br />Create a <em><u>n</u>ew</em> partition numbered x, starting at y and ending at z:<br />
<div class="codecolorer-container text default" style="overflow:auto;border:1px solid #9F9F9F;width:435px">
<div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace"># sgdisk -n 1:1MiB:2MiB /dev/sda</div>
</div>
</li>
<li><strong>-c x:y</strong><br /><em><u>C</u>hange</em> the name of partition x to y:<br />
<div class="codecolorer-container text default" style="overflow:auto;border:1px solid #9F9F9F;width:435px">
<div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace"># sgdisk -c 1:grub /dev/sda</div>
</div>
</li>
<li><strong>-t x:y</strong><br />Change the <em><u>t</u>ype</em> of partition x to y:<br />
<div class="codecolorer-container text default" style="overflow:auto;border:1px solid #9F9F9F;width:435px">
<div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace"># sgdisk -t 1:ef02 /dev/sda</div>
</div>
</li>
<li><strong>–list-types</strong><br />List the partition type codes:<br />
<div class="codecolorer-container text default" style="overflow:auto;border:1px solid #9F9F9F;width:435px">
<div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace"># sgdisk –list-types</div>
</div>
<p></li>
</ol>
<figure class="wp-block-image"><img src="http://www.sickgaming.net/blog/wp-content/uploads/2019/04/managing-partitions-with-sgdisk.jpg" alt="" class="wp-image-27107" /><figcaption>The SGDisk Command</figcaption></figure>
<p>As you can see in the above examples, most of the commands require that the <a href="https://en.wikipedia.org/wiki/Device_file" target="_blank" rel="noreferrer noopener" aria-label=" (opens in a new tab)">device file name</a> of the hard disk drive to operate on be specified as the last parameter.</p>
<p>The parameters shown above can be combined so that you can completely define a partition with a single run of the sgdisk command:</p>
<div class="codecolorer-container text default" style="overflow:auto;border:1px solid #9F9F9F;width:435px">
<div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace"># sgdisk -n 1:1MiB:2MiB -t 1:ef02 -c 1:grub /dev/sda</div>
</div>
<p>Relative values can be specified for some fields by prefixing the value with a <strong>+</strong> or <strong>–</strong> symbol. If you use a relative value, sgdisk will do the math for you. For example, the above example could be written as:</p>
<div class="codecolorer-container text default" style="overflow:auto;border:1px solid #9F9F9F;width:435px">
<div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace"># sgdisk -n 1:1MiB:+1MiB -t 1:ef02 -c 1:grub /dev/sda</div>
</div>
<p>The value <strong>0</strong> has a special-case meaning for several of the fields:</p>
<ul>
<li>In the <em>partition number</em> field, 0 indicates that the next available number should be used (numbering starts at 1).</li>
<li>In the <em>starting address</em> 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.</li>
<li>In the <em>ending address</em> field, 0 indicates that the end of the largest available block of free space should be used.</li>
</ul>
<p>By using <strong>0</strong> 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:</p>
<div class="codecolorer-container text default" style="overflow:auto;border:1px solid #9F9F9F;width:435px">
<div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace"># sgdisk -n 0:0:+1MiB -t 0:ef02 -c 0:grub /dev/sda<br />
# sgdisk -n 0:0:+1GiB -t 0:ea00 -c 0:boot /dev/sda<br />
# sgdisk -n 0:0:+4GiB -t 0:8200 -c 0:swap /dev/sda<br />
# sgdisk -n 0:0:0 -t 0:8300 -c 0:root /dev/sda</div>
</div>
<p>The above example shows how to partition a hard disk for a BIOS-based computer. The <a href="https://en.wikipedia.org/wiki/BIOS_boot_partition" target="_blank" rel="noreferrer noopener" aria-label=" (opens in a new tab)">grub partition</a> 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.</p>
<p>There is also a short-cut for deleting all the partitions from a hard disk with a single command:</p>
<div class="codecolorer-container text default" style="overflow:auto;border:1px solid #9F9F9F;width:435px">
<div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace"># sgdisk –zap-all /dev/sda</div>
</div>
<p>For the most up-to-date and detailed information, check the man page:</p>
<div class="codecolorer-container text default" style="overflow:auto;border:1px solid #9F9F9F;width:435px">
<div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace">$ man sgdisk</div>
</div>
</div>
<div style="margin: 5px 5% 10px 5%;"><img src="http://www.sickgaming.net/blog/wp-content/uploads/2019/04/managing-partitions-with-sgdisk.jpg" width="720" height="540" title="" alt="" /></div><div><p><a href="https://www.rodsbooks.com/" target="_blank" rel="noreferrer noopener" aria-label=" (opens in a new tab)">Roderick W. Smith</a>‘s <em>sgdisk</em> 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.</p>
<p>The following six parameters are all that you need to know to make use of sgdisk’s most basic features:</p>
<ol>
<li><strong>-p</strong><br /><em><u>P</u>rint</em> the partition table:<br />
<div class="codecolorer-container text default" style="overflow:auto;border:1px solid #9F9F9F;width:435px">
<div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace"># sgdisk -p /dev/sda</div>
</div>
</li>
<li><strong>-d x</strong><br /><em><u>D</u>elete</em> partition x:<br />
<div class="codecolorer-container text default" style="overflow:auto;border:1px solid #9F9F9F;width:435px">
<div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace"># sgdisk -d 1 /dev/sda</div>
</div>
</li>
<li><strong>-n x:y:z</strong><br />Create a <em><u>n</u>ew</em> partition numbered x, starting at y and ending at z:<br />
<div class="codecolorer-container text default" style="overflow:auto;border:1px solid #9F9F9F;width:435px">
<div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace"># sgdisk -n 1:1MiB:2MiB /dev/sda</div>
</div>
</li>
<li><strong>-c x:y</strong><br /><em><u>C</u>hange</em> the name of partition x to y:<br />
<div class="codecolorer-container text default" style="overflow:auto;border:1px solid #9F9F9F;width:435px">
<div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace"># sgdisk -c 1:grub /dev/sda</div>
</div>
</li>
<li><strong>-t x:y</strong><br />Change the <em><u>t</u>ype</em> of partition x to y:<br />
<div class="codecolorer-container text default" style="overflow:auto;border:1px solid #9F9F9F;width:435px">
<div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace"># sgdisk -t 1:ef02 /dev/sda</div>
</div>
</li>
<li><strong>–list-types</strong><br />List the partition type codes:<br />
<div class="codecolorer-container text default" style="overflow:auto;border:1px solid #9F9F9F;width:435px">
<div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace"># sgdisk –list-types</div>
</div>
<p></li>
</ol>
<figure class="wp-block-image"><img src="http://www.sickgaming.net/blog/wp-content/uploads/2019/04/managing-partitions-with-sgdisk.jpg" alt="" class="wp-image-27107" /><figcaption>The SGDisk Command</figcaption></figure>
<p>As you can see in the above examples, most of the commands require that the <a href="https://en.wikipedia.org/wiki/Device_file" target="_blank" rel="noreferrer noopener" aria-label=" (opens in a new tab)">device file name</a> of the hard disk drive to operate on be specified as the last parameter.</p>
<p>The parameters shown above can be combined so that you can completely define a partition with a single run of the sgdisk command:</p>
<div class="codecolorer-container text default" style="overflow:auto;border:1px solid #9F9F9F;width:435px">
<div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace"># sgdisk -n 1:1MiB:2MiB -t 1:ef02 -c 1:grub /dev/sda</div>
</div>
<p>Relative values can be specified for some fields by prefixing the value with a <strong>+</strong> or <strong>–</strong> symbol. If you use a relative value, sgdisk will do the math for you. For example, the above example could be written as:</p>
<div class="codecolorer-container text default" style="overflow:auto;border:1px solid #9F9F9F;width:435px">
<div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace"># sgdisk -n 1:1MiB:+1MiB -t 1:ef02 -c 1:grub /dev/sda</div>
</div>
<p>The value <strong>0</strong> has a special-case meaning for several of the fields:</p>
<ul>
<li>In the <em>partition number</em> field, 0 indicates that the next available number should be used (numbering starts at 1).</li>
<li>In the <em>starting address</em> 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.</li>
<li>In the <em>ending address</em> field, 0 indicates that the end of the largest available block of free space should be used.</li>
</ul>
<p>By using <strong>0</strong> 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:</p>
<div class="codecolorer-container text default" style="overflow:auto;border:1px solid #9F9F9F;width:435px">
<div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace"># sgdisk -n 0:0:+1MiB -t 0:ef02 -c 0:grub /dev/sda<br />
# sgdisk -n 0:0:+1GiB -t 0:ea00 -c 0:boot /dev/sda<br />
# sgdisk -n 0:0:+4GiB -t 0:8200 -c 0:swap /dev/sda<br />
# sgdisk -n 0:0:0 -t 0:8300 -c 0:root /dev/sda</div>
</div>
<p>The above example shows how to partition a hard disk for a BIOS-based computer. The <a href="https://en.wikipedia.org/wiki/BIOS_boot_partition" target="_blank" rel="noreferrer noopener" aria-label=" (opens in a new tab)">grub partition</a> 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.</p>
<p>There is also a short-cut for deleting all the partitions from a hard disk with a single command:</p>
<div class="codecolorer-container text default" style="overflow:auto;border:1px solid #9F9F9F;width:435px">
<div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace"># sgdisk –zap-all /dev/sda</div>
</div>
<p>For the most up-to-date and detailed information, check the man page:</p>
<div class="codecolorer-container text default" style="overflow:auto;border:1px solid #9F9F9F;width:435px">
<div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace">$ man sgdisk</div>
</div>
</div>