Saturday, March 15, 2008

Moving FreeBSD to a New Hard Drive

This tutorial explain how to move a FreeBSD operating system and data to a new hard drive. This is useful if your hard drive is old/slow or too small. Also this can be used if you experience problems with your hard drive and you want to make sure the hard drive will not crash.

Warning! before doing this saves your data! We are not responsable if you use wrong commands and lose your data!

In this tutorial ad0 is old hard drive and ad1 is the new hard drive. Depending on your configurations your drives might have other names, for example da for SCSI, or ad4 and ad5 for SATA drives (depending on how bios is configured, for SATA you might have ad0 too), so replace ad0 and ad1 with your drives. Make sure you know exactly what are your drives name otherwise you might run command on the wrong hard drives and loose data.

Use "dmesg" command to find your hard drives names, that will also tell you the drive size and manufacturer.


Step 1. Add your second hard drive
-------------------------------------------------
Add your new hard drive to the system as a second hard drive.
If you use an IDE drive make sure your new added drive is on secondary IDE, still setup as master.


Step 2. Boot to single mode
--------------------------------------
Boot to single mode, by pressing SPACE at boot time, when loader starts and then type:

boot -s

This will boot the system in single mode. You could also choose "single mode" from menu that appears when FreeBSD starts.


Step 3. Check all partitions for errors
---------------------------------------------------

fsck -p


Step 4. Mount all partitions and activate swap
--------------------------------------------------------------
First, we will mount root partition with read/write support:

mount -u /

Then mount all partitions:

mount -a


Activate swap:

swapon -a

If the computer is set to local time we will also need to run:

adjkerntz -i


Step 5. Create directories where we will mount the new hard drive
--------------------------------------------------------------------------------------------
Next we must create directories where we will then mount partitions from the new hard drive that, of course must be created too.

mkdir /mnt/backup/ /mnt/backup/root /mnt/backup/usr /mnt/backup/var


Step 6. Create slice and partitions for the new hard drive
------------------------------------------------------------------------------

To create slice and then partitions for the new drive you have two options: to do that from sysinstall or from command line. Either will work. To create partitions from sysinstall tool is trivial so we will show how to create partitions from command line.

Clear boot and disklabel (sector0):

dd if=/dev/zero of=/dev/ad1 bs=1k count=1

Initialize sector 0 of the disk.  Existing slice entries will be cleared. Then reinitialize the boot code contained in sector 0 of the disk.

fdisk -BI ad1

Label the disk. Bootstrap code will be read from the file /boot/boot and written to the disk. Also a standard label will be written (-w option).

disklabel -B -w ad1s1 auto

Edit disklabel and add new partitions:

disklabel -e ad1s1

Last command will enter an text editor where you can add partitions. For a default/generic/usual FreeBSD system we will have
ad1s1a - / (root partition)
ad1s1b - swap
ad1s1d - /tmp
ad1s1e - /var
ad1s1f - /usr

Letter at the end of ad1s1 are partition identifiers and can have values from a to h. By convention, 'c' is reserved for describing entire disk.

The partition table list partitions on multiple rows, with every partition on a single row. We can have up to 8 entries (values) for a
partition - partition identifier, size, offset, fstype, fsize, bsize, bps/cpg.
Example:
# /dev/ad1s1:
8 partitions:
#        size   offset    fstype   [fsize bsize bps/cpg]
  a:  2097152        0    4.2BSD     2048 16384 28552
  b:  1024000  2097152      swap
  c: 156296322        0    unused        0     0         # "raw" part, don't edit
  d: 10485760  3121152    4.2BSD     2048 16384 28552
  e:  2097152 13606912    4.2BSD     2048 16384 28552
  f: 140592258 15704064    4.2BSD     2048 16384 28552

size - is the size of partition and can be in sectors (of 512 bytes in size), K bytes, M bytes or G bytes.

offset
- is the offset of the start of partition from begining of drive in sectors. Also you can add instead * to let disklabel calculate the offset for you.

fstype - partition type: for UFS is 4.2BSD, for vinum drives is vinum. Other types could be swap or unused.

fsize - the fragment size

bsize - block size

bps/cpg - number of cylinders in a cylinder group.

For more info: man disklabel and man newfs.

After you've created all partitions you need save and exit with :wq (vi commands).


Step 7. Create filesystem for all newly created partitions
------------------------------------------------------------------------------
We will create file systems for partition we've setup with disklabel:

newfs /dev/ad1s1a
newfs /dev/ad1s1e
newfs /dev/ad1s1f


Step 8. Mount newly created partitions
------------------------------------------------------
We will create directories in /mnt on current system, where we will mount the newly created partitions from second hard drive:

mkdir /backup/root
mkdir /backup/var
mkdir /backup/usr

Now we will mount the newly created partitions from second hard drive:

mount /dev/ad1s1a /backup/root
mount /dev/ad1s1e /backup/var
mount /dev/ad1s1f /backup/usr


Step 9. Dump data from old drive
----------------------------------------------
We will move everything from root, var and usr partitions to newly created partitions on new hard drive:

(dump -0f - /) | ( cd /backup/root; restore -rf - )
(dump -0f - /var) | ( cd /backup/var; restore -rf - )
(dump -0f - /usr) | ( cd /backup/usr; restore -rf - )


Step 10. Umount the new drive partitions
---------------------------------------------------------
umount /backup/root
umount /backup/var
umount /backup/usr


Step 11. Enable soft updates for partitions on new drive
-----------------------------------------------------------------------------

tunefs -n enable /dev/ad1s1a
tunefs -n enable /dev/ad1s1e
tunefs -n enable /dev/ad1s1f

Now you should have a new hard drive with all your old info. Replace the old hard drive and make sure it is connected on the same device. If it has different drive name, for example instead of ad0 it is on SATA and is named ad4, edit /etc/fstab and change partition names from ad0 to ad4, for example for root will be /dev/ad4s1a.
 

No comments: