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)

Dani AI

Generated

A concise, safety-first addendum to the existing replies from , and .

You already have the right two options on the table: a block-level image ( safest restore point ) or a file-level copy (lighter, faster). Build on those answers with these concrete, high‑value checks and actions: save the partition table/MBR before you touch anything; verify the filesystem type on /dev/sda1 (ext vs XFS vs btrfs) so you use the correct grow-tool; boot from live media so the root filesystem is not in use; and do a full filesystem check before resizing. Preserve the original partition start exactly — that is the single most critical detail for keeping the system bootable (this is what highlighted).

Suggested high-level workflow (no commands here so it does not duplicate earlier posts): 1) take both a file backup and a device/partition-table backup and verify the backups; 2) from live media delete sda2 and recreate sda1 using the exact original start and extend it to the end of the disk; 3) run the filesystem check and then grow the filesystem with the appropriate tool for that FS; 4) copy the former sda2 data into the newly available space and update any UUIDs or /etc/fstab entries if they changed; 5) verify bootloader and reinstall if needed; 6) reboot and test thoroughly.

Extras: prefer filesystem-aware tools (fsarchiver/partclone) over blind block copies when you only need file recovery; keep a small swap if you need it; consider converting to LVM or btrfs for easier future resizing, but only after a verified full backup.

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.