Creating New Partition From Old
===========
:Author:     Jim Lynch
:Email:      <jim@fayettedigital.com>
:Date:       November 2013
:Revision:   1.0

Here’s a document that satisfies this problem.

"I have a partition that is going bad. I have a new drive and I want to instal the same Ubuntu version to it with all the same packages. I don’t have time to save everything I need from the old drive, so I need to keep a copy of everything."

Here’s one solution to the problem. First format the new disk into two partitions. The size of the second one needs to be a bit bigger than the old partition. We will use dd to save an image. The size of the first partition can be the rest of the disk or doesn’t even have to be one partitition. The only requirement is that one of the partitions needs to be a bit bigger than the old one.

We will then copy the old partition using dd and save the package configuration using apt-clone.

Important Remember to change the /dev/sdd to whatever device your drive is on.

This example only shows removal of only one partition, but if there are more be sure to remove them all.

Create the partions
parted /dev/sdd
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) print
Model: ST380011 A (scsi)
Disk /dev/sdd: 80.0GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos

Number  Start   End     Size    Type     File system  Flags
 1      32.3kB  80.0GB  80.0GB  primary  ext4

(parted) rm 1
(parted) mktable msdos
Warning: The existing disk label on /dev/sdd will be destroyed and all data on this disk will be lost. Do you want to continue?
Yes/No? yes
(parted) quit

blackie ~ # fdisk /dev/sdd
Command (m for help): n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): p
Partition number (1-4, default 1):
Using default value 1
First sector (2048-156301487, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-156301487, default 156301487): +60G

Command (m for help): n
Partition type:
   p   primary (1 primary, 0 extended, 3 free)
   e   extended
Select (default p): p
Partition number (1-4, default 2):
Using default value 2
First sector (125831168-156301487, default 125831168):
Using default value 125831168
Last sector, +sectors or +size{K,M,G} (125831168-156301487, default 156301487):
Using default value 156301487

Command (m for help): p

Disk /dev/sdd 80.0 GB, 80026361856 bytes
255 heads, 63 sectors/track, 9729 cylinders, total 156301488 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: 0x00030999

   Device Boot      Start         End      Blocks   Id  System
/dev/sdd1            2048   125831167    62914560   83  Linux
/dev/sdd2       125831168   156301487    15235160   83  Linux

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.
Create the file systems
root@blackie:~# mkfs.ext4  /dev/sdd1
mke2fs 1.42 (29-Nov-2011)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
3842048 inodes, 15359744 blocks
767987 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=4294967296
469 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
        32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
        4096000, 7962624, 11239424

Allocating group tables: done
Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done

root@blackie:~# mkfs.ext4  /dev/sdd2
mke2fs 1.42 (29-Nov-2011)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
1044480 inodes, 4177664 blocks
208883 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=4278190080
128 block groups
32768 blocks per group, 32768 fragments per group
8160 inodes per group
Superblock backups stored on blocks:
        32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
        4096000

Allocating group tables: done
Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done
Create an image of the device we want to save
root@blackie:~# mkdir /media/tmpmnt
root@blackie:~# mount /dev/sdd2 /media/tmpmnt
root@blackie:~# dd if=/dev/sdc6 of=/media/tmpmnt/sdc6.img bs=4M
3576+1 records in
3576+1 records out
15002878464 bytes (15 GB) copied, 599.255 s, 25.0 MB/s
Note

apt-clone may throw errors, but as long as you get a file written, you’re OK. You may miss some packages but getting some is better than needing to reinstall everything.

Use apt-clone
blackie ~ # apt-clone  clone --with-dpkg-repack /media/tmpmnt/
blackie ~ # ls -l /media/tmpmnt/
total 14672164
-rw-r--r-- 1 root root    21392404 Nov 14 09:51 apt-clone-state-blackie.tar.gz
drwx------ 2 root root       16384 Nov 14 09:28 lost+found
-rw-r--r-- 1 root root 15002878464 Nov 14 09:43 sdc6.img
blackie ~ #

Now it is time to install the OS to the first partition of the new drive. Reboot from a live CD or USB stick and install.

It is important when installing the OS to the new partition that we do not destroy the second partition. This mean we will have to do a manual disk configuration during the install step. Since we format the partitions after creating them, we do not have to format them during the install.

Since I didn’t actually build a new system, I didn’t show any output from the next set of commands. But they should be straight forward enough.

Once you have the new system up and running, mount the second partition via:

sudo -i
mkdir /media/tmpmnt
mount /dev/sdd2 /media/tmpmnt

Before you run the restore, make sure you have the latest packages.

apt-get update
apt-get dist-upgrade

Then you may restore your previous packages

apt-clone restore /media/tmpmnt/apt-clone-state-blackie.tar.gz

Next we can mount the copy of the old partition via:

mkdir /media/old
mount /media/tmpmnt/sdc6.img /media/old
ls /media/old

Copy the files you need from there.