Hi .

I have two partitions that I want to merge into one large disk. partition 1 has systems running on it while partition 2 does not have any. they are as follows:

Disk /dev/sda: 107.4 GB, 107374182400 bytes
255 heads, 63 sectors/track, 13054 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000edd29

Device Boot Start End Blocks Id System
/dev/sda1 * 1 5100 40960000 83 Linux
/dev/sda2 5100 13054 63895231 83 Linux

that is the output of fdisk -l. the disk does not have volume gropus or logical volumes. How can I merge the two without loosing data in partition 1(/dev/sda1)

Recommended Answers

All 3 Replies

  1. Boot to a live cd/dvd/usb drive.
  2. Do a bit copy backup of the data in /dev/sda and a separate bit copy of /dev/sda2 to another drive (an external drive would be appropriate). You use dd to copy /dev/sda, and then /dev/sda2. The bit copy of the entire drive is so if things go badly, you can restore the drive to the old image, using dd to copy the data back.
  3. Repartition with fdisk /dev/sda, removing /dev/sda1 and /dev/sda2 and then recreating /dev/sda1 to the full size of the disc. Then use the (w)rite command from fdisk to save the new configuration. Be VERY sure that the starting block of /dev/sda1 is exactly the same as it was originally.
  4. Assuming that the /dev/sda1 partition is any of ext2/3/4, then you can use the /sbin/resize2fs tool to resize the file system on /dev/sda1, expanding it to fill the new partition size.
  5. Mount the new version of /dev/sda1, loop mount the backup copy of /dev/sda2, and then copy the contents of /dev/sda2 to the new file system.
  6. Reboot.

For the bit copies, do this:

dd if=/dev/sda of=/mountpointofexternaldrive/sda bs=1M
dd if=/dev/sda2 of=/mountpointofexternaldrive/sda2 bs=1M

To resize the /dev/sda1 partition (after repartitioning the drive), do this:

/sbin/resize2fs /dev/sda1

To mount/copy the data from /dev/sda2 to /dev/sda1, do this:

mkdir /mnt/sda1 /mnt/sda2
mount /dev/sda1 /mnt/sda1
mount -o loop /mountpointofexternaldrive/sda2 /mnt/sda2
cd /mnt/sda2
cp -rfp . /mnt/sda1

BTW, you may want to save some space to create a swap partition unless you already have a swap file available.

I have used this technique myself (or variations of it) in the past. In fact I do it quite regularly on Amazon cloud systems (AWS), except there I can't boot from a live cd/dvd/usb drive. I detach the discs from one image, load them into another, and then do my work there. After that, I detach them from the work system and reattach them to the original image, reboot, and voila! One advantage of AWS is that you can easily create a snapshot of a disc image so restoring the disc if you mess up is simple.

I'd rather not do the bit-copies, but to do a copy of the /dev/sda2's data to another disk (rsync is a good method I use for these purposes ;), then you could try/check with gparted or similar (Hiren's Bootcd have some of these tools) which you can then delete partition sda2, and expand sda1.

My rationale for the bit copy is so you can restore the disk or partition exactly if things go wrong. I work on client systems when this sort of work is necessary, and it is the ONLY way you can be 100% certain that you have a recoverable image. The rsync tool is incredibly useful, and for file-based backups just fine. For device-level backups, not so...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.