Skip to content

Commit

Permalink
Merge pull request #179 from dteslya/add-freebsd
Browse files Browse the repository at this point in the history
Add FreeBSD support
  • Loading branch information
hellt committed Mar 22, 2024
2 parents 58a07b3 + 937bc7f commit e939d59
Show file tree
Hide file tree
Showing 8 changed files with 425 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
IMAGES_DIR=
VRS = vr-xcon vr-bgp csr nxos routeros sros veos vjunosswitch vjunosevolved vmx vsr1000 vqfx vrp xrv xrv9k vsrx openbsd ftdv
VRS = vr-xcon vr-bgp csr nxos routeros sros veos vjunosswitch vjunosevolved vmx vsr1000 vqfx vrp xrv xrv9k vsrx openbsd ftdv freebsd
VRS_PUSH = $(VRS:=-push)

.PHONY: all $(VRS) $(VRS_PUSH)
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ Since the changes we made in this fork are VM specific, we added a few popular r
* Juniper vJunosEvolved
* Nokia SR OS
* OpenBSD
* FreeBSD

The rest are left untouched and can be contributed back by the community.

Expand Down
17 changes: 17 additions & 0 deletions freebsd/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
VENDOR=FreeBSD
NAME=FreeBSD
IMAGE_FORMAT=qcow2
IMAGE_GLOB=*.qcow2

# match versions like:
# freebsd-13.2-zfs-2023-04-21.qcow2
VERSION=$(shell echo $(IMAGE) | sed -e 's/freebsd-\([0-9]\+\.[0-9]\)-.*/\1/')

-include ../makefile-sanity.include
-include ../makefile.include

download:
/bin/bash download.sh

build: download
$(MAKE) docker-image
52 changes: 52 additions & 0 deletions freebsd/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# vrnetlab / FreeBSD

This is the vrnetlab docker image for FreeBSD.

This docker image requires a custom-built FreeBSD image with pre-installed [cloud-init](https://cloudinit.readthedocs.io/en/latest/). You can download such images from https://bsd-cloud-image.org/.

## Building the docker image

Run `make download`. It will try to download the latest FreeBSD release from https://bsd-cloud-image.org/ to this directory. Then run `make` to build a docker image.

If for some reasons you can't obtain an image from https://bsd-cloud-image.org/, you can build it yourself with the script from [this repository](https://github.com/goneri/pcib).

It's been tested to boot, respond to SSH and have correct interface mapping
with the following images:

* freebsd-13.2-zfs-2023-04-21.qcow2

## Usage

```
docker run -d --privileged --name <container_name> vrnetlab/vr-freebsd:<tag> --username <username> --password <password>
```

Where:

* `container_name` - name of the created container.
* `tag`- FreeBSD release version (e.g., 13.2).
* `username`, `password` - FreeBSD VM credentials.

Example:

```
docker run -d --privileged --name my-obsd-router vrnetlab/vr-freebsd:13.2 --username admin --password admin
```

It will take about 1 minute for the container to boot. After that, you can try to ssh to the container's IP or telnet to port 5000 for console access.

To obtain the container's IP run:

```
docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' <container_name>
```

## Interface mapping

Interface `vtnet0` is always configured as a management interface. Interfaces `vtnet1` to `vio16` can be used for data plane.

## System requirements

CPU: 1 core
RAM: 512MB
DISK: 4.0GB
34 changes: 34 additions & 0 deletions freebsd/docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
FROM debian:bookworm-slim

ARG DEBIAN_FRONTEND=noninteractive
ARG DISK_SIZE=4G

RUN apt-get update -qy \
&& apt-get upgrade -qy \
&& apt-get install -y \
bridge-utils \
iproute2 \
python3-ipy \
socat \
qemu-kvm \
tcpdump \
ssh \
inetutils-ping \
dnsutils \
iptables \
nftables \
telnet \
cloud-utils \
sshpass \
&& rm -rf /var/lib/apt/lists/*

ARG IMAGE
COPY $IMAGE* /
COPY *.py /
COPY --chmod=0755 backup.sh /

RUN qemu-img resize /${IMAGE} ${DISK_SIZE}

EXPOSE 22 5000 10000-10099
HEALTHCHECK CMD ["/healthcheck.py"]
ENTRYPOINT ["/launch.py"]
84 changes: 84 additions & 0 deletions freebsd/docker/backup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
#!/bin/bash

DEFAULT_USER="admin"
DEFAULT_PASSWORD="admin"
BACKUP_FILE="backup.tar.gz"
BACKUP_PATH=/config/$BACKUP_FILE
REMOTE_BACKUP_PATH=/tmp/$BACKUP_FILE

handle_args() {
# Parse options
while getopts 'u:p:' OPTION; do
case "$OPTION" in
u)
user="$OPTARG"
;;
p)
password="$OPTARG"
;;
?)
usage
exit 1
;;
esac
done
shift "$(($OPTIND -1))"

# Assign defaults if options weren't provided
if [ -z "$user" ] ; then
user=$DEFAULT_USER
fi
if [ -z "$password" ] ; then
password=$DEFAULT_PASSWORD
fi

SSH_CMD="sshpass -p $password ssh -o LogLevel=ERROR -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -p2022"
SCP_CMD="sshpass -p $password scp -o LogLevel=ERROR -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -P2022"
HOST="$user@localhost"

# Parse commands
case $1 in

backup)
backup
;;

restore)
restore
;;

*)
usage
;;
esac
}

usage() {
echo "Usage: $(basename $0) [-u USERNAME] [-p PASSWORD] COMMAND"
echo "Options:"
echo " -u USERNAME VM SSH username (default: admin)"
echo " -p PASSWORD VM SSH password (default: admin)"
echo
echo "Commands:"
echo " backup Backup VM /etc directory to $BACKUP_PATH"
echo " restore Restore VM /etc directory from $BACKUP_PATH"
exit 0;
}

backup() {
echo "Backing up..."
$SSH_CMD $HOST "sudo tar zcf $REMOTE_BACKUP_PATH /etc > & /dev/null"
$SCP_CMD $HOST:$REMOTE_BACKUP_PATH $BACKUP_PATH
}

restore() {
if [ -f "$BACKUP_PATH" ]; then
echo "Restoring from backup..."
# Put backup file to VM, untar, and reboot.
$SCP_CMD $BACKUP_PATH $HOST:$REMOTE_BACKUP_PATH && $SSH_CMD $HOST "sudo tar xzf $REMOTE_BACKUP_PATH -C /" && $SSH_CMD $HOST "sudo shutdown -r now || true"
else
echo "$BACKUP_PATH not found. Nothing to restore."
fi
}

handle_args "$@"
Loading

0 comments on commit e939d59

Please sign in to comment.