Fralix Blog

Create FreeBSD Current OpenStack image on OVH Public Cloud

freebsd-current build openstack image (1).png

Introduction

For my project, I want to use FreeBSD on the OVH’s Public Cloud.
Behind this service, it’s OpenStack who run your instances.
Actually, OVH just offer FreeBSD-11.0. Too old, and I prefer CURRENT branch and recompile it from source every week.
Another problem is bscloud-init doesn’t run on FreeBSD-12 and older because it use an too old version of Python.


Well, before start, I list my needs:

First, I wrote a lightweight shell script to be my cloud-init during instance boot.
You’ll find it into this github repo:
GitHub - fredericalix/cloud-init: OpenStack FreeBSD cloud-init in bash script

After some tests, I toke the decision to use HashiCorp Packer to compile the kernel from source and update packages every week.
Before that, I need a minimum things. My first image must boot and configure ip address by DHCP and include the public ssh key used by my packer template.
I’ll create it on my workstation in qemu, and upload it with OpenStack CLI.
To finish, I’ll execute the Packer template who install cloud-init, pull FreeBSD-CURRENT source, compile it, update pkg and create the final OpenStack image.

Install FreeBSD-CURRENT in a QEMU VM

Prerequisites

Go to https://www.ovh.com/manager/public-cloud/index.htm

Project Management → Users & Roles → Create User

6391c28b-4c94-46f6-8019-d632afce65d4.png

73db26da-4228-4045-bb66-4332fd350f9f.png

3e4684ca-1149-4b1d-b973-41c32786fe8c.png

Save your password into your password manager ;)

099a5fa4-440e-4e6f-8a46-94d9e6e5dcdf.png


Project Management → Users & Roles → … → Download OpenStack’s RC file

147c9ec2-05b2-49e4-9740-b38ad4a53eef.png

Select your region and user OpenRC v3 config file and click Download

f85d0f20-ce5d-4a1f-b7bf-51196ab68bbf.png

Create VM on the workstation

I’ll don’t explain how to use virt-manager. You’ll find a lot of documentation into your favorite search engine.
The most important is:

FreeBSD installation steps

fca4b106-c716-4d38-9620-731abc0644bd.png

71690d09-90ce-44d7-98b2-cb027f0e3022.png

aa457c2a-1b00-4d94-87bd-7a9072b1c364.png

e8ec9d3e-aef8-4051-be84-8fd1c5b63eef.png

4db8a30e-8440-4196-b586-0a7d5cb428d7.png

e6e5f965-66d0-4fa3-ad37-50029860c5f4.png

0eee6b04-1936-41e9-be18-5dbedb0107bd.png

d0c9a312-4e4a-437b-b932-bd15676fcbe9.png

e279909d-c662-4335-b358-bb9dd8711840.png

cbee95c6-0bd7-47c3-84be-33183a1d4753.png

cc270ce9-d7c6-47fb-856f-2a0b19839748.png

fc8d51a0-9184-4e00-97f7-b22b187aaa98.png

6a241cf7-f220-45c2-8caf-f43cb8634be8.png

f7c9c9b6-076a-46fe-a4d1-5289786a3db1.png

04e1bb56-b7af-44ce-90d6-ad9368dd3da6.png

e2119d3c-aebf-4ad9-af56-57bba34ec1f5.png

1cb3881d-385b-441f-8502-44651481173b.png

bce2b6a4-7584-4bfa-9cac-187c0fb6927c.png

2d4c793a-831d-4cd2-8142-ad441158dc50.png

282e558f-1b92-4978-9bb6-f74f67f48a3c.png

Create a freebsduser. It will be the only user of your instance when you’ll create it.
Later we’ll give it admin privileges with sudo.

da420cb6-8999-477a-8765-3fedc0d2c8e1.png

cdd6ca6e-34aa-4fa0-9349-6534a3b668e6.png

1d7aa65a-3d02-4ba1-b8e0-4e9ce97545c0.png

Configure FreeBSD before push it on OpenStack

On the console connect with root user to show the ip address

54d25f92-e0bc-434e-9142-84cc7a7067c9.png

Now you can connect to it by ssh

ssh freebsd@192.168.122.250

But before we need to install and configure sudo. Stay connected in your QEMU console and type:

pkg update
pkg install -y sudo
visudo

Insert this line into the visudo editor:

freebsd ALL=(ALL) NOPASSWD: ALL

Now you can connect to your vm by ssh. It will be more easy to edit your files.

Put packer ssh key on freebsd account

mkdir .ssh
chmod go-rwx .ssh
cd .ssh
vi authorized_keys

Paste your \~/.ssh/id_rsa_packer.pub in authorized_keys

Install packages

sudo pkg install -y bash wget curl

dhcp client configuration for OVH network route

sudo vi /etc/dhclient-exit-hooks

# This script fixes injection of the default route on OVH /cloud.
# See dhclient-script(8) for more details.

# Force-add a static route to our gateway (otherwise unreachable)
# and re-declare it as the default route (as previous declaration failed)
case "${reason}" in
    "BOUND"|"RENEW"|"REBIND"|"REBOOT")
        route add "${new_routers}" -iface "${interface}"
        route add default "${new_routers}"
        ;;
    *)
        ;;
esac

Few OpenStack-specific changes

sudo -s
echo 'console="comconsole,vidconsole"' >> /boot/loader.conf
echo 'autoboot_delay="1"' >> /boot/loader.conf

Shrink image

Halt your vm

halt -p

Now it it time to shrink your image to be more smaller.
Locate your vm data file. In my case, it is /var/lib/libvirt/images/

root@ubuntu-desktop:/var/lib/libvirt/images# ls -lh
total 11G
-rw------- 1 root root 11G août  15 21:11 freebsd-current.qcow2

To shrink it:

qemu-img convert -O qcow2 freebsd-current.qcow2 freebsd-current_small.qcow2

ls -lh

root@ubuntu-desktop:/var/lib/libvirt/images# ls -lh
total 12G
-rw------- 1 root root  11G août  15 21:11 freebsd-current.qcow2
-rw-r--r-- 1 root root 1,2G août  15 21:18 freebsd-current_small.qcow2

Upload your FreeBSD openstack image to OVH Public Cloud

It’s time to upload our image.
First, you need to load your openrc.sh profile (read prerequisites section) and display actual images to see if your openstack client is correctly configured

. ~/openrc.sh
openstack image list

ffa5e804-890b-41ad-9012-65e240aa2b6a.png

Now upload the image:

openstack image create --private --disk-format qcow2 --container-format bare --property image_original_user=freebsd --property hw_disk_bus='scsi' --property hw_scsi_model=virtio-scsi --file freebsd-current_small.qcow2 freebsd-current_phase-1
+------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Field            | Value                                                                                                                                                                                                               |
+------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| checksum         | XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX                                                                                                                                                                                    |
| container_format | bare                                                                                                                                                                                                                |
| created_at       | 2019-08-15T19:33:38Z                                                                                                                                                                                                |
| disk_format      | qcow2                                                                                                                                                                                                               |
| file             | /v2/images/XXXXXXXXXXXXXXXX/file                                                                                                                                                                |
| id               | XXXXXXXXXXXXXXXXXXXXXXXXXX                                                                                                                                                                                |
| min_disk         | 0                                                                                                                                                                                                                   |
| min_ram          | 0                                                                                                                                                                                                                   |
| name             | freebsd-current_phase-1                                                                                                                                                                                             |
| owner            | XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX                                                                                                                                                                                    |
| properties       | direct_url='swift+config://ref1/glance/XXXXXXXXb3-fb65d7c74b9f', hw_scsi_model='virtio-scsi', locations='[{'url': 'swift+config://ref1/glance/XXXXXXXXXXXXXXb3-fb65d7c74b9f', 'metadata': {}}]' |
| protected        | False                                                                                                                                                                                                               |
| schema           | /v2/schemas/image                                                                                                                                                                                                   |
| size             | 1196621824                                                                                                                                                                                                          |
| status           | active                                                                                                                                                                                                              |
| tags             |                                                                                                                                                                                                                     |
| updated_at       | 2019-08-15T22:19:29Z                                                                                                                                                                                                |
| virtual_size     | None                                                                                                                                                                                                                |
| visibility       | private                                                                                                                                                                                                             |
+------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

Verify image

openstack image list --private
+--------------------------------------+-------------------------+--------+
| ID                                   | Name                    | Status |
+--------------------------------------+-------------------------+--------+
| f5d9db47-ddc1-49c3-a7b3-fb65d7c74b9f | freebsd-current_phase-1 | active |
+--------------------------------------+-------------------------+--------+

Important:
Don’t use this image for your production or anything else. It is just a draft and we’ll use it in the next section to build the final image, with cloud-init and another things.

Using HashiCorp Packer to build the final FreeBSD-CURRENT OpenStack image

In this step, we’ll use Packer to build our final image.
The template will:

First, add your packer ssh key we created earlier

ssh-add ~/.ssh/id_rsa_packer

Display and note your ID Ext-Net network. You’ll need it to configure Packer.

openstack network list --name Ext-Net -c ID
+--------------------------------------+
| ID                                   |
+--------------------------------------+
| xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx |
+--------------------------------------+

Clone my Packer template

git clone https://github.com/fredericalix/packer-freebsd.git
cd packer-freebsd
mkdir /var/tmp/packer

Edit openstack_ovh.json and replace the networks section with your network uuid

{
  "builders": [
    {
      "flavor": "b2-7",
      "image_name": "freebsd-current_final",
      "image_visibility": "private",
      "insecure": "false",
      "networks": [
        "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
      ],
      ...

Now run Packer and take a very long coffee.
The buildworld operation take ~ 4 hours.

packer build openstack_ovh.json

When it will be over, verify image availability

openstack image list --private
+--------------------------------------+-------------------------+--------+
| ID                                   | Name                    | Status |
+--------------------------------------+-------------------------+--------+
| 70556385-12ea-43fc-967b-4b3afa8326bf | freebsd-current_final   | active |
| f5d9db47-ddc1-49c3-a7b3-fb65d7c74b9f | freebsd-current_phase-1 | active |
+--------------------------------------+-------------------------+--------+

Now you can use it ;)

Have fun !