1

Good day!

I have FreeBSD installation with next partitions: mount and df output enter image description here

Now FreeBSD is installed in 500GB hard drive. I want to move it to 250GB hard drive. What is safest and correct way to do it? I'm afraid that:

  • clone with dd will fail because destination drive is smaller;
  • if I copy all files wirh cp utility, I will ruin file rights.

Please explain. I prefer the way to use one program which will do everything in one step. P.S. I found such one but now I can't answer my own question.

2

2 Answers 2

3

If all you want is to make ad0 bootable, you don't need the contents of the /dev and /proc file systems. They are dynamically generated by the kernel when you mount these file systems and access them.

If the used disk space in ad6 (the clone source drive) is less than the size of the clone target, it should be possible to copy the source files using any of:

* tar
* dump & restore
* cpio

Some care must be taken to avoid copying special file systems, like /dev and /proc that you mentioned. If you use something like a Live CD-ROM to boot, instead of booting from the 'source' disk, this should be easy, because the special /dev and /proc file systems will be mounted only for the boot device (the CD-ROM in this case), so the hard disk partitions will merely include empty directories as `potential mount-points' for the /dev, /proc and other special places.

FWIW, the steps I followed when I cloned my old laptop installation to a new hard disk are the ones shown below. I didn't want to open the laptop, because that would violate its guarantee terms. So I kept the 'target' disk as ad0 and used a USB-attached enclosure for 2.5" hard disks to attach the original 'source' disk (taken out of my old, dead laptop).

[1] Boot from CD-ROM using a FreeSBIE installation(*).

(*) Many thanks to the FreeSBIE folks, for making such an easy to
use Live CD-ROM.  I have found it very useful far too many times to
mention all of them in an email post!

[2] Create an /mnt/source and /mnt/target directory.

    # mkdir -p /mnt/source
    # mkdir -p /mnt/target

[3] Mount the source root partition, and then use the existing mount points under that source tree to mount the target partitions:

    # mount -o ro /dev/da0s1a /mnt/source
    # mount -o ro /dev/da0s1e /mnt/source/home

Note that, for extra safety, I mounted the source partitions as
read-only.  This way I would at least get a warning if I botched the
copying process, and avoid messing my original 'source' data.

[4] Partition and mount the target disk (the internal ad0 disk of the laptop). This is where booting from a Live CD-ROM helped a lot, because I didn't have to do anything special to 'resize' or 'keep' parts of the disk unpartitioned. I could use the full disk for the new installation.

    # fdisk -BI /dev/ad0
    # bsdlabel -w -B /dev/ad0s1
    # bsdlabel -e /dev/ad0s1

When I had configured the new ad0s1a and ad0s1e partitions, I saved
the label and exited bsdlabel's editor.

[5] Format the target partitions:

    # newfs -L MYROOT /dev/ad0s1a
    # newfs -L MYHOME -U /dev/ad0s1e

The -L labels are entirely optional, and, as you can see, I only
enabled softupdates on the new /home partition.

[6] Mount the target partitions under `/mnt/target'. The mounts were read-write this time:

    # mount /dev/ad0s1a /mnt/target
    # mkdir /mnt/target/home
    # mount /dev/ad0s1e /mnt/target/home

Note that the second command is not optional.  The new root file
system was brand new, so it appears completely empty after being
mounted.

[7] Copy everything using BSD tar(1):

    # tar -C /mnt/source -cf - . | tar -C /mnt/target xvf -

[8] The final step was to chroot into the new 'target' system, and fix-up any special directory permissions, by using the mtree(8) specifications from /etc/mtree'. This restores any special flags likenoschg' or the permissions required for proper daemon operation in `/var/run' and so on.

To avoid side-effects from the runtime environment of the shell I
was using `outside' of the chroot, I cleared all environment
variables, and manually set only the bare minimum of stuff I needed
`inside' the chroot:

    # env -i USER='root' SHELL='/bin/csh' HOME='/root' \
          PATH='/bin:/sbin:/usr/bin:/usr/sbin' \
          chroot /mnt/chroot /bin/csh
    freesbie# set prompt = 'chroot# '

Then I run the `/etc/mtree/BSD.*.dist' files through mtree inside
the chroot:

    chroot# cd / ; mtree -deU -f /etc/mtree/BSD.root.dist
    chroot# cd /usr ; mtree -deU -f /etc/mtree/BSD.usr.dist
    chroot# cd /usr/include ; mtree -deU -f /etc/mtree/BSD.include.dist
    chroot# cd /var ; mtree -deU -f /etc/BSD.var.dist

[9] Finally, I checked the new `/etc/fstab' file to make sure it was ok (in my case the partitions did not change name or order, so I already everything was fine; I was just being careful).

Then I exited from the chroot, unmounted all partitions, and
rebooted the laptop.  The internal ad0 disk was a 'copy' of my old
disk, so I expected it to boot normally into FreeBSD... which, much
to my amusement (since this was one of the few times I had to move
_everything_ to a new disk), it promptly did :)
4
  • Thanks for your reply. It shows comprehensive algorithm. I like it. I will try it a little bit later. Also tell me please - will your algorithm also copy the loader of FreeBSD? Or do I need actions to make partition bootable? Commented Apr 1, 2016 at 11:58
  • A lot of steps. I found one small and cool program to make things easy. It is called clonehdd and is located in ports collection under /usr/ports/sysutils/clonehdd catalogue. Commented Apr 1, 2016 at 23:11
  • Also I try dump and restore way now. Commented Apr 1, 2016 at 23:23
  • These instructions, along with wonkity.com/~wblock/docs/html/disksetup.html, helped me migrate my overprovisioned 256GB disk to a more sensible and sufficient 24GB disk.
    – T0xicCode
    Commented Jan 8, 2017 at 3:03
2

Have you tried with dd?

That would give: dd if=/dev/sdb1 of=/dev/sda2 bs=1M

7
  • dd came to my mind immediately. Btw, don't worry about "not enough reputation to comment", this counts as an answer Commented Apr 1, 2016 at 11:22
  • The good point, is that I can comment my own response. But need 50 to comment other.
    – aurelien
    Commented Apr 1, 2016 at 11:26
  • This might interest you meta.stackexchange.com/questions/214173/… Commented Apr 1, 2016 at 11:32
  • Please read my question carefully. Source disk has partition with 480GB size and 241GB free on it. Destination disk (physical drive) has just only 250GB space. It is enough to store all data from source disk, but dd as I know works good only if destination is larger than source. Otherwise it will copy only part of data and result partition will be corrupted and unusable. Thanks. Commented Apr 1, 2016 at 11:55
  • dd could normaly shrunk the file-system size dd if=/dev/sda of=/dev/sdb bs=4096 count=5 but you have to calculate your bs= and count= to make it fit.
    – aurelien
    Commented Apr 1, 2016 at 12:42

Not the answer you're looking for? Browse other questions tagged .