FreeBSD Move To New Disk And Combine Filesystems

So I have virtual machine with configured FreeBSD server but mbr and several partition.

I decide to move it to GPT and one single partition. So let’s go:

0 Add new virtual disk to VM with FreeBSD.

I create new virtual disk with size 100GB

1 Partitioning the target using gpart(8)

Execute as user root the following sequence, where vtbdX is the device identifier of the new disk (!!! verify !!! — in my case it was vtbd1):
gpart destroy -F vtbdX
gpart create -s gpt vtbdX
gpart add -s 128 -a 4k -t freebsd-boot vtbdX
gpart add -s 80G -a 4k -t freebsd-ufs -l system vtbdX
gpart add -a 4k -t freebsd-swap -l swap vtbdX
# variant with size of swap:
gpart add -s 4G -a 4k -t freebsd-swap -l swap vtbdX
(Note: if you need more swap space then use 8G or 16G, instead of 4G)

Place the boodcode:
gpart bootcode -b /boot/pmbr -p /boot/gptboot -i 1 vtbdX
Set the bootme attribute:

gpart set -a bootme -i 3 vtbdX
Set the entire disk active in order the disk can be recognized by some too restrictive BIOS:

gpart set -a active /dev/vtbdX

Create the UFS2 filesystem:
newfs -j /dev/vtbdXp2
tunefs -a enable /dev/vtbdXp2

2 Clone the content of the filesystems of the current disk to the new SYSTEM partition Mount the new SYSTEM partition onto a temporary mount point:
mount -o noatime /dev/vtbdXp2 /mnt
For cloning the filesystems, I use the tool sysutils/clone from the ports.
Others like more net/rsync or sysutils/cpdup or even dump(8)|restore(8), here I show the use of clone(1):
pkg install clone
clone -x .snap:.sujournal / /mnt
clone -x .snap:.sujournal /usr /mnt/usr
clone -x .snap:.sujournal /var /mnt/var

The swap partition must not be cloned, and instead of cloning the tmp partition, it is sufficient to create the respective directory:
mkdir -p -m 1777 /mnt/tmp

Then I need to adjust the file /etc/fstab on the new SYSTEM partition, so it matches the partition labels as set in the gpart commands above, for example:
ee /mnt/etc/fstab
Code:
Device Mountpoint FStype Options Dump Pass
#/dev/gpt/SWAP none swap sw 0 0
/dev/gpt/swap none swap sw 0 0
#/dev/gpt/SYSTEM / ufs rw,noatime 0 1
/dev/gpt/system / ufs rw,noatime 0 1

Finally I unmont the new SYSTEM disk from its temporary mount location:
cd umount /mnt

I power down the computer
shutdown -p now
and replace the old startup disk by the new one.

2.1 Clone using tar:

Clone the disk contents

Having created GPT disk, next you need to copy contents from old disk to new disk.

This can be done with tar or rsync, which you use if personal preference. I have successfully used tar with no issues. With tar the important thing it is to backup of the entire containing directory not from within the containing directory.

The process is:

a: Reboot from Live CD
b: Mount each of the source and target directories
c: Clone the source directory to target
d: Copy EFI boot directory contents
e: Update “/etc/fstab” to be consistent with new disk driver

Here is sample run on machine booted via install CD and selecting “Live CD” startup:

cd /tmp
mkdir old_root old_var olt_tmp old_usr new_root
mount -t ufs /dev/da0s1a /tmp/old_root
mount -t ufs /dev/da0s1d /tmp/old_tmp
mount -t ufs /dev/da0s1f /tmp/old_usr
mount -t ufs /dev/da0s1e /tmp/old_var
mount -t ufs /dev/ad8s2 /tmp/new_root <<=== s2 as s1 is for EFI

 

Copy the directory contents using tar or rsync
This example uses tar (if you are rsync expert use that…

tar cf - old_root | (cd /tmp/new_root; tar xvf - --strip-components 1)
tar cf - old_var | (cd /new_root/var; tar xvf - --strip-components 1)
tar cf - old_tmp | (cd /new_root/tmp; tar xvf - --strip-components 1)
tar cf - old_usr | (cd /new_root/usr; tar xvf - --strip-components 1)

 

Thanks 2:
obsigna at – https://forums.freebsd.org/threads/move-to-new-disk-and-combine-filesystems.57788/

John Hartley at – https://tips.graphica.com.au/converting-freebsd-bios/


Leave a comment