Force a new partition to start at a certain sector with parted

Written by - 1 comments

Published on - Listed in Linux


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

Show form to leave 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.


RSS feed

Blog Tags:

  AWS   Android   Ansible   Apache   Apple   Atlassian   BSD   Backup   Bash   Bluecoat   CMS   Chef   Cloud   Coding   Consul   Containers   CouchDB   DB   DNS   Database   Databases   Docker   ELK   Elasticsearch   Filebeat   FreeBSD   Galera   Git   GlusterFS   Grafana   Graphics   HAProxy   HTML   Hacks   Hardware   Icinga   Icingaweb   Icingaweb2   Influx   Internet   Java   KVM   Kibana   Kodi   Kubernetes   LVM   LXC   Linux   Logstash   Mac   Macintosh   Mail   MariaDB   Minio   MongoDB   Monitoring   Multimedia   MySQL   NFS   Nagios   Network   Nginx   OSSEC   OTRS   Office   PGSQL   PHP   Perl   Personal   PostgreSQL   Postgres   PowerDNS   Proxmox   Proxy   Python   Rancher   Rant   Redis   Roundcube   SSL   Samba   Seafile   Security   Shell   SmartOS   Solaris   Surveillance   Systemd   TLS   Tomcat   Ubuntu   Unix   VMWare   VMware   Varnish   Virtualization   Windows   Wireless   Wordpress   Wyse   ZFS   Zoneminder   


Update cookies preferences