*New 11.3 series Release:
2019-10-19: XigmaNAS - released

*New 12.0 series Release:
2019-10-05: XigmaNAS - released!

*New 11.2 series Release:
2019-09-23: XigmaNAS - released!

We really need "Your" help on XigmaNAS https://translations.launchpad.net/xigmanas translations. Please help today!

Producing and hosting XigmaNAS costs money. Please consider donating for our project so that we can continue to offer you the best.
We need your support! eg: PAYPAL

How to use pkg --rootdir and unionfs to persistently install packages in N4f Embedded

XigmaNAS Scripts and shell tips
Forum rules
Set-Up GuideFAQsForum Rules
Post Reply
Posts: 45
Joined: 23 Jan 2014 15:31
Location: nelson twp, OH, USA
Status: Offline

How to use pkg --rootdir and unionfs to persistently install packages in N4f Embedded


Post by karlandtanya » 02 Oct 2016 22:30

This is a followup from a more naive method described earlier here.

The title pretty much tells you the method; if you google pkg -rootdir and unionfs it will become obvious.
A more correct way to install packages would be to use jails and extensions, BUT I haven't found out how to do zfs stuff like sending snapshots from inside a jail.

So...anyway here goes:

To install packages persistently, make a couple of persistent dirs:
There is no /opt according to hier, but it's not sacrilege to customize it (also according to hier).

Anyhow, like the penguin said...make the /usr/local hierarchy in /opt

Code: Select all

mkdir -p /mnt/data/opt/usr/local/bin         # Common utilities, programming tools, and applications.
mkdir -p /mnt/data/opt/usr/local/include     # Standard C include files.
mkdir -p /mnt/data/opt/usr/local/lib         # Archive libraries.
mkdir -p /mnt/data/opt/usr/local/libdata     # Miscellaneous utility data files.
mkdir -p /mnt/data/opt/usr/local/libexec     # System daemons and system utilities executed by other programs.
mkdir -p /mnt/data/opt/usr/local/obj         # Architecture-specific target tree produced by building the /usr/src tree.
mkdir -p /mnt/data/opt/usr/local/ports       # The FreeBSD Ports Collection (optional).
mkdir -p /mnt/data/opt/usr/local/sbin        # System daemons and system utilities executed by users.
mkdir -p /mnt/data/opt/usr/local/share       # Architecture-independent files.
mkdir -p /mnt/data/opt/usr/local/src         # BSD and/or local source files.
make a place for pkg to keep track of what it installed:

Code: Select all

mkdir -p /mnt/data/opt/var/db/pkg            # SQLite database for pkg
now you can install some packages using the --rootdir switch--for example zrep needs ksh and perl:

Code: Select all

pkg --rootdir /mnt/data/opt install ksh93
pkg --rootdir /mnt/data/opt install perl5
Make any links or other customizations:--for example (in addition to the zrep stuff), logs for dnsmasq extension are piling up, so I have newsyslog.conf stashed in ..._rebuild notes

Code: Select all

ln -s /mnt/data/opt/usr/local/bin/ksh93    /mnt/data/opt/usr/local/bin/ksh
ln -s /mnt/data/scripts/zrep    /mnt/data/opt/usr/local/sbin/zrep
mkdir -p /mnt/data/opt/usr/local/etc/newsyslog.conf.d
cp /mnt/data/backups/server_configs/_rebuild\ notes/newsyslog.conf    /mnt/data/opt/usr/local/etc/newsyslog.conf.d/newsyslog.conf
chown root:wheel /mnt/data/opt/usr/local/etc/newsyslog.conf.d/newsyslog.conf
chmod 644 /mnt/data/opt/usr/local/etc/newsyslog.conf.d/newsyslog.conf
Now everything is set up, you make the persistent mounts.
You don't really need to mount /var/db/pkg since you're only using pkg with the new rootdir. But it does make things consistent.
Note you also need to mount each time you reboot, so add these to postinit

Code: Select all

mkdir -p /var/db/pkg
mount_unionfs -o noatime -o below /mnt/data/opt/usr/local /usr/local
mount_unionfs -o noatime -o below /mnt/data/opt/var/db/pkg /var/db/pkg
FWIW, here's the newsyslog.conf that lives in /usr/local/etc/newsyslog.conf.d/ (by the opt above).
After installing dnsmasq I left the logging turned up to "informational" or some such and filled up the directory with logfiles.
Obviously not logging a bunch of extraneous stuff is a good idea, and the logs aren't persistent through reboots.
But nas4free has newsyslog built in, so it seems a good idea to use it. Modifying the one found in a jail:

Code: Select all

# configuration file for newsyslog
# $FreeBSD: stable/10/etc/newsyslog.conf 267113 2014-06-05 15:21:25Z bdrewery $
# Entries which do not specify the '/pid_file' field will cause the
# syslogd process to be signalled when that log file is rotated.  This
# action is only appropriate for log files which are written to by the
# syslogd process (ie, files listed in /etc/syslog.conf).  If there
# is no process which needs to be signalled when a given log file is
# rotated, then the entry for that file should include the 'N' flag.
# The 'flags' field is one or more of the letters: BCDGJNUXZ or a '-'.
# Note: some sites will want to select more restrictive protections than the
# defaults.  In particular, it may be desirable to switch many of the 644
# entries to 640 or 600.  For example, some sites will consider the
# contents of maillog, messages, and lpd-errs to be confidential.  In the
# future, these defaults may change to more conservative ones.
# logfilename							[owner:group]	mode	count	size	when	flags	[/pid_file]	[sig_num]
/var/log/daemon.log                 					600		3       100		*		J
/var/log/dnsmasq.log                					640     3       100     *       J
/var/log/ftp.log                    					600     3       100     *       J
/var/log/lastlog                    					644     3       100     *       J
/var/log/manageSnapshots.sh.data.log					644     3       100     *       J
/var/log/minidlna.log               					644     3       100     *       J
/var/log/notifications.log          					755     3       100     *       J
/var/log/rsync_client.log           					666     3       100     *       J
/var/log/rsync_local.log            					666     3       100     *       J
/var/log/rsyncd.log                 					600     3       100     *       J
/var/log/samba4/log.nmbd            					644     3       100     *       J
/var/log/samba4/log.smbd            					644     3       100     *       J
/var/log/smartd.log                 					600     3       100     *       J
/var/log/sshd.log                   					600     3       100     *       J
/var/log/system.log                 					600     3       100     *       J
/var/log/utx.lastlogin              					644     3       100     *       J
/var/log/utx.log                    					600     3       100     *       J
/var/log/zrep.log                   					644     3       100     *       J

Posts: 19
Joined: 11 Oct 2017 10:43
Status: Offline

Re: How to use pkg --rootdir and unionfs to persistently install packages in N4f Embedded


Post by dimitry » 19 Oct 2017 10:19

Nice thing! Work like a charm for me :) Thanks!

The only thing I tuned - I added unionfs mount as a Post-Init script, and all my installed packages are available after reboot.

Code: Select all

mount_unionfs -o noatime -o below /mnt/data/opt/usr/local /usr/local

Post Reply

Return to “Scripts and shell tips”