Home     About     Archive     Links

Use USB hard disk & flash drives with Raspberry Pi

Here we have a summary of the commands on how to mount a USB drive on Raspberry.

Connecting and Mounting a USB Drive

After inserting a USB drive you will need to manually mount the device in order to use it as additional USB storage for Raspberry Pi.

To reveal the correct name for the USB device:

sudo ls /dev/sd*
/dev/sda  /dev/sda1

If there is no partitions on device the result can be:

/dev/sda

Create a partition on the divice:

sudo fdisk /dev/sda


Command (m for help): p

Disk /dev/sda: 29.6 GiB, 31750881280 bytes, 62013440 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

Disklabel type: dos

Disk identifier: 0xe85e0063

Command (m for help): n

Partition type

 p primary (0 primary, 0 extended, 4 free)

 e extended (container for logical partitions)

Select (default p): p

Partition number (1-4, default 1):

First sector (2048-62013439, default 2048):

Last sector, +sectors or +size{K,M,G,T,P} (2048-62013439, default
62013439):

Created a new partition 1 of type 'Linux' and of size 29.6 GiB.

Command (m for help): p

Disk /dev/sda: 29.6 GiB, 31750881280 bytes, 62013440 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

Disklabel type: dos

Disk identifier: 0xe85e0063

Device Boot Start End Sectors Size Id Type

/dev/sda1 2048 62013439 62011392 29.6G 83 Linux

Command (m for help): w

The partition table has been altered.

Calling ioctl() to re-read partition table.

Syncing disks.

Now, fdisk -l, will show the created partition

sudo fdisk -l

Device Boot Start End Sectors Size Id Type

/dev/sda1 2048 62013439 62011392 29.6G 83 Linux

To list your file systems:

sudo fdisk -l
sudo mount -l
df -h

Format a drive to EXT4

sudo mkfs.ext4 /dev/sda1 -L untitled

Add Apple OS X HFS+ read/write support

sudo apt-get install hfsutils hfsprogs hfsutils

Format a drive to HFS+

sudo mkfs.hfsplus /dev/sda1 -v untitled

Add Windows NTFS read/write support

sudo apt-get install ntfs-3g

Format a drive to NTFS

sudo mkfs.ntfs /dev/sda1 -f -v -I -L untitled

Add Windows/DOS FAT32 read/write support

sudo apt-get install dosfstools

Format a drive to FAT32

sudo mkfs.vfat /dev/sda1 -n untitled

We choose EXT4, so Format a drive to EXT4:

sudo mkfs.ext4 /dev/sda1 -L untitled



mke2fs 1.42.12 (29-Aug-2014)

Creating filesystem with 7751424 4k blocks and 1941504 inodes

Filesystem UUID: 1dc1e005-05d8-42c1-a7a0-325c204f67a9

Superblock backups stored on blocks:

 32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208, 4096000



Allocating group tables: done

Writing inode tables: done

Creating journal (32768 blocks): done

Writing superblocks and filesystem accounting information: done

To mount a USB drive:

Create the mount point:

sudo mkdir /mnt/usbdrive

Now connect the device to its mount point:

sudo mount /dev/sda1 /mnt/usbdrive\
ls /mnt/usbdrive
df -h

Filesystem      Size  Used Avail Use% Mounted on

/dev/root        15G  1.4G   13G  11% /
devtmpfs        459M     0  459M   0% /dev
tmpfs           463M     0  463M   0% /dev/shm
tmpfs           463M   18M  446M   4% /run
tmpfs           5.0M  4.0K  5.0M   1% /run/lock
tmpfs           463M     0  463M   0% /sys/fs/cgroup
/dev/mmcblk0p1   63M   21M   43M  33% /boot
/dev/sda1        29G   44M   28G   1% /mnt/usbdrive

Now we need to make sure the Pi user owns this folder:

sudo chown -R pi:pi /mnt/usbdrive

Test:

touch /mnt/usbdrive/test1
ls -l /mnt/usbdrive
total 16
drwx------ 2 pi pi 16384 Jul  3 14:52 lost+found
-rw-r--r-- 1 pi pi     0 Jul  3 15:00 test1

Before disconnecting a USB drive:

sudo umount /dev/sda1

You don’t need to manually un-mount if you shutdown your Pi but if you need to remove the drive at any other time you should un-mount it first.

Auto Mount

If we want the USB drive to be mounted when the system starts we can edit the fstab file:

sudo nano /etc/fstab

Then add the following line at the end :

/dev/sda1 /mnt/usbdrive ext4 defaults,noatime 0 1

#/etc/fstab
proc /proc proc defaults 0 0
/dev/mmcblk0p1 /boot vfat defaults 0 2
/dev/mmcblk0p2 / ext4 defaults,noatime 0 1
# a swapfile is not a swap partition, no line here
# use dphys-swapfile swap\[on|off\] for that
/dev/sda1 /mnt/usbdrive ext4 defaults,noatime 0 1

The drive will mount at boot if it is attached to the Pi. If you want to mount the drive after you have plugged it in, use mount with the -a option. Which mean: Mount all filesystems (of the given types) mentioned in fstab.

sudo mount -a

References:

Using a custom domain with GitHub Pages

Setup custom domain on the user page and then all the project pages of Github repositories will automatically appear under the same url.

Github provides two types of pages,

  • User pages
  • Project pages

User pages

It’s a Github repository with a special name username.github.io and all the contents of this repository must be in the master branch.

By default, the user pages are available under the url, http://username.github.io.

Project pages

Project pages are project specific files lying in the gh-pages branch of the repository. These pages can be accessed via the url username.github.io/repository_name.

For example,

Repository name -> Test2

URL -> http://mpmendespt.github.io/Test2

Custom domain for user pages

Custom domain can be set for both user and project pages.

Step 1
Create the repository username.github.io on Github.

Step 2
Add a CNAME file containing the custom domain name that you want to map. The CNAME file contains, yourdomain.com

Step 3
Follow your DNS provider’s instructions to create a CNAME record that points from your default pages domain to your YOUR-GITHUB-USERNAME.github.io.

Step 4

Adding a custom domain for your GitHub Pages site ( see Custom domain for project pages, step 3, next)

Once the propagation is complete, whenever you visit YOUR-GITHUB-USERNAME.github.io you will be redirected to the custom domain yourdomain.com.

Custom domain for project pages

Create a gh-pages branch

create a new repository on the command line

echo "\# Test2" >> README.md   
git init   
git add README.md   
git commit -m "first commit"   
git remote add origin https://github.com/mpmendespt/Test2.git   
git push -u origin master

you’ll need to create the new gh-pages branch and remove all content from the working directory and index:

cd *repository*

git checkout --orphan gh-pages   
\# Creates our branch, without any parents (it's an orphan!) Switched to a new branch 'gh-pages'

git rm -rf .   
\# Remove all files from the old working tree

rm '.gitignore'

git branch --set-upstream-to=origin/gh-pages gh-pages   

Add content and push

Now you have an empty working directory. You can create some content in this branch and push it to GitHub. For example:

echo "My Page" > index.html   
git add index.html   
git commit -a -m "First pages commit"    
git push origin gh-pages

Your GitHub Pages site should now be available. You’ll receive an email if your build is unsuccessful.

Load your new GitHub Pages site

After your push to the gh-pages branch, your Project Pages site will be available at http(s)://<username>.github.io/<projectname> .

https://mpmendespt.github.io/Test2/

Custom domain for project pages

To Setup a custom domain for a gh-pages Project Pages repo that handles www.yourdomain.com and yourdomain.com:

  1. From your project repo, gh-pages branch. Create a CNAME file with the contents yourdomain.com. Commit then push.

  2. Follow your DNS provider’s instructions to create a CNAME record that points from your default pages domain to your YOUR-GITHUB-USERNAME.github.io.
  3. Adding a custom domain for your GitHub Pages site
  1. On GitHub, navigate to your GitHub Pages site’s repository.
  2. Under your repository name, click Settings.
  3. Under “Custom domain”, add your custom domain and click Save.
  • Wait til the name servers update, and then confirm that the DNS record is set up correctly:

    dig www-mpmendespt.ddns.net +nostats +nocomments +nocmd

; <<>> DiG 9.9.5-9+deb8u6-Raspbian <<>> www-mpmendespt.ddns.net +nostats +nocomments +nocmd   
;; global options: +cmd     
;www-mpmendespt.ddns.net.       IN      A   
www-mpmendespt.ddns.net. 60     IN      CNAME   mpmendespt.github.io.   
mpmendespt.github.io.   2045    IN      CNAME   github.map.fastly.net.   
github.map.fastly.net.  10      IN      CNAME   prod.github.map.fastlylb.net.   
prod.github.map.fastlylb.net. 10 IN     A       151.101.12.133   
 

References:

Install Arch Linux on Raspberry Pi

Currently neither Raspberry Pi nor the Arch Linux ARM website offer an image ready for use of Arch Linux for Raspberry Pi. So i followed this steps on Linux (in my case another Raspberry with a USB Pen Drive):

  1. Download the image for Raspberry Pi (ARM).

    wget http://archlinuxarm.org/os/ArchLinuxARM-rpi-2-latest.tar.gz
    
  2. Create a image file of 2GB:

    dd if=/dev/zero of=arch.img bs=1M count=1850
    
    count=1850   
    1850+0 records in   
    1850+0 records out   
    1939865600 bytes (1.9 GB, 1.8 GiB) copied, 53.2323 s, 36.4 MB/s
    
  3. Partition the image file:

    fdisk arch.img
    
    1. Type o. This will clear out any partitions on the drive.
    2. Type p to list partitions. There should be no partitions left.
    3. Type n, then p for primary, 1 for the first partition on the drive, press ENTER to accept the default first sector, then type +100M for the last sector.
    4. Type t, then c to set the first partition to type W95 FAT32 (LBA).
    5. Type n, then p for primary, 2 for the second partition on the drive, and then press ENTER twice to accept the default first and last sector.
    6. Write the partition table and exit by typing w.
  4. Check the partitions created:

    fdisk -l arch.img    
    	
    Device Boot Start End Sectors Size Id Type   
    arch.img1 2048 206847 204800 100M c W95 FAT32 (LBA)    
    arch.img2 206848 3788799 3581952 1.7G 83 Linux
    
  5. Mount the boot partition 1:

    sudo losetup -v -f -o $((2048 * 512)) --sizelimit 104857600
    arch.img
    
  6. Mount the root partition:

    sudo losetup -v -f -o $((206848 * 512)) --sizelimit 1833959424
    arch.img
    
    sudo fdisk -l /dev/loop0
    
    Disk /dev/loop0: 100 MiB, 104857600 bytes, 204800 sectors    
    
    sudo fdisk -l /dev/loop1    
    
    Disk /dev/loop1: 1.7 GiB, 1833959424 bytes, 3581952 sectors
    
  7. Format and mount boot partition

    sudo mkfs.vfat /dev/loop0
    
    mkdir boot
    
    sudo mount /dev/loop0 boot
    
  8. Format and mount root partition

    sudo mkfs.ext4 /dev/loop1
    
    mkdir root
    
    sudo mount /dev/loop1 root
    
  9. Extract image files:

    sudo bsdtar -xpf ArchLinuxARM-rpi-2-latest.tar.gz -C root
    
    sync
    
  10. Copy boot files:

    sudo mv root/boot/* boot   
    
    sync   
    
  11. Unmount partitions:

    sudo umount boot root
    
    rmdir boot root
    
  12. Detach partitions:

    sudo losetup -d /dev/loop0
    
    sudo losetup -d /dev/loop1
    

The image is ready!!

Now I copy it to my windows:

"C:\Program Files (x86)"\PuTTY\pscp pi@192.168.1.110:/mnt/usbdrive/archlinux/arch.img "C:\tmp"

Then we can write it on SD card with Win32DiskImager utility.

Insert the SD card into the Raspberry Pi, connect ethernet, and apply 5V power.

  • Login as the default user alarm with the password alarm.
  • The default root password is root.

References

Dynamic DNS on Raspberry Pi

How are web requests processed?

When a user visits a web site, they use the site’s domain name to address the site. Their browser then sends a request to a DNS server to translate the domain name into an IP address. Once the browser has the site’s IP address, it sends a request to the server.

There are two problems with this:

  • 1. the external IP address of your home network can change at any time, and
  • 2. your Pi is behind a firewall and can’t be accessed directly from the internet.

Dynamic DNS

The first problem can be solved by using a dynamic DNS service. These services act as DNS servers that update automactically if your IP address changes. There are numerous dynamic DNS services available. Most of them provide a free service where you can choose a subdomain name.

You need to install a small piece of software on your Pi. This software is supplied by your dynamic DNS provider, and it monitors the external address of your router. If that address changes, the software sends a message to the dynamic DNS server to update its records.

Port forwarding

When a user on the internet wants to see your web site, their browser can get the IP address of your home network from your dynamic DNS service. But requests still need to find a way through your router’s firewall.

By default web servers listen for connections on port 80. When a user’s request to see a page on your site reaches your router, the router must forward that request to your Pi’s IP address. You don’t want to forward all incoming traffic to your Pi, just the traffic for the web server which is on port 80. Most routers allow you to set up port forwarding so that any traffic on port 80 will be sent to your Pi.

To create a domain name that always points to your home ip:

Goto www.noip.com > create an account

  1. Sign in to your noip account > goto Add Host> create your host name with one of the offered domain names (mpmendespt.ddns.net)
  2. Keep the Host type as DNS Host A ( If your ISP blocks Port 80 for example, and you’re trying to run a webserver or other service on port 80, then you can choose Port 80 Redirect (at that point you’ll be asked to specify the port to use for the redirection)
  3. In the field marked IP Address: you should already see your current IP address.
  4. The next two options aren’t used for a basic account setup so leave them as they are.
  5. click the “Add Host” button at the bottom of the page to save it.

Install No-IP’s Dynamic Update Client

Create a directory for the client software:

mkdir /home/pi/noip

Download the client software:

wget https://www.noip.com/client/linux/noip-duc-linux.tar.gz

Extract the archive:

tar zxvf noip-duc-linux.tar.gz

Compile and install:

sudo make
sudo make install

After typing “sudo make install” you will be prompted to login with your No-IP account username and password.

After logging into the DUC answer the questions to proceed. When asked how often you want the update to happen you must choose 5 or more. The interval is listed in minutes, if you choose 5 the update interval will be 5 minutes. If you choose 30 the interval will be 30 minutes.

Please enter an update interval:[30] 10

Do you wish to run something at successful update?[N] (y/N) \^M

New configuration file ‘/tmp/no-ip2.conf’ created.

mv /tmp/no-ip2.conf /usr/local/etc/no-ip2.conf

The Dynamic Update client is started by running:

sudo /usr/local/bin/noip2

To confirm that the service is working properly you can run the following command:

sudo noip2 -S

To start the client when the system reboots.

We write a start up script:

sudo nano /etc/init.d/noip2

with the following lines:

#! /bin/sh
# /etc/init.d/noip2

# Supplied by no-ip.com
# Modified for Debian GNU/Linux by Eivind L. Rygge <eivind@rygge.org>
# Updated by David Courtney to not use pidfile 130130 for Debian 6.
# Updated again by David Courtney to "LSBize" the script for Debian 7.

### BEGIN INIT INFO
# Provides:     noip2
# Required-Start: networking
# Required-Stop:
# Should-Start:
# Should-Stop:
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start noip2 at boot time
# Description: Start noip2 at boot time
### END INIT INFO

# . /etc/rc.d/init.d/functions  # uncomment/modify for your killproc

DAEMON=/usr/local/bin/noip2
NAME=noip2

test -x $DAEMON || exit 0

case "$1" in
    start)
    echo -n "Starting dynamic address update: "
    start-stop-daemon --start --exec $DAEMON
    echo "noip2."
    ;;
    stop)
    echo -n "Shutting down dynamic address update:"
    start-stop-daemon --stop --oknodo --retry 30 --exec $DAEMON
    echo "noip2."
    ;;

    restart)
    echo -n "Restarting dynamic address update: "
    start-stop-daemon --stop --oknodo --retry 30 --exec $DAEMON
    start-stop-daemon --start --exec $DAEMON
    echo "noip2."
    ;;

    *)
    echo "Usage: $0 {start|stop|restart}"
    exit 1
esac
exit 0

Then give it executable permissions and update the rc.d scripts:

sudo chmod +x /etc/init.d/noip2
sudo update-rc.d noip2 defaults

Now it automatically update it’s ip on boot.

References