?
Solved

moving/resizing partitions between drives

Posted on 2007-07-26
12
Medium Priority
?
343 Views
Last Modified: 2013-12-16
I've got an 80GB drive with the following partitions.

Disk /dev/hda: 81.9 GB, 81964302336 bytes
255 heads, 63 sectors/track, 9964 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot Start End Blocks Id System
/dev/hda1 * 1 13 104391 83 Linux
/dev/hda2 14 5112 40957717+ 83 Linux
/dev/hda3 5113 5239 1020127+ 82 Linux swap / Solaris

/dev/hda1 is boot, /dev/hda2 is everything else. This box is used as a CCTV server (using Zoneminder - highly recommended btw), and the images it records are all written to a directory on /dev/hda1. This doesn't cause the disk to fill up because Zoneminder auto deletes when the free space gets below a certain percentage, but I want to move the images to a separate partition and add a much larger disk at the same time.

I think I can copy the existing partitions to a new larger disk using the following (or equivalent - here hda is the existing disk, hdb is the new larger disk - obviously I'll have to boot from a rescue cd to do this):

dd if=/dev/hda of=/dev/hdb

and then add a new partition at the end, but what I really want to do is also resize /dev/hda2 down to about 10Gb (it's currently ~40Gb) to maximise the amount of space available. I'll delete all the image files from /dev/hda2, which should bring the spaced used down to about 7Gb.

I think I can create the partitions on the new disk with the desired size using fdisk and then copy them individually. So /dev/hdb1 would be exactly the same as /dev/hda1, and /dev/hdb2 would be about 10Gb. I could then do

dd if=/dev/hda1 of=/dev/hdb1

which should work fine because the partitions are identically sized, but if I try

dd if=/dev/hda2 of=/dev/hdb2

then this will fail as hda2 is bigger than hdb2. I guess I could add conv=noerror but how do I guarantee that I'll get all the files off hda2. The total space used should be less than the size of the new partition, but is there the possibility that some files might be missed because they're located higher up the partition that the 10Gb point (remember that I've deleted all the images by this point so it's just important files left). Or is ext3 cleverer than this and will move all the files towards the start of the partition when I delete the images files. Or is there a totally different and better way to do this?
0
Comment
Question by:neil9999
  • 6
  • 5
12 Comments
 
LVL 43

Expert Comment

by:ravenpl
ID: 19574330
>dd if=/dev/hda of=/dev/hdb
No it will not work (it may, but it it fail as well due to changed geometry)
dd is not the tool for copying data between hdds.

Why will not You just repartition new hdd, reformat, then copy files, and reinstall grub/lilo/etc?
0
 

Author Comment

by:neil9999
ID: 19574602
i was trying to avoid having to reinstall anything, as it's taken me long enough to get everything working how i wanted.
0
 
LVL 43

Accepted Solution

by:
ravenpl earned 375 total points
ID: 19574626
Not reinstall. Not at at all. But You see, linux is not like Win*.
You can just copy all files from one drive to another (but don't forget to keep file attributes like owner/perms)
Then You just have to reinstall the bootloader and You done.

I was practicing this many times.
0
Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

 

Author Comment

by:neil9999
ID: 19574723
ok, but i presume i still need to partition the new hd how i want it and then copy over the files from each partition (boot and / only in this case). i think that cp -a would do the job?
0
 
LVL 43

Expert Comment

by:ravenpl
ID: 19575067
> cp -a
That's right. And if You use selinux, You may want to relabel whole new filesystem.
0
 

Author Comment

by:neil9999
ID: 19575364
is it necessary to do anything with the swap partition?
0
 
LVL 43

Expert Comment

by:ravenpl
ID: 19575594
Format it.
mkswap -L label_if_any /dev/sdbX

Also, verify /etc/grub.conf /etc/fstab if any partition is mounted by label and not address. If so, don't forget to add label while formatting partitions.
0
 

Author Comment

by:neil9999
ID: 19575715
ah, i wondered where those labels in fstab came from. is there any way to query an existing partition to determine it's label (if one exists)?
0
 
LVL 43

Expert Comment

by:ravenpl
ID: 19575809
if it's ext2/ext3: tune2fs -l /dev/something
0
 

Author Comment

by:neil9999
ID: 19581146
thanks for all the info, i'll give it a try over the weekend
0
 

Expert Comment

by:jorlandobr
ID: 19586626
Try gparted:

http://gparted.sourceforge.net/

Add a new hard drive to your computer, boot with a Gparted live CD, then you will have a graphical interface to manage your partitions. You can copy the partitions from origin to destination with the same size or a different size (larger or smaller), with warnings about minimum secure sizes to avoid data loss.

If you use LILO as your boot loader after copying the files you must use some live cd to mount a chrooted enviroment so you can run /sbin/lilo in the new drive.

GRUB is geometry independent and should boot normally.

hth
0
 

Author Comment

by:neil9999
ID: 19587239
i did it last night using ravenpl's method - worked like a dream
0

Featured Post

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Little introduction about CP: CP is a command on linux that use to copy files and folder from one location to another location. Example usage of CP as follow: cp /myfoder /pathto/destination/folder/ cp abc.tar.gz /pathto/destination/folder/ab…
Introduction We as admins face situation where we need to redirect websites to another. This may be required as a part of an upgrade keeping the old URL but website should be served from new URL. This document would brief you on different ways ca…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Suggested Courses
Course of the Month15 days, 3 hours left to enroll

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question