How to list start and end sectors of a partition by parted in Linux?
How to list start and end of a partition by the sectors in parted on Linux? The default behavior seems be listing the start and end by bytes in parted .
# parted /dev/sdc print Model: Innostor IS888 ext. HDD (scsi) Disk /dev/sdc: 2000GB Sector size (logical/physical): 512B/512B Partition Table: gpt Disk Flags: Number Start End Size File system Name Flags 1 2097kB 2000GB 2000GB ext4 home
parted has a unit command to specify the unit to use.
unit unit
Set unit as the unit to use when displaying locations and sizes, and for interpreting those given by the user when not suffixed with an explicit unit. unit can be one of “s” (sectors), “B” (bytes), “kB”, “MB”, “MiB”, “GB”, “GiB”, “TB”, “TiB”, “%” (percentage of device size), “cyl” (cylinders), “chs” (cylinders, heads, sectors), or “compact” (megabytes for input, and a human-friendly form for output).
For sectors, the unit is “s”.
To print the stand and end sections of a disk such as sda , the command will be
parted /dev/sda 'unit s print'
# parted /dev/sda 'unit s print' Model: ATA ST2000DM001-1CH1 (scsi) Disk /dev/sda: 3907029168s Sector size (logical/physical): 512B/4096B Partition Table: gpt Disk Flags: Number Start End Size File system Name Flags 1 4096s 1229064191s 1229060096s data 3 1229064192s 3907029134s 2677964943s
How to calculate partition Start End Sector?
I am wondering what Start and End value to choose when partitioning my ext. SSD using fdisk . fdisk suggests 2048-250069679, default 2048 but 250069679 cannot be divided by 512 nor by 2048. Wouldn’t it be better to set the Start and End value to a number that can be divided by 512 or 2048 or 4096? For example: Start 4096 and End 250068992
Command (m for help): p Disk /dev/sda: 119,2 GiB, 128035676160 bytes, 250069680 sectors Units: sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disklabel type: dos Disk identifier: 0xa4b57300 Command (m for help): n Partition type p primary (0 primary, 0 extended, 4 free) e extended (container for logical partitions) Select (default p): p Partition number (1-4, default 1): First sector (2048-250069679, default 2048): Last sector, +sectors or +size (2048-250069679, default 250069679): Created a new partition 1 of type 'Linux' and of size 119,2 GiB. Command (m for help): p Disk /dev/sda: 119,2 GiB, 128035676160 bytes, 250069680 sectors Units: sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disklabel type: dos Disk identifier: 0xa4b57300 Device Boot Start End Sectors Size Id Type /dev/sda1 2048 250069679 250067632 119,2G 83 Linux Command (m for help): i Selected partition 1 Device: /dev/sda1 Start: 2048 End: 250069679 Sectors: 250067632 Cylinders: 15566 Size: 119,2G Id: 83 Type: Linux Start-C/H/S: 0/32/33 End-C/H/S: 206/29/63 mkfs.ext4 /dev/sda1 mke2fs 1.43.4 (31-Jan-2017) Ein Dateisystems mit 31258454 (4k) Blöcken und 7815168 Inodes wird erzeugt. UUID des Dateisystems: fdce9286-4545-447c-9cca-7d67f5bb9f43 Superblock-Sicherungskopien gespeichert in den Blöcken: 32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208, 4096000, 7962624, 11239424, 20480000, 23887872 fdisk -l Disk /dev/sda: 119,2 GiB, 128035676160 bytes, 250069680 sectors Units: sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disklabel type: dos Disk identifier: 0xa4b57300 Device Boot Start End Sectors Size Id Type /dev/sda1 2048 250069679 250067632 119,2G 83 Linux
Command (m for help): i Selected partition 1 Device: /dev/sda1 Start: 2048 End: 250069679 Sectors: 250067632 Cylinders: 15566 Size: 119,2G Id: 83 Type: Linux Start-C/H/S: 0/32/33 End-C/H/S: 206/29/63
Force a new partition to start at a certain sector with parted
We needed to increase a file system which ran out of space. Luckily this file system was a logical volume. Unfortunately the volume group didn’t have free space anymore.
As this machine is a virtual machine under VMware, the simplest solution would be to add a new virtual disk and declare it as a physical volume, extend the volume group and voilà, done. But in this case we wanted to increase the existing HDD and adapt the partitions. The logical partition /dev/sda5 is used as LVM PV, therefore it is part of an extended partition (/dev/sda3).
After the virtual disk’s size was increased in vCenter, we started up Knoppix. My colleague used gparted to increase the size of the extended partition which worked like a charm. But he wasn’t able to increase the logical partition (/dev/sda5) to fill up the rest of the extended partition. So I took a look at it. Indeed, in gparted nothing could be done.
I wanted to do it with the classic fdisk . To increase the logical partition it actually needs to be deleted and then recreated as a new partition — starting at the same sector as before and ending with the new (increased) end of the extended partition. Because the partition is not formatted after it is recreated, the data is not lost.
Before doing anything, I copied the output of fdisk -l as reference (and I created a VMware snapshot, just in case).
root@Microknoppix:~# fdisk -l /dev/sda
Disk /dev/sda: 32.2 GB, 32212254720 bytes
255 heads, 63 sectors/track, 3916 cylinders, total 62914560 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000ee3a4
Device Boot Start End Blocks Id System
/dev/sda1 * 2048 9764863 4881408 83 Linux
/dev/sda2 9764864 25389055 7812096 82 Linux swap / Solaris
/dev/sda3 25391102 62914559 18761729 5 Extended
/dev/sda5 25391104 41940991 8274944 8e Linux LVM
So /dev/sda5 started at sector 25391104. Noted.
Let’s delete the partition:
root@Microknoppix:~# fdisk /dev/sda
Commmand (m for help): d
Partition number (1-5): 5
Command (m for help): w
The partition table has been altered
Calling ioctl() to re-read partition table.
Syncing disks.
And now create the new partition, starting at sector 25391104:
root@Microknoppix:~# fdisk /dev/sda
Commmand (m for help): p
Disk /dev/sda: 32.2 GB, 32212254720 bytes
255 heads, 63 sectors/track, 3916 cylinders, total 62914560 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000ee3a4
Device Boot Start End Blocks Id System
/dev/sda1 * 2048 9764863 4881408 83 Linux
/dev/sda2 9764864 25389055 7812096 82 Linux swap / Solaris
/dev/sda3 25391102 62914559 18761729 5 Extended
Commmand (m for help): n
Partition type:
p primary (2 primary, 1 extended, 1 free)
l logical (numbered from 5)
Select (default p): l
Adding logical partition 5
First sector (25391165-62914559, default 25391165): 25391104
Value out of range
First sector (25391165-62914559, default 25391165):
Damn it! Value out of range!
The partition is not aligned and fdisk decided the new partition’s start sector must begin with a proper alignment (although the previous partition was set up this way. ).
As an alternative I also tried to create the partition with cfdisk but there I couldn’t even tell cfdisk the start sector.
parted to the rescue! With parted I was able to create the new partition with the same start sector as before!
First print the partition table in parted . To show the sectors instead of the default (human readable Bytes) I used the «unit» command and set it to s (sectors):
root@Microknoppix:~# parted /dev/sda
GNU Parted 2.3
Using /dev/sda
Welcome to GNU Parted! Type ‘help’ to view a list of commands.
(parted) unit s
(parted) print
Model: VMware Virtual disk (scsi)
Disk /dev/sda: 62914560s
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Number Start End Size Type File system Flags
1 2048s 9764863s 9762816s primary ext4 boot
2 9764864s 9764863s 15624192s primary linux-swap(v1)
3 25391102s 62914559s 37523458s extended
And now create the new partition:
(parted) mkpart logical 25391104s -1s
The mkpart command creates a new partition. The syntax is the following:
mkpart [part-type fs-type name] start end
In my mkpart command above I didn’t define the fs-type as its optional and I’m not about to create a new filesystem here, only a logical partition. And that’s the first argument (part-type).
The next arguments (25391104s and -1s) define the start and the end of the new partition. By default mkpart would expect the start and end in Bytes/MB/GB but if the argument is followed with a ‘s’, sectors can also be defined. -1s means that last usable sector of the disk. As we’re within the extended partition this means pretty much use the last usable sector of the extended partition.
There was no error message or warning after the command. Let’s verify with print again:
(parted) print
Model: VMware Virtual disk (scsi)
Disk /dev/sda: 62914560s
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Number Start End Size Type File system Flags
1 2048s 9764863s 9762816s primary ext4 boot
2 9764864s 9764863s 15624192s primary linux-swap(v1)
3 25391102s 62914559s 37523458s extended
4 25391104s 62914559s 37523456s logical
(parted) quit
Yes, it worked! With parted I was able to force the starting sector of the partition.
Now I could increase the physical volume:
root@Microknoppix:~# pvresize /dev/sda5
Physical volume «/dev/sda5» changed
1 physical volume(s) resize / 0 physical volume(s) not resized
vgdisplay / vgs now showed the available disk space in the volume group and an lvextend followed by resize2fs solved the disk space problem.
Add a comment
Comments (newest first)
George from United Kingdom wrote on Jun 4th, 2020:
Thank you. What a brilliant description of how to use parted. I spent a few hours and this was exactly what I needed.
I have a HP server with a RAID drive. I had extended the logical partion and wanted to then increase the size of the partion and file system. But. fdisk had originall set the start block to 2048, and after the logical drive was increased it then wanted me to recreate the partition starting at block 2560.
This helped me understand how I could use parted to manually set the start block.
Blog Tags:
© 2008 — 2023 by Claudio Kuenzler. Powered by .
This website uses own and third-party 🍪 cookies to improve your browsing experience. By continuing using our website you agree to the Cookie and Privacy Policy. I agree