Loading...
Loading...
Marco Franssen

In the previous post of this series, I explained how to install Raspbian on your Raspberry Pi, enable SSH access, and configure a static IP for Wi-Fi. In this post, let's take that setup a little further: improve the command-line experience, mount network storage, and install Kodi.
On any server, I usually work with Git and Vim. I keep many of my Bash scripts and utilities in Git repositories, so I want to be able to clone them easily. My preferred command-line editor is Vim, and I also keep its configuration, themes, and coding plugins in Git.
I also make a few tweaks to my Bash shell to make it more convenient to use.
Let's start by installing Git and Vim.
sudo apt install -y git vimThe first thing I always do is configure Vim for a consistent experience across all the servers I access. I've made a publicly available shell script for this. You can run it by fetching it with curl and piping it to Bash as follows.
curl -sS https://raw.githubusercontent.com/marcofranssen/dotfiles/master/install_plugins_vim.sh | bashNext, I want Git available so I can pull the repositories containing my scripts. I wouldn't be me if I didn't have a configuration for my Git setup as well.
curl -sSo ~/.gitconfig https://raw.githubusercontent.com/marcofranssen/dotfiles/master/.gitconfig
GIT_EMAIL=your.email@mail.tld
git config --global user.name="Marco Franssen"
git config --global user.email=$GIT_EMAIL
sed -i "s/AUTHOR_EMAIL/$GIT_EMAIL/" ~/.gitconfigMy Git configuration includes some handy aliases to speed up my command-line workflow.
To improve the experience with the arrow-up and arrow-down keys in Bash, I always use the following .inputrc.
curl -sSo ~/.inputrc https://raw.githubusercontent.com/marcofranssen/dotfiles/master/.inputrcIf your Raspberry Pi's SD card doesn't have enough storage, you have two options: connect an external disk or mount a network volume. Let's look at the second option. Below, I'll show you how I mount three shared folders from my Synology NAS so Kodi can use them. More on Kodi later. If you need help setting up NFS shares on your Synology NAS, check out this manual.
Assuming we have three NFS shares available, we'll create three folders on the Raspberry Pi to mount them.
sudo apt install -y nfs-common
sudo mdkir -p /mnt/nfs/{video,music,photo}The script above first installs the latest available nfs-common package. Then we create three directories for the NFS shares I want to mount.
$ tree -d -L 2 /mnt/
/mnt/
└── nfs
├── music
├── photo
└── videoNow that the mount directories are available, I'll update /etc/fstab to mount the shares automatically on the next boot. I'll also mount them manually now so I don't need to reboot.
sudo tee -a /etc/fstab <<EOL
192.168.1.2:/volume1/video /mnt/nfs/video nfs auto,nofail,noatime,nolock,intr,tcp,actimeo=1800 0 0
192.168.1.2:/volume1/music /mnt/nfs/music nfs auto,nofail,noatime,nolock,intr,tcp,actimeo=1800 0 0
192.168.1.2:/volume1/photo /mnt/nfs/photo nfs auto,nofail,noatime,nolock,intr,tcp,actimeo=1800 0 0
EOL
sudo mount /mnt/nfs/video
sudo mount /mnt/nfs/music
sudo mount /mnt/nfs/photoMake sure you replace the IP address above with your own and adjust the folders to point to the NFS shares on your NAS.
Now that I have access to the files on my NAS, I'm ready to install Kodi. I'll also create a dedicated user for Kodi for a more secure setup.
$ sudo apt install -y kodi
$ sudo adduser --disabled-password --gecos "Kodi Media Center" kodi
Adding user 'kodi' ...
Adding new group 'kodi' (1002) ...
Adding new user 'kodi' (1002) with group 'kodi' ...
Creating home directory '/home/kodi' ...
Copying files from '/etc/skel' ...
$ sudo usermod -aG audio,video,input kodiIt's important to add the Kodi user to the audio and video groups so it has permission to use those devices. Add it to the input group as well if you want a keyboard to work. You can find more information about groups on a Debian-based OS here. Some guides also add the user to plugdev, but because I mount the NFS shares outside Kodi, this isn't required. I also found slightly better performance when mounting NFS shares at the OS level rather than through Kodi, with the added benefit of being able to access them outside Kodi.
First, I want Kodi to start automatically when my Raspberry Pi boots. For that, we'll add a systemd service. The Bash script below creates the service file, which we'll manage with systemctl.
sudo tee /etc/systemd/system/kodi.service <<EOL
[Unit]
Description = Kodi Media Center
# if you don't need the MySQL DB backend, this should be sufficient
After = systemd-user-sessions.service network.target sound.target
# if you need the MySQL DB backend, use this block instead of the previous
# After = systemd-user-sessions.service network.target sound.target mysql.service
# Wants = mysql.service
[Service]
User = kodi
Group = kodi
Type = simple
ExecStart = /usr/bin/kodi --standalone
Restart = always
RestartSec = 30
[Install]
WantedBy = multi-user.target
EOLAs you can see, this service starts Kodi using the kodi user we created.
Now it's time to enable the Kodi service and start Kodi.
$ sudo systemctl daemon-reload
$ sudo systemctl enable kodi
$ sudo systemctl start kodi
$ sudo systemctl status kodi
● kodi.service - Kodi Media Center
Loaded: loaded (/etc/systemd/system/kodi.service; enabled; vendor preset: enabled)
Active: active (running) since Thu 2019-08-04 11:17:44 CEST; 6s ago
Main PID: 8380 (kodi-standalone)
Tasks: 32 (limit: 4915)
CGroup: /system.slice/kodi.service
├─8380 /bin/sh /usr/bin/kodi-standalone
├─8387 /bin/sh /usr/bin/kodi --standalone
└─8415 /usr/lib/arm-linux-gnueabihf/kodi/kodi-rbpi_v7 --lircdev /var/run/lirc/lircd --standalone
Aug 04 11:17:44 raspberrypi systemd[1]: Started Kodi Media Center.After a reboot, the systemd service will automatically start Kodi because we've enabled it.
Now that Kodi is installed and running, we can configure it. Although we could do all of this through the GUI, I prefer using the terminal because it's faster for me.
I always enable the web server so I can use my mobile app as a remote control for Kodi. When Kodi starts for the first time, it creates a folder named .kodi in the user's home directory. You'll find various XML configuration files there. Since we started Kodi using the kodi user, the folder is at /home/kodi/.kodi.
In the guisettings.xml file, you can enable the web server. The Bash commands below enable it and adjust a few other settings.
sudo sed -i '/id="services.webserver"/c\ <setting id="services.webserver">true</setting>' /home/kodi/.kodi/userdata/guisettings.xml
sudo sed -i '/id="lookandfeel.skinzoom"/c\ <setting id="lookandfeel.skinzoom">-4</setting>' /home/kodi/.kodi/userdata/guisettings.xml
sudo sed -i '/id="myvideos.extractflags"/c\ <setting id="myvideos.extractflags">true</setting>' /home/kodi/.kodi/userdata/guisettings.xml
sudo sed -i '/id="myvideos.extractthumb"/c\ <setting id="myvideos.extractthumb">true</setting>' /home/kodi/.kodi/userdata/guisettings.xml
sudo sed -i '/id="myvideos.usetags"/c\ <setting id="myvideos.usetags">true</setting>' /home/kodi/.kodi/userdata/guisettings.xml
sudo sed -i '/id="services.airplay"/c\ <setting id="services.airplay">true</setting>' /home/kodi/.kodi/userdata/guisettings.xml
sudo sed -i '/id="services.airplayvideosupport"/c\ <setting id="services.airplayvideosupport">true</setting>' /home/kodi/.kodi/userdata/guisettings.xml
sudo sed -i '/id="videolibrary.groupmoviesets"/c\ <setting id="videolibrary.groupmoviesets">true</setting>' /home/kodi/.kodi/userdata/guisettings.xml
sudo sed -i '/id="videolibrary.tvshowsselectfirstunwatcheditem"/c\ <setting id="videolibrary.tvshowsselectfirstunwatcheditem">true</setting>' /home/kodi/.kodi/userdata/guisettings.xml
sudo sed -i '/id="videolibrary.updateonstartup"/c\ <setting id="videolibrary.updateonstartup">true</setting>' /home/kodi/.kodi/userdata/guisettings.xml
sudo sed -i '/id="weather.addon"/c\ <setting id="weather.addon">weather.yahoo</setting>' /home/kodi/.kodi/userdata/guisettings.xmlYou might prefer different settings, but this gives you an idea of what you can automate in your own setup.
Next, I'll configure the Kodi sources.
sudo tee /home/kodi/.kodi/userdata/sources.xml <<EOL
<sources>
<programs>
<default pathversion="1"></default>
</programs>
<video>
<default pathversion="1"></default>
<source>
<name>Home</name>
<path pathversion="1">/mnt/nfs/video/Home/</path>
<allowsharing>true</allowsharing>
</source>
</video>
<music>
<default pathversion="1"></default>
<source>
<name>music</name>
<path pathversion="1">/mnt/nfs/music/</path>
<allowsharing>true</allowsharing>
</source>
</music>
<pictures>
<default pathversion="1"></default>
<source>
<name>photo</name>
<path pathversion="1">/mnt/nfs/photo/</path>
<allowsharing>true</allowsharing>
</source>
</pictures>
<files>
<default pathversion="1"></default>
</files>
<games>
<default pathversion="1"></default>
</games>
</sources>
EOL
sudo chown kodi:kodi /home/kodi/.kodi/userdata/sources.xmlSince sources.xml is newly created, I need to make sure it's owned by the Kodi user. With the configuration above, we've added my music library, photos, and home video folder as data sources for Kodi. This lets me enjoy all my family moments on my TV with some nice music :-). Feel free to add more sources, such as your TV shows and movies. The last step is to restart Kodi so it picks up the configuration.
$ sudo systemctl restart kodi
$ sudo systemctl status kodi
● kodi.service - Kodi Media Center
Loaded: loaded (/etc/systemd/system/kodi.service; enabled; vendor preset: enabled)
Active: active (running) since Thu 2019-08-04 11:23:11 CEST; 5s ago
Main PID: 8604 (kodi-standalone)
Tasks: 33 (limit: 4915)
CGroup: /system.slice/kodi.service
├─8604 /bin/sh /usr/bin/kodi-standalone
├─8611 /bin/sh /usr/bin/kodi --standalone
└─8639 /usr/lib/arm-linux-gnueabihf/kodi/kodi-rbpi_v7 --lircdev /var/run/lirc/lircd --standalone
Aug 04 11:23:11 raspberrypi systemd[1]: Started Kodi Media Center.Thanks for reading this post, and enjoy your Kodi setup.
Marco Franssen
Set up SSH keys, an SSH agent, host aliases, and Bash autocompletion to connect to servers and use Git from macOS or Git Bash on Windows.
Marco Franssen
Manage Go development tools with Go modules using a tools.go file, build constraints, and a Makefile to keep local and CI tool versions aligned.
Marco Franssen
Flash a Raspbian image with Etcher, enable SSH, update packages, and configure Wi-Fi with a static IP in this Raspberry Pi Stretch setup walkthrough.
Marco Franssen
Follow my Raspberry Pi upgrade from Raspbian Stretch to Buster: update packages, change APT sources, resolve broken packages, clean up, and verify the release.