Booting into FreeBSD
Booting into FreeBSD

Let’s assume you rendered your FreeBSD system unbootable.. Yeah, happens to the best, but how can you still copy the data stored on a ZFS to another machine? You probably just shouted RSYNC - but it’s not that easy.

You would need a FreeBSD live os (either on a USB pen drive or on a CD/DVD) and boot into that system. However, by default you do not have network, the ZPool is not mounted, there is no rsync and SSH is not running, and the live os is not writable, which brings another few issues…

This is a step-by-step how-to through all the obstacles. Just boot into your live os (get it from freebsd.org) and go on with the following…

Get Networking

By default your live system does not have networking setup correctly. Call ifconfig to see if the network interface is up. If it’s not you can bring it up using:

ifconfig em0 up

(assuming your inteface is called em0)

If it is up, you need to configure it. When you’re using a DHCP server you can just ask for an IP address using:

dhclient em0

Otherwise you need to configure the addresses manually:

ifconfig em0 inet 1.2.3.4 netmask 255.255.255.0

Afterwards you should be able to ping other machines, such as

ping 8.8.8.8

Mount the ZPool

Your ZPool won’t be mounted by default; you need to do it manually. To list all pools available on that machine just call:

zpool import

This searches through the devices in /dev to discover ZPools. You may specify a different directory with -d (see man page for zpool). To actually import and mount your ZPool you need to provide its name, for example:

zpool import -f -o altroot=/mnt zroot

This will import the ZPool zroot. Moreover, the argument -o altroot=/mnt will mount it to /mnt instead of / and the -f will mount it even if it may be in use by another system (here we’re sure it isn’t, aren’t we?).

Create some Writeable Directories

The next problem is, that you do not have permissions to write to /etc, which you need to e.g. create SSH host keys etc. However, that’s also not a big issue as we have the unionfs filesystem! :)

UnionFS will mount a directory as an overlay over another directory. Let’s assume you have some space in $SPACE (maybe in the ZPool that you just mounted or on another USB drive), then you can just create a few directories:

mkdir $SPACE/{etc,var,usr,tmp}

and mount it as unionfs to the root’s equivalents:

mount_unionfs $SPACE/etc /etc
mount_unionfs $SPACE/var /var
mount_unionfs $SPACE/usr /usr
mount_unionfs $SPACE/tmp /tmp

Now we can write to /etc, while the actual changes will be written to $SPACE/etc! Isn’t that a great invention?

Start the SSH service

Now that /etc is writable we can start caring about the SSH daemon. First, we need to configure it to allow root to login. Add the follwing line to the /etc/ssh/sshd_config:

PermitRootLogin yes

Then, we can start the ssh daemon using:

service sshd onestart

It will automatically create host keys and all the necessary things for a first start of SSH. If that was successful, port 22 should now be open:

# sockstat -4 -l
USER     COMMAND    PID   FD PROTO  LOCAL ADDRESS         FOREIGN ADDRESS
root     sshd       938   4  tcp4   *:22                  *:*
root     syslogd    542   7  udp4   *:514                 *:*

Set root Password

To be able to login you of course need to set a root password:

passwd root

Aftwerwards, you should be able to login through SSH from any other machine. Go ahaed and give it a try!

Install and Run rsync

Almost there, but the freeBSD live image doesn’t come with rsync installed. So we need to do it manually:

pkg install rsync

This will first tell us that not even pkg is installed, but answering the question with y it will automatically install itself. And as everything is mounted as UnionFS, the stuff will actually be installed to $SPACE/... instead of /. However, you should now be able to do the rsync job from where ever you want :)


Martin Scharm

stuff. just for the records.


Post a comment

read more about submitting comments