Migrating NetBSD to a New Drive

July 31, 2016

I recently had to migrate a NetBSD installation to a new hard drive. The old one was on the verge of death so I replaced it with a shiny new SSD. This turned out to be a fairly easy task - much easier than I thought.

The tools to use are the classic dump and restore. Typically you would do this "offline" by doing it from a boot/LiveCD. However, NetBSD's dump lets you do it live for FFS filesystems with the -X option. The -X uses file system snapshots so you don't have to worry about files changing while performing the dump.

The Process

This assumes the old drive is wd0 and the new drive is wd1.

  1. Create MBR partition

    Use fdisk to create a NetBSD partition. I used the whole disk, but you might have a reason not. Make sure you toggle it active/bootable. This will also install the MBR bootcode.

    # fdisk -u /dev/rwd1
    
  2. Disklabel BSD partitions

    I created the same partition table that my old drive had, but the root fs was larger because the drive was slightly larger. That is fine - dump/restore won't care.
    I vaguely recall some requirement that the root partition had to start at the beginning (sector 63) of the drive. Otherwise I had trouble with the bootloader. Unfortunately I forget the details.

    For reference, mine looks like this:

    #        size    offset     fstype [fsize bsize cpg/sgs]
    a: 226052977        63     4.2BSD      0     0     0  # (Cyl.      0*- 224258*)
    b:   8388608 226053040       swap                     # (Cyl. 224258*- 232580)
    c: 234441585        63     unused      0     0        # (Cyl.      0*- 232580)
    d: 234441648         0     unused      0     0        # (Cyl.      0 - 232580)
    
  3. Create filesystems

    You need to create a blank filesystem on which to restore. Make sure you get the drive number right, otherwise you could blow away the original filesystem!

    # newfs -O 2 /dev/rwd1a
    
  4. dump and restore

    First you need to mount the new drive partition and cd into it.

    # mount /dev/wd1a /mnt
    # cd /mnt
    

    You can directly pipe the output of dump to restore. When this is done all the content will be on the new drive.

    # dump -0 -X -f - / | restore -r -f -
    
  5. Fix fstab

    If you're going to keep the old drive in your machine then you need to fix fstab. Otherwise you may skip this step. The other alternative is to rearrange the drives in your machine so wd1 becomes wd0.

    # vi /mnt/etc/fstab
    :%s/wd0/wd1/g
    :wq
    
  6. Install bootstrap software

    Although the MBR bootcode was installed, you still need to install the primary and secondary bootcode. You can check installboot(8) for details. The primary loader is written into the disklabel section of the NetBSD partition and it loads the secondary from the filesystem. The secondary then does more setup, loads the kernel, and finally hands off control to the kernel.

    # cp /mnt/usr/mdec/boot /mnt/boot
    # installboot /dev/rwd1a /mnt/usr/mdec/bootxx_ffsv2
    
  7. Reboot and pray

    You're done! Don't forget to remove or rearrange the old drive if needed.

Previous Post

Next Post