Learning/Linux

From Renesas.info

This page contains useful General Embedded Linux information

Using SD Card, MMC Card or USB Flash Drive

What is Mounting

When an SD card, MMC card or a USB Flash drive is plugged into a Linux machine, the Linux kernel detects it and assigns it a ‘device name’. However, for the user to be able to access the attached device, it needs to be ‘mounted’ to some location in the Linux filesystem. In most desktop Linux distribution that mounting is done automatically by the OS. The RZ/G2 Linux OS does not do that though, so the external drive needs to be mounted manually before using it.

To mount a device interface, you need an empty target directory to mount to. There is usually a /mnt directory by default in any Linux system with no files in it. You can either use that directory or make your own. Realize that as soon as you use that directory as a mount point, the files and directories that were there (before you mounted) will then be inaccessible until the device is unmounted.

Note: Since SD Card and MMC or so similar, SD devices are referred to as ‘mmc’ devices in Linux.

Mounting an SD Card or MMC Card

This section is for the case when the SD card is connected to the system via a dedicated SD card read, NOT via a USB adapter. Go to the next section for instructions on mounting a USB flash drive or and SD card plugged into a USB adapter.

When you plug an SD card in, you will see a message on the serial console like this:

mmc1: new high speed SDHC card at address e624
mmcblk0: mmc1:e624 SU04G 3.69 GiB
mmcblk0: p1 p2

This is showing you that a SD/MMC card was detected and it discovered that it has two partitions (p1 and p2). You will then see that you have some new devices under the /dev directory:

$ ls -l /dev/mmc*
brw-------    1 root     root      179,   0 Jan  1 09:16 /dev/mmcblk0
brw-------    1 root     root      179,   1 Jan  1 09:16 /dev/mmcblk0p1
brw-------    1 root     root      179,   2 Jan  1 09:16 /dev/mmcblk0p2

The interface mmcblk0 is the raw interface to the entire card starting at sector 0 (otherwise known as the Master Boot Record). The interfaces mmcblk0p1 and mmcblk0p2 are also raw interfaces, but they are index to start at the beginning of each partition the kernel found making it easy for mounting. Note, you can only “mount” a partition /dev/mmcblk0p0, /dev/mmcblk0p1, etc… You cannot mount the entire root device itself ( /dev/mmcblk0 ).

Next, to mount the first partition to directory /mnt you would type

$ mount /dev/mmcblk0p1 /mnt

Then you can see those files by doing:

$ cd /mnt
$ ls -l

Note: To see everything in the system that is currently mounted, simply type the command mount with no parameters. As you look through the list that is displayed up, realize that some mounts are not actually physical device, but rather virtual device with various types of file systems. This is an example of how Linux likes to keep interfaces and functionality standard and common across the system (ie, everything is accessible ‘through a file system’), but then hides all the ugly difference deeper in semantics of how you ‘use’ these standard interfaces. For example, Stringed Instruments; Many instruments have strings, but how you play a violin is much different than how you play a banjo.

$ mount

Mounting a USB Flash Drive

Like the SD Card, when you plug in an USB Flash drive (or an SD card via a USB adapter), you will see a message on the serial console. However, the /dev names will be “sda”, “sdb” and so on (instead of mmc). The ‘s’ and ‘d’ stand for SCSI Device (because a SUB Flash driver is basically the SCSI protocol over USB). The ‘a’ just means the first device plugged in, meaning if you had a USB hub and multiple driver plugged in, you would have sda, sdb, sdc, etc.

IMPORTANT: If you are working on a Linux Host machine, that machine’s hard drive would most likely already be assigned the name ‘sda’. That is the case for example on your Ubuntu 16 laptop, assuming that you accepted the standard options during the installation of Ubuntu. That means that on a desktop machine your USB flash drive will be recognized as ‘sdb’ (or a later letter).

The RZ/G2 boards do not have hard drives, so there is no ‘sda’ device in the system. When you plug in a USB flash drive into an RZ/G board, it will most likely be recognized as ‘sda’.

As before, the kernel automatically breaks the device into separate partition interfaces.

$ ls -l /dev/sda*
brw-------    1 root     root        8,   0 Jan  1 09:50 /dev/sda
brw-------    1 root     root        8,   1 Jan  1 09:50 /dev/sda1
brw-------    1 root     root        8,   2 Jan  1 09:50 /dev/sda2

Next, to mount the first partition to directory /mnt you would type

$ mount /dev/sda1 /mnt

Then you can see those files by doing:

$ cd /mnt
$ ls -l

Sync and Un-mount

When writing to a device, the kernel will buffer up the data and only write it out occasionally or if you are writing a lot of data, the system will return that it’s done, but really in the background it will keep writing the data out to the physical device. The command ‘sync’ in Linux means to flush out any pending buffers and not return from the sync command until everything is written.

To unmount a drive (to safely remove it), use the ‘umount’ command. You can pass either the /dev name or the target directory. Example:

$ umount /dev/sda1

or

$ umount /mnt

Note: A sync is done automatically during the umount procedure. So if it takes a long time, it could be that data was still being written out.

Note: Before you umount a drive, make sure you change in your console to a different directory that the one that you mounted (ie, use the ‘cd’ command to somewhere), otherwise you will get an error “Device or resource busy”…..because your console process is still in there and it can’t unmount a location that someone is current look at.

Networking

Use ifconfig to find your IP address

  • In Windows, you use ipconfig. In Linux, you use ifconfig.
  • The ifconfig command will show you your IP address and MAC address as well as how many packets have been sent/received.

Using Ethernet

The Linux OS used on the RZ boards brings up the Ethernet interface automatically during startup. This is done by the "systemd-networkd" service that is automatically start each time you boot. If your RZ board is connected to a network that has a DHCP server, the board will obtain an IP address from it automatically. You just need to execute ‘ifconfig’ to see what the assigned IP address is.

$ ifconfig

In some scenarios the Ethernet interface may not be active (meaning "systemd-networkd" is not started or used). In this case, you would enter the follow to enable it and use the ifconfig to show what interfaces are active.

$ ifconfig
$ ifconfig eth0 up
$ ifconfig

After the interface is enabled, you will need to assign an IP address. You can either do that manually or use DHCP if it is available on the network.

Manual:

$ ifconfig eth 1925.168.0.55

DHCP:

$ udhcpc -i eth0

NOTE: After ‘ifconfig eth0 up’, you may notice that it already has an IPv6 address even though it doesn’t have a IPv4 address yet. No one really assigned that IPv6 address. In IPv6, you can always assign yourself a 'link local' address by just taking your MAC address and putt putting "fe80" at the beginning and "fe00" in the middle.


Working with SSH

Secure Shell (SSH) is a cryptographic network protocol for operating network services securely over an unsecured network. Typical applications include remote command-line, login, and remote command execution, but any network service can be secured with SSH. (https://en.wikipedia.org/wiki/Secure_Shell)

To log into your board using SSH, in your Linux PC you would type: $ ssh xx.xx.xx.xx (ip address of board)

Note that you will rarely use ‘ssh’ on the command line like this. It is much better to use some application that knows how to talk over SSH. One such very popular application is PyTTY, and it is available on both Windows and Linux.

To log into your board, you would use: $ ssh root@xx.xx.xx.xx


Copying Files using SCP (in Linux)

SCP (Secure Copy) is a Linux program that allows to copy files over SSH. You can use this to copy files to/from your board, or to/from PC to PC.

SCP examples (taken from https://haydenjames.io/linux-securely-copy-files-using-scp/ )

Copy file from local host to a remote host SCP example:

$ scp file.txt username@to_host:/remote/directory/

This is the command that you would use the most to copy files from your Ubuntu host machine to the RZ/G2 board. In that case the command typically looks like this:

$ scp file.txt root@192.168.xxx.xxx:/home/root/

Copy file from a remote host to local host SCP example:

$ scp username@from_host:file.txt /local/directory/

Copy directory from a remote host to local host SCP example:

$ scp -r username@from_host:/remote/directory/  /local/directory/

Copy directory from local host to a remote host SCP example:

$ scp -r /local/directory/ username@to_host:/remote/directory/

Copying Files using WinSCP (Windows Application)

WinSCP is a Windows program that makes it easy to copy files from a Windows PC to either a Linux PC or board. It also allows you to create and edit files on an RZ/G board directly from your Windows machine.

WinSCP can be found here: https://winscp.net/eng/index.php It is pretty simple to use: Just put in the IP address of what you want to connect to.

When connecting to an RZ/G board make sure to select ‘SCP’ as file protocol. The default protocol is SFTP, and the RZ/G Linux OS does not support that by default.

Terminal login using PuTTY (Windows or Linux Application)

PuTTY is a good program, and it is supported in Windows or Linux. Install and usage is pretty straight forward. You simple create a new connection and enter IP address, username and password. Remember, for the RZ boards, the username is “root” with no password (leave password section blank)

Linux Install:

  • Type this into a Terminal in Linux
$ sudo apt-get install

Windows Install:

Terminal login using TeraTerm (Windows Application)

  • TeraTerm also supports SSH, so you can use that as well.

TeraTerm SSH Connection.png

Working with Files in Linux

🚧 coming soon

Edit a text file on the board using vi

🚧 coming soon

Partitioning and Formatting an SD Card

🚧 coming soon

Using PuTTY for your Serial Terminal

🚧 coming soon

Showing CPU Usage While Executing an Application

🚧 coming soon