MOUNT AN SMB NETWORK DRIVE ON RASPBERRY PI

original post: http://geeks.noeit.com/mount-an-smb-network-drive-on-raspberry-pi/

In this tutorial we will describe how to connect your Raspberry Pi to a network drive and permanently mount it to your system. Even though this article uses a Raspberry Pi as an example to connect to an SMB drive, the steps used can be applied to any Debian based system, such as Ubuntu.

If you have a Raspberry Pi you might have noticed that the storage possibilities are kind of limited unless you have some external storage. Even though you can get SD cards with 64+gb of storage, you probably want more if you have a lot of music and movies that you are streaming through your Pi.

There are several choices when it comes to storage for your Pi, such as network drives, flash drives, or external USB HDDs. Using a network drive you can not only access your files from the Pi, but from any computer connected to your network, which is very convenient.

Prerequisites

Before we start, I will assume you already

  1. have a network drive connected to your LAN, and
  2. know its LAN IP address

Note: remember to change all text in red to your own values.

 

Installation

In order to mount the drive, you need to have cifs-utils installed on your system. If you are running a newer version of Raspbian or RaspBMC you should already have this installed. You can check whether it is installed or not by running the following command:

dpkg -s cifs-utils

If it is installed, it should output something like this:

Package: cifs-utils
 Status: install ok installed
 Priority: optional
 Section: otherosfs
 Installed-Size: 189
 Maintainer: Debian Samba Maintainers <pkg-samba-maint@lists.alioth.debian.org> Architecture: armhf
 Version: 2:5.5-1
 ...

If it says that it’s not installed, you need to install it:

sudo apt-get install cifs-utils

 

Mounting unprotected (guest) network folders

You might have public folders on your network drive that can be accessed by anyone without having to provide any credentials. These are mounted in the same way as password-protected folders (we will mount these in the next section), but with a few different options.

First, let’s create a directory on the Pi where we want to mount the directory. You will need a separate directory for each network folder that you want to mount. I will create the folders in the /media folder:

sudo mkdir -p /media/networkshare/public

Then edit your /etc/fstab file (with root privileges) and add this line:

//192.168.0.18/publicshare /media/networkshare/public cifs guest,uid=1000,gid=1000,iocharset=utf8 0 0

The first part is the IP of your network drive, and the public folder you want to mount.
The second part is the folder on your local machine where you want to mount the network share.
The third part indicates what type of drive you want to mount (cifs).

The last part is the set of options you can pass, and here’s an explanation of the ones we are using:

  • guest is basically telling the network drive that it’s a public share, and you don’t need a password to access it (not to confuse with username),
  • uid=1000 makes the Pi user with this id the owner of the mounted share, allowing them to rename files,
  • gid=1000 is the same as uid but for the user’s group,
  • iocharset=utf8 allows access to files with names in non-English languages.

Note: If there is any space in the server path, you need to replace it by \040, for example //192.168.0.18/My\040Documents

To find the uid and gid for your username, use the following command:

id username

Now that we have added this to our /etc/fstab, we need to (re)mount all entries listed in /etc/fstab:

sudo mount -a

Now you can access your network drive from /media/networkshare/public (or wherever you chose to mount it).

 

Mount password-protected network folders

Mounting a password-protected share is very similar to mounting a public ones, with some minor changes to the options we pass. Let’s start by making a new folder where we want to mount the password-protected share:

sudo mkdir -p /media/networkshare/protected

Again, open /etc/fstab (with root privileges), and add this line:

//192.168.0.18/protectedshare /media/networkshare/protected cifs username=msusername,password=mspassword,uid=1000,gid=1000,iocharset=utf8 0 0

While this is a perfectly valid way of mounting your protected folder, it’s not very secure. Since /etc/fstab is readable by everyone, so are your credentials in it. So, let’s make it more secure by using a credentials file. This file will contain nothing else but your username and password, but we will make readable only to you. This way, other users on the system won’t be able to see your credentials.

Using a text editor, create a file that will contain the credentials for your protected network share:

vim ~/.smbcredentials

Enter your username and password for the protected share in the file:

username=msusername
password=mspassword

Save the file.

Change the permissions of the file to make sure only you can read it:

chmod 600 ~/.smbcredentials

Then edit your /etc/fstab file and change the line where we are mounting the protected share to look like this:

//192.168.0.18/protectedhare /media/networkshare/protected cifs credentials=/home/username/.smbcredentials,uid=1000,gid=1000,iocharset=utf8 0 0

Save the file.

Again, we need to (re)mount all entries listed in /etc/fstab:

sudo mount -a

 

Now you’re all set to access your public and protected folders from your Pi.

 

在树莓派上的XBMC增加一个静音按钮(mute button for XBMC on Raspberry Pi)

也许有人会问有了手机控制或者红外控制为什么还要有一个物理的静音按钮呢?设想一下以下场景:

当你在电视上看大片看得正爽的时候,来了个电话,是到手机上找app来控制呢,还是手忙脚乱的找遥控器来静音,或者直接到电视机边上按个静音键方便呢?

正是因为如此,才有了这个创意。

基本思路:一个简单的轻触式按钮连接到Pi的gpio口,在Pi上运行一个python程序,当按钮按下时,给XBMC发送静音指令。

附加功能:每按一次,LED闪一下

扩展功能:不满足于静音功能的,还可以多增加几个按钮来完成其他的功能,比如播放、暂停、快进、发送邮件、甚至求救报警功能。

开始动手

原料:

树莓派 1个(这个你肯定有了吧,没有就入一个吧)

轻触式按钮 1个

LED 1个

3k3电阻 2个

洞洞板、连接线、面包板之类,看自己情况使用。

步骤1:

焊洞洞板,示意电路图如下:

dianlu

 

为了防止LED以及GPIO口电流太大,加了限流电阻。

board1

实际焊接图,请忽略左边五个按钮和左边的电阻,这是我留着备用的。

按照电路图连接到Pi上。

步骤2,安装python的gpio库,如果已经安装了,请跳过此步

cd /tmp
cd gpio
wget https://pypi.python.org/packages/source/R/RPi.GPIO/RPi.GPIO-0.5.7.tar.gz
tar xvzf RPi.GPIO-*.tar.gz
cd RPi.GPIO-*/
sudo python setup.py install
如果安装的时候出现错误:
source/py_gpio.c:23:20: fatal error: Python.h: No such file or directory
这是因为缺少Python.h文件,没安装python编译环境:
sudo apt-get install python-dev
步骤3:开始我们的python程序吧
#!/user/bin/env python

import RPi.GPIO as GPIO
import time
import urllib
import urllib2
import json

def mute():
url=’http://你的Pi的ip地址:8080/jsonrpc’
values = {“jsonrpc”:”2.0″,”method”:”Application.SetMute”,”id”:1,”params”:{“mute”:”toggle”}}

jdata = json.dumps(values)
req = urllib2.Request(url, jdata)
req.add_header(‘Content-Type’,’application/json’)
response = urllib2.urlopen(req)
return response.read()

GPIO.setmode(GPIO.BOARD)
GPIO.setup(11,GPIO.OUT)
GPIO.setup(12,GPIO.IN)
while True:
in_value= GPIO.input(12)
if in_value ==False:
mute();
GPIO.output(11,False)
time.sleep(0.5)
GPIO.output(11,True)
while in_value == False:
in_value = GPIO.input(12)

然后运行python key.py,按下按钮看看屏幕右上角已经有了静音标志了?再按一下,又消失了。
还可以在这个代码基础上修改做别的操作,希望这个能够起到抛砖引玉的作用。