Linux development Lection
What we gonna do today Root privileges Software packages Managing software packages Build procedures Build components Cross-compiling tool chains
Root privileges
What is root root is the user name or account that by default has access to all commands and files on a Linux or other Unix-like operating system. It is also referred to as the root account, root user and the superuser.
root directory The top level directory of a systems cd /
root data directory /root Same as /home/
root privileges Powers that the root has on the systems Root account has the absolute power on the system Can grant and revoke access permissions
rootkit Software tools inserted as an intruder Has the same privileges as root Can be used to do bad things to the OS
User ID (UID) Each user has UID. In order to check the UID of a user use: echo $UID
/etc/passwd There is information about the users Use cat /etc/passwd to open the info about all the users How many users we see ?
How to create new user :# adduser testuser Follow the instructions
User groups /etc/shadow – secure user account info /etc/passwd – user accounts info /etc/group – defines the groups that user belongs /etc/sudoers – list of users who can execute sudo Used to determine equal privileges to all users in a group
How to add user as sudo # adduser newSudoUser # adduser newSudoUser sudo
How to remove user # userdel –r newSudoUser (if newSudoUser is used you must logout)
How to edit user # usermod –u UID username – this will change the UID number
Administrator vs root Every user can become administrator The root is a user that is administrator with highest possible privileges
Questions ?
Software packages
Software packages are packages of source files that are unachieved and build for each unique device. That way each software on Linux/UNIX can be build for every different machine One package can run on different OS architectures
How to install software on Debian Dpkg APT Unachieve
# dpkg Debian package management system Low level Installs, remove, provide information about.deb packages
# dpkg use # dpkg –i - to install something # dpkg –l - to list available packages # dpkg –r - to remove
APT – Advanced Packaging Tool High-level user friendly interface to manage packages Automatic retrieves, configures and install software packages
# apt-get APT package handling utility – cmd interface # apt-get install # apt-get update – retrieve new list of packages # apt-get upgrade – performs an upgrade # apt-get remove, – removes a package # apt-get clean – Erase downloaded archive files # apt-get dist-upgrade – distribution upgrade
# apt-get # apt-get source – Download source archives # apt-get download - Download binary # apt-get build-dep - Finds the libraries required to build a source
# apt-cache Used for searching and identifying a package # apt-cache search
# dpkg and # apt lab – task 1 – 10 min Update / upgrade Search for openssh Download source and binary of openssh Install with# dpkg Remove openssh Clean
Build procedure Install build-essential (# apt-get install build-essential) # apt-get build-dep #./configure If configure successful continue else google it # make # make install
In case of failure in the middle of installation # apt-get –f install # dpkg –configure –a
# dpkg and # apt lab task 2 – 15 min Configure compile and install openssh
# aptitude GUI for installing packages Press + or - to change the status of application
Qustions ?
Creating build procedures
Before you start In order to create a build procedure you must first know the target of the build procedure You must know all the libraries and dependencies that the build needs to build
Target Linux/GNU is supported by almost all processors with more than 84 MHz clock frequency In most of the cases the whole core must be recompiled and then uploaded to the target using UART/SPI/JTAG The code must be optimized as possible in order to work fast and not to waste resources
Libraries There are different options for core libraries to be used for the different processors. For simple application developed for embedded systems it is better to use libraries like: uClibc-ng (uclibc-ng.org) – optimized for space not for speed Musl C – optimized for speed and simplicity
uClibc High configurability: many features can be enabled or disalbled Supports most embedded architectures Must recompile every time there is a change Low performance Size of helloworld using uClibc – 18 kB Size of helloworld using glibc – 361 kB download
Important note NEVER USE : sudo rm –rf /*
Build steps 1.Optimize for target 2.Make configure file 3.Make Makefile 4.(Optional make Makefile for install) 5.(Optional make uploading to board script)
Optimize for target Part of the process is reviewing the code for any unused libraries and functions, because if no code optimization is used in the compiler all the libraries and their functions will be used If used uClibc start its GUI (graphic user interface) in order to set all that you are going to need in the compilation
Build and release tool chain for Debian
Make configuration file Used to check if all the dependencies are available in the system Used to collect and add all system information into a Makefile
Make configuration file Install autoconf and automake Write hello wolrd.c file in a folder Then we start making the lines configure file $ nano configure.ac
configure.ac AC_INIT([programName], [ver], AM_INIT_AUTOMAKE AC_PROG_CC AC_CONFIG_FILES([Makefile]) AC_OUTPUT
Making configuration file We must add a Makefile.in in order to be autoset after the execution of./configure Developers are lazy and they we have an automation script that is generating this file too $ nano Makefile.am
Makefile.am AUTOMAKE_OPTIONS = foreign # we say to linux that this software is not following the standart layout of GNU project bin_PROGRAMS = helloworld # this is the name of the final app helloworld_SOURCES = main.c # the sources
Putting all together $ aclocal # set up on m4 enviroment $ autoconf # generate configuration from configure.ac $ automake –add-missing # generate makefile.in from makefile.am $./configure # generate Makefile $ make dist # use makefile to build $ make distcheck # use this to check the tarball to distribute
How to convert binary to.deb Make a new folder hello_world_for_debian Copy the main.c file from the first folder and paste in in the new one Make directory DEBIAN Then $ nano DEBIAN/control
DEBIAN/control Package: hello_world Version: 0.1 Section: custom Priority: optional Architecture: all Essential: no Installed-Size: 1024 Maintainer: Description: Print hello world to the screen
How to convert binary to.deb Inside the directory hello_world_for_deb make directori usr/bin/ Then move the executable file to usr/bin/ Go one level up from the directories DEBIAN/ and usr/ $ dpkg-deb --build hello_world_for_deb Then $ ls Then $ sudo dpkg –i hello_world_for_deb.deb Then $ cd /usr/bin And finally./helloworld
Questions ?
Make a tool-chain for embedded platforms
How to compile for ARM based CPU Two approaches: Use buildroot Create custom makefile
Using Buildroot Install Buildroot with $ wget Run $ make menuconfig Run $ make
Create custom Makefile You can write only one Makefile for all of your software and just change few lines in order to compile everything This is the most comfortable way to have full control of the code you compile
Makefiles – starting point all: [tab] GCC main.c hello_master.c –o outputfile
Makefiles – define macros CC = GCC # define compiler CFLAGS = -O –lm # define output compilation flags LIBS = “-lnurses –lm –lsdl” PINGUIN = (“)>
Makefiles – spacial macros - the name of the file to be made $? – the names of the changed dependents $< - the name of the related file that caused the action $* - the prefix shared by target and dependent files // using $ make –p in the terminal will show you the default predefined macros
Makefiles – with macros CC:=gcc CFLAGS:=-O -Wall all: $(CC) main.c hello_master.c $(CFLAGS) -o outputfile
Makefiles - dependencies Similar to the functions in the programming languages Helps to recompile only what you need, not everything
Makefiles – with dependencies CC:=gcc CFLAGS:=-O -Wall –c all: compile compile: main.o hello_master.o $(CC) main.o hello_master.o -o outputfile main.o: main.c $(CC) $(CFLAGS) main.c hello_master.o: hello_master.c $(CC) $(CFLAGS) hello_master.c
Makefiles What if we have 500 files that we must compile ????
Makefiles – using macros in dependencies CC=gcc CFLAGS=-c -Wall -O LDFLAGS= SOURCES=main.c hello_master.c OBJECTS=$(SOURCES:.c=.o) EXECUTABLE=binary all: $(SOURCES) $(EXECUTABLE) $(EXECUTABLE): $(OBJECTS) $(CC) $(LDFLAGS) $(OBJECTS) -o $(CC) $(CFLAGS) $< -o clean: -rm -f *.o core *.core
And cross compilation for AVR Install arduino-mk Use avr-gcc instead of gcc in the Makefile
Qustions ?
Lab
Task 1 Write a C program that: Calculates perimeter and surface of given equilateral figure by given side and number of angles Program must have files main.c surface.c and perimeter.c Compile and run Pack to archive Pack to.deb Prepare manual Makefile