📜 ⬆️ ⬇️

Tale of Raspberry and external HDD. The first experience of developing a platform

Good day, Habr! A couple of weeks ago, the geek's hands were itching - I wanted to buy a rather sensational and well-known single board mini-computer Raspberry Pi. The model was chosen the "coolest" - version "B" with 512Mb of RAM on board.

This post is about something else. After all the manipulations with the setting, I wanted to try the machine, so to speak, “in action”. The idea arose almost immediately. At home, I have 3 computers, 2 smartphones, a budget router, and an external hard disk on 2Tb - Seagate Expansion External. HDD connection interface - USB. The router of the connectors has only Ethernet and a hole for the power cord. All my devices connect to the router only via WiFi, and none can work in constant mode. But then Raspberry appears. Miniature board sizes allow you to place a system like [HDD <= USB => RPi <= Ethernet => DIR300NRU (router) <= WiFi => LAN] directly on the windowsill and use the drive on the local network, and its scanty power consumption allows you to keep it turned on all the time . RPi is running the Linux family of operating systems, namely, I installed Raspbian on it. It would seem to put a samba server and share the disk ... but that would be too easy. The final task has become complicated: it is necessary to make an external disk available on the local network only if my smartphone is currently connected to this network, otherwise the disk will be unmounted, thereby reducing the load and power consumption on it. So we will write a demon, and we will write in Python. Go!

First of all, first thing ... samba!


First you need to configure the samba and iron. We cling hard to Malin through USB, Malin to router via Ethernet. All included in the outlet. We connect via SSH to RPi, I use PuTTY under Windows as a client.
In Raspbian "out of the box" there is no possibility to connect an NTFS partition of a disk for recording, it is mounted only as Read-Only and does not allow access to itself over a local network.
It does not matter, now we will install the necessary driver:
pi@raspberrypi ~ $ sudo apt-get install ntfs-3g 

Next we need to know the name of the partition to mount, we find out like this:
 pi@raspberrypi ~ $ sudo fdisk -l 

And we get something like this:
Disk / dev / sda: 2000.4 GB, 2000398931968 bytes
255 heads, 63 sectors / track, 243201 cylinders, total 3907029164 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical / physical): 512 bytes / 512 bytes
I / O size (minimum / optimal): 512 bytes / 512 bytes
Disk identifier: 0x0006573a

Device Boot Start End Blocks Id System
/ dev / sda1 2048 409602047 204800000 83 Linux
/ dev / sda2 409602048 419842047 5120000 82 Linux swap / Solaris
/ dev / sda3 419842048 3907028991 1743593472 7 HPFS / NTFS / exFAT

My external HDD has the name sda, the section is called sda3, you may have another one. Remember it.
Then look where to mount. By default, the disk is automatically mounted in / media / Tom_name. I decided not to bother and leave it there. My path to the directory: / media / DataR.
')
Now we set up the samba server itself. Open the configuration file for writing:
 pi@raspberrypi ~ $ sudo nano /etc/samba/smb.conf 

You can read about the settings in detail in the network, I just give my configuration file:
 [global] workgroup = WORKGROUP server string = RPi Fileserver netbios name = fileserver dns proxy = no log file = /var/log/samba/log.%m max log size = 1000 syslog = 0 panic action = /usr/share/samba/panic-action %d encrypt passwords = true passdb backend = smbpasswd obey pam restrictions = yes unix password sync = yes passwd program = /usr/bin/passwd %u passwd chat = *Enter\snew\s*\spassword:* %n\n *Retype\snew\s*\spassword:* %n\n *password\supdated\ssucce$ pam password change = yes map to guest = bad user #======================= Share Definitions ======================= [ExternalHDD] comment = HDD Seagate Expansion External 2Tb path = /media/DataR writable = yes printable = no guest ok = yes read only = no 


Set up? Go ahead. We check the performance of the whole structure. We mount the partition (first unmount, for everyone), restart the samba server.
 pi@raspberrypi ~ $ sudo umount /media/DataR pi@raspberrypi ~ $ sudo mount /dev/sda3 /media/DataR pi@raspberrypi ~ $ sudo /etc/init.d/samba restart 

If no errors are observed, then try to find the server on the network. If everything is fine here, then go ahead.

We write a script


We will write in Python. The interpreter is already available to us preinstalled on Raspbian. I decided to write directly to the console:
 pi@raspberrypi ~ $ nano shrdsk.py 

What do we need:

So, first of all we import the necessary modules:
 import socket as s #   from time import sleep #   from os import system #     from errno import * #     

Then everything will fit in a small infinite loop:
 while 1: sock=s.socket(s.AF_INET,s.SOCK_STREAM) #   try: sock.connect(('192.168.0.14',1001)) #     , IP     ,     system('mount /dev/sda3 /media/DataR') #  ,   system('/etc/init.d/samba restart') #   except socket.error, v: #  ,  2 : if v[0]==ECONNREFUSED: # 1 - IP ,    (  !) system('mount /dev/sda3 /media/DataR') #  system('/etc/init.d/samba restart') #  else: # 2 -  IP  system('umount /media/DataR') #  system('/etc/init.d/samba stop') #   sock.close() #        sleep(60) #   60  

This is the minimum efficient option, but I decided to slightly modify it by adding the ability to configure and output debugging messages to the console. The finished version looks like this:
 # Coding: utf8 # Author: HeffCodeX # Version: 1.0 # !!!!!!!!!!!!!!!!!!!! # !START ME WITH ROOT! # !!!!!!!!!!!!!!!!!!!! ### # socket config: HOST='192.168.0.14' # host to detect PORT=1001 # any random port WAITING=60 # time to wait between connections (in secs) ### # mount config: MOUNT=1 # do mount/umount (1/0) DEV='sda3' # device (without "/dev/") DIR='/media/DataR' # directory to mount ### import socket as s from time import sleep from os import system from errno import * while (1): sock=s.socket(s.AF_INET,s.SOCK_STREAM) try: print "connecting..." sock.connect((HOST,PORT)) print "socket ok" if MOUNT: print "mount device" system("mount /dev/%s %s"%(DEV,DIR)) print "samba restart:" system("/etc/init.d/samba restart") except s.error, v: print "socket err" if v[0]==ECONNREFUSED: if MOUNT: print "mount device" system("mount /dev/%s %s"%(DEV,DIR)) print "samba restart:" system("/etc/init.d/samba restart") else: if MOUNT: print "umount device" system("umount %s"%DIR) print "samba stop:" system("/etc/init.d/samba stop") sock.close() print "waiting..." sleep(WAITING) 

Results

It turned out that you can configure absolutely everything, as well as disable (once) the mounting partition. It remains to register a static IP on the device to connect to our local network and everything, a peculiar disk access key is ready! For full automation, you can add a script to startup at system startup. Open the rc.local system file:
 pi@raspberrypi ~ $ sudo nano /etc/rc.local 

And we add the following line into it:
 su pi -c "python /home/pi/shrdsk.py" 

The path, of course, indicate your own.
That's all, thanks for reading ! This was my first development experience under the Raspberry Pi, and indeed under Linux in general.

UPD1: thanks for telling me about the reboot and autorun, threw the first one, corrected the second

Source: https://habr.com/ru/post/191760/


All Articles