Pages

Monday, January 31, 2011

Create static library with GCC

  1. Info
    Multiple object modules can be packed together into a single file called static library. It can be then passed to the linker. The linker builds the executable and copies the object modules from the library that are referenced by the program.

    The static library was developed to workaround the disadvantages of linking a whole dynamic library (a program would contain the code for the whole library (waste of space) and also when it's loaded into memory, the memory will contain code for all the function in the library(waste of memory)). With a static library, at link time the linker will copy only the object modules that are referenced by the application (This reduces the size of executable on disk and also on memory!)

  2. Creating the library
    • Building the library from 2 initial source files:
      mult.c

      /* no bounds checking*/
      int mult(int a, int b) {
          return a*b;
      }
      
      add.c
      /* no bounds checking*/
      int add(int a, int b) {
          return a+b;
      }
      
      The 2 files are compiled with gcc -Wall -c add.c mult.c

      Library is created with ar rcs libmyfunctions.a add.o mult.o
      More details on parameters for the ar utility with man ar. Also, to list the content of the archive the command ar -t libmyfunctions.a is useful.
  3. Linking
    • One way: gcc -o executable-name prog.c -L/path/to/library-directory -lmyfunctions
    • Or: gcc -o executable-name foo.c libmyfunctions.a
  4. Testing
    foo.c
    #include <stdio.h>
    
    #include "myfunctions.h"
    
    int main() {
        printf("%d", add(1,3));
        
        return 0;
    }
    
    myfunctions.h
    #ifndef  __MYFUNCTIONS_H__
    #define  __MYFUNCTIONS_H__
    
    int add(int a, int b);
    int mult(int a, int b);
    
    #endif  /* MYFUNCTIONS_H */
    
    Compiling and running:
    $ gcc -o foo foo.c -L. -lmyfunctions
    $ ./foo
    

Friday, January 28, 2011

Move/add swap partition in Ubuntu

    To see the amount of existing swap space:
cat /proc/meminfo |grep -i swap
The output should be something like below. Full explanation of /proc/meminfo here.
SwapCached:  2244 kB
SwapTotal:  1052248 kB
SwapFree:  1043908 kB
 If you want to add a new swap partition, or delete the existing one and create another:
sudo swapoff -a
sudo mkswap /dev/sda3
#Edit fstab to add the UUID given by sudo blkid command. Then:
sudo swapon -a
#Check
swapon -s
References:
  1. /proc/meminfo Explained 

Saturday, January 22, 2011

Partition/Disk imaging with CloneZilla

    CloneZilla it's a free and open source software for partition imaging and disk cloning. Some features I liked:

  1. It supports ext4 partitions
  2. It's possible to do all the steps from command line (do imaging/restoring in unattended mode)
  3. The image file can be located on the local disk, on external USB drive, on an ssh server, samba server or NFS server
The complete list of features and limitations on the site.

I used CloneZilla Live, a bootable Linux based on Debian, on an USB stick, using Method A. Till now I restored one Ubuntu ext4 partition and one NTFS with Windows. 


Tips (from faqs):

  • Restore *.ntfs-img.* images into a partition manually:
    If the image is /home/partimag/YOURIMAGE/hda1.ntfs-img.aa, hda1.ntfs-img.ab..., and you want to restore the image to /dev/sda2.
    Before you do it, make sure the partition size of /dev/hda2 is equal to or larger than the original partition size of hda1 image.
    Then
    file /home/partimag/YOURIMAGE/hda1.ntfs-img.aa
    (to see it's gzip, bzip or lzop image). For gzip, then you can run
    cat /home/partimag/YOURIMAGE/hda1.ntfs-img.* | gzip -d -c | ntfsclone --restore-image -o /dev/hda2 -
  • Restore an image of a partition to different partition:
    1. Make sure the destination partition is larger than the original one.
    2. Rename all the files /home/partimag/my-image-new/sda4* as /home/partimag/my-image-new/sda2* (the original was sda4, now it's sda2)
    3. Modify the content of /home/partimage/my-image-new/parts, replace sda4 with sda2.
ToDO:
  • Test the unattended automated mode. 

Reinstall Ubuntu after installing Windows

   Long story here.  Ubuntu is well documented and documentation updated. Ubuntu forums are great also. So I had Ubuntu Maverick already installed, and installed Windows XP. It wrote over the mbr. Now, to restoration:
  • I booted using an USB live image
  • Mounted the ext4 partition containing the Ubuntu 10 installation and restored burg:
    sudo burg-install --root-directory=/media/0d104aff-ec8c-44c8-b811-92b993823444 /dev/sda
    
    (0d104aff-ec8c-44c8-b811-92b993823444 is the label of the mounted Ubuntu root partition. I use burg, so for grub it's the same, except grub-install instead.)
  • To list Windows also in the menu list of grub,
    sudo update-burg 
    
    or is using grub2:
    sudo update-grub2
    
  • I also had to modify /boot/burg/burg.cfg file, because Windows installation was automatically detected on /dev/sda3 (hd0,3), instead of /dev/sda4 ((hd0,4)).
    Note: changes made manually in the burg.cfg file are not persistent after burg-update, because burg-update automatically generates it based on /etc/burg.d files.
Hope I didn't miss any steps. For more detailed information:

Install Windows XP from USB

    After trying many tutorials to install XP from USB, I found something that worked. The WinSetupFromUSB tool.  I've tested with Windows XP 32bit SP2 and SP3 and worked fast and easy. Some prerequisites:

  • USB stick >1GB
  • Windows XP CD/DVD or iso
  • WinSetupFromUSB program
Notes:
  1. First I tried with an XP sp3 iso(XP Performance Edition), modified with nLite,  I got an error (missing winnt32...). A trick I've read, if you make modify your iso with nLite, is to NOT remove "Manual Install and Upgrade” under Operating System Options.
  2. It's better to integrate SP3 if you have just sp1 or sp2.  Details, reason and ste by step instructions  here. (the process is called slipstreaming drivers)
   The usage is straight-forward, click-click.., select the source, format the USB device (I used the HP format tool, already integrated with WinSetupFromUSB ), select USB, click Go. The program takes care of making the device bootable, after copying XP files. 
   Change from BIOS to select USB as first device to boot, and you'll get to a grub4dos where you choose XP setup first part. 

Note: I had a problem at the second part. There are 8 options, one default, with the suggestion to choose one that works. For me it was the 4th.

Next, how to get grub (or burg) back in the mbr, after XP installation, to be able to boot the existent Ubuntu also.

Tuesday, January 18, 2011

Trace function calls with GCC

     With gcc it is possible to trace offline the execution of a program. GNU GCC has a feature that allows to gather a function call tree. When you compile your program with a special instrumentation flag, and link with a trace library that contains 4 specific functions, you can mark every entrance end exit from functions. From GCC manual:
-finstrument-functions
Generate instrumentation calls for entry and exit to functions. Just 
after function entry and just before function exit, the following 
profiling functions will be called with the address of the current 
function and its call site. (On some platforms, __builtin_return_address
 does not work beyond the current function, so the call site information 
may not be available to the profiling functions otherwise.)
          void __cyg_profile_func_enter (void *this_fn,
                                         void *call_site);
          void __cyg_profile_func_exit  (void *this_fn,
                                         void *call_site);
     
The first argument is the address of the start of the current function, 
which may be looked up exactly in the symbol table.
    If a function does not need to be instrumented, it can be excluded, using __attribute__ ((no_instrument_function)) in the function definition. (This helps to tell gcc to not instrument __cyg_* profiling functions.)
Profiling functions for enter and exit:
void __attribute__ ((no_instrument_function))
__cyg_profile_func_enter (void *func, void *caller);
void __attribute__ ((no_instrument_function))
__cyg_profile_func_exit (void *func, void *call_site);

    When an instrumented function is called,__cyg_profile_func_enter is also called, passing in the address of the function called as func and the address from which the function was called as caller. Conversely, when a function exits, the __cyg_profile_func_exit function is called, passing the function's address as func and the actual site from which the function exits as call_site.
    Within these profiling functions, you can record the address pairs for later analysis. To request that gcc instrument all functions, every file must be compiled with the options -finstrument-functions and-g to retain debugging symbols.
    So, now you can provide profiling functions to gcc that it will transparently insert into your application's function entry and exit points. But when the profiling functions are called, what do you do with the addresses that are provided? One option would be to just write the addresses to a file, together with the time function enters and exits.
    There are other 2 functions invoked: constructor when main is called, and destructor when the application exits. To create them, create 2 functions and apply constructor and destructor attributes to them:
void __attribute__ ((constructor))
trace_begin (void);
void __attribute__ ((destructor))
trace_end (void)
    In constructor and destructor you can open the trace files, and in __cyg_profile_func_* register info like function address, caller address, and time. 
To resolve the addresses to actual function names, the Addr2line tool (which is part of the standard GNU Binutils) is an utility that translates an instruction address and an executable image into a filename, function name, and source line number. The usage for this tool is as follows:
$ addr2line.exe -f -e prog 0x004011FF
f
/cygdrive/d/work/prog.c:35
    Here prog is the binary name, and 0x004011FF is the function address. The information returned gives the function name and location.
Here is an example on how to use this feature to link a small problem with the trace library and view a call tree.

Tuesday, January 11, 2011

Ubuntu 10 remove encryption from home directory

     I just installed Ubuntu 10.10 (great choice!). One nice feature that can be enabled at install time is to encrypt your home directory. Full details about this and usage in this wiki.  Anyway, encrypting the whole home folder introduced very small delays and I decided to keep my home unencrypted, but atill have a Private folder there that will be encrypted.  The steps to achieve this (if you already have your whole home encrypted):

  • You should not be logged on the system. One way to do this is to start Ubuntu in recovery-mode, and chose root prompt. That will drop you at # prompt. 
  • Your account should not be mounted (and it isn't if yo used the root prompt). Btw, when logged in, if you use 'T' flag at df ( $df -hT )  you can see the type of your home partition as ecryptfs,  mounted in /home/username.
  • Create a new folder that can be used lated as a new encrypted private folder, within your home (mkdir /home/username/Private
  • chown username.username /home/username/Private
  • Now modify in the file /home/.ecryptfs/username/.ecryptfs/Private.mnt the new encrypted path, (/home/username/Private)
  • Switch from root to username (#su - username)
  • Mount private encrypted files, by running the wrapper script ecryptfs-mount-private.  This will prompt for the username password. Now your private encrypted home folder will be mounted under the new specified path, /home/username/Private
  • Synchronize that with your /home/username: $cd ~/Private; rsync -av ./ /home/username/   
  • Exit ( $exit ) the shell where you're logged in as username and #reboot from root prompt.
  • After restart check that all the files are there, in your /home/username, unencrypted.
  • The content of the ~/Private can be deleted now. This folder can be further used as a storage for encrypted files
To check that files created in ~/Private are really encrypted:
  • create a secret file there
  • verify (with $df -T) the type of the filesystem mounted under /home/username/Private. It should be ecryptfs.
  • if you log in as another user, (or as root at the root prompt from rescue mode) you won't be able to mount it..
Very nice thing to make this feature really easy to use and document it
GG! 

Monday, January 10, 2011

USB Camera in Skype in Ubuntu 10.10

    I've connected a basic USB webcam. It seemed to work correctly, but in Skype it didn't start showing anything. It's detected by lsusb as Microdia:
$ lsusb 
Bus 003 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 002 Device 002: ID 0c45:612a Microdia PC Camera (SN9C325)
Bus 002 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
To see that the camera it's working correctly (has modules installed), I used cheese (sudo apt-get install cheese  and then Applications->Sound and Video->Cheese Webcam Booth and I see myself:). 
The workaround to work also in Skype is to specify some libraries at start, in the LD_PRELOAD environment variable. If I run Skype with:


sh -c 'export XLIB_SKIP_ARGB_VISUALS=1 && LD_PRELOAD=/usr/lib/libv4l/v4l1compat.so /usr/bin/skype'


I got image, so I modified the command to start the program (Right click on Applications Menu, Edit Menus and edited the command for Skype).


Saturday, January 8, 2011

Install drivers for SIS 771/671 graphic card in Ubuntu 10.10 and 11.04

   My Esprimo Mobile V5535 laptop came with an unsupported by default graphic card SiS 771/671.

$ lspci -nn |grep -i vga
01:00.0 VGA compatible controller [0300]: Silicon Integrated Systems [SiS] 771/671 PCIE VGA Display Adapter [1039:6351] (rev 10)

After infinite searching (that took almost 4 hours:) on forums about that problem,  I finally got some working drivers (still without 3d acceleration that doesn't seem to be supported in the near future) from the 5th post from here (I still have the files if the links are not working).  This contains instructions also:

  • copy the driver (sisimedia_drv.so)  to /usr/lib/xorg/modules/drivers
  • modify (or copy xorg.conf if you don't have that file) /etc/x11/xorg.conf with the lines from xorg.conf contained in the archive.

Warning!!
Check the refresh rate supported by your monitor!  An incorrect setting may damage the monitor.I modified mine like below, in the "Monitor" section of xorg.conf:
    .....

#      HorizSync 30-63
#      VertRefresh 50-75
        HorizSync 28-72
        VertRefresh 43-60
   .....


P.S.:

  • If possible stay away from SiS graphics cards, at least until they decide to support Linux. Stick with Intel, AMD, nVidia.
  • SiS Graphic on Linux site contains lot of other drivers, that have been used  successfully by others in Linux 
  • http://ubuntuforums.org is a great resource   

Enjoy and hope it helps!

EDIT for Ubuntu 11.04 Natty :
Using the instructions on this post worked for me, and I encountered 2 issues:

1. I had a blank screen just after reboot (instead of grub). I solved that (or it solved by itself ?!) by rebooting with an ubuntu live usb and deleting some resolution modes from xorg.conf. (dont know if this actually was the solution but he computer booted correctly.
2. I had to modify in xorg.conf in the Device section Driver "sis", after 'sudo make install'

After that, because this graphic card does not support 3d acceleration, you have to install Unity 2d, (which looks kindof ok compared to the real deal). Complete instructions on installing Unity 2D in Ubuntu 11.04 here.

EDIT2:
Still on that blog, the drivers compiled!