Showing posts with label Linux PC. Show all posts

Attach webcam to VirtualBox



1. Download and install VirtualBox_Extension_Pack
Check version of installed VirtualBox and download extension pack for this VirtualBox version

root# vboxmanage extpack install Oracle_VM_VirtualBox_Extension_Pack-5.1.30-118389.vbox-extpack

2. Enable USB controller

3. List up camera by below command
user$ VBoxManage list webcams
Video Input Devices: 1
.1 "Integrated_Webcam_HD: Integrate"
/dev/video0

4. Start virtual machine (ex: pi)
5. Attach camera to started machine
user$ vboxmanage controlvm "pi" webcam attach .1

with:
- pi: virtual machine name
- .1 : camera device list up by step 3

Note: Using VLC to test on virtual machine "pi"


Tuesday, December 31, 2019
Tag :

FFmpeg


1. Merge files
Create a file mylist.txt with all the files you want to have concatenated in the following form (lines starting with a # are ignored):

 # this is a comment  
 file '/path/to/file1.mp4'  
 file '/path/to/file2.mp4'  
 file '/path/to/file3.mp4'  

Note that these can be either relative or absolute paths. Then you can stream copy or re-encode your files:

 ffmpeg -f concat -i mylist.txt -c copy output.mp4  

2. Split

 ffmpeg -i input.avi -vcodec copy -acodec copy -ss 00:00:00 -t 00:30:00 output1.avi  

3. Convert

 ffmpeg -i input.avi output1.mp3 

 find . -exec ffmpeg -i {} {}.mp3 \;

Sunday, December 6, 2015
Tag :

Tự động mount phân vùng ổ cứng trong Fedora

1. Cài đặt phần mềm quản lý disk cho Fedora

 yum install gnome-disk-utility  

2. Sử dụng
- Mở theo đường dẫn:
Applications > Accessories > Disks

- Chọn phân vùng ổ cứng muốn auto mount, ví dụ là Data (NTFS)
- Chọn More Actions > Edit Mount Actions
- Các options như hình thứ 2





- Từ đây, mỗi lần máy tính bootup lên phân vùng Data sẽ tự động được mount vào.


Saturday, September 19, 2015
Tag :

Environment, Parameter Variables


1. Environment variables
Biến môi trường thường được dùng để chứa các thông tin như là options hoặc các đường dẫn (path) đến một thư mục nào đó.
Để show các biến môt trường mặc định của Linux OS và một số phần mềm tạo ra, bạn dùng lệnh env:

 $ env  
 XDG_VTNR=1  
 MATE_DESKTOP_SESSION_ID=this-is-deprecated  
 SSH_AGENT_PID=1421  
 XDG_SESSION_ID=1  
 HOSTNAME=localhost.localdomain  
 XDG_GREETER_DATA_DIR=/var/lib/lightdm-data/ninhld  
 IMSETTINGS_INTEGRATE_DESKTOP=yes  
 GPG_AGENT_INFO=/home/ninhld/.cache/keyring-fbkBhK/gpg:0:1  
 TERM=xterm-256color  
 SHELL=/bin/bash  
 HISTSIZE=1000  
 WINDOWID=73400351  
 GNOME_KEYRING_CONTROL=/home/ninhld/.cache/keyring-fbkBhK  
 QTDIR=/usr/lib64/qt-3.3  
 QTINC=/usr/lib64/qt-3.3/include  
 IMSETTINGS_MODULE=IBus  
 QT_GRAPHICSSYSTEM_CHECKED=1  
 USER=ninhld  
 LS_COLORS=rs=0:di=38;5;27:ln=38;5;51:mh=44;38;5;15:pi=40;38;5;11:so=38;5;13:do=38;5;5:bd=48;5;232;38;5;11:cd=48;5;232;38;5;3:or=48;5;232;38;5;9:mi=05;48;5;232;38;5;15:su=48;5;196;38;5;15:sg=48;5;11;38;5;16:ca=48;5;196;38;5;226:tw=48;5;10;38;5;16:ow=48;5;10;38;5;21:st=48;5;21;38;5;15:ex=38;5;34:*.tar=38;5;9:*.tgz=38;5;9:*.arc=38;5;9:*.arj=38;5;9:*.taz=38;5;9:*.lha=38;5;9:*.lzh=38;5;9:*.lzma=38;5;9:*.tlz=38;5;9:*.txz=38;5;9:*.tzo=38;5;9:*.t7z=38;5;9:*.zip=38;5;9:*.z=38;5;9:*.Z=38;5;9:*.dz=38;5;9:*.gz=38;5;9:*.lrz=38;5;9:*.lz=38;5;9:*.lzo=38;5;9:*.xz=38;5;9:*.bz2=38;5;9:*.bz=38;5;9:*.tbz=38;5;9:*.tbz2=38;5;9:*.tz=38;5;9:*.deb=38;5;9:*.rpm=38;5;9:*.jar=38;5;9:*.war=38;5;9:*.ear=38;5;9:*.sar=38;5;9:*.rar=38;5;9:*.alz=38;5;9:*.ace=38;5;9:*.zoo=38;5;9:*.cpio=38;5;9:*.7z=38;5;9:*.rz=38;5;9:*.cab=38;5;9:*.jpg=38;5;13:*.jpeg=38;5;13:*.gif=38;5;13:*.bmp=38;5;13:*.pbm=38;5;13:*.pgm=38;5;13:*.ppm=38;5;13:*.tga=38;5;13:*.xbm=38;5;13:*.xpm=38;5;13:*.tif=38;5;13:*.tiff=38;5;13:*.png=38;5;13:*.svg=38;5;13:*.svgz=38;5;13:*.mng=38;5;13:*.pcx=38;5;13:*.mov=38;5;13:*.mpg=38;5;13:*.mpeg=38;5;13:*.m2v=38;5;13:*.mkv=38;5;13:*.ogm=38;5;13:*.mp4=38;5;13:*.m4v=38;5;13:*.mp4v=38;5;13:*.vob=38;5;13:*.qt=38;5;13:*.nuv=38;5;13:*.wmv=38;5;13:*.asf=38;5;13:*.rm=38;5;13:*.rmvb=38;5;13:*.flc=38;5;13:*.avi=38;5;13:*.fli=38;5;13:*.flv=38;5;13:*.gl=38;5;13:*.dl=38;5;13:*.xcf=38;5;13:*.xwd=38;5;13:*.yuv=38;5;13:*.cgm=38;5;13:*.emf=38;5;13:*.axv=38;5;13:*.anx=38;5;13:*.ogv=38;5;13:*.ogx=38;5;13:*.aac=38;5;45:*.au=38;5;45:*.flac=38;5;45:*.mid=38;5;45:*.midi=38;5;45:*.mka=38;5;45:*.mp3=38;5;45:*.mpc=38;5;45:*.ogg=38;5;45:*.ra=38;5;45:*.wav=38;5;45:*.axa=38;5;45:*.oga=38;5;45:*.spx=38;5;45:*.xspf=38;5;45:  
 XDG_SESSION_PATH=/org/freedesktop/DisplayManager/Session0  
 XDG_SEAT_PATH=/org/freedesktop/DisplayManager/Seat0  
 SSH_AUTH_SOCK=/home/ninhld/.cache/keyring-fbkBhK/ssh  
 SESSION_MANAGER=local/unix:@/tmp/.ICE-unix/1324,unix/unix:/tmp/.ICE-unix/1324  
 PATH=/usr/lib64/qt-3.3/bin:/usr/lib64/ccache:/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/home/ninhld/.local/bin:/home/ninhld/bin  
 MAIL=/var/spool/mail/ninhld  
 DESKTOP_SESSION=mate  
 QT_IM_MODULE=xim  
 PWD=/home/ninhld  
 XMODIFIERS=@im=ibus  
 GNOME_KEYRING_PID=1320  
 LANG=en_US.utf8  
 KDE_IS_PRELINKED=1  
 GDM_LANG=en_US.utf8  
 KDEDIRS=/usr  
 GDMSESSION=mate  
 HISTCONTROL=ignoredups  
 HOME=/home/ninhld  
 XDG_SEAT=seat0  
 SHLVL=2  
 LOGNAME=ninhld  
 QTLIB=/usr/lib64/qt-3.3/lib  
 DBUS_SESSION_BUS_ADDRESS=unix:abstract=/tmp/dbus-MoTJpiQTi2,guid=1471c082de74b829c532fffc5593dfa5  
 LESSOPEN=||/usr/bin/lesspipe.sh %s  
 XDG_RUNTIME_DIR=/run/user/1000  
 DISPLAY=:0.0  
 CCACHE_HASHDIR=  
 COLORTERM=mate-terminal  
 XAUTHORITY=/var/run/lightdm/ninhld/xauthority  
 _=/usr/bin/env  


Biến môi trường mặc định:

Để có thể show nội dung của một biến môi trường cụ thể nào đó bạn dùng lệnh echo:
 [ninhld@localhost env]$ echo $HOME  
 /home/ninhld  
 [ninhld@localhost env]$   
 [ninhld@localhost env]$   
 [ninhld@localhost env]$ echo $PATH  
 /usr/lib64/qt-3.3/bin:/usr/lib64/ccache:/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/home/ninhld/.local/bin:/home/ninhld/bin  
 [ninhld@localhost env]$   

Phạm vi hoạt động biến môi trường:
+ Local
Biến chỉ có giá trị trong phạm vi ngữ cảnh của terminal, các script hoặc chương trình khác không thể đọc được nội dung của biến
+ Global
Phạm vị toàn cục, các script hoặc chương trình ngoài có thể đọc được nội dung biến, để tạo ra biến global dùng lệnh export trên terminal

Ví dụ có một Shell script để show các biến môi trường như sau:
env.sh
 #!/bin/sh  
 echo "HELLO=$HELLO"
 echo "XINCHAO=$XINCHAO"  

Command
 [ninhld@localhost env]$   
 [ninhld@localhost env]$ HELLO=hello             #bien local
 [ninhld@localhost env]$ export XINCHAO=xinchao  #bien global
 [ninhld@localhost env]$     
 [ninhld@localhost env]$ ./env.sh   
 HELLO=             #khong doc duoc 
 XINCHAO=xinchao    #doc duoc gia tri
 [ninhld@localhost env]$  


2. Parameter variable
Là các đối số được đưa vào một Shell script, gần giống như đối số đưa vào một hàm trong C/C++.



para.sh

 #!/bin/sh  
 echo "para1 = $1"  
 echo "para2 = $2"  
 echo "para3 = $3"  
   
 echo "\$* = $*"  
 echo "\$@ = $@"  
   

Execute
 [ninhld@localhost env]$   
 [ninhld@localhost env]$ ./para.sh pa1 pa2 pa3  
 para1 = pa1  
 para2 = pa2  
 para3 = pa3  
 $* = pa1 pa2 pa3  
 $@ = pa1 pa2 pa3  
 [ninhld@localhost env]$  

3. Một số biến môi trường quan trọng
+ PATH
Chỉ đường dẫn đến các thư mục chứa file chương trình nhị phân hoặc script.

Ex:
 [ninhld@localhost env]$   
 [ninhld@localhost env]$ export PATH=$PATH:/home/ninhld/Github/eslinuxprogramming/Shell  
 [ninhld@localhost env]$      
 [ninhld@localhost env]$ cd $HOME  
 [ninhld@localhost ~]$    
 [ninhld@localhost ~]$ para.sh pa1 pa2 pa3  
 para1 = pa1  
 para2 = pa2  
 para3 = pa3  
 $* = pa1 pa2 pa3  
 $@ = pa1 pa2 pa3  
 [ninhld@localhost ~]$   

Chương trình para.sh ở trong thư mục Shell, nhờ việc export biến PATH mà para.sh có thể được chạy ở bất cứ đâu mà không cần thư mục làm việc hiện tại trên terminal phải là Shell.



+ LD_LIBRARY_PATH
Biến môi trường của linker chỉ dẫn đến thư mục chứa các thư viện đã được installed, dùng trong quá trình compile thư viện cũng như khi chạy chương trình ứng dụng có sử dụng thư viện đã installed đó.
Bài viết liên quan:
+ Makefile
+ Cross compiling the libraries


+ PKG_CONFIG_PATH
Biến môi trường chỉ dẫn đến thư mục chứa các file ".pc", được dùng bởi chương trình pkg-config, dùng trong quá trình compile thư viện.
Bài viết liên quan:
+ Pkg-config

Thursday, July 2, 2015

Cross compiling the libraries

Bìa viết liên quan:
+ Cross compler 
+ Makefile (Part 1)
+ Makefile (Part 2)
+ Cài đặt môi trường làm việc

1. Library configuration
Trong bài viết này hướng dẫn compile/cross compiling thư viện taglib-1.6.3 (thư viện có chức năng lấy thông tin meta-data của file nhạc) để minh họa cho dễ hiểu. Yêu cầu để thực hiện được phần này thì ít nhất phải biến cách sử dụng máy tính Linux Fedora, biết các command cơ bản trên terminal, biết phân quyền trong Linux, xem lại các bài viết trong mục  Linux PC và Linux Basic nếu bạn chưa rõ.

Một số thuật ngữ:
+ Compile: compile thư viện cho host
+ Cross compiling: compile thư viện cho target (board nhúng), ở blog này sử dụng board nhúng Linux là FriendlyARM Mini2440
+ Build: tương đương với compile / cross compiling

Thư viện được cung cấp dưới dạng source code C/C++ và một shell script đi kèm có tên configure dùng để cài đặt các options phụ vụ cho việc build thư viện, file configure này có thể mở được bằng các text editor thông thường như gedit, geany. Sau khi cấu hình xong với configure thì sẽ thu được một Makefile, từ đó dùng lệnh make để build thư viện. Kết quả thu được sau khi build xong thư viện thường có cấu trúc thư mục như sau:

 bin         #chứa các file execute nhị phân (tương tự file .exe bên Windows)
 etc         #chứa các file cấu hình (vd: file.conf)
 include     #chứa các file header để include vào các chương trình 
              có sử dụng thư viện này (vd: tag.h)
 lib         #chứa các file thư viện tĩnh/động .la/.so, cùng với header file để phục vụ 
              cho các chương trình khác có sử dụng thư viện này (vd: libtag.la, libtag.so)
 man         #chứa các file tài liệu / hướng dẫn sử dụng dạng text
 share       #chứa nhiều loại file như hướng dẫn / script / media, ...
 ...

Để bắt đầu bạn download taglib-1.6.3 (có thể tải các version khác ngoài 1.6.3), sau đó giải nén, chuyển terminal vào thư mục taglib-1.6.3 vừa giải nén, chạy lệnh ./configure --help để xem hướng dẫn build thư viện:

./configure --help
 [ninhld@localhost taglib-1.6.3]$ ./configure --help  
 `configure' configures this package to adapt to many kinds of systems.  
   
 Usage: ./configure [OPTION]... [VAR=VALUE]...  
   
 To assign environment variables (e.g., CC, CFLAGS...), specify them as  
 VAR=VALUE. See below for descriptions of some of the useful variables.  
   
 Defaults for the options are specified in brackets.  
   
 Configuration:  (1)
  -h, --help       display this help and exit  
    --help=short    display options specific to this package  
    --help=recursive  display the short help of all the included packages  
  -V, --version      display version information and exit  
  -q, --quiet, --silent  do not print `checking...' messages  
    --cache-file=FILE  cache test results in FILE [disabled]  
  -C, --config-cache   alias for `--cache-file=config.cache'  
  -n, --no-create     do not create output files  
    --srcdir=DIR    find the sources in DIR [configure dir or `..']  
   
 Installation directories:  (2)
  --prefix=PREFIX     install architecture-independent files in PREFIX  
              [/usr/local]  
  --exec-prefix=EPREFIX  install architecture-dependent files in EPREFIX  
              [PREFIX]  
   
 By default, `make install' will install all the files in  
 `/usr/local/bin', `/usr/local/lib' etc. You can specify  
 an installation prefix other than `/usr/local' using `--prefix',  
 for instance `--prefix=$HOME'.  
   
 For better control, use the options below.  
   
 Fine tuning of the installation directories:  (3)
  --bindir=DIR      user executables [EPREFIX/bin]  
  --sbindir=DIR      system admin executables [EPREFIX/sbin]  
  --libexecdir=DIR    program executables [EPREFIX/libexec]  
  --sysconfdir=DIR    read-only single-machine data [PREFIX/etc]  
  --sharedstatedir=DIR  modifiable architecture-independent data [PREFIX/com]  
  --localstatedir=DIR   modifiable single-machine data [PREFIX/var]  
  --libdir=DIR      object code libraries [EPREFIX/lib]  
  --includedir=DIR    C header files [PREFIX/include]  
  --oldincludedir=DIR   C header files for non-gcc [/usr/include]  
  --datarootdir=DIR    read-only arch.-independent data root [PREFIX/share]  
  --datadir=DIR      read-only architecture-independent data [DATAROOTDIR]  
  --infodir=DIR      info documentation [DATAROOTDIR/info]  
  --localedir=DIR     locale-dependent data [DATAROOTDIR/locale]  
  --mandir=DIR      man documentation [DATAROOTDIR/man]  
  --docdir=DIR      documentation root [DATAROOTDIR/doc/PACKAGE]  
  --htmldir=DIR      html documentation [DOCDIR]  
  --dvidir=DIR      dvi documentation [DOCDIR]  
  --pdfdir=DIR      pdf documentation [DOCDIR]  
  --psdir=DIR       ps documentation [DOCDIR]  
   
 Program names:  (4)
  --program-prefix=PREFIX      prepend PREFIX to installed program names  
  --program-suffix=SUFFIX      append SUFFIX to installed program names  
  --program-transform-name=PROGRAM  run sed PROGRAM on installed program names  
   
 System types:  (*5)
  --build=BUILD   configure for building on BUILD [guessed]  
  --host=HOST    cross-compile to build programs to run on HOST [BUILD]  
  --target=TARGET  configure for building compilers for TARGET [HOST]  
   
 Optional Features:  (6)
  --disable-option-checking ignore unrecognized --enable/--with options  
  --disable-FEATURE    do not include FEATURE (same as --enable-FEATURE=no)  
  --enable-FEATURE[=ARG] include FEATURE [ARG=yes]  
  --disable-fast-perl   disable fast Makefile generation (needs perl)  
  --enable-debug=ARG   enables debug symbols (yes|no|full) default=no  
  --disable-debug     disables debug output and debug symbols default=no  
  --enable-strict     compiles with strict compiler options (may not  
              work!)  
  --disable-warnings   disables compilation with -Wall and similar  
  --enable-profile    creates profiling infos default=no  
  --disable-dependency-tracking speeds up one-time build  
  --enable-dependency-tracking  do not reject slow dependency extractors  
  --enable-pch      enables precompiled header support (currently only  
              KCC or gcc >=3.4+unsermake) default=no  
  --enable-coverage    use gcc coverage testing  
  --enable-new-ldflags  enable the new linker flags  
  --enable-final     build size optimized apps (experimental - needs lots  
              of memory)  
  --enable-closure    delay template instantiation  
  --enable-nmcheck    enable automatic namespace cleanness check  
  --enable-shared[=PKGS] build shared libraries [default=yes]  
  --enable-static[=PKGS] build static libraries [default=no]  
  --enable-libsuffix   /lib directory suffix (64,32,none,auto=default)  
  --enable-fast-install[=PKGS]  
              optimize for fast installation [default=yes]  
  --disable-libtool-lock avoid locking (might break parallel builds)  
  --enable-mp4      add MP4 support  
  --enable-asf      add ASF support  
   
 Optional Packages:  (7)
  --with-PACKAGE[=ARG]  use PACKAGE [ARG=yes]  
  --without-PACKAGE    do not use PACKAGE (same as --with-PACKAGE=no)  
  --with-gnu-ld      assume the C compiler uses GNU ld [default=no]  
  --with-pic       try to use only PIC/non-PIC objects [default=use  
              both]  
  --with-tags[=TAGS]   include additional configurations [automatic]  
   
 Some influential environment variables:  (*8)
  CC     C compiler command  
  CFLAGS   C compiler flags  
  LDFLAGS   linker flags, e.g. -L<lib dir> if you have libraries in a  
        nonstandard directory <lib dir>  
  LIBS    libraries to pass to the linker, e.g. -l<library>  
  CPPFLAGS  C/C++/Objective C preprocessor flags, e.g. -I<include dir> if  
        you have headers in a nonstandard directory <include dir>  
  CPP     C preprocessor  
  CXX     C++ compiler command  
  CXXFLAGS  C++ compiler flags  
  CXXCPP   C++ preprocessor  
  F77     Fortran 77 compiler command  
  FFLAGS   Fortran 77 compiler flags  
   
 Use these variables to override the choices made by `configure' or to help  
 it to find libraries and programs with nonstandard names/locations.  
   
 [ninhld@localhost taglib-1.6.3]$   


1.1 Configuration
Các options để show thông tin về thư viện, ví dụ:
 configure --help hoặc configure -h  
 configure --version hoặc configure -V  

1.2 Installation directories
Thư mục để install thư viện vào đó:
 export PREFIX=/home/ninhld/Documents/taglib_install   #export biến môi trường
 ./configure --prefix=$PREFIX  

hoặc
./configure --prefix=/home/ninhld/Documents/taglib_install   

Nếu không cài đặt --prefix thì mặc định nó sẽ install vào thư mục /usr/local

--exec-prefix : mặc định được gán với PREFIX, giống với --prefix

1.3 Fine tuning of the installation directories
  --bindir=DIR   user executables [EPREFIX/bin]   
  --sbindir=DIR   system admin executables [EPREFIX/sbin]   
  --libexecdir=DIR  program executables [EPREFIX/libexec]   
  --sysconfdir=DIR  read-only single-machine data [PREFIX/etc]   
  ...  

Các thư mục sẽ được tạo ra để chứa thư viện: bin, include, lib, etc, ...
Mặc định chúng sẽ là thư mục con của --prefix/PREFIX đã đươc đặt ở bước (2)

1.4 Program names
Không cần quan tâm

1.5 System types
Các options rất quan trong để compile cho host hoặc cross-compiling cho target (board nhúng), nếu bạn compile cho host, bạn không cần cài đặt các options này, nếu bạn cross-compiling cho target bạn cài đặt như sau:
 export HOST=arm-none-linux-gnueabi  
 export BUILD=x86_64  
 ./configure --build=${BUILD} --host=${HOST}  

hoặc
 ./configure --build=x86_64 --host=arm-none-linux-gnueabi  

+ HOST
Là nơi mà chương trình sẽ chạy, trong ví dụ trên là board nhúng chạy chíp ARM

+BUILD
Là nơi mà chương trình compiler chạy, hoặc được build ra, ở đây là máy tính chạy chip Inter x86/64bits

+TARGET
Tương tự như HOST, thông số này không cần đặt

1.6 Optional Features
Các options có tiền tố --enable / --disable để cấu hình bật / tắt một tính năng nào đó của thư viện. Các tính năng này được cấu hình mặc định default=yes hoặc default=no ghi rõ trong phần chú thích, nhưng thông thường thì option thuộc dạng --enable thì mặc định là no, option dạng --disable thì mặc định là yes.

Ví dụ:
 ./configure --disable-debug --disable-warnings --enable-static=no --enable-mp4  

+ Tắt debug (default=yes)
+ Tắt warning (default=yes)
+ Không build thư viện static (default=no, cho thêm vào cho chắc :D)
+ Bật support MP4 (default=no)

Có rất nhiều options, bạn nên xem kỹ để lựa chọn enable/disable những options cần quan tâm, cái nào không quan tâm cứ để mặc định, thông thường bạn để tất cả chúng mặc định là đã ok rồi.

1.7 Optional Packages
Options này gần giống với --enable

1.8 Some influential environment variables
Ở đây là các biến môi trường sử dụng bởi compiler

+ CFLAGS / CXXFLAGS / CPPFLAGS
C/C++ compiler flags được pass vào cho compiler, bao gồm:
    - Đường dẫn header file của thư viện phụ thuộc (nếu có), có dạng -I/path/to/include
      Thư viện phụ thuộc: A là thư viện phụ thuộc của B nếu trong B có sử dụng các API do A cung cấp, vì thế A phải được build trước thì mới có thể build B được, vì thế -I/path/to/include có tác dụng cung cấp các API của A cho B trong quá trình compile ra object file.
    - Các options riêng của compiler, ví dụ: -Wll, -g

+ CC / CXX / CPP
C compiler và C++ compiler
 
 #x86
 CC=gcc  
 CXX=g++
 CPP=g++
   
 hoặc   
 #arm
 CC=arm-none-linux-gnueabi-gcc  
 CXX=arm-none-linux-gnueabi-g++  
 CPP=arm-none-linux-gnueabi-g++

+ LDFLAGS
Linker flags, gồm các thông tin:
    - Đường dẫn đến thư mục chứa thư viện phụ thuộc (.la, .so), có dạng -L/path/to/lib
    - Các thư viện sẽ dùng, có dạng -l<name>; ví dụ:  -ltag tương ứng với libtag

+ LIBS
Tương tự như phần "Các thư viện sẽ dùng" trong LDFLAGS, thông thường LIBS ít được dùng vì nó đã được bao hàm trong LDFLAGS

Ex:
 #export biến môi trường 
 export CC_PATH=/opt/FriendlyARM/toolschain/4.4.3  
 export CROSS_COMPILE=${CC_PATH}/bin/arm-none-linux-gnueabi-  
 export PATH=$PATH:${CC_PATH}/bin  
   
   
 export CC=${CROSS_COMPILE}gcc  
 export CXX=${CROSS_COMPILE}g++  
   
 export CFLAGS+="-I/friendlyarm/usr/local/include -DDEBUG -Wall -g"  
 export CXXFLAGS=$CFLAGS  
 export LDFLAGS+="-L/friendlyarm/usr/local/lib -lm"  
   
 ./configure   

hoặc

 export CC_PATH=/opt/FriendlyARM/toolschain/4.4.3  
 export CROSS_COMPILE=${CC_PATH}/bin/arm-none-linux-gnueabi-  
 export PATH=$PATH:${CC_PATH}/bin  
 
 #nhúng các flags vào thẳng configure  
 ./configure CC=${CROSS_COMPILE}gcc CXX=${CROSS_COMPILE}g++ \  
 CFLAGS="-I/friendlyarm/usr/local/include -Wall -g" \  
 CXXFLAGS=$CFLAGS LDFLAGS="-L/friendlyarm/usr/local/lib -lm"   

Note:
PATH: là biến môi trường chỉ ra đường dẫn đến file nhị phân của compiler


2. Compile taglib-1.6.3 for Linux Fedora x86_64

./configure: cấu hình mặc định, có chỉ ra thư mục để install, sau bước này sẽ có được Makefile
   
 [ninhld@localhost taglib-1.6.3]$   
 [ninhld@localhost taglib-1.6.3]$ export PREFIX=/home/ninhld/Documents/taglib_install  
 [ninhld@localhost taglib-1.6.3]$ ./configure --prefix=$PREFIX
 checking build system type... x86_64-unknown-linux-gnu  
 checking host system type... x86_64-unknown-linux-gnu  
 checking target system type... x86_64-unknown-linux-gnu  
 ...  
 ... 
 config.status: creating Makefile  
 config.status: creating bindings/Makefile  
 config.status: creating bindings/c/Makefile  
 config.status: creating examples/Makefile  
 config.status: creating taglib/Makefile  
 config.status: creating taglib/ape/Makefile  
 config.status: creating taglib/asf/Makefile  
 config.status: creating taglib/flac/Makefile  
 config.status: creating taglib/mp4/Makefile  
 config.status: creating taglib/mpc/Makefile  
 config.status: creating taglib/mpeg/Makefile  
 config.status: creating taglib/mpeg/id3v1/Makefile  
 config.status: creating taglib/mpeg/id3v2/Makefile  
 config.status: creating taglib/mpeg/id3v2/frames/Makefile  
 config.status: creating taglib/ogg/Makefile  
 config.status: creating taglib/ogg/flac/Makefile  
 config.status: creating taglib/ogg/speex/Makefile  
 config.status: creating taglib/ogg/vorbis/Makefile  
 config.status: creating taglib/riff/Makefile  
 config.status: creating taglib/riff/aiff/Makefile  
 config.status: creating taglib/riff/wav/Makefile  
 config.status: creating taglib/toolkit/Makefile  
 config.status: creating taglib/trueaudio/Makefile  
 config.status: creating taglib/wavpack/Makefile  
 config.status: creating tests/Makefile  
 config.status: creating taglib-config  
 config.status: creating taglib.pc  
 config.status: creating bindings/c/taglib_c.pc  
 config.status: creating config.h  
 config.status: creating taglib/taglib_config.h  
 config.status: executing depfiles commands  
   
 Good - your configure finished. Start make now  
   
 [ninhld@localhost taglib-1.6.3]$   

make
 [ninhld@localhost taglib-1.6.3]$   
 [ninhld@localhost taglib-1.6.3]$ make  
 make all-recursive  
 make[1]: Entering directory `/home/ninhld/Documents/taglib-1.6.3'  
 Making all in taglib  
 make[2]: Entering directory `/home/ninhld/Documents/taglib-1.6.3/taglib'  
 make all-recursive  
 make[3]: Entering directory `/home/ninhld/Documents/taglib-1.6.3/taglib'  
 Making all in toolkit  
 make[4]: Entering directory `/home/ninhld/Documents/taglib-1.6.3/taglib/toolkit'  
 /bin/sh ../../libtool --silent --tag=CXX  --mode=compile g++ -DMAKE_TAGLIB_LIB -DHAVE_CONFIG_H -I. -I../.. -I../../taglib -I../../taglib   -O2 -fno-exceptions -fno-check-new -fno-common -fexceptions -MT tstring.lo -MD -MP -MF .deps/tstring.Tpo -c -o tstring.lo tstring.cpp  
 ..  
 ...  
 make[3]: Leaving directory `/home/ninhld/Documents/taglib-1.6.3/bindings/c'  
 make[3]: Entering directory `/home/ninhld/Documents/taglib-1.6.3/bindings'  
 make[3]: Nothing to be done for `all-am'.  
 make[3]: Leaving directory `/home/ninhld/Documents/taglib-1.6.3/bindings'  
 make[2]: Leaving directory `/home/ninhld/Documents/taglib-1.6.3/bindings'  
 Making all in tests  
 make[2]: Entering directory `/home/ninhld/Documents/taglib-1.6.3/tests'  
 make[2]: Nothing to be done for `all'.  
 make[2]: Leaving directory `/home/ninhld/Documents/taglib-1.6.3/tests'  
 make[2]: Entering directory `/home/ninhld/Documents/taglib-1.6.3'  
 make[2]: Leaving directory `/home/ninhld/Documents/taglib-1.6.3'  
 make[1]: Leaving directory `/home/ninhld/Documents/taglib-1.6.3'  
 [ninhld@localhost taglib-1.6.3]$   
 [ninhld@localhost taglib-1.6.3]$  


make install
 [ninhld@localhost taglib-1.6.3]$ make install  
 Making install in taglib  
 make[1]: Entering directory `/home/ninhld/Documents/taglib-1.6.3/taglib'  
 Making install in toolkit  
 make[2]: Entering directory `/home/ninhld/Documents/taglib-1.6.3/taglib/toolkit'  
 make[3]: Entering directory `/home/ninhld/Documents/taglib-1.6.3/taglib/toolkit'  
 make[3]: Nothing to be done for `install-exec-am'.  
 test -z "/home/ninhld/Documents/taglib_install/include/taglib" || /usr/bin/mkdir -p "/home/ninhld/Documents/taglib_install/include/taglib"  
  /usr/bin/install -c -p -m 644 'taglib.h' '/home/ninhld/Documents/taglib_install/include/taglib/taglib.h'  
  /usr/bin/install -c -p -m 644 'tstring.h' '/home/ninhld/Documents/taglib_install/include/taglib/tstring.h'  
  /usr/bin/install -c -p -m 644 'tlist.h' '/home/ninhld/Documents/taglib_install/include/taglib/tlist.h'  
  /usr/bin/install -c -p -m 644 'tlist.tcc' '/home/ninhld/Documents/taglib_install/include/taglib/tlist.tcc'  
  /usr/bin/install -c -p -m 644 'tstringlist.h' '/home/ninhld/Documents/taglib_install/include/taglib/tstringlist.h'  
  /usr/bin/install -c -p -m 644 'tbytevector.h' '/home/ninhld/Documents/taglib_install/include/taglib/tbytevector.h'  
  /usr/bin/install -c -p -m 644 'tbytevectorlist.h' '/home/ninhld/Documents/taglib_install/include/taglib/tbytevectorlist.h'  
  /usr/bin/install -c -p -m 644 'tfile.h' '/home/ninhld/Documents/taglib_install/include/taglib/tfile.h'  
  /usr/bin/install -c -p -m 644 'tmap.h' '/home/ninhld/Documents/taglib_install/include/taglib/tmap.h'  
  /usr/bin/install -c -p -m 644 'tmap.tcc' '/home/ninhld/Documents/taglib_install/include/taglib/tmap.tcc'  
 make[3]: Leaving directory `/home/ninhld/Documents/taglib-1.6.3/taglib/toolkit'  
 make[2]: Leaving directory `/home/ninhld/Documents/taglib-1.6.3/taglib/toolkit'  
 Making install in mpeg  
 ...  
 ...  
  /usr/bin/install -c -p -m 644 'taglib.pc' '/home/ninhld/Documents/taglib_install/lib64/pkgconfig/taglib.pc'  
 make[2]: Leaving directory `/home/ninhld/Documents/taglib-1.6.3'  
 make[1]: Leaving directory `/home/ninhld/Documents/taglib-1.6.3'  
 [ninhld@localhost taglib-1.6.3]$   




tagreader_c.c (chương trình ví dụ có sử dụng thư viện taglib vừa build xong)
 /* Copyright (C) 2003 Scott Wheeler <wheeler@kde.org>  
  *  
  * Redistribution and use in source and binary forms, with or without  
  * modification, are permitted provided that the following conditions  
  * are met:  
  *  
  * 1. Redistributions of source code must retain the above copyright  
  *  notice, this list of conditions and the following disclaimer.  
  * 2. Redistributions in binary form must reproduce the above copyright  
  *  notice, this list of conditions and the following disclaimer in the  
  *  documentation and/or other materials provided with the distribution.  
  *  
  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR  
  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES  
  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  
  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,  
  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT  
  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,  
  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY  
  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT  
  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF  
  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.  
  */  
   
 #include <stdio.h>  
 #include <tag_c.h>  
   
 #ifndef FALSE  
 #define FALSE 0  
 #endif  
   
 int main(int argc, char *argv[])  
 {  
  int i;  
  int seconds;  
  int minutes;  
  TagLib_File *file;  
  TagLib_Tag *tag;  
  const TagLib_AudioProperties *properties;  
   
  taglib_set_strings_unicode(FALSE);  
   
  for(i = 1; i < argc; i++) {  
   printf("******************** \"%s\" ********************\n", argv[i]);  
   
   file = taglib_file_new(argv[i]);  
   
   if(file == NULL)  
    break;  
   
   tag = taglib_file_tag(file);  
   properties = taglib_file_audioproperties(file);  
   
   if(tag != NULL) {  
    printf("-- TAG --\n");  
    printf("title  - \"%s\"\n", taglib_tag_title(tag));  
    printf("artist - \"%s\"\n", taglib_tag_artist(tag));  
    printf("album  - \"%s\"\n", taglib_tag_album(tag));  
    printf("year  - \"%i\"\n", taglib_tag_year(tag));  
    printf("comment - \"%s\"\n", taglib_tag_comment(tag));  
    printf("track  - \"%i\"\n", taglib_tag_track(tag));  
    printf("genre  - \"%s\"\n", taglib_tag_genre(tag));  
   }  
   
   if(properties != NULL) {  
    seconds = taglib_audioproperties_length(properties) % 60;  
    minutes = (taglib_audioproperties_length(properties) - seconds) / 60;  
   
    printf("-- AUDIO --\n");  
    printf("bitrate   - %i\n", taglib_audioproperties_bitrate(properties));  
    printf("sample rate - %i\n", taglib_audioproperties_samplerate(properties));  
    printf("channels  - %i\n", taglib_audioproperties_channels(properties));  
    printf("length   - %i:%02i\n", minutes, seconds);  
   }  
   
   taglib_tag_free_strings();  
   taglib_file_free(file);  
  }  
   
  return 0;  
 }  
   


Makefile (Makefile cho ví dụ tagreader_c.c)
 .PHONY: all, install, clean  
   
 TARGET:=tagreader  
   
 HDRS+=  
 CSRCS+= tagreader_c.c  
 CPPSRCS+=  
   
 OBJSDIR=./build  
 OBJS:= $(patsubst %.cpp, $(OBJSDIR)/%.o, $(CPPSRCS))  
 OBJS+= $(patsubst %.c, $(OBJSDIR)/%.o, $(CSRCS))  
   
   
 INCDIR+= -I/home/ninhld/Documents/taglib_install/include \  
      -I/home/ninhld/Documents/taglib_install/include/taglib  
 CFLAGS += -DDEBUG -Wall -g  
 LDFLAGS += -L/home/ninhld/Documents/taglib_install/lib64 -ltag -ltag_c  
   
 CC:= gcc  
 CXX:= g++  
   
 all: ${TARGET}  
 ${TARGET} : $(OBJS)  
      @echo " [LINK] $@"  
      @mkdir -p $(shell dirname $@)  
      @$(CXX) $(OBJS) -o $@ $(LDFLAGS)  
        
 $(OBJSDIR)/%.o: %.c $(HDRS)  
      @echo " [CC]  $@"  
      @mkdir -p $(shell dirname $@)  
      @$(CC) -c $< -o $@ $(CFLAGS) ${INCDIR}  
   
 $(OBJSDIR)/%.o: %.cpp $(HDRS)  
      @echo " [CXX] $@"  
      @mkdir -p $(shell dirname $@)  
      @$(CXX) -c $< -o $@ $(CFLAGS) ${INCDIR}  
   
 install:  
      cp -rf ${TARGET} /usr/local/bin  
             
 clean:  
      rm -rf ${OBJSDIR}  
      rm -rf ${TARGET}  


Make (Build application, cần có các biến môi trường để chỉ dẫn đến thư viện là PATH và LD_LIBRARY_PATH)
 [ninhld@localhost examples]$   
 [ninhld@localhost examples]$ export TAGLIBDIR=/home/ninhld/Documents/taglib_install  
 [ninhld@localhost examples]$ export PATH=$PATH:${TAGLIBDIR}/bin  
 [ninhld@localhost examples]$ export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:${TAGLIBDIR}/lib64  
 [ninhld@localhost examples]$   
 [ninhld@localhost examples]$   
 [ninhld@localhost examples]$ make  
  [CC]  build/tagreader_c.o  
  [LINK] tagreader  
 [ninhld@localhost examples]$     
 [ninhld@localhost examples]$ ldd tagreader  #kiểm tra các lib được dùng trong chương trình
      linux-vdso.so.1 => (0x00007fff7a8af000)  
      libtag.so.1 => /home/ninhld/Documents/taglib_install/lib64/libtag.so.1 (0x00007f72c4eb6000)  
      libtag_c.so.0 => /home/ninhld/Documents/taglib_install/lib64/libtag_c.so.0 (0x00007f72c4cb0000)  
      libstdc++.so.6 => /lib64/libstdc++.so.6 (0x0000003839a00000)  
      libm.so.6 => /lib64/libm.so.6 (0x000000382ae00000)  
      libgcc_s.so.1 => /lib64/libgcc_s.so.1 (0x000000382ca00000)  
      libc.so.6 => /lib64/libc.so.6 (0x0000003829e00000)  
      libz.so.1 => /lib64/libz.so.1 (0x000000382a600000)  
      /lib64/ld-linux-x86-64.so.2 (0x0000003829600000)  
 [ninhld@localhost examples]$   
 [ninhld@localhost examples]$   


Execute (Cũng cần có biến môi trường như khi buil app)
 [ninhld@localhost examples]$ export TAGLIBDIR=/home/ninhld/Documents/taglib_install  
 [ninhld@localhost examples]$ export PATH=$PATH:${TAGLIBDIR}/bin  
 [ninhld@localhost examples]$ export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:${TAGLIBDIR}/lib64  
 [ninhld@localhost examples]$  
 [ninhld@localhost examples]$ ./tagreader /home/ninhld/Music/hanh_phuc_neu_co_em.mp3   
 ******************** "/home/ninhld/Music/hanh_phuc_neu_co_em.mp3" ********************  
 -- TAG --  
 title  - "Hạnh Phúc Nếu Anh Có Em "  
 artist - "Phúc bồ "  
 album  - "mp3.zing.vn"  
 year  - "0"  
 comment - "Zing MP3 - Dinh cao am nhac"  
 track  - "0"  
 genre  - ""  
 -- AUDIO --  
 bitrate   - 128  
 sample rate - 44100  
 channels  - 2  
 length   - 3:05  
 [ninhld@localhost examples]$  


3. Cross compiling taglib-1.6.3 for Friendlyarm mini2440
Phần build sẽ thực hiện trên máy tính (host), còn khi chạy chương trình thì trên board nhúng. Các command sẽ được biểu diễn ngắn gọn vì nó tương tự như trên mục 2.
Nếu bạn không có board nhúng thì bạn chỉ cần thực hiện xong bước build là ok rồi.
Trước hết, download toolchain cho board Friendlyarm Mini2440 arm-linux-gcc; giải nén, copy thư mục FriendlyARM vào thư mục /opt.

Build taglib (on Host)
 export CC_PATH=/opt/FriendlyARM/toolschain/4.4.3  
 export PATH=$PATH:${CC_PATH}/bin  
 export CROSS_COMPILE=${CC_PATH}/bin/arm-none-linux-gnueabi-  
 export CC=${CROSS_COMPILE}gcc  
 export HOST=arm-none-linux-gnueabi  
 export BUILD=x86_64  
   
 export PREFIX=/home/ninhld/Documents/taglib_install_target  
 ./configure --prefix=$PREFIX --build=${BUILD} --host=${HOST}  
 make  
 make install  



tagreader_c.c (như mục 2)
Makefile
 .PHONY: all, install, clean  
   
 TARGET:=tagreader  
   
 HDRS+=  
 CSRCS+= tagreader_c.c  
 CPPSRCS+=  
   
 OBJSDIR=./build  
 OBJS:= $(patsubst %.cpp, $(OBJSDIR)/%.o, $(CPPSRCS))  
 OBJS+= $(patsubst %.c, $(OBJSDIR)/%.o, $(CSRCS))  
   
   
 INCDIR+= -I/home/ninhld/Documents/taglib_install_target/include \  
      -I/home/ninhld/Documents/taglib_install_target/include/taglib  
 CFLAGS += -DDEBUG -Wall -g  
 LDFLAGS += -L/home/ninhld/Documents/taglib_install_target/lib -ltag -ltag_c  
   
 CC=${CROSS_COMPILE}gcc  
 CXX=${CROSS_COMPILE}g++  
   
 all: ${TARGET}  
 ${TARGET} : $(OBJS)  
      @echo " [LINK] $@"  
      @mkdir -p $(shell dirname $@)  
      @$(CXX) $(OBJS) -o $@ $(LDFLAGS)  
        
 $(OBJSDIR)/%.o: %.c $(HDRS)  
      @echo " [CC]  $@"  
      @mkdir -p $(shell dirname $@)  
      @$(CC) -c $< -o $@ $(CFLAGS) ${INCDIR}  
   
 $(OBJSDIR)/%.o: %.cpp $(HDRS)  
      @echo " [CXX] $@"  
      @mkdir -p $(shell dirname $@)  
      @$(CXX) -c $< -o $@ $(CFLAGS) ${INCDIR}  
   
 install:  
      cp -rf ${TARGET} /usr/local/bin  
             
 clean:  
      rm -rf ${OBJSDIR}  
      rm -rf ${TARGET}  

Build application (on Host)
 export TAGLIBDIR=/home/ninhld/Documents/taglib_install_target 
 export PATH=$PATH:${TAGLIBDIR}/bin  
 export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:${TAGLIBDIR}/lib  
 make


Execute application (on board - Friendlyarm mini2440)
Để thực hiện được bước này, bạn cần biết cách setup board Mini2440 kết nối với máy tính như thế nào, điều khiển board qua chương trình minicom, bước này chỉ có ý nghĩ khi bạn đã xem các hướng dẫn trong phần FriendlyARM, nếu bạn chưa biết thì tạm thời tạm không thực hiện bước này.
Copy toàn bộ thư viện, file app, file nhạc để test vào thư mục taglib trên SDCARD. Khi Mini2440 hoạt động, SDCARD sẽ được mount vào thư mục /sdcard, vì vậy thư mục taglib trên SDCARD sẽ có đường dẫn là /sdcard/taglib



 export TAGLIBDIR=/sdcard/taglib  
 export PATH=$PATH:${TAGLIBDIR}/bin  
 export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:${TAGLIBDIR}/lib  
   
 cd /sdcard/taglib 
 ./tagreader /sdcard/taglib/hanh_phuc_neu_co_em.mp3  

******************** "/sdcard/taglib/hanh_phuc_neu_co_em.mp3" ********************
-- TAG --
title   - "H�á�º�¡nh Ph�Ã�ºc N�á�º�¿u Anh C�Ã�³ Em "
artist  - "Ph�Ã�ºc b�á�»��“ "
album   - "mp3.zing.vn"
year    - "0"
comment - "Zing MP3 - Dinh cao am nhac"
track   - "0"
genre   - ""
-- AUDIO --
bitrate     - 128
sample rate - 44100
channels    - 2
length      - 3:05



Note: Biến môi trường PATH và LD_LIBRARY_PATH là cực kỳ quan trọng, chúng luôn phải được sử dụng trong tất cả các quá trình từ build library, build application đến khi execute chương trình.

Monday, June 29, 2015

Display message or driver message - dmesg


Lệnh dmesg sẽ trace ra rất nhiều thông tin từ lúc hệ thống khởi động cho đến thời điểm hiện tại, các thông tin có thể là về kernel, device driver, ... hoặc các thành phần phần cứng.

 [ninhld@localhost docs]$ dmesg   
 [  0.000000] Initializing cgroup subsys cpuset  
 [  0.000000] Initializing cgroup subsys cpu  
 [  0.000000] Initializing cgroup subsys cpuacct  
 [  0.000000] Linux version 3.18.6-100.fc20.x86_64 (mockbuild@bkernel02.phx2.fedoraproject.org) (gcc version 4.8.3 20140911 (Red Hat 4.8.3-7) (GCC) ) #1 SMP Fri Feb 6 22:55:01 UTC 2015  
 [  0.000000] Command line: BOOT_IMAGE=/vmlinuz-3.18.6-100.fc20.x86_64 root=UUID=e1f37513-df0d-4ca7-9ec4-c4c9987dbe60 ro vconsole.font=latarcyrheb-sun16 rhgb quiet LANG=en_US.UTF-8  
 [  0.000000] Disabled fast string operations  
 [  0.000000] e820: BIOS-provided physical RAM map:  
 [  0.000000] BIOS-e820: [mem 0x0000000000000000-0x000000000009d7ff] usable  
 [  0.000000] BIOS-e820: [mem 0x000000000009d800-0x000000000009ffff] reserved  
 [  0.000000] BIOS-e820: [mem 0x00000000000e0000-0x00000000000fffff] reserved  
 [  0.000000] BIOS-e820: [mem 0x0000000000100000-0x00000000da99efff] usable  
 [  0.000000] BIOS-e820: [mem 0x00000000da99f000-0x00000000dae9efff] reserved  
 [  0.000000] BIOS-e820: [mem 0x00000000dae9f000-0x00000000daf9efff] ACPI NVS  
 [  0.000000] BIOS-e820: [mem 0x00000000daf9f000-0x00000000daffefff] ACPI data  
 [  0.000000] BIOS-e820: [mem 0x00000000dafff000-0x00000000daffffff] usable  
 [  0.000000] BIOS-e820: [mem 0x00000000db000000-0x00000000df9fffff] reserved  
 [  0.000000] BIOS-e820: [mem 0x00000000f8000000-0x00000000fbffffff] reserved  
 [  0.000000] BIOS-e820: [mem 0x00000000fec00000-0x00000000fec00fff] reserved  
 [  0.000000] BIOS-e820: [mem 0x00000000fed08000-0x00000000fed08fff] reserved  
 [  0.000000] BIOS-e820: [mem 0x00000000fed10000-0x00000000fed19fff] reserved  
 [  0.000000] BIOS-e820: [mem 0x00000000fed1c000-0x00000000fed1ffff] reserved  
 [  0.000000] BIOS-e820: [mem 0x00000000fee00000-0x00000000fee00fff] reserved  
 [  0.000000] BIOS-e820: [mem 0x00000000ffd20000-0x00000000ffffffff] reserved  
 [  0.000000] BIOS-e820: [mem 0x0000000100000000-0x000000011e5fffff] usable  
 [  0.000000] NX (Execute Disable) protection: active  
 [  0.000000] SMBIOS 2.6 present.  
 [  0.000000] DMI: LENOVO 4180AD9/4180AD9, BIOS 83ET61WW (1.31 ) 07/07/2011  
 [  0.000000] e820: update [mem 0x00000000-0x00000fff] usable ==> reserved  
 ...  
 [  82.586529] Bluetooth: RFCOMM TTY layer initialized  
 [  82.586562] Bluetooth: RFCOMM socket layer initialized  
 [  82.587813] Bluetooth: RFCOMM ver 1.11  
 [  96.951311] SELinux: initialized (dev proc, type proc), uses genfs_contexts  
 [  98.859836] SELinux: initialized (dev proc, type proc), uses genfs_contexts  
   
   

Bởi vì lệnh dmesg trace thông tin từ dưới kernel nên nó rất hữu ích trong một số trường hợp như:
+ Xem thông tin về peripheral device mới kết nối vào hệ thống
+ Debug kernel khi viết driver (khi viết driver, muốn trace thông tin thì phải dùng lệnh printk, tuy nhiên lệnh in này sẽ không trace trực tiếp ra terminal mà phải dùng lệnh dmesg mới thấy được)

Ex:
Gắn một thiết bị USB-to-COM (dùng với minicom phục vụ debug board nhúng) vào máy tính và dùng lệnh dmesg sẽ được các thông tin như dưới, cái quan trong nhất là biết được device node của thiết bị là /dev/ttyUSB0


 [ 7377.063526] usb 1-1.1: new full-speed USB device number 6 using ehci-pci  
 [ 7377.149745] usb 1-1.1: New USB device found, idVendor=067b, idProduct=2303  
 [ 7377.149756] usb 1-1.1: New USB device strings: Mfr=1, Product=2, SerialNumber=0  
 [ 7377.149762] usb 1-1.1: Product: USB 2.0 To COM Device  
 [ 7377.149766] usb 1-1.1: Manufacturer: Prolific Technology Inc.  
 [ 7378.264479] usbcore: registered new interface driver pl2303  
 [ 7378.264505] usbserial: USB Serial support registered for pl2303  
 [ 7378.264534] pl2303 1-1.1:1.0: pl2303 converter detected  
 [ 7378.267971] usb 1-1.1: pl2303 converter now attached to ttyUSB0  

Xem thêm tại:
+ Link 1
Sunday, June 21, 2015

File permissions

Bài viết liên quan: Users and Groups 

1. File permissions
Với mỗi file,thư mục trong Linux, người dùng có thể có quyền r(read), w(write), x(excute).
Thường một normal user có thể không có đủ 3 quyền trên với một số file, thư mục nào đó. Riêng root có tất cả các quyền r, w, x với tất cả các file, thư mục trong hệ thống (dù hệ thống đang ở trạng thái single user hay multi-user).

Với mỗi file, người dùng được xếp vào một trong 3 loại đối tượng:
- Chủ sở hữu file, kí hiệu là u (user).
- Cùng group với nhóm chủ sở hữu file, kí hiệu là g (group). Tất cả các user thuộc nhóm chủ sở hữu file có cùng một quyền với file
- Khác với 2 loại trên, kí hiệu là o (other)

Mỗi loại đối tượng trên sẽ có một số quyền trong 3 quyền r, w, x. Lệnh ls -l cho biết quyền của mỗi loại đối tượng trên theo format như dưới đây:




Ex:

 [ninhld@localhost docs]$ ls -l  
 total 14544  
 -rwxrwxr-x. 1 ninhld ninhld 1449498 Jun 13 2013   Building Embedded Linux Systems.pdf  
 drwxrwxr-x. 2 ninhld ninhld    4096 Jun 23 21:02  mydir  


2. Thiết lập chủ sở hữu, nhóm chủ sở hữu
Khi một file được tạo ra thì mặc định chủ sở hữu file chính là user tạo ra file, nhóm chủ sở hữu file là primary group của user tạo ra file. Khi một user copy file X thành file Y thì file Y được coi là user X tạo ra do đó chủ sở hữu, nhóm chủ sở hữu của Y chính là user X, primary group của X. Chỉ có root mới có quyền thay đổi chủ sở hữu của file, ngay cả chủ sở hữu của một file cũng không thể chuyển quyền sở hữu của mình cho người khác. Tuy nhiên chủ sở hữu của file có quyền thay đổi nhóm chủ sở hữu của file. Để thay đổi chủ sở hữu, nhóm chủ sở hữu:
chown  [chủ]  [.nhóm]  <file>
chown là viết tắt của "change own"

Ex:
User "lan" vừa tạo ra file "document", user "lan" trở thành chủ sở hữu của file "document". Primary group của user "lan" là "lan" trở thành nhóm chủ sở hữu của file "document". Nếu user "lan" muốn chuyển quyền sở hữu sang user "thanh" thì user "lan" cần đăng nhập bằng root và thực hiện lệnh:
chown thanh document
Nếu user "lan" muốn thay đổi nhóm chủ sở hữu từ "lan" sang "ITdepart" thì user "lan" cần đăng nhập bằng root và thực hiện lệnh:
chown .ITdepart document
Hai lệnh trên có thể gộp lại thành:
chown thanh.ITdepart document
(chú ý dấu chấm viết ngay sau chủ sở hữu mới)
Nếu chỉ thay đổi nhóm chủ sở hữu có thể dùng lệnh
chgrp <nhóm> <file>
(chgrp là viết tắt của "changegroup")
Vẫn ở ví dụ trên khi đổi nhóm chủ sở hữu có thể dùng lệnh:
chgrp ITdepart document
Khi thay đổi chủ sở hữu đối với thư mục cần thêm tham số -R

Ex: thay đổi chủ sở hữu, nhóm chủ sở hữu cho thư mục Dir
chown -R .ITdepart Dir
hoặc
chgrp -R ITdepart Dir

3. Thay đổi quyền truy cập r,w,x với các đối tượng u,g,o 
Ở trên đã đề cập đến 3 loại quyền truy cập đối với file là read, write, execute; cụ thể hơn thì:
- read: quyền đọc file và do đó có thể sao chép lại file. Khi có quyền đọc file thì không có nghĩa là có quyền ghi (sửa đổi) file nhưng vì có thể sao chép lại file nên có thể sửa đổi bản sao chép
- write: quyền ghi file. Chú ý rằng có thể có quyền ghi mà không có quyền đọc một file. Ví dụ: có file text "document.txt". Nếu có quyền đọc và quyền ghi file này thì người dùng có thể dùng trình soạn thảo văn bản vi, emacs,...để hiển thị toàn bộ nội dung file và tìm những chỗ không vừa ý để sửa chữa. Nếu chỉ có quyền ghi file mà không có quyền đọc thì người dùng không thể dùng trình soạn thảo để sửa chữa file nhưng người dùng có thể dùng lệnh cat >> document.txt để ghi thêm vào cuối file hoặc cat > document.txt để ghi đè lên toàn bộ nội dung của file
- execute: quyền thực thi file. Có hai loại file có thể thực thi được là:
(1) file chứa các lệnh máy mà CPU có thể thực thi luôn được, những file kiểu này gọi là executable file
(2) file text chứa các lệnh cần thực hiện, những file kiểu này được gọi là script. Quyền thực thi file độc lập với quyền đọc và ghi file. Ví dụ có file script "Procedure.sh" với quyền thực thi mà không đọc, ghi; người dùng có thể thực thi các lệnh chứa trong file này nhưng không thể mở file ra xem các lệnh đó là gì và cũng không thể thêm bớt, sửa chữa các lệnh trong file này. Việc xóa, đổi tên một file không được quyết định bởi quyền ghi với file đó mà bởi quyền ghi với thư mục chứa filed đó.

Ba quyền read, write, execute cũng được áp dụng với thư mục nhưng ý nghĩa của chúng thì khác hoàn toàn:
- read: quyền đọc một thư mục là quyền liệt kê các file, thư mục con của một thư mục.
- write: quyền ghi một thư mục là quyền tạo mới, xóa, đổi tên file và thư mục con của thư mục. Có thể xảy ra trường hợp: có quyền ghi một file tức là sửa chữa nội dung của file nhưng lại không có quyền đổi tên, xóa file nếu như không có quyền ghi với thư mục chứa file đó.
- execute: quyền thực thi một thư mục là quyền "đi vào" thư mục, chẳng hạn bằng lệnh cd. Có thể xảy ra trường hợp: có quyền thực thi nhưng lại không có quyền đọc một thư mục. Khi đó có thể "cd" một thư mục nhưng lại không thể "ls" nó ra. Khi thư mục không có quyền excute thì không thể truy cập nội dung các file trong thư mục đó.

Ex:
Giả sử user U1 có file FileOfU1 đặt trong /home/U1 muốn chia sẻ với các user khác nên đã đặt quyền r,w cho file. Tuy nhiên bản thân thư mục /home/U1 khi được tạo ra đã bị mặc định là không có quyền excute nên các user khác không thể đọc, ghi file /home/U1/FileOfU1 được.
Chủ sở hữu và root có quyền thay đổi quyền truy cập đối với cả 3 loại đối tượng: chủ sở hữu (user), nhóm chủ sở hữu (group), những user khác không thuộc 2 loại trên (others).
Lệnh thay đổi quyền truy cập:
chmod <quyền> <file> 
(viết tắt chmod = change mode)
Có 2 cách biểu diễn <quyền>: bằng kí tự hoặc bằng chữ số
- Cách1: Biểu diễn quyền bằng kí tự: quyền được biểu diễn bằng tổ hợp 3 loại kí tự sau


Ex:
 chmod u+r,o+s myfile  
 chmod uo=rx,g=-myfile  
 chmod a=rwx myfile (chmod ugo=rwx myfile)  
 chmod +x myfile (chmod u+x,g+x,o+x myfile)  

Chú ý: chmod u+r,o+x myfile và chmod u=r,o=x myfile là hai lệnh khác nhau

- Cách2: Biểu diễn quyền bằng chữ số octal: dùng 3 chữ số để biểu diễn quyền của 3 loại đối tượng u,g,o. Giá trị mỗi chữ số octal biểu diễn một quyền theo bảng sau:






Ex:
 chmod 567 myfile (chmod u=rx,g=wx,o=rwx myfile)  
 chmod 640 myfile (chmod u=wx,g=x,o=- myfile)  

Note: Khi chmod đối với thư mục, nếu muốn áp dụng quyền nào đó cho tất cả các file và thư mục con trong đó thì cần thêm option là "-R"

Ex:
 chmod 755 -R mydir  

Xem thêm tại:
+ Link 1
Saturday, June 20, 2015

- Copyright © Lập trình hệ thống nhúng Linux . Powered by Luong Duy Ninh -