RasPi Banner


This section describes configuring a fresh RasPi installation to be run from USB and to have content shared over networks (including Windows networks). This is a snippet of configuration for many usecases: running a torrent server, running a media share etc. As a typical setup is on 2 partitions (usually 'System' and 'Data'), we will do so as well.

Actions described:

  • A Raspbian base setup is moved to a USB drive, to extend the life of the SD card (by avoiding frequent reads and writes to the system, caches etc.). This includes the system itself and a data partition
  • The data partition (or part of it) is shared over the network

Moving Raspbian to USB

Make sure your usage scenario corresponds to what you want to achieve. This configuration is more complex than others shared elsewhere. You might want to save some time if you want something more simple.

USB Storage

  • Start with a fresh Raspbian setup (described in the section Raspberry Pi Headless Setup (of course this will work with headed setup as well). If you are going to continue headless, you can change the amount of graphics memory to something like 16 megabytes.
  • Plugin a USB stick with big enough storage for your purpose (note you need to extract 4GB for the system, as described below). It need not be formatted. If it contains data however, make sure it is backed up: we will wipe it
    • Note that using a USB hub might bring some issues, it is a good idea to plugin this USB stick directly into RasPi (after all it boots a system)
  • Login remotely using your credentials of a SUDO user. Now is the time to tweak the mentioned memory setting to 16 MB (via raspi-config)

Now is the time to setup partitions. For this scenario, we will setup two: one for system, one for data. As mentioned, the system partition is going to be used mainly for preserving the life of your SD card. The second will be used for data of your choice.

  • Figure out first which drive is your USB
tail /var/log/messages

NOTE: you can find your devices in many other ways: try for instance lsblk

Typically, your USB storage would be identified as sda: We will assume this is the case in further documentation.

  • Enter the fdisk utility for the appropriate device ('sda')
sudo fdisk /dev/sda
  • Configure your drive using fdisk

    • Press p to list current partitions.
    • To delete existing partitions, press d
    • Create a primary partition: Use n, then p. When it asks you for size, enter +8G.
    • Create a second partition for your data (again, primary). Repeat this step for all partitions you wish to create
    • Write the changes by pressing w once done
  • Format the drives. Now the partition table has been created, we format all drives (in the below example the system drive and the data drive) as linux ext4

sudo mkfs.ext4 /dev/sda1
sudo mkfs.ext4 /dev/sda2
sudo mkdir /mnt/systemdrive
sudo mkdir /mnt/datadrive
sudo mount /dev/sda1 /mnt/systemdrive
sudo mount /dev/sda2 /mnt/datadrive

NOTE: In this scenario we have created the new drives as systemdrive and datadrive respectively. Use your own naming if you wish. To confirm the drives have been created, run

df -h
  • To copy data, first install rsync
sudo apt install rsync
  • Copy system data to the system drive: systemdrive. This will take some time - go and get yourself a coffee :)
sudo rsync -axv / /mnt/systemdrive
  • We are not done yet: now we configure the system to boot into the new system drive. First backup cmdline.txt
sudo cp /boot/cmdline.txt /boot/cmdline.orig
  • edit cmdline.txt
sudo nano /boot/cmdline.txt

Adjust this to read:

dwc_otg.lpm_enable=0 console=ttyAMA0,115200 kgdboc=ttyAMA0,115200 console=tty1 root=/dev/sda1 rootfstype=ext4 elevator=deadline rootwait rootdelay=5
  • Modify fstab to mount our drives on startup
sudo nano /etc/fstab

and add the following lines (substitute 'datadrive' with your own name):

/dev/sda1 / ext4 defaults,noatime 0 1
/dev/sda2 /mnt/datadrive ext4 defaults 0 2

Comment out the following line which refers to the SD card:

#/dev/mmcblk0p2 / ext4 defaults,noatime 0 1
  • Reboot the Pi with
sudo reboot

NOTE Now we share our new created drive with Samba in the next steps

  • Optional: you can do a cleanup now to reduce the system size so it contains only the necessary parts: check the dedicated section to learn more

  • Update and install Samba, then lauch it's configuration file

sudo apt update
sudo apt full-upgrade
sudo apt install samba samba-common-bin
sudo nano /etc/samba/smb.conf
  • We have a section to configure Samba elsewhere. Enough to say, setup 'wins' and domain name, then add a shared folder definition for your data as seen below:
comment = my data drive
path = /mnt/datadrive
valid users = @users
force group = users
create mask = 0775
force create mode = 0775
security mask = 0775
force security mode = 0775
directory mask = 2775
force directory mode = 2775
directory security mask = 2775
force directory security mode = 2775
browseable = yes
writeable = yes
guest ok = no
read only = no
  • Restart the Samba service: (jessie: doesn’t work anymore, 'samba.service is masked')
sudo service samba restart
  • Add user(s) to the system. Replace 'myname' with whatever you want. Note user configuration (security) has a separate section, also Samba users are described elsewhere.
sudo useradd myname -m -G users
sudo passwd myname
sudo smbpasswd -a myname
sudo chown pi:users /mnt/datadrive
sudo chmod g+w /mnt/datadrive
  • Test: you should be able to connect from another computer and test read/write, folder creation etc. Check the created files appear on 'mnt/datadrive'