# HOWTO: Clone, Repair and Restore corrupted boot disk



## ghostcorps (May 31, 2013)

Hi all,

I have just spent a day trying to restore a broken /var partition because I had made it too small. While the FAQs here were a great help I didn't seem to find any one method that suited my purpose from start to finish as I also needed to increase the size of /var and shrink /usr. 

nb. All steps have been sourced or adapted from other FAQs and even some posts here, if there is an extraneous flag or better-practice please let me know and I will update this post.

*OS: FreeBSD 8.3-RELEASE*

*Scenario: *

Bad sectors - Disk is accessible but OS will not boot. In my case syslogd would not start.
fsck from LiveOS reports errors but cannot fix them
There is no unformatted space on disk to juggle


*Target Devices:*

/dev/DiskA = Temporary disk. This must be unformatted 
/dev/DiskB = Source Disk/Corrupted OS


*Format /dev/DiskA
*

Please use THIS FAQ to build /dev/DiskA. Be sure to match structure of /dev/DiskB and make double sure you allocate enough space to the target partition to fit the data from the source partition.

Mount these partitions to your home directory as follows or however you prefer:

```
#mkdir ~/DiskA ~/DiskA/root ~/DiskA/usr ~/DiskA/var
#mount -t ufs /dev/gpt/gprootfs ~/DiskA/root
#mount -t ufs /dev/gpt/gprootfs ~/DiskA/usr
#mount -t ufs /dev/gpt/gprootfs ~/DiskA/var
```

*Clone /dev/DiskB
*

Chances are any high level access to /dev/DiskB will be met will errors or crashes. Not to mention doing more damage: so do not mount it at any stage of this process. 

Instead we will create a disk image using dd

`#dd if=/dev/DiskB conv=sync,noerror bs=64K of=/PATH/DiskB.img`

Make a backup of this image and never mount it

`#cp /PATH/DiskB.img /PATH/DiskB.Backup.img`

Then attach the first clone image to a virtual node:
`mdconfig -a -t vnode -f DiskB.img -u 1`

This will appear as a new disk /dev/md1 with partitions mirroring /dev/DiskB. Do not mount them.

You can confirm the matching structure with gpart


```
#gpart show /dev/md1
#gpart show /dev/DiskB
```

*Repair /dev/DiskB clone
*
At this point the mirror is an exact duplicate of /dev/, complete with errors. Make sure you still have the backup of this image and proceed to run fsck over each partition of /dev/md1 as follows:


```
#fsck -t ufs /dev/md1s1a
#fsck -t ufs /dev/md1s1d
#fsck -t ufs /dev/md1s1e
```

NB. be sure to read the report at the end of each run. It may ask you to run fsck again. Personally I always run it one extra time to be sure.

You now should have a cleaned copy of /dev/DiskB mounted as /dev/md1.

*Dump Clone to /dev/DiskA
*

There are a few ways to dump the partitions into /dev/DiskA but the one that worked for me dumps the contents of the source partition directly into the newly formatted partition:


```
#dump -C16 -b64 -0uan -h0 -f - /dev/md1s1a | (cd /usr/home/USER/DiskA/root && restore -ruf -)
#dump -C16 -b64 -0uan -h0 -f - /dev/md1s1d | (cd /usr/home/USER/DiskA/var && restore -ruf -)
#dump -C16 -b64 -0uan -h0 -f - /dev/md1s1e | (cd /usr/home/USER/DiskA/usr && restore -ruf -)
```

*/etc/fstab
*

The last thing to do is to check fstab in /dev/DiskA will work with the new device:


```
#mkdir ~/DiskA ~/DiskA/root
#mount /dev/DiskA ~/DiskA/root
```

#~/DiskA/root/etc/fstab

```
# Device                Mountpoint      FStype  Options         Dump    Pass#
/dev/gpt/gpswapfs       none            swap    sw              0       0
/dev/gpt/gprootfs       /               ufs     rw              1       1
/dev/gpt/gpusrfs        /usr            ufs     rw              2       2
/dev/gpt/gpvarfs        /var            ufs     rw              2       2
```

If all is well you should now be able to boot up DiskA. I strongly advise running some diagnostics afterwards as you may have lost a file or two to the corruption.

Finally. If files have become corrupted you may be unlucky enough that those files are critical and irreplaceable. In this case you can consider the above process as a way of moving your data to a more stable medium while you rebuild from scratch.

Thanks, I hope this helps someone.

Please be gentle, this is my first FAQ


----------



## jb_fvwm2 (May 31, 2013)

I once fixed a bad /var by `mtree`-ing it into the root partition... though I eventually had to replace the disk anyway.  (The filesystem was only bad if one subdirectory on it was accessed... so subdirectories could be directly copied off with one exception... ).  It was a laptop drive that probably failed because of heat issues. And fortunately the root partition was large enough to include /var.


----------



## ghostcorps (Jun 1, 2013)

Thanks jb, I'll look into that


----------

