Category Archives: Linux

Clone or Backup Raspberry Pi SD Card with Mac OS X

If you need to backup or make a copy of Raspberry Pi SD Card then there are some tools available. However, this post explain how to use command line on Mac OS X in creating an image file (backup file) of an SD Card and later this image file can be used to put into another SD Card.

First of all run the terminal on your Mac and type command df -h

AnandMac-2:~ anand$ df -h
Filesystem                          Size   Used  Avail Capacity  iused   ifree %iused  Mounted on
/dev/disk0s2                       247Gi  231Gi   16Gi    94% 60530779 4093113   94%   /
devfs                              187Ki  187Ki    0Bi   100%      648       0  100%   /dev
map -hosts                           0Bi    0Bi    0Bi   100%        0       0  100%   /net
map auto_home                        0Bi    0Bi    0Bi   100%        0       0  100%   /home
localhost:/_orgYFQCOsa4qp3lbDpDV7  247Gi  247Gi    0Bi   100%        0       0  100%   /Volumes/MobileBackups
/dev/disk0s4                        51Gi   46Gi  5.0Gi    91%   254868 5354408    5%   /Volumes/BOOTCAMP

Then connect SD Card that contains data to backup to the SD Card reader. Run the command df -h again.

AnandMac-2:~ anand$ df -h
Filesystem                          Size   Used  Avail Capacity  iused   ifree %iused  Mounted on
/dev/disk0s2                       247Gi  231Gi   16Gi    94% 60530901 4092991   94%   /
devfs                              190Ki  190Ki    0Bi   100%      656       0  100%   /dev
map -hosts                           0Bi    0Bi    0Bi   100%        0       0  100%   /net
map auto_home                        0Bi    0Bi    0Bi   100%        0       0  100%   /home
localhost:/_orgYFQCOsa4qp3lbDpDV7  247Gi  247Gi    0Bi   100%        0       0  100%   /Volumes/MobileBackups
/dev/disk0s4                        51Gi   46Gi  5.0Gi    91%   254868 5354408    5%   /Volumes/BOOTCAMP
/dev/disk1s1                       7.4Gi  1.9Mi  7.4Gi     1%        0       0  100%   /Volumes/Kingston

Now compare the results of running command df -h before and after inserting the SD card and look for the new device. In my case it is /dev/disk1s1. This is the device name of the partition.

After finding the device name of the partition now we are going to determine the raw device name of the entire disk by omitting the final “s1” and replacing “disk” with “rdisk” (this is very important: you will lose all data on the hard drive on your computer if you get the wrong device name). Make sure the device name is the name of the whole SD card as described above, not just a partition of it, for example, rdisk1, not rdisk1s1. In my case it is /dev/rdisk1

Run the following command to create an image file from the SD Card

sudo dd if=/dev/rdisk1 of=/path/to/filename.img bs=1m

The above command is simply taking the whole disk (SD card) and create an image file to the location “/path/to/filename.img”. You may use any filename. Note that dd command will not feedback anything to the terminal unless there is an error or it is finished. While waiting for the dd command to finish you may see the progress by pressing Control+t

In order to create an SD card from the image file (any image file or the one you just created), determine the device name of the partition of the SD card using df -h as described above. In my case it is /dev/disk1s1. Then unmount the partition so that you will be allowed to overwrite the disk:

sudo diskutil unmount /dev/disk1s1

Then run the following command

sudo dd bs=1m if=/path/to/filename.img of=/dev/rdisk1

After the dd command finishes, eject the card:

sudo diskutil eject /dev/rdisk1

 

Raspberry Pi with 3G Air Card using Sakis3G

I am involving in a project where I have to connect to the Internet using 3G USB Air Card. install the following packages

sudo apt-get install usb-modeswitch ppp libusb-dev

Sakis3G is a tweaked shell script that helps end user connecting through through 3G/UMTS/GPRS. Get sakis3g source from git

git clone git://github.com/Trixarian/sakis3g-source
cd sakis-source
mkdir build
./compile
sudo cp build/sakis3gz /usr/bin/sakis3g

When I first compiled it failed to compile and I had to install libusb-dev. After successfully compile you need to run as root. In fact after compilation it recommend:

sudo bash || su
to connect use
sakis3g connect
or
sakis3g --interactive connect

My operator requires to put APN but no username and password required. Sakis3g will guide you how to enter APN but if you do not put username it will quit and cannot connect. What I do is I enter a blank space for my username and password then it connects.

Ubuntu – start Firefox in fullscreen mode from command line (Terminal)

Somehow I want to start firefox on my Ubuntu 12.04 LTS from command line in fullscreen mode. Firefox can be invoked from command line using command:

firefox

However, the command does not seem to have fullscreen option. This site uses xdotool to simulate that button F11 on the keyboard is pressed so the firefox goes on fullscreen. Hence, I a simple bash script helps achieving this task. Here is the script:

firefox http://localhost/abc.html & # one can use any url
xdotool key F11 # hit full screen

I save the above script as “firefoxfullscreen.sh” and I run it whenever I need it. Note that the script is relatively simple and it should be modified based on your purposes. One possibility is to add delay in between the two line as shown below:

firefox http://localhost/abc.html & # one can use any url
sleep 5
xdotool key F11 # hit full screen

Send Commands or Data to Other Terminals

Somehow I need to send commands from one terminal to another. Luckily I found this post which saves my time. Here is a set of steps that one need to follow:

Copy the code below to a C file (e.g. ttyecho.c)

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <string.h>
#include <unistd.h>

void print_help(char *prog_name) {
        printf("Usage: %s [-n] DEVNAME COMMAND\n", prog_name);
        printf("Usage: '-n' is an optional argument if you want to push a new line at the end of the text\n");
        printf("Usage: Will require 'sudo' to run if the executable is not setuid root\n");
        exit(1);
}

int main (int argc, char *argv[]) {
    char *cmd, *nl = "\n";
    int i, fd;
    int devno, commandno, newline;
    int mem_len;
    devno = 1; commandno = 2; newline = 0;
    if (argc < 3) {
        print_help(argv[0]);
    }
    if (argc > 3 && argv[1][0] == '-' && argv[1][1] == 'n') {
        devno = 2; commandno = 3; newline=1;
    } else if (argc > 3 && argv[1][0] == '-' && argv[1][1] != 'n') {
        printf("Invalid Option\n");
        print_help(argv[0]);
    }
    fd = open(argv[devno],O_RDWR);
    if(fd == -1) {
        perror("open DEVICE");
        exit(1);
    }
    mem_len = 0;
    for ( i = commandno; i < argc; i++ ) {
        mem_len += strlen(argv[i]) + 2;
        if ( i > commandno ) {
            cmd = (char *)realloc((void *)cmd, mem_len);
        } else { //i == commandno
            cmd = (char *)malloc(mem_len);
        }

        strcat(cmd, argv[i]);
        strcat(cmd, " ");
    }
  if (newline == 0)
        usleep(225000);
    for (i = 0; cmd[i]; i++)
        ioctl (fd, TIOCSTI, cmd+i);
    if (newline == 1)
        ioctl (fd, TIOCSTI, nl);
    close(fd);
    free((void *)cmd);
    exit (0);
}

Run the following command in the directory where the C file (ttyecho.c) has been created

make ttyecho

The above command will create an executable file named ttyecho Copy this file to the bin directory under your Home Directory. In my case it is /home/pi/bin . Create the bin directory if it doesn’t exist. It is a good practice to keep all custom binaries/executables in this bin directory.

Now change the owner/group to root using the following commands

sudo chown root:root ttyecho
sudo chmod u+s ttyecho

The above two commands change the owner/group to root and set the setuid bit for the executable which will allow you to run the utility with root permissions.

Start another terminal or switch to another existing terminal that you wish to control and execute the command tty in order to determine the terminal. You can see a sample output below.

@~$ tty
/dev/pts/5

Now to execute a command on /dev/pts/5, run the following command in the controlling/original terminal.

ttyecho -n /dev/pts/5 ls

You will see that the ls command is executed in /dev/pts/5. The -n option makes ttyecho send a newline after the command, so that the command gets executed and not just inserted.

 

 

Setting up Wifi (EDIMAX) on Raspberry Pi

This post is adapted from http://kerneldriver.wordpress.com/2012/10/21/configuring-wpa2-using-wpa_supplicant-on-the-raspberry-pi/

First you need to see if the EDIMAX usb Wifi is recognized by using command

lsusb

Once you see the device connected, edit the file /etc/network/interfaces (requires root)

....

auto wlan0
allow-hotplug wlan0
iface wlan0 inet manual
wpa-roam /etc/wpa_supplicant/wpa_supplicant.conf

iface default inet dhcp

Then edit /etc/wpa_supplicant/wpa_supplicant.conf (requires root):

network={
    ssid="Your SSID Here"
    proto=RSN
    key_mgmt=WPA-PSK
    pairwise=CCMP TKIP
    group=CCMP TKIP
    psk="YourPresharedKeyHere"
}

After that, you will need to bring up the “wlan0″ interface using “ifup” (requires root):

$ sudo ifup wlan0

You may see the following messages like this:

ioctl[SIOCSIWAP]: Operation not permitted
ioctl[SIOCSIWENCODEEXT]: Invalid argument
ioctl[SIOCSIWENCODEEXT]: Invalid argument

You may ignore the messages but the adapter should be usable now

Installing debian package (nano) on FriedlyArm (Arch Linux with QT)

I am a fan of nano editor and somehow FriendlyArm does not come with it. The following is a script which I use in installing nano

wget "http://ftp.tw.debian.org/debian/pool/main/n/nano/nano-tiny_2.2.4-1_armel.deb" 
wget "http://ftp.tw.debian.org/debian/pool/main/s/slang2/libslang2_2.2.2-4_armel.deb" 
FILES=$(ls /root/*deb)
cd /
for NAME in $FILES ; do
     ar -x "${NAME}"
     tar zxf data.tar.gz
     rm debian-binary data.tar.gz control.tar.gz
     rm "${NAME}"
done
cd –
ln -sf /bin/nano-tiny /bin/nano

Note: copy the content of extracted lib to /lib also
 

Building and installing wxPython 2.9.4.0 on Raspberry Pi (Raspbian)

As of writing the official release of the wxPython is version 2.8 which one could use apt-get install:

sudo apt-get install python-wxgtk2.8 python-wxtools wx2.8-i18n libwxgtk2.8-dev libgtk2.0-dev

However, in my case, I need to use the WebView and it is available in version 2.9. This is the reason why I have to build and install wxPython 2.9.4.0 from source.

Download and Patch Source

cd ~
wget "http://downloads.sourceforge.net/project/wxpython/wxPython/2.9.4.0/wxPython-src-2.9.4.0.tar.bz2"
wget "http://downloads.sourceforge.net/project/wxpython/wxPython/2.9.4.0/wxPython-src-2.9.4.1.patch" 
tar xvjf wxPython-src-2.9.4.0.tar.bz2
patch -p 0 -d wxPython-src-2.9.4.0/ < wxPython-src-2.9.4.1.patch
mkdir wxPython-src-2.9.4.0/bld

Install Dependencies

sudo apt-get install dpkg-dev build-essential swig python-dev libwebkit-dev libjpeg-dev libtiff-dev checkinstall freeglut3 freeglut3-dev libgtk2.0-dev  libsdl1.2-dev libgstreamer-plugins-base0.10-dev libgstreamer0.10-dev libgconf2-dev libglapi-mesa libosmesa6 libglu1-mesa-dev libglu1-mesa mesa-common-dev

Note that I install mesa as well since wxPython requires OpenGL and mesa packages include OpenGL headers. Also, some packages above may not necessary but I install then since I was not sure.

Build and Install

This step takes hours (~8 hours).

cd wxPython-src-2.9.0.1/wxPython
python build-wxpython.py --build_dir=../bld --install

Note: make sure that python command that you use matches the version of the python that you are planning to use with wxPython. You may check the version by

python -V

Final Touch

The wx modules are placed under /usr/local/lib but when I run demo.py it does not recognize wx since modules were misplaced. I copy all the wx modules under /usr/local/lib to /usr/lib

sudo cp /usr/local/lib/libwx_*  /usr/lib

Now you can try the demo under

python demo/demo.py