Linux FAQs - andreimironenko/oe-davinci GitHub Wiki

Table of Contents

How to see CPU load?

Use this command

 $ top

You should see the output similar to that:

File:Linux-cpu-utilization.png

How gracefully to stop threads?

This is an excerpt from Linux write man:



...

"This function is a cancellation point in multi-threaded programs. This is a problem if the thread allocates some resources (like memory, file descriptors, semaphores or whatever) at the time write is called. If the thread gets canceled these resources stay allocated until the program ends. To avoid this, calls to write should be protected using cancellation handlers."

...

Literally, it says that if you kill (by calling pthread_kill) or exit from the main while the write operation is in progress in other threads, it can be a problem! We've observed this behaviour while playing sound on HTC. Sometimes I2C bus gets stuck. Although this is partly a driver problem, the application should always exits gracefully.

The easiest way for this, when for instance your program received SIGINT(Ctrl+C), introducing a stop_flag which can be seen and checked by all threads. I have a simple test app, which performs stress test for I2C bus. There some threads which perform read/write to the certain I2C devices like EEPROM, audio, RTC. You can get it from here:

 git clone git@amuxi:/opt/git/htc/tests/i2c.git

password - git

In practice read/write to this flag should be atomic operations, but as in this implementation I only change this flag from signal handler, I skipped it for simplicity.

How to check available serial ports

Simple run dmesg command

 $ dmesg | grep tty

Output:

 [   37.531286] serial8250: ttyS0 at I/O 0x3f8 (irq = 4) is a 16550A
 [   37.531841] 00:0b: ttyS0 at I/O 0x3f8 (irq = 4) is a 16550A
 [   37.532138] 0000:04:00.3: ttyS1 at I/O 0x1020 (irq = 18) is a 16550A

Another way - use of setserial command

setserial is a program designed to set and/or report the configuration information associated with a serial port. This information includes what I/O port and IRQ a particular serial port is using, and whether or not the break key should be interpreted as the Secure Attention Key, and so on. Just type the following command:

 $ setserial -g /dev/ttyS[0123]

Output:

 /dev/ttyS0, UART: 16550A, Port: 0x03f8, IRQ: 4
 /dev/ttyS1, UART: 16550A, Port: 0x1020, IRQ: 18
 /dev/ttyS2, UART: unknown, Port: 0x03e8, IRQ: 4
 /dev/ttyS3, UART: unknown, Port: 0x02e8, IRQ: 3
 setserial with -g option help to find out what physical serial ports your Linux box has.

How to create USB bootable pen-drive?

Have ready .iso file and plug-in pen-drive. By issuing this command

 dmesg | tail -20  

find the device node assigned to the pen-drive. Usually it should be like /dev/sdg1 (or /dev/sdb).

Unmount all the partions of the pen-drive

 sudo umount /dev/sdg

Write ISO image to the destination drive

 dd if=~/ubuntu.iso of=/dev/sdg 

wait until it completes and make sync with cache to avoid any possible loss

 sync

It's ready now just remove pen-drive.

How to check free space from command line?

To check all drives use this command

 $ df -h

To check the concrete directory structure use

 $ du -ahc

Look at this article for more "du" command examples.

How to check available memory

First use of free command

 root@dm365-htc:~# free                                                          
               total         used         free       shared      buffers         
   Mem:       125672        21956       103716            0         1180         
  Swap:            0            0            0                                   
 Total:       125672        21956       103716

Or use of top

 root@dm365-htc:~# top

the output should be like this:

File:Linux-ram-size-top-command-usage.png

How to remove duplications in Vim

It's very useful article. I used this VIM command:

 : %s/^\(.*\)\(\n\1\)\+$/\1/

How to disover other devices in the network?

Install nmap util

 $ sudo apt-get install nmap

then run this command

 $ sudo nmap -sP 10.42.43.*

you should get the output like this:

 Starting Nmap 5.21 ( http://nmap.org ) at 2012-06-26 14:32 BST
 Nmap scan report for 10.42.43.1
 Host is up.
 Nmap scan report for htc (10.42.43.2)
 Host is up (0.00081s latency).
 MAC Address: 36:4B:97:7A:2C:5A (Unknown)
 Nmap scan report for 10.42.43.26
 Host is up (0.0016s latency).
 MAC Address: 00:14:2D:48:71:32 (Toradex AG)
 Nmap done: 256 IP addresses (3 hosts up) scanned in 2.17 seconds

How to print out last 50 lines of kernel log?

Use this line

 dmesg | tail -n 50

it prints last 50 lines of the kernel log

How to mute/unmute sound input/output from command line?

Run

 alsamixer 

And find what widgets(input/output lines) are available

File:Alsamixer.png

In our case, there is "Digital" output so run this command to mute

 amixer set Digital 0%

to restore the desired level run the same command

 amixer set Digital 93%

Use rsync instead of scp (ssh copy) !

What scp shouldn't be used for:

  1. When you are copying more than a few files, as scp spawns a new process for each file and can be quite slow and resource intensive when copying a large number of files.
  2. When using the -r switch, scp does not know about symbolic links and will blindly follow them, even if it has already made a copy of the file. The can lead to scp copying an infinite amount of data and can easily fill up your hard disk, so be careful.
rsync

rsync has very similar syntax to scp:

 rsync -e ssh [-avz] /some/file [ more ... ] host.name:/destination/file

-or-

rsync -ave ssh user@server:/path/to/source /destination/dir

rsync's speciality lies in its ability to analyse files and only copy the changes made to files rather than all files. This can lead to enormous improvements when copying a directory tree a second time.

Switches:

-a Archive mode, most likely you should always keep this on. Preserves file permissions and does not follow symlinks.

-v Verbose, lists files being copied

-z Enable compression, this will compress each file as it gets sent over the pipe. This can greatly decrease time depending on what sort files you are copying.

-e ssh Uses ssh as the transport, this should always be specified.

Disadvantages of using rsync:

  1. Picky syntax, use of trailing slashes can be confusing.
  2. Have to remember that you are using ssh.
  3. rsync is not installed on all computers.

NAT in linux ?

Look at this article about how to set NAT up in linux.

How to change linux kernel start address ?

Look at this very useful article from TI.

How to write SD card image ?

When you plug-in your card system assigns device ID for the card. You need this information for passing it to dd command. To define your card device ID run this command

 $ dmesg | tail -n 20

which will print last 20 lines of kernel log messages, so it should look like this

 [1552603.677544] sd 39:0:0:0: Attached scsi generic sg7 type 0
 [1552603.680738] sd 39:0:0:0: [sdg] Attached SCSI removable disk
 [1552820.685519] usb 1-4: USB disconnect, address 62
 [1557138.262634] sd 37:0:0:2: [sdd] 3926016 512-byte logical blocks: (2.01 GB/1.87 GiB)
 [1557138.263727] sd 37:0:0:2: [sdd] Assuming drive cache: write through
 [1557138.265621] sd 37:0:0:2: [sdd] Assuming drive cache: write through
 [1557138.266686]  sdd: sdd1
 [1557139.099516] EXT2-fs (sdd1): warning: mounting unchecked fs, running e2fsck is recommended

From this print, it easy to define that my card is /dev/sdd.

Now we are ready. Have your image on place, in my case this is sdk1.2.x.img and issue dd command to write to SD card

 $ sudo dd if=sdk1.2x.img of=/dev/sdd

Be patient for sometime, depending on the image size it can take up to 5-10 minutes.

Don't remove your card while dd command is in the progress! It might destroy the card!

Once it finishes your card is ready to use :-)

Image Writer for Windows

RPi Easy SD card setup

Query for the biggest files/dirs

 $ du -k | sort -n | tail -40

How to test serial connection ?

Serial port loopback duplex test

Or I used these simple method

On a target run this command, which will generate infinite stream of random bytes to serial:

 $ cat /dev/urandom > /dev/ttyUSB0

on the desktop just run minicom. You might need to alter the serial port settings, try to play with settings util you get a flow of random bytes.

How to start demon (background program) ?

Look at this article.

How to log and retrieve system/application messages ?

For the application side, syslog is the answer, use this link for further information.

For the kernel side (printk) messages use dmesg command to retrieve the latest log.

How to profile any operation in Linux

The command "time" with followed command under investigation will give the execution time:

 $ time cp xdctools_setuplinux_3_16_03_36.bin  xdctools_setuplinux.bin
 real	0m21.773s
 user	0m0.008s
 sys	0m0.268s

I ran "copy" while OE build was in progress so it slowed down write operation and as you see copy the image took 21.773 s.

Enable/Disable firewall in Ubuntu 12.04

To disable

 $ sudo ufw disable

to enable back

 $ sudo ufw enable

How to maintain multiple RSA keys?

I found this article quite useful. I also copy context of it here.

In quite a few situations its preferred to have ssh keys dedicated for a service or a specific role. Eg. a key to use for home / fun stuff and another one to use for Work things, and another one for Version Control access etc. Creating the keys is simple, just use

 ssh-keygen -t rsa -f ~/.ssh/id_rsa.work -C "Key for Word stuff"

Use different file names for each key. Lets assume that there are 2 keys, ~/.ssh/id_rsa.work and ~/.ssh/id_rsa.misc . The simple way of making sure each of the keys works all the time is to now create config file for ssh:

 touch ~/.ssh/config
 chmod 600 ~/.ssh/config
 echo "IdentityFile ~/.ssh/id_rsa.work" >> ~/.ssh/config
 echo "IdentityFile ~/.ssh/id_rsa.misc" >> ~/.ssh/config

This would make sure that both the keys are always used whenever ssh makes a connection. However, ssh config lets you get down to a much finer level of control on keys and other per-connection setups. And I recommend, if you are able to, to use a key selection based on the Hostname. My ~/.ssh/config looks like this :

  Host *.home.lan
  IdentityFile ~/.ssh/id_dsa.home
  User kbsingh
  Host *.vpn
  IdentityFile ~/.ssh/id_rsa.work
  User karanbir
  Port 44787
  Host *.d0.karan.org
  IdentityFile ~/.ssh/id_rsa.d0
  User admin
  Port 21871

Ofcourse, if I am connecting to a remote host that does not match any of these selections, ssh will default back to checking for and using the 'usual' key, ~/.ssh/id_dsa or ~/.ssh/id_rsa.

How to convert OpenSSH private key to DropBear format

You can get Hanover public and private keys here:

 hanover-system-dev/recipes/hanover-security/hanover-security-git

Unfortunately it must be done on a target

 dropbearconvert openssh dropbear ./hanover.id_rsa hanover.id_rsa.db

Afterwards this private key can be used for password-less SSH access to other devices

 ssh -i ./hanover.id_rsa.db [email protected] ls




How to mount CIFS(Samba) Fileserver share ?

Make sure to install cifs-utils package:

 sudo apt-get install cifs-utils

create a mount point under /media

 sudo mkdir -p /media/development

then mount the share by issuing this command

 sudo mount.cifs //192.168.0.10/development /media/development -o user=yourusername pass=yourpassword dom=HANOVER.LOCAL

Start browsing /media/development!

Using curl to put/get files from HTTP/FTP server

Examples of using curl

CURL FAQs

Copy filet to FTP server

 curl --anyauth -T ./ready_IP_TFT ftp://[email protected]/obc-iv/

Get list of files from FTP server

 for (( f=0 ; f < ${#list_files[*]} ; f++ )) ; do
    echo ${list_files[$f]}
 done

Download a single file

 curl -O ftp://[email protected]/obc-iv/ipk/IP_TFT/amironenko/iptft.r01-rc03.tar.bz2

Delete a single file obc-iv/ready_IP_TFT from server ftp://[email protected]/, using anonymous FTP user which denoted ftp@

 curl --quote "DELE obc-iv/ready_IP_TFT" ftp://[email protected]/

How to use BusyBox /etc/inittab

While modifying or creating a custom BSP for the embedded Linux, one of the many things which needs better understanding is Inittab. Inittab describes which processes are started during bootup. It is the job of init to read the inittab file and start the necessary application.

Format of each entry in the inittab resembles:

 < id >:< runlevels >:< action >:< process >

Where < id >: The id field is used by BusyBox init to specify the controlling tty for the specified process. If BusyBox detects that a serial console is in use, then the id field is ignored.

< runlevels >: The runlevels field is ignored buy busybox. Init used in desktop systems also understands runlevels. However, init which comes with busybox doesn't support runlevels. If runlevels are needed sysvinit should be used in place init.

< action >: Valid actions include sysinit, respawn, askfirst, wait, once, restart, ctrlaltdel, and shutdown.

  1. sysinit :The process will be executed during system boot. It will be executed before any boot or bootwait entries.
  2. respawn :The process will be restarted whenever it terminates
  3. askfirst : The askfirst actions acts just like respawn, except that before running the specified process it displays the line "Please press Enter to activate this console." and then waits for the user to press enter before starting the specified process.
  4. ctrlaltdel :The process will be executed when init receives the SIGINT signal. This means that someone on the system console has pressed the CTRL-ALT-DEL key combination. Typically one wants to execute some sort of shutdown either to get into single-user level or to reboot the machine.
< process >: Specifies the process to be executed and it's command line.

Few examples from busysybox inittab:

 ::sysinit:/etc/init.d/rcS

Put a getty on the serial line (for a terminal)

 ::respawn:/sbin/getty -L ttyS 115200 vt100
 # Stuff to do when restarting the init process
 ::restart:/sbin/init
 # Stuff to do before rebooting
 ::ctrlaltdel:/sbin/reboot

How to access device registers from shell

At first list all available I/O areas

 $ cat /proc/iomem

You should see something like this

 48030100-480301ff : omap2_mcspi.1
 48030100-480301ff : omap2_mcspi.1
 48032000-48032fff : omap_gpio.0
 4804c000-4804cfff : omap_gpio.1

Please notice, these are PHYSICAL addresses not VA. To list first 10 16-bit words in gpio.0, beginning at address 0x48032000

 $ dd if=/dev/mem skip=$((0x481ac000)) bs=$((0x10)) count=10 2> /dev/null | hexdump -C
 00000000  37 ff ff bb ff ff fa db  bf ff fe ef f7 ff ff b7  |7...............|  
 00000010  2f df f7 c9 7f ff ff bf  67 ff ff fe ff cf ff fe  |/.......g.......|  
 00000020  ff eb ff fe ff ff ff f7  ff df ff ff ff 9a ff ff  |................|   
 00000030  7f bb ef ff ff ff f7 7f  ff df bf bf ff ef ff fb  |................|  
 00000040  eb 7f 97 f7 f3 ff ef eb  f7 ff ff f9 fa bf fe af  |................|  
 00000050  ff dd ff ff df f5 ff df  ff ed be fd df bf fe dc  |................|  
 00000060  df ff f6 f7 df ff ff bf  f7 ff be f7 ff d6 ef bf  |................|  
 00000070  ff f3 ff 7f bb ff ff fc  db ff ff ff ef ff f7 fe  |................|  
 00000080  b7 73 df fb ff ff ff ff  fb db ef ff ff be b7 7f  |.s..............|  
 00000090  f8 df ff fe ff df ff ff  df 76 7f fe bf fd 9f fc  |.........v......|

Write data

 dd if=datafile of=/dev/kmem bs=1 seek=805e000 conv=notrunc

How to search for textparten in the files with certain extensions

 find . -regex ".*\.\(c\|sh\|inc\|cpp\|h\|hpp\)"  -exec grep -Hn "\/dev\/null" {} \; -print | tee -a ~/dev.null.log

There are two parts in this command: find and grep. Find searches for all files with the extensions given in regex and grep search for the text pattern "/dev/null" in them.

How to redirect output to other TTY or remote machine via ssh

One possibility I found for redirecting logs to the remote machine:

 ls -R | ssh [email protected] "cat > /dev/tty1"

Similarly to the other terminal on the same machine:

 ls -R | cat > /dev/tty1

to store output to the local file as well do

for the remote tty:

 ls -R | tee -a cmd.log | ssh [email protected] "cat > /dev/tty1"

for other local tty:

 ls -R | tee -a cmd.log | cat > /dev/tty1

Crontab examples

Fifteen practical crontab examples

How to manage services with update-rc.d util

Examples of update-rc.d usage

Network util nmap usage examples

The nmap usage examples

Installing VirtualBox on Ubuntu

Read this article ...

Useful links

Ibiblio Linux HOWTO docs

Linux Standard Base, Specification Archive

⚠️ **GitHub.com Fallback** ⚠️