Setting up OctoPrint on FreeNAS 11.3 server inside iocage Jails

Setting up OctoPrint on FreeNAS 11.3 server inside iocage Jails

THIS IS STILL A WORK IN PROGRESS

I have a FreeNAS server setup wich I use to run a lot of services and would like to use it to run my OctoPrint instances to save resources and reallocate my Linux server for something else.

UPDATED 28/02/2020: FreeNAS updated from 11.2 to 11.3 and some things have changed, also added the rc.d script to run octoprint as a service (thanks to James_Burnett for the script!).

Since I didn’t find any documentation regarding setting up and running OctoPrint on FreeBSD (much less FreeNAS), I thought it was a good idea to post a guide describing how I did it and save some time for anyone else trying to accomplish the same. I plan to update and correct this guide from time to time, i’m open to suggestions as this is my first guide.

I won’t cover how to install FreeNAS or setup iocage as it is already well covered in the FreeNAS User Guide.

Creating a Jail for OctoPrint

Open your FreeNAS server dashboard on your browser, select Jails in the left-side drop-down list:

The Jails menu will open, now select add in order to create a new Jail:

Select advanced Jail Creation :

Give the Jail a recognizable name, select 11.3 for Release, select your NIC in IPv4 Interface, give it an IP address(we’ll use it to access the OctoPrint interface later) and an according Netmask. Mark Auto-start then click NEXT:

Now in Jail properties, change devfs_ruleset to 3, this will allow the Jail to access the /dev folder and consequently talk to USB devices , that wouldn’t be possible if you left it as default (ruleset 4), and the Jail wouldn’t be able to talk to your 3D Printer.

Mark mount.devfs, allow.mount and allow.mount.devfs

You don’t need to mess with anything else here, press SAVE, we’re done setting up the Jail.

Installing necessary packages

You need to install python, pip and other packages for OctoPrint to run on the Jail, for that we need to start a shell inside the Jail. There’s 2 ways you can go about it:

You can just click the 3 dots and start a Shell from the WebUI:

Or you can SSH into your FreeNAS server (you have to enable and setup SSH beforehand for that) and only then start a Shell inside the Jail using the commands:

iocage list

will list active Jails, useful for seing the Jail ID and name

 root@freenas[~]# iocage list
 
 +-----+------------+-------+--------------+----------------+
 
 | JID | NAME | STATE | RELEASE | IP4 |
 
 +=====+============+=======+==============+================+
 
 | 8 | Octo1 | up | 11.3-RELEASE | 192.168.0.6/24 |
 
 +-----+------------+-------+--------------+----------------+
 
 | 2 | Octo2 | up | 11.3-RELEASE | 192.168.0.8 |
 
 +-----+------------+-------+--------------+----------------+
 
 | 9 | Octo3 | up | 11.3-RELEASE | 192.168.0.9 |
 
 +-----+------------+-------+--------------+----------------+
 
 | 3 | Reslilio | up | 11.3-RELEASE | 192.168.0.3 |
 
 +-----+------------+-------+--------------+----------------+
 
 | 4 | Trans | up | 11.3-RELEASE | 192.168.0.4 |
 
 +-----+------------+-------+--------------+----------------+
 
 | 5 | mineserver | up | 11.3-RELEASE | 192.168.0.12 |
 
 +-----+------------+-------+--------------+----------------+
 
 [root@freenas](mailto:root@freenas)[~]# iocage console Octo2

#iocage is the default Jail manager used in FreeNAS 11.3, use this command with the respective name of your Jail.

root@freenas[~]# iocage console NameOfYourJail

Last login: Wed Feb 6 18:13:18 on pts/2

FreeBSD 11.2-STABLE (FreeNAS.amd64) #0 r325575+fc3d65faae6(freenas/11.2-stable): Wed Dec 5 15:08:42 EST 2018

Welcome to FreeBSD!

Release Notes, Errata: https://www.FreeBSD.org/releases/

Security Advisories: https://www.FreeBSD.org/security/

FreeBSD Handbook: https://www.FreeBSD.org/handbook/

FreeBSD FAQ: https://www.FreeBSD.org/faq/

Questions List: https://lists.FreeBSD.org/mailman/listinfo/freebsd-questions/

FreeBSD Forums: https://forums.FreeBSD.org/

Documents installed with the system are in the /usr/local/share/doc/freebsd/

directory, or can be installed later with: pkg install en-freebsd-doc

For other languages, replace "en" with a language code like de or fr.

Show the version of FreeBSD installed: freebsd-version ; uname -a

Please include that output and any error messages when posting questions.

Introduction to manual pages: man man

FreeBSD directory layout: man hier

Edit /etc/motd to change this login announcement.

root@OctoPrint2:~ #

Now that you have a shell as root inside the Jail, let’s install pkg, python, pip and everything else

root@OctoPrint2:~ #pkg install pkg nano python python27 py27-virtualenv py27-pip git bash

now we can create a folder and download OctoPrint:

root@OctoPrint2:~ # mkdir OctoPrint && cd OctoPrint
root@OctoPrint2:~/OctoPrint # python2.7 -m virtualenv venv
root@OctoPrint2:~/OctoPrint # source venv/bin/activate.csh
[venv] root@OctoPrint2:~/OctoPrint # pip install pip –upgrade
[venv] root@OctoPrint2:~/OctoPrint # pip install https://get.octoprint.org/latest

You should then be able to manually start the OctoPrint server, if you want to, if you followed all the steps correctly until now, you won’t need to:

[venv] root@OctoPrint2:~/OctoPrint # venv/bin/octoprint serve –iknowwhatimdoing

OctoPrint should start without issues, press Ctrl + c to close it.

USB Connectivity

Connect your 3D Printer to an USB port on your FreeNAS server and run usbconfig inside the Jail and note wich serial device is your 3D Printer:

[venv] root@OctoPrint2:~/OctoPrint # usbconfig 
ugen4.2: <FTDI FT232R USB UART> at usbus4, cfg=0 md=HOST spd=FULL (12Mbps) pwr=ON (90mA)
 
 ugen2.2: <Generic Mass Storage> at usbus2, cfg=0 md=HOST spd=HIGH (480Mbps) pwr=ON (200mA)

Here we can see that my 3D Printer is connected to usbus4 and is listed as ugen4.2 in FreeNAS.

Creating a Start script for OctoPrint in the Jail

Thanks to user James_Burnett for this rc.d init script, i take no credit for this script, i’m adding it as part of this guide to increase functionality for the users, i take no credit for this script.

Let’s create the script using nano:

[venv] root@OctoPrint2:~ # nano /etc/rc.d/octoprint

copy and paste this in the file and press Ctrl + o to save it:

#!bin/sh

# PROVIDE: octoprint
# REQUIRE: DEAMON NETWORKING

. /etc/rc.subr

base_path="/root/OctoPrint"

name="octoprint"
rcvar="octoprint_enable"
pidfile="${base_path}/octoprint.pid"

command="/usr/sbin/daemon"
command_args="-p ${pidfile} -f -c ${base_path}/venv/bin/octoprint serve --iknowwhatimdoing"

procname="${base_path}/venv/bin/octoprint"
command_interpreter="/root/OctoPrint/venv/bin/python2.7"

run_rc_command "$1"

Add octoprint_enable="YES" to rc.conf and everything should just work.

Once you have the file saved, grant it running permissions:

[venv] root@OctoPrint2:~ # chmod +x /etc/rc.d/octoprint

Now we need to add octoprint_enable=”YES” to the rc.conf file.

root@OctoPrint2:~ # nano /etc/rc.conf

cron_flags="$cron_flags -J 15"

# Disable Sendmail by default

sendmail_enable="NO"

sendmail_submit_enable="NO"

sendmail_outbound_enable="NO"

sendmail_msp_queue_enable="NO"

# Run secure syslog

syslogd_flags="-c -ss"

# Enable IPv6

ipv6_activate_all_interfaces="YES"

hostname="OctoPrint2"

octoprint_enable="YES"

Don’t copy this and put into your file, just add the last line: octoprint_enable=”YES”

Press Ctrl + c to save, exit out of the jail and restart it:

root@OctoPrint2:~ # exit
root@freenas:~ # iocage restart OctoPrint2

Now check the Jail assigned IP using port 5000 in your browser to access octoprint, it should be running at this point. rc.d is a little sketchy on FreeNAS Jails, if you try using the normal “service octoprint start/restart” commands, it will error out, but somehow rc.d manages to run the script when the Jail boots, for that sole reason, put “service octoprint onerestart” in “Restart OctoPrint” on the “Commands” tab when you start setting it up.

3 Likes

Thanks for the guide, it saved me some time trying to figure out the devfs ruleset. There are probably better init scripts, but I cooked this up quick and it seems to work.

/etc/rc.d/octoprint

#!bin/sh

# PROVIDE: octoprint
# REQUIRE: DEAMON NETWORKING

. /etc/rc.subr

base_path="/root/OctoPrint"

name="octoprint"
rcvar="octoprint_enable"
pidfile="${base_path}/octoprint.pid"

command="/usr/sbin/daemon"
command_args="-p ${pidfile} -f -c ${base_path}/venv/bin/octoprint serve --iknowwhatimdoing"

procname="${base_path}/venv/bin/octoprint"
command_interpreter="/root/OctoPrint/venv/bin/python2.7"

run_rc_command "$1"

Add octoprint_enable="YES" to rc.conf and everything should just work.

2 Likes

I'm happy to see that this guide helped you.
For sure the devfs ruleset can be a pain in the ass since there isn't a lot of documentation about it.
Thank you for providing your rc.d init script, i plan to test it on one of my jails and give some feedback.
If it works out fine for me, i'll include it in the guide and give you the proper credit.

This is a great resource for people running FreeBSD! I use OctoPrint on FreeBSD 12.1 and it works perfectly! Just wanted to add I made an rc.d startup script that uses a non-privileged user so you can avoid using root with the "iknowhatiamdoing". Here's the link to the script: FreeBSD 12.1 Guide with rc.d script to start OctoPrint Automatically and as a Service

1 Like

Thank you for this guide. I followed the guide without issues until where I create the virtual environment:
root@OctoPrint2:~/OctoPrint # virtualenv venv

I am now getting: virtualenv command not found

I have tried to google a solution but have so far come up with nothing. Any ideas?

Thanks!

sudo apt-get update            # Upgrade the list of things which can be updated
sudo apt-get -y upgrade        # Upgrade them globally
sudo pip install --upgrade pip # Upgrade pip globally
sudo pip install virtualenv    # Install virtualenv globally

Thanks, I actually got it working by using the following line instead of the one in the guide:

python2.7 -m virtualenv venv

i'll update the guide in the near future, thank you for pointing out that some commands have changed.

I also had to use python2.7 -m virtualenv venv, virtualenv by itself was not a found command.

Additionally to use pip I had to install it via:
pkg install py27-pip

I just updated the guide, rc.d script included, i hope it's more helpful for you all now.
If there's any problems, let me know.

1 Like