# Icecast Streaming media server in IOCage Jail



## zader (Jan 22, 2020)

*Part I : Install update and mount dataset /playlist in icecast jail as /media/playlist*

Host configuration:
`iocage create -n "name.domain.local" -r latest vnet="on" allow_raw_sockets="1" boot="on" defaultrouter="10.100.10.1" ip4_addr="10.100.10.200/24" --thickjail`
`zfs create -o mountpoint=/playlist zpool/playlist`
`mkdir /zpool/iocage/jails/name.domain.local/root/media/playlists`
`iocage fstab -a name.domain.local /playlists /media/playlists nullfs rw 0 0`
`iocage console name`

Everything else is done within the jail:
Update the Jail and install and configure portmaster
`pkg`
`portsnap fetch extract`
`cd /usr/ports/ports-mgmt/portmaster`
`make install clean && rehash`

configure /usr/local/etc/portmaster.rc
`cat <<EOF > /usr/local/etc/portmaster.rc
PM_LOG=/var/log/portmaster.log
PM_NO_CONFIRM=pm_no_confirm
PM_DEL_BUILD_ONLY=pm_dbo
SAVE_SHARED=wopt
SAVE_SHARED=wopt
ALWAYS_SCRUB_DISTFILES=dopt
BACKUP=bopt
EOF`

*Part II : Install Icecast2*

`portmaster` audio/icecast2
echo "icecast_enable="YES"" >> /etc/rc.conf
`cd /usr/local/etc`
`cp icecast.xml.sample icecast.xml`
`vi icecast.xml`
#1 change the following (clients is the total max clients, sources is the total number of stream)

```
<clients>100</clients>
        <sources>12</sources>
```
#2 change the default password (you will need for the ics configuration and admin page)
`:%s/hackme/yourpassword/g`
`:%s/hackmemore/yourpassword/g`
#3 towards the end uncomment and change the user/group

```
<security>
        <chroot>0</chroot>
        <changeowner>
            <user>icecast</user>
            <group>radio</group>
        </changeowner>
    </security>
</icecast>
```
`:wq!`

create and own the log file
`mkdir /var/log/icecast`
`chown -R nobody:nogroup /var/log/icecast`

start the icecast2 service
`service icecast2 start`
check the log for errors
`tail /var/log/icecast/error.log`

you should see something like

```
INFO main/main Icecast 2.4.0 server started
INFO connection/get_ssl_certificate No SSL capability on any configured ports
INFO yp/yp_update_thread YP update thread started
```

checkout your new server at
`http://localhost/:8000`

*Part III : Install Ices and prepare some media*

`portmaster [port] audio/ices [/port]
[cmd]pw groupadd radio && pw useradd radio -g radio –m`
`mkdir /var/log/ices`
`chown –R radio:radio /var/log/ices`

Let's get some media ready.  You can use what ever converter you like.  I highly recommend getting poikosoft's ezcdda converter.. its well worth the few dollars and they have not charged for updates in decades.  The registered version allows for fast mass conversion / ripping and burning.









						MP3 Converter Software | FLAC to MP3, WAV to MP3 conversion optimized for best audio quality
					

Convert MP3, FLAC and all audio files to/from Apple Lossless, AAC, Opus, WAV, DSD, M4A, plus more audio formats. Supports over 60 media file formats. Converter FLAC to MP3 is optimized for highest audio quality.




					www.poikosoft.com
				




I also found it handy to set up a vsftp/smb or other shared volume for converting and transferring files.  but that's optional

click on audio converter
drag and drop your mp3s from the top to the middle.

at the bottom right select:
.ogg Q10 44khz
output folder  (point it to your playlist share)

once you have your music files converted

`mkdir /media/playlist/rock`
`mv /convertedfiles/* /media/playlist/rock`

You should now have ices installed and a folder of music to create a play list from.

*Part IV : Creating playlists, xml configuration and making your own daemon*

this section consists of 4 parts
I created a simple script to save you some time.. lets make a rock playlist. you will need to change the genere, name, password and bit rates etc.

`pw groupadd radio && pw useradd radio -g radio –m`
`mkdir /var/log/ices`
`chown –R radio:radio /var/log/ices`
`chown -R radio:radio /home/radio`
`mkdir /home/radio/conf`
`cd /home/radio`
`vi build-playlist.sh`
modify and paste..  (change the path to suit your needs)

```
#!/bin/sh

cd /media/playlists/$1
find /media/playlists/$1 -depth -name "*.ogg" -print | sort --random-sort > /home/radio/conf/$1.txt

cat <<EOF > /home/radio/conf/$1.xml
<?xml version="1.0"?>
<ices>
    <background>1</background>
    <logpath>/var/log/ices</logpath>
    <logfile>$1.log</logfile>
    <loglevel>1</loglevel>
    <consolelog>0</consolelog>

    <pidfile>/home/radio/$1.pid</pidfile>

    <stream>
        <metadata>
            <name>$1</name>
            <genre>ROCK!</genre>
            <description>ROCK till U Drop</description>
        </metadata>

        <input>
            <module>playlist</module>
            <param name="type">basic</param>
            <param name="file">/home/radio/conf/$1.txt</param>
            <param name="random">1</param>
            <param name="restart-after-reread">1</param>
            <param name="once">0</param>
        </input>

        <instance>
            <hostname>name.domain.local</hostname>
            <port>8000</port>
            <password>PASSWORD</password>
            <mount>/$1.ogg</mount>
            <reconnectdelay>2</reconnectdelay>
            <reconnectattempts>5</reconnectattempts>
            <maxqueuelength>80</maxqueuelength>

            <encode>
                <nominal-bitrate>500000</nominal-bitrate>
                <samplerate>44100</samplerate>
                <channels>2</channels>
            </encode>
        </instance>
     </stream>
</ices>
EOF
```

`:wq!`
`chmod +x build-playlist.sh`

once your default template is created ..
`./build-playlist.sh rock`
this will search the directory /media/playlists/rock .. generate a text list of all the files in the directory .. then randomise the list and write it to the user radios home directory .. conf as rock.txt  .. the ics configuration is written as rock.xml  ..

if you make a a directory called /media/playlists/country . to add it use `./build-playlist.sh country`

You should now have
/media/playlists/rock  with some .ogg files
/home/radio/conf/rock.txt
/home/radio/conf/rock.xml
/home/radio/build-playlist.sh

the next step is to create a daemon (you can add this to the build-playlist script, but for sake of ease I'm going to split it up.. just change the demo "rock" to $1

`cd /usr/local/etc/rc.d`
`vi rock`
paste in the daemon template

```
#!/bin/sh
#
# PROVIDE: rock
# REQUIRE: DAEMON icecast2
# BEFORE:  LOGIN
# KEYWORD: shutdown

. /etc/rc.subr

name="rock"
rcvar=rock_enable

command="/usr/local/bin/ices"
extra_commands="reload"
pidfile="/home/radio/rock.pid"
sig_reload="USR1"

load_rc_config "$name"
: ${rock_enable="NO"}
: ${rock_config="/home/radio/conf/super_rock.xml"}
: ${rock_flags="${super_rock_config}"}
: ${rock_user="radio"}
required_files="${rock_config}"

run_rc_command "$1"
```
`:wq!`
you should now have a "rock"daemon (you can search / replace rock and replace it with the $1 if you want to add this step to the first script)

finally we need to add it to rc.conf just like any other service
echo "rock_enable="YES"" >> /etc/rc.conf
`service icecast2 start`
`service rock start`

and your shiny new server should be running at



			http://localhost:8000
		


icecast2 should be installed with ices random playlists all working as services within the radio users home directory.. all nicely housed in iocage and updated each day / startup.

I also made another script that I added with a @restart cron tab to systematically start the streams.. (i found they try and start to fast sometimes)
you can crontab -e something like this.


```
#!/bin/sh

# nightly/startup script

service icecast restart
sleep 3
rm -rf /home/radio/conf/*.txt
sleep 4
pkill -u radio

find /media/playlists/rock -depth -name "*.ogg" -print | sort --random-sort > /home/radio/conf/rock.txt
sleep 3
service rock start
```

I crontabed this script to run once per day and as a reboot.. so anything added to the playlist dataset is automatically updated, re-shuffled and restarted.

rock on and have fun


----------

