Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
HowTo Convert your primary (root) to a logical partition
View unanswered posts
View posts from last 24 hours

Reply to topic    Gentoo Forums Forum Index Documentation, Tips & Tricks
View previous topic :: View next topic  
Author Message

Joined: 16 Oct 2004
Posts: 73
Location: Australia

PostPosted: Sat Mar 08, 2008 1:51 am    Post subject: HowTo Convert your primary (root) to a logical partition Reply with quote

How To Convert A Primary Partition to a Logical Partition Without Losing Data

Hi All,

Thought I would share my experience with resolving this problem. This post can be considered an extension to for people like me who did not partition their hard drive in the best possible way when installing gentoo! But I think it may also be applicable to anyone who wants to convert a working primary partition to an logical partition. YMMV.

DISCLAIMER: This post contains methods that are risky and could destroy your data - always backup

Here is the picture. I had a working gentoo installation with a 10Gb partition set aside for Windows. It turns out this was not big enough, so I wanted to reclaim some of the space in my root partition for a new FAT32 data partition accessible to both operating systems. The problem: I'd already used up the four primary partitions and could not create a new one. Here is how my partition table looked:


/dev/sda1     ext2     /boot        100MB
/dev/sda2     fat32    /mnt/winxp   10GB
/dev/sda3     swap                  4GB
/dev/sda4     ext3     /            146GB (the rest)

I followed the HOWTO above and successfully resized my ext3 partition down to 100GB. The steps for this are basically:
  • remove ext3 journal to make it ext2
  • use resizefs to make the changes
  • use fdisk to delete and recreate a smaller partition (make sure it's big enough for the new filesystem!), creating unused space at the end of the disk

This left around 40GB free, but I faced a challenge not discussed in the HOWTO. How to convert the new root partition from a primary to an extended? The extra space was useless as I couldn't partition it!

First I tried using fdisk. I simply deleted the fourth partition, created a new extended partition (/dev/sda4) and a new logical root partition (/dev/sda5).

I then tried to mount sda4 to change my boot parameters for the new partition number. The sector numbers in fdisk were the same before and after so I thought this might work, however when I then tried to "e2fsck /dev/sda5" the system was unable to find that partition's superblock. A bit of reading confirmed the superblock is a 1Kb almost at the beginning of the partition. Somehow the difference between a logical and a primary partition had resulted in this not being where the system expected it to be. The partition still started in hard disk sector 1957, but it was apparently at a different point in that sector.

I knew I had to shift all my data slightly on the disk..but how?

Here are the tools I used:
  • g4u source (
    "Ghost for Unix" is a great little NetBSD-based package for backing up drives or partitions - but it's a little out of date driver-wise
  • gentoo minimal install or liveCD
  • USB hard drive with capacity at least as big as your root partition
    FTP server with decent amount of space

I'll deal with the USB hard drive option first. Luckily I had a 110GB lying around, and my new partion was 100GB.

Method 1

This method assumes you have already resized your partition if required.

1 ) Boot off the gentoo CD. g4u has a boot CD but it doesn't contain the right drivers for my system.

2 ) Plug in your USB drive (or firewire or PCMCIA etc) and note the device names of both drives.
Note we are going to DESTROY all data on your USB drive so move data off it if you need to

3 ) fdisk your USB drive so it contain a single partition the same size or slightly bigger than your problem partition
In my case I created an ext3 partition of 101GB

4 ) wget the source code for g4u, unzip it and find "copypart"
This is a basic shell script but we need to modify it a little.

5 ) Open copypart in an editor.

Scroll to the dd command line at the bottom of the file, remove the word "progress".
Change "1m" to "1024" or higher if you know what you are doing.
Change device references to match linux system not BSD. Here's how it looked for me:


dd if=/dev/${part1} ibs=4096 | dd of=/dev/${part2} obs=4096

6 ) Save the script. You may need to "chmod +x it" Now we are going to copy the entire partition to the USB drive bit for bit.

./copypart sda4 sdb1

sda4 is the source partition and sdb1 is the destination partition
This will run for a while. On my system moving 100GB took 1hr 40mins.

7 ) Now fire up fdisk on your hard drive (sda). Delete that pesky primary partition as mentioned in the intro, and create a new extended partition (/dev/sda4) and a new logical partition (/dev/sda5) slightly larger than the partition your just backed up. For example:

  • My resized primary partition was on sector 1957 - 16200.
  • My new extended partition was on sectors 1957 to end of disk.
  • My new logical partition was on sectors 1957 to 16201.

So you can see I added a sector in case the new partitioning schema took up slightly more space than the old one and my backed up partition slightly overran the new space allocated.
Save the partion table and exit.

8 ) Now we are going to copy back the data. But we'll be copying back to /dev/sda5 not /dev/sda4 where it came from. The system will automatically align the bytes on the drive correctly for the logical partition and no data will be lost (touch wood).
./copypart sdb1 sda5

This will take the same time as before

9 ) Now the final test - can we read the partion?
e2fsck -f /dev/sda5

If e2fsck appears to be doing it's thing it has been able to read the superblock.

10 ) When it's finished you need to mount the volume ...


mount /dev/sda5 /mnt/gentoo
mount /dev/sda1 /mnt/gentoo/boot

...and update /boot/grub/grub.conf and /etc/fstab to reflect the new partition scheme.

Finished!! :P

Method 2

This is if you don't have a spare drive of large enough capacity. Use g4u's 'uploadpart' and 'ftpput' to ftp a gzipped image of the partition to another box. We then use 'slurppart' to restore it. I'll leave it to you to work out the details but a couple of points:
  • Use ethernet cable instead of wireless to speed things up
  • Definitely use the "zero blocks" method detailed in the g4u FAQ so you are not backing up useless bits

Hope somebody finds this experience gained from two days of my life helpful :lol:

Back to top
View user's profile Send private message

Joined: 30 Jan 2007
Posts: 2494
Location: /home/zach

PostPosted: Sat Mar 08, 2008 2:52 am    Post subject: Reply with quote

Excellent write-up; thanks for taking the time to document! It really should be under Documentation, Tips, & Tricks though. I will request a move. :)
“Truth, like infinity, is to be forever approached but never reached.” --Jean Ayres (1972)
---avatar cropped from =AimanStudio---
Back to top
View user's profile Send private message

Joined: 11 Sep 2004
Posts: 6066
Location: Melbourne, Australia

PostPosted: Sat Mar 08, 2008 2:57 am    Post subject: Reply with quote

Moved from Installing Gentoo to Documentation, Tips & Tricks.
_________________ || #gentoo-au
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Documentation, Tips & Tricks All times are GMT
Page 1 of 1

Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum