MEeedia Server


August 31, 2009

Contents

1 Background
2 Objective
3 Hardware and System Setup
 3.1 Install Ubuntu server
 3.2 Sound
4 Software
 4.1 X Window Manager
 4.2 SSH (Remote Access)
  4.2.1 Automatic Login
  4.2.2 Desktop-like Access
 4.3 NFS
5 Media Software
 5.1 Music On Console
 5.2 Stream Music
 5.3 Streaming Video
6 Sharing Software
 6.1 Torrent
 6.2 OneSwarm
7 Other Useful Information/Tools
 7.1 Screen Backlight
 7.2 Reduce Disk Writes
  7.2.1 Reduce writing of log-files
 7.3 Convert Music
  7.3.1 AAC/M4A to MP3
 7.4 Backup Script
 7.5 Miscellaneous

1 Background

I bought an Asus Eee-PC 701 4G in spring 2008 but I realised after some time it was too small to not be used ”painfully”, at least compared to my MacBook. Since noone is interested of buying that computer I wanted to create a media server of it. The disk space inside is only 4 gigabytes as well as the SD card, i.e. totally 8 gigabytes. However, that’s not enough if you want to store music and films. So I bought a Western Digital Extern HDD My Passport Essential 500GB since it is small, quiet and does not require an external power source (it is USB-powered). The downside is that it is a bit more expensive that those larger in dimension.

As base system I went for Ubuntu Server since I am familiar with Ubuntu and I know how to set up most needed software.

2 Objective

Following the steps below will do mainly this:

Since we create a server, we’ll use the command line. Therefore lots of commands are given here, most often preceeded by a dollar sign ($). It is not meant to be typed, but indicates what has to be written in the console. Yeah, I guess you already knew that!

Also, my username is hans. Change where appropriate.

3 Hardware and System Setup

3.1 Install Ubuntu server

I installed using netboot installer and GRUB, since I had already a Linux distribution installed. Find appropriate version and look for files named linux and initrd.gz at this location: http://cdimage.ubuntu.com/netboot/
Install to harddrive (SSD). Just answer all questions, at the end I only checked the "Ubuntu Base Server" (or something like that, the first option in list).

I didn’t have the external harddrive connected during install so I afterwards added it to fstab as /media/Titan. If unsure how it is done, the best way is to connect it before install and let the installer take care of it. The way I mounted it physically is shown in figure 1.


PIC PIC

Figure 1: Front and backside of the Eee Pc with the external drive fixed with adhesive tape! Be careful to have good tape that will not let the drive fall off (which happened to me once!).


3.2 Sound

Install ALSA:

$ sudo apt-get install alsa-base alsa-utils alsa-tools libasound2

Running lspci gives:

Audio device: Intel Corporation 82801FB/FBM/FR/FW/FRW (ICH6 Family) High Definition Audio Controller (rev 04)

To make it work run

$ modprobe snd-hda-intel
$ asoundconf list 
Names of available sound cards: 
Intel 
$ asoundconf set-default-card Intel

Test if sound does work with:

$ aplay -l 
aplay: device_list:217: no soundcards found...

In this case it is not and may be solved by adding the user to the same group as those listed in /dev/snd/, probably audio. Fix with

$ sudo usermod -a -G audio hans

then logout and relogin. You should now be able to set volume with alsamixer.

4 Software

4.1 X Window Manager

OneSwarm currently requires X to run. Fluxbox is a small window manager.

$ sudo apt-get install xorg fluxbox

Start X with startx.

4.2 SSH (Remote Access)

Install the SSH server with:

$ sudo apt-get install openssh-server

If you do not want to be logged out automatically after inactivity, add the following to /etc/ssh/sshd_config:

KeepAlive yes 
ClientAliveInterval 60

4.2.1 Automatic Login

Based on guide at https://help.ubuntu.com/8.10/serverguide/C/openssh-server.html. Generate the keys on remote machine (not server), from a terminal prompt enter:

ssh-keygen -t dsa

This will generate the keys using a DSA authentication identity of the user. During the process you will be prompted for a password. Simply hit Enter when prompted to create the key.

By default the public key is saved in the file ~/.ssh/id_dsa.pub, while ~/.ssh/id_dsa is the private key. Now copy the id_dsa.pub file to the remote host and appended it to ~/.ssh/authorized_key:

cat id_dsa.pub >> .ssh/authorized_key

Finally, double check the permissions on the authorized_key file, only the authenticated user should have read and write permissions.

Uncomment line in /etc/ssh/sshd_config:

AuthorizedKeysFile      %h/.ssh/authorized_key

To disable login without this key, also change to this:

PasswordAuthentication no

It will now not be possible to login with a password as before, but only with these keys. Don’t forget to restart the SSH server:

$ sudo /etc/init.d/ssh restart

4.2.2 Desktop-like Access

If you have X installed you can run it from another computer (similar to VNC) using

$ Xnest -geometry 800x600 :1 & DISPLAY=:1 ssh -X -Y -p 22 USER@REMOTEIP /usr/bin/fluxbox

4.3 NFS

Based on http://ubuntuforums.org/showthread.php?t=249889, this will install an NFS server which gives better transfer speeds than mounting as SSH. In this case I’ll use unfs3 since it can handle mappings of user id’s, good if your user on local machine has different UID than that on the server. Install with:

$ sudo apt-get install unfs3 portmap

Add share to /etc/exports:

# Read-only for 192.168.1.1-255: 
/media/Titan    192.168.1.0/24(ro,async,insecure,map_static=/etc/nfs/hikari.map) 
 
# ro = read-only, rw = read-write (however, it does not seem to work from my Mac) 
# insecure for use with Mac OS X. 
# static mapping of UID/GID from  http://ubuntuforums.org/showthread.php?p=4387032#post4387032

Use id username to see username’s UID and GID. Then set up file /etc/nfs/hikari.map with mappings:

# NFS UID and GID mapping for Hikari 
#       remote  server 
uid     501     1000 
gid     20      1000

Restart server:

$ sudo /etc/init.d/unfs3 restart

5 Media Software

5.1 Music On Console

Install Music On Console Player:

$ sudo apt-get install moc

To start the player (including the server), run mocp.

This program plays the sound locally, i.e. through the speakers of the Eee PC or connect it to an amplifier. It plays FLAC, MP3, OGG and Wave. Information on how to convert AAC to playable format, read 7.3.

5.2 Stream Music

Enable “remote access” in OneSwarm, see section 6.2.

5.3 Streaming Video

Due to hardware limitations this may not work flawlessly.

Install VLC, FFMPEG and some codecs using

$ sudo apt-get install vlc ffmpeg libavcodec-unstripped-52

On server terminal, write

vlc --sout ’#transcode{vcodec=mp4v,acodec=mpga,vb=800,ab=128,deinterlace}:rtp{mux=ts,dst=192.168.1.2,sap,name=Video}’ -I dummy video.avi

On client machine (IP: 192.168.1.2), start VLC check the SAP announcements setting. Name ”Video” will show up, click to start playing. However, it is streamed and pausing is not possible.

6 Sharing Software

6.1 Torrent

A small and text-based torrent software is rTorrent. Install by

$ sudo apt-get install rtorrent

User guide found at http://libtorrent.rakshasa.no/wiki/RTorrentUserGuide. When logged in to the server by SSH it is very convenient to use the program screen. It is then possible to initiate an rtorrent session, then log off (or perform other tasks) and let it continue. The command to use is

$ screen rtorrent

Hiding the “screen” is done by Ctrl+a+d. You may now disconnect the SSH session and resume it another time from any other computer. To resume the screen again, use

$ screen -r

6.2 OneSwarm

Download OneSwarm, current version is 0.6, from http://oneswarm.cs.washington.edu/. It requires Java and a browser, for example:

$ sudo apt-get install sun-java6-bin firefox

Set it up from within X, see 4.1.

It is possible to use the X virtual frame buffer which doesn’t need to run X.org. The downside is that you can currently not add swarms manually (only automatically). But if you’ve set up remote access you can as always access your swarms. To install the X virtual frame buffer, run

$ sudo apt-get install xvfb

It is then possible to run OneSwarm from the terminal using

Xvfb :1 & env DISPLAY=:1 OneSwarm

Using Xvbf saves resources compared to using normal X. But if you need to manually add files, stop OneSwarm, start normal X with startx, open Firefox and then add files. Hopefully OneSwarm developers will implement functionality for adding files remotely soon.

7 Other Useful Information/Tools

7.1 Screen Backlight

Turn the backlight of the LCD on or off (NB: it will not turn on automatically by keyboard or mouse use!!):

$ sudo vbetool dpms on 
$ sudo vbetool dpms off

7.2 Reduce Disk Writes

Make files written to /tmp only be in RAM instead, hence these will be lost when computer shutdowns or reboots. Add this line to /etc/fstab:

tmpfs      /tmp            tmpfs        defaults           0    0

7.2.1 Reduce writing of log-files

Reduce disk writes by putting the /var/log directory into RAM, and save it to disk only on halt or reboot, and eventually every n:th hour or so.

Add this line to /etc/fstab:

tmpfs      /var/log        tmpfs        defaults           0    0

Create the Upstart jobs logsave and logload, save to /etc/event.d/. logsave reads:

start on runlevel [06] 
start on logsave 
 
script 
 
FILE=/var/log.tar.gz 
cd / 
if [ -f $FILE ]; then 
        rm $FILE 
fi 
tar czf $FILE /var/log 
 
end script

And logload reads:

start on runlevel 2 
 
script 
 
FILE=/var/log.tar.gz 
cd / 
tar xzf $FILE 
 
end script

Eventually add command initctl emit logsave to crontab (see section 7.4 for information).

Reduce what is logged by editing /etc/syslog.conf.

To get a warning when the /var/log memory is full, put these lines in /etc/bash.bashrc which is run every time you log in:

PROCENT=$(df -h | grep /var/log | awk ’{print $5}’ | cut -d "%" -f1 - ) 
WARNLEVEL=80 
if [ $PROCENT -gt $WARNLEVEL ]; then 
echo "WARNING: /var/log is $PROCENT % full!!!" 
fi

7.3 Convert Music

Following based on information from http://gimpel.gi.funpic.de/wiki/index.php?title=Howto:convert_aac/mp4_to_wav/mp3/ogg_on_Linux.

7.3.1 AAC/M4A to MP3

Install faad (read AAC-files) and lame (MP3-converter) with the following command:

$ sudo apt-get install faad lame

Now convert files with

$ faad --quiet -o - inputfile.m4a | lame --quiet - outputfile.mp3

To convert many files at once, use this script:

#!/bin/bash 
# Script to convert AAC/M4A to MP3-files since my version of MOC cannot 
# play those files. It uses faad to read the music stream and lame 
# to convert to MP3. 
# 
# Created by Hans, 2009-08-11. 
 
# Define temporary file: 
INFOFILE=infofile.tmp 
REMOVE=true    # If true, remove after conversion 
 
# Function to extract music information from a file: 
function extract() 
{ 
  grep $1 $INFOFILE | cut --delimiter=: -f2 | sed -e s/^ //’ 
} 
 
if [ $# -eq 0 ]; then 
        echo "Usage: $0 file(s)" 
        echo " -- or with find --" 
        echo " find -H . -name ’*.m4a -print0 | xargs -0 $0" 
        exit 1 
fi 
 
# Loop through all files. 
for FILE; do 
        # Remove the extension: 
        DIR=$(dirname "$FILE") 
        F=$(basename "$FILE" .m4a) 
        F=$(basename "$F" .aac) 
        F=$DIR/$F 
 
        # Write track information to the infofile (2&> redirects both stdout and stderr to file): 
        faad -i "$FILE" 2&> $INFOFILE 
        # Extract titles etc: 
        TITLE=$(extract title) 
        ARTIST=$(extract artist) 
        #WRITER=$(extract writer) 
        ALBUM=$(extract album) 
        DATE=$(extract date) 
        GENRE=$(extract genre) 
        TRACK=$(extract track) 
 
        echo "$FILE..." 
        # Convert the song using faad and lame: 
        faad --quiet -o - "$FILE" | lame --quiet -h --tt "$TITLE" --ta "$ARTIST" --tl "$ALBUM" --ty "$DATE" --tn "$TRACK" --tg "$GENRE" - "$F.mp3"; 
 
        # Remove the AAC-file: 
        $REMOVE && rm "$FILE" 
done 
 
# Remove the temporary info file: 
rm $INFOFILE 
 
exit 0

7.4 Backup Script

The external disk is also used as backup of important files. Backups should according to Linux Standard Base (LSB) be stored at /mnt/backup. Therefore I made a soft link to the external disk. The following script is based on script found at https://help.ubuntu.com/8.10/serverguide/C/backup-shellscripts.html and is run once a day (in my case below at 23:45). Put

# m h  dom mon dow   command 
45 23 * * * /usr/local/bin/sysbackup.sh

in the crontab file (edit it by running sudo crontab -e). The script, sysbackup.sh follows below and the filename of the backup file will contain the name of the day, hence only the last 7 days will be stored.

#!/bin/sh 
#################################### 
# 
# Backup to NFS mount script. 
# 
#################################### 
# Script hämtat från https://help.ubuntu.com/8.10/serverguide/C/backup-shellscripts.html 
 
 
# What to backup. 
backup_files="/etc /boot /home /root /usr/local" 
 
# Where to backup to. 
dest="/mnt/backup" 
 
# Create archive filename. 
day=$(date +%A) 
hostname=$(hostname -s) 
archive_file="$hostname-$day.tgz" 
 
# Print start status message. 
echo "Backing up $backup_files to $dest/$archive_file" 
date 
echo 
 
# Backup the files using tar. 
tar czf $dest/$archive_file $backup_files 
 
echo 
echo "Done" 
date 
 
ls -lhL $dest 
 
 
########################### 
## Testa arkiv med: 
# tar -tzvf /mnt/backup/eee-chan-Monday.tgz 
## Återställ filen /etc/hosts till /tmp: 
# tar -xzvf /mnt/backup/eee-chan-Monday.tgz -C /tmp etc/hosts 
## Återställ allt: 
# cd /; sudo tar -xzvf /mnt/backup/eee-chan-Monday.tgz

7.5 Miscellaneous

lltag

Music Tag Editor for console.