How to create a Media Server out of a router

How to create a Media Server out of a router

Hello folks. I’m here with yet another tutorial. This time, we are going to create a media server out of a router. Sounds cool, ain’t it? Let’s do it then.

Before proceeding, I want you to go through the prerequisites for this tutorial. First of all, your router should have OpenWrt installed on your router. You can install it by following links like this. Secondly, your router should have a USB port. We will use this port to connect the mass storage device (in our case, a 16GB flash drive).  The router I’m using is TP-LINK 1043ND. The version of OpenWrt I’m using is Chaos Calmer 15.05. Let’s start!

router_media_server (copy)

 

Assuming you are connected to your router through WLAN or LAN, ssh into the router console.

$ ssh root@192.168.1.1

We need to install necessary packages first. Run following command in the router’s console.

# opkg update

# opkg install kmod-usb-core kmod-usb2 kmod-usb-storage kmod-usb-storage-extras block-mount kmod-usb-uhci kmod-usb-ohci kmod-fs-btrfs kmod-nls-cp437 kmod-nls-iso8859-1 block-mount luci-app-samba luci-i18n-samba-en samba36-server minidlna luci-app-minidlna

Notice package kmod-fs-btrfs. It is used to recognize btrfs filesystem i.e. if your USB storage is formatted with that filesystem. If it is formatted with a FAT filesystem, use kmod-fs-exfat package.

The above command will install packages that will allow us to mount the USB storage and run a Samba server on top of it. minidlna package will allow us to create a media server on top of the mounted USB storage.

We need to tell the router to auto mount the USB storage so that we do not need to mount the USB storage manually each time the router boots up. Run following commands:

# mkdir /mnt/sda1

# block detect > /etc/config/fstab/etc/init.d/fstab enable

Plugin the USB drive and reboot the router. Run following command and you will see your USB drive mounted.

# df -h

We will now configure Samba server with/without authentication. Note that we will not use any authentication for our media server in this tutorial.

I. Without authentication: We have to edit /etc/config/samba to tell samba where are the share points in the system. This file allows us to share file system as well as the mount point.

# vi /etc/config/samba

My config file looks like below:

config samba
option name 'OpenWrt'
option workgroup 'WORKGROUP'
option description 'OpenWrt'
option homes '1'

config sambashare
option name 'sda1'
option path '/mnt/sda1'
option read_only 'no'
option create_mask '0766'
option dir_mask '0766'
option guest_ok 'no'
option users 'nobody'

config sambashare
option name 'root'
option path '/'
option read_only 'no'
option guest_ok 'no'
option create_mask '0766'
option dir_mask '0766'

Now we will edit /etc/samba/smb.conf. What is smb.conf?

My smb.conf file looks like this:

[global]
netbios name = OpenWrt
display charset = UTF-8
interfaces = 127.0.0.1/8 lo 192.168.1.1/24 br-lan
server string = OpenWrt
unix charset = UTF-8
workgroup = WORKGROUP
browseable = yes
deadtime = 30
domain master = yes
encrypt passwords = true
enable core files = no
guest account = nobody
guest ok = yes
invalid users = root
local master = yes
load printers = no
map to guest = Bad User
max protocol = SMB2
min receivefile size = 16384
null passwords = yes
obey pam restrictions = yes
os level = 20
passdb backend = smbpasswd
preferred master = yes
printable = no
security = user
smb encrypt = disabled
smb passwd file = /etc/samba/smbpasswd
socket options = TCP_NODELAY IPTOS_LOWDELAY
syslog = 2
use sendfile = yes
writeable = yes

[homes]
comment = Home Directories
browsable = no
read only = no
create mode = 0750

[sda1]
path = /mnt/sda1
valid users = nobody
read only = no
guest ok = yes
create mask = 0766
directory mask = 0766

[root]
path = /
read only = no
guest ok = no
create mask = 0766
directory mask = 0766

Notice the line guest ok = yes under [sda1]. It allows a guest user to log in without any authentication.

II. With authentication: No points for guessing what changes we  need to make to provide an authentication based samba access. We have to smb.conf along with an extra step.

Change the line guest ok = yes to following under [sda1]:

guest ok = no

One more step and we’re done. OpenWrt has a user by name nobody. We will use this username to access the samba file system. Type following command to provide a password to nobody:

# smbpasswd -a nobody

Enter password and you’re done! Next time you access the samba file system, it will prompt you for username and password. Provide nobody as the username and password as the password you just entered.

Samba configuration is done. We will use a samba server without any authentication. So change the line in smb.conf under [sda1] to guest ok = yes and finally run the following command:

/etc/init.d/samba enable

Reboot the router.

Now we will configure minidlna so that our media server gets ready in no time.

For that, we will edit /etc/config/minidlna. Run following command:

# vi /etc/config/minidlna

A basic configuration should look like this:

config minidlna 'config'
option enabled '1'
option port '8200'
option interface 'br-lan'
option log_dir '/var/log'
option inotify '1'
option notify_interval '900'
option serial '12345678'
option model_number '1'
option root_container '.'
option album_art_names 'Cover.jpg/cover.jpg/AlbumArtSmall.jpg/albumartsmall.jpg/AlbumArt.jpg/albumart.jpg/Album.jpg/album.jpg/Folder.jpg/folder.jpg/Thumb.jpg/thumb.jpg'
option friendly_name 'Super Media Server'
option db_dir '/mnt/sda1/db'
option presentation_url 'http://192.168.1.1:8200'
list media_dir 'A,/mnt/sda1/audio'
list media_dir 'V,/mnt/sda1/video'

Notice the last two lines. They are prefixed by A, and V, telling the minidlna utility where to find audio and video files respectively. You can also add a picture directory by prefixing the directory path by P,. e.g. P,/mnt/sda1/picture.

You’re almost done. Run following commands:

# /etc/init.d/minidlna enable
# /etc/init.d/minidlna start
# /usr/bin/minidlna -f /tmp/minidlna.conf  -d -R

And you’re done. Just reboot the router and use your own media server to stream audio and video files.

To see the media server in action.

I. On Ubuntu:

Open your file system. And click on Browse Network. You would be able to see the name of the workgroup (in our case it is WORKGROUP) under which you would be able to find the content of your USB storage device. An example is shown in the following screenshot following screenshot:

Screenshot from 2016-04-05 20:53:55

II. On Windows:

Click on Network and you would be able to see the friendly name of your media server. See the below screenshot.

windows_media_server

III. On Android: Yes you can also stream your content on an Android phone. Download and install an app called BubbleUPnP from here. Read instructions on how to access your media server on the Google Play page (or through the app) and see your content stream on your smart phone.

That’s it for the tutorial. I’m experimenting with OpenVPN on OpenWrt so that anyone on the internet can access my media server. Will update on this blog post as soon as I manage to make it running. Thanks for your patience. Let me know if you’re stuck or have any doubts/questions.

Happy Hacking!

References:

[1] http://www.geektalks.org/setup-usb-drive-and-sambanas-on-openwrt-from-scratch/

[2] http://diantokam.blogspot.in/2012/11/openwrt-minidlna-server.html

YET ANOTHER NETWORK CONTROLLER (PART 2) – RUNNING YANC

Once you have set up yanc using this link, we now need to run yanc filesystem, yanc-of-adapter and mininet.

Go to yanc folder and run the following command:

$ ./yanc -f /net

This will mount the yanc filesystem under /net directory.

Open new terminal. Go to <path-to-yanc>/apps/of-adapter and run following command:

$ ./yanc-of-adapter /net unix:path=/var/run/dbus/system_bus_socket -vvv

This will start yanc-of-adapter at port 6633 on the localhost. It will use unix:path=/var/run/dbus/system_bus_socket as the D-Bus for IPC (read about D-Bus here).

Now we will create a simple topology using mininet. Open a new terminal and go to <path-to-mininet>/bin to find mininet executable called mn. Run following command:

# mn –controller=remote,ip=127.0.0.1,port=6633

Note: You must have root privileges to run above command.

Mininet is now running with the topology depicted in the below figure:

mininet_simple_top

 

Great! You just completed the 2nd tutorial of the series. Will be back with more awesome stuff shortly. Stay tuned.

 

Cheers!

Yet Another Network Controller (Part 1) – Getting started

So this is a blog post (after a very long time) explaining how to start Yet Another Network Controller (yanc – https://github.com/ngn-colorado/yanc) on your Linux system. Clone yanc repository in your local machine if you haven’t already.

$ git clone https://github.com/ngn-colorado/yanc.git

$ cd yanc/

$ make

# mkdir /net

# chown <user> : <group> /net

$ ./yanc -f /net

This has started the yanc filesystem with /net as its mountpoint.

Now we’ll start yanc-of-adapter, which is an utility to allow yanc filesystem to connect to OpenFlow switches created using emulators like mininet (we’ll see how it is done in a while).

Open a new terminal instance and navigate to /<path-to-yanc>/apps/of-adapter/. To run yanc-of-adapter, run following commands in your terminal.

$ git submodule init

$ git submodule update

The above commands would allow you to have om-lib submodule (https://bitbucket.org/omichel/om-lib).

Now let’s make:

$ make

This will create an executable named yanc-of-adapter. Now type following command to check if the necessary files have been created successfully.

$ yanc-of-adapter -h 

The output of the above command should be:

Usage: yanc-of-adapter [-v[v[v]]] [-h] [-p listening-port] <yanc-fs-path> <dbus-addr>
Try `yanc-of-adapter -h’ for more information.

If the above output does not appear, then you would have to follow the above steps again.

Now, yanc is all ready to rock n’ roll. We just need OpenFlow switches to let yanc-of-adapter connect them with the yanc filesystem. We will use mininet to create virtual OpenFlow switches and make them connect to yanc-of-adapter which would ack like an SDN controller for them. Follow the steps given on http://mininet.org/download/ to get mininet up and running. Run following command to check if you installed mininet properly.

# mn –test pingall

Congratulations! You just completed the first step to awesomeness. We will look into how to use yanc filesystem with the help of yanc-of-adapter and mininet.

Feel free to ask any questions.

Peace.

How To Setup Your Own Web Server with or without a Network Router

All the web developers out there know the thrill of developing a website or a web-service. And their main objective is always to make their end users happy. But, they must first ensure that their websites/services are available to the end users.

This tutorial will help you to make your website/service available to your end users without purchasing a server-space on some remote servers, as your personal computer will act like a web server. Obviously, you will not get all the benefits of a real web server like processing power, etc. but it will be beneficial if you want to pre-launch your website/service before hitting the WWW. So, lets do it.

As the title says, you can set up a web server with a network router or without it. It becomes simpler when you want to do it without a network router as you will be the only user for your IP (Internet Protocol) address provided by your ISP (Internet Service Provider). Setting up the server with router needs a little extra effort as we need to tell router to which user it needs to route the packets as there could be more than one users and we want only a single machine to act like a server. Don’t worry if you didn’t get what I just said. You’ll learn as we go through the tutorial. So lets not waste more time.

Here’s the list of what you’ll need:

  • An internet-enabled PC having WampServer installed in it. I’ve done it using WampServer, but you can also do it using its alternatives like XAMPP, LAMP, etc. What you will need to do is to change the configuration file (if the server is running on the ports other than 80 by default. Port 80 is for http). You can always use Google to help you change the configuration file. By the way, I’ve also tested it using Apache Tomcat.
  • Network Router, if you want to establish the server using the router otherwise you won’t need it.
  • 30 minutes (maximum). Includes time to go through this tutorial and setting up the server. 😀

I assume that you have installed WampServer on your PC by now. Now follow these steps:

Step 1:  Go to the directory where you installed the wamp (for e.g. C:\wamp\). Now find a folder named ‘bin‘. Now follow this path: bin\apache\apache2.2.22\conf and open httpd.conf using a text editor like Notepad. Note that apache2.2.22 folder depends on the version of apache used by WampServer. If your WampServer is using a different version then you’d have a different folder name always starting with the prefix apache.

Step 2: Okay. Now find a line saying ‘Listen 80′. This instruction tells the server on which port it needs to wait for the request or it needs to give response. You may change it to any other port ranging from 0-65535 but it is recommended to leave it as it is as the default port for HTTP is 80. Now close the text editor.

Step 3: Go to C:\wamp\www\ which is the root folder. Create a new folder and name it as per your convenience. In this tutorial we’ll name it ‘My_Site‘. Copy necessary files of your website into this folder. You should be aware of the name of the file (either .html or .php) of your homepage. Suppose it is ‘myhome.php‘. This page would be displayed first when someone visits your site.

Step 4: Start WampServer. To start, open up the start menu and find it. Other way is to go to the folder where you installed wamp and double-click wampmanager.exe. You’ll find the WampServer icon on the Task Bar’s Notification Area which is at the bottom-right of the screen. Click on the icon. You’ll get a menu. Click on ‘Put Online‘. Ensure that the icon is green which indicates the server is up and running. Click the icon again and in the menu, click on ‘Localhost’. You’ll get the following page in your browser. You can notice the folder My_Site under Your Projects in the image.

wamp_start
http://localhost

Step 5: The above page is the home page or index page of wamp. We need to change it to the home page of our site. One way to do this is to change the root folder and name your home page as index.php. The other and better way is to redirect to your home page. This is better as you only need to change the parameter of a php command to the page you want to go after redirection. So, lets do it.

Go to the root folder which is C:\wamp\www in our case. Find index.php. Copy index.php and paste it in the same directory by any other name. Suppose the copied file is named original_index.php. We did this to create a backup for the original file so that if something goes wrong we can always rename original_index.php to index.php.

Step 6: Open index.php and delete all its content. Now write  a php command: header(‘Location: \My_Site\myhome.php’); and save it. Close the editor. Now check if WampServer is online if not repeat Step 4. You’ll see myhome.php.

home
http://localhost/My_site/myhome.php

Note: For those who will not use a network router, this tutorial almost ends here. You must keep an important thing in mind that the port on which your server is listening must be 80! Your next step would be entering your IP address into the URL bar and voila! You rule! If you want to use a domain name instead, you can use dyndns. As your IP address would keep changing, the same IP won’t work all the time. dyndns will help you to bind your dynamic IP to a static domain name. And for those who are going to use a network router, the fun starts now!

Step 7: I assume that you have access to your router’s configuration page through your web browser. Lets assume it is accessed through IP 192.168.0.1. Type the username and password (If you don’t know then try typing ‘admin’ in both as it is common in most of the routers as factory defaults.) When I wrote this post, I was using a TP-LINK router with model number TL-WR740N which is I guess one of the most basic network routers TP-LINK has. So it won’t be a problem if you have other routers as they would offer same features. I will use the options offered  by the TP-LINK’s configuration page throughout this tutorial.

Step 8: Okay. So now we have the configuration page. Find DHCP option and under it, find Address Reservation. This option would  permanently bind the MAC Address of your PC to the IP address of your machine in the Local NetworkThus every time you join the Local Network, your machine would get the same IP address. This will help to tell the router that the requests made on the web page should be directed to your machine only and making your local IP address static will make it easy for the router.  Suppose the router provided an IP 192.168.0.100 to our PC. If you want to know the local IP address of your PC then you can find it in DHCP Clients List under DHCP option. This option would give us the list of clients currently connected to the router.

Step 9: Now we shall tell the router on which port our web server is. Find an option called Virtual Servers under Forwarding. Click Add New. We will get a bunch of text fields. First is Service Port, this is the port on which the request is made. Provide 80  (it is for HTTP remember?) to it. Next is Internal Port, the port at which the server would listen to. Type 80  in it. Click Save. If you want your server to listen to a port other than 80 then change the command to ‘Listen <your port no>’ in Step 2  and thus change the Internal port to that number.

Step 10: Your wait is over now. Now start WampServer just like we did in Step 4 and type your External IP Address in to the URL bar. And voila! You rule! You can always look up to dyndns for providing a dynamic DNS service. I have a facility of logging in to dyndns from the router’s configuration page. Your router may have or may not have this facility, so it is recommended to check dyndns’ web site.

Final Note: Don’t worry if you can’t find the features we talked about on your router’s configuration page. They may be categorized in different manner. So it is advisable to check out every option on that page.

Thanks. You’re free to ask any queries regarding the things covered in this post. All the suggestions are welcome. Have fun!