Run musicpd (mpd) inside FreeBSD jail.

These are rough notes that I made while going about installing mpd inside a jail.

  • Install and activate iocage if not already present. Follow the instructions for vnet jails.

  • Create /etc/devfs.rules if it does not already exist by copying over defaults.

    $ sudo cp /etc/defaults/devfs.rules /etc/devfs.rules
    
  • Append the text below to /etc/devfs.rules

    [devfsrules_desktop_jail=5]
    add include $devfsrules_hide_all
    add include $devfsrules_unhide_basic
    add include $devfsrules_unhide_login
    add path zfs unhide
    add path 'mixer*' unhide
    add path 'dsp*' unhide
    
  • Create a jail for mpd using iocage

    $ sudo iocage create -r 11.1-RELEASE --name=jukebox \
    ip4_addr="vnet0|10.0.0.9/16" \
    defaultrouter="10.0.0.1" \
    vnet=on \
    devfs_ruleset=5
    

    Note:

    • 10.0.0.1 => ip-address of default router (private ip of your FreeBSD host)
    • 10.0.0.9/16 => ip/subnet as you have configured for jails.
  • Add pf rule for NAT (/etc/pf.conf)

    ext_if = "em0"
    .
    .
    nat on $ext_if inet from 10.0.0.9 to any -> $ext_if
    

    Note:

    • em0 => ethernet device interface (ifconfig -l to list them)
  • Reload pf:

    $ sudo service pf reload
    
  • Add primary interface to bridge0 (default bridge for iocage-created jails)

    $ sudo ifconfig bridge0 addm em0
    $ sudo sysrc ifconfig_bridge0="addm em0 up"
    
  • Start the jail and login

    $ sudo iocage start jukebox
    $ sudo iocage console jukebox
    
  • Set appropriate output device inside jail

    # cat /dev/sndstat
        Installed devices:
        pcm0: <NVIDIA GT21x (HDMI/DP 8ch)> (play)
        pcm1: <NVIDIA GT21x (HDMI/DP 8ch)> (play)
        pcm2: <NVIDIA GT21x (HDMI/DP 8ch)> (play)
        pcm3: <NVIDIA GT21x (HDMI/DP 8ch)> (play)
        pcm4: <Realtek ALC899 (Analog 2.0+HP/2.0)> (play/rec)
        pcm5: <Realtek ALC899 (Rear Analog Line-out)> (play) default
        pcm6: <Intel Skylake (HDMI/DP 8ch)> (play)
        No devices installed from userspace.
    
    # sysctl hw.snd.default_unit=5
    
  • Test audio inside jail (keep master volume on speakers low, raise slowly):

    # cat /dev/random > /dev/dsp
    
  • Install musicpd (server) and musicpc (client)

    # pkg install musicpd musicpc
    
  • Enable the service

    In file /usr/local/etc/musicpd.conf, uncomment:

    bind_to_address         "any"
    

    Run the following commands:

    # sysrc musicpd_enable=YES
    # mkdir -p /var/mpd/.mpd/playlists
    # mkdir -p /var/mpd/music
    # service musicpd start
    
  • Add some music to your library

    # cd /var/mpd/music
    # fetch https://archive.org/download/gd87-04-03.sennme80.clark-miller.24898.sbeok.shnf/gd87-04-03d1t01.ogg
    # fetch https://archive.org/download/mogwai2008-09-08.fob.flac16/mogwai2008-09-08d1t01.flac
    # fetch https://archive.org/download/mono2009-03-23.flacf/mono09-03-23t03.ogg
    
  • Use mpc from same machine to test mpd

    In FreeBSD default shell (sh):
    $ setenv MPD_HOST 10.0.0.9
    
    In bash:
    $ export MPD_HOST="10.0.0.9"
    
    $ mpc update
    $ mpc listall | mpc add
    $ mpc volume 30
    $ mpc play
    
  • Test on MacOS

    $ brew install mpc
    $ export MPD_HOST="10.0.0.9"
    
    $ mpc update
    $ mpc listall | mpc add
    $ mpc volume 30
    $ mpc play
    
  • Or if you prefer, a text-ui client that’s easier to use. Once running, press 1 for help.

    $ brew install ncmpc
    $ export MPD_HOST="10.0.0.9"
    
    $ ncmpc