Saturday, April 9, 2011

Create a simple linux disk image file

Let's consider the next problem. We have a Linux system installed on a computer or a virtual machine and the Linux partitions size is just around of 10 GB. We need an important disk space to compile a huge code source line Android for example, or a Linux distribution. 
The Linux system occupy around of 6 GB and 3-4 GB of free space is not enough for our needs. So to create a new partition of 10 GB would be great. But don't have empty space to create this partition. If the system is installed on a Virtual machine, we can create a new Virtual Hard Disk.
But we have a lot of free disk space on a external or internal hard disk formatted on NTFS for example. We can mount NTFS partitions on Linux but we can't use its to builds sources because usually the build system need a Linux partition. 
So, the best solution in our case is to create a Linux disk image file and format it using a Linux file system like ext3 or ext3.
First we have to create a file having the size of new partition and filled with 0:

dd if=/dev/zero of=./build_partition.img bs=1MiB count=200

The disk.img file of 200 MB size should be created.

Next we have to build the ext3 Linux file system on the created file:

mkfs.ext3 ./build_partition.img

Now we can mount the new partition:

sudo mount -oloop ./build_partition.img ./mnt/build_partition/

Or unmount the partition:

sudo umount /dev/loop0

Tuesday, March 29, 2011

ALSA tips and tricks

1. If a single channel is selected, the left channel is used. For example using the next function:

int snd_pcm_hw_params_set_channels ( snd_pcm_t *  pcm,

snd_pcm_hw_params_t *  params,

unsigned int  val


If val parameter take value 1, meaning 1 channel (mono), the left channeel of the hardware input will be used.

Wednesday, March 9, 2011

Creating a simple RPM binary package

Let take a simple example, you have a binary and a configuration file which you want to install on a Linux machine using a RPM. The binary file “ExampleBin” should be copy on /usr/bin and the configuration file “ExampleBin.conf” should be copy on /etc.
First of all, to be able to create a RPM you need to have installed the rpm-build tool.
Any RPM is created based on a “spec file”. This file basically explains to rpm-build tool how to create and configure the RPM pack.
So let create the “spec file” name “ExampleBin.spec”:

Summary: ExampleBin
Name: ExampleBin
Version: 0.1
Release: 1
Group: ExampleBin
License: ExampleBin
Source: %{expand:%%(pwd)}
BuildRoot: %{_topdir}/BUILD/%{name}-%{version}-%{release}


mkdir -p $RPM_BUILD_ROOT/usr/bin
mkdir -p $RPM_BUILD_ROOT/etc
cp %{SOURCEURL0}/iButtonServer ./usr/bin/
cp %{SOURCEURL0}/iButtonServer.cfg ./etc/

rm -r -f "$RPM_BUILD_ROOT"

%config(noreplace) %{_sysconfdir}/ ExampleBin.cfg
%{_bindir}/ ExampleBin

ExampleBin start

ExampleBin stop

On this example, the binary and configuration file are placed on the current directory (pwd).

In the %prep section, the build process is prepared. In our example the required files are copied to $RPM_BUILD_ROOT (build directory).

The %post section executes after the package has been installed. 
The %preun section executes before the user uninstall the package.

This script need _topdir environment variable. It should be set inside .rpmmacros file, placed on your home directory.
The next bash script set the _topdir enviroment variable and create the RPM calling using rpm-build tool:

echo '%_topdir '$(pwd) > ~/.rpmmacros
rpmbuild -bb ExampleBin.spec

Running this script placed on the same level like “spec file” the RPM should be generated.

Monday, February 28, 2011

Web authentication (Apache server) using system users

If you intend to use the Apache Web server and you are looking for a simple and robust authentication mechanism based on OS authentication mechanism the Apache mod_auth_externet (Apache2  mod_authnz_external) module is a good solution.
The OS authentication mechanism means authenticate against the shadow file of your linux OS, where all users are in a central system.
This article will provide you with an installation procedure for Ubuntu and OpenSuse, to get mod_authnz_external running with pwauth and PAM authentication.
I tested using Ubuntu 10.04 with Apache 2.2.14 and OpenSuse 11.3 with Apache 2.2.15. Also Ubuntu 10.1 0 I think should have the same behavior like 10.04 regarding web authentication using mod_authnz_external.

You can download the source code of the mod_auth_external from the official web site:
 Make sure that you are using the correct version of mod_auth_external for you version of Apache:
    Apache 2.2:     mod_authnz_external-3.1.x or mod_authnz_external-3.2.x
    Apache 2.0:     mod_auth_external-2.2.x
    Apache 1.3:     mod_auth_external-2.1.x
As you can see the Apach 2.2 which is usually installed on the newest Linux distro (Ubuntu 10.04 and OpenSuse 11.3) use mod_authnz_external.
You can get Apache version:
        OpenSuse:   httpd2 -v
        Ubuntu:       apache2 –v

Install mod_auth_external

On Ubuntu I build the module from source code. Please read the helpful instructions from the mod_auth_extrernal page.

On Suse I installed the mod_auth_extrernal using Yast tool from the next repository:

Also using YaST tool the mod_authnz_extrernal module should be loaded by Apache server:
     -Start YaST/ System/ sysconfig Editor
     - Select Network, WWW, Apache2.
    - Highlight APACHE_MODULES. Add authnz_extern into the Modules line. Note: Don’t hand-edit dav_svn into /etc/apache2/sysconfig.d/loadmodule.conf, as YaST will just remove it next time it runs.

Install pwauth

Pwauth tool is a good solution to check the given system user and password.
On Ubuntu pwauth could be found on the default repository, just using:
apt-get install pwauth

On Suse the repository used for mod_autnx_extern contain the pwauth tool too.
Also on Suse server the access rights should be cheange for  pwauth tool:
chmod 4755 /usr/bin/pwauth

As soon as the pwauth is installed it could be tested as follow:
Run "pwauth", than type a user and a password.  Than type "echo $?" . If return 0 the user and password is correct. Returning 1 means invalid user or password.
If  a pwauth test doen’t work for a valid user and password, the pwauth PAM system should be configure. The PAM rules for pwauth could be configured on  /etc/pam.d/pwauthfile. If this file doesn’t exist itr should be created. I tested using the same rules as /etc/pam.d/login files.
Nou you can retest running pwauth tool.
Configure Apache Server

On Ubuntu I created a configuration file for authnz_external module: /etc/apache2/modules-available/authnz_external.conf and a symbolic link to this file inside /etc/apache2/mods-enabled. This is the mechanism of loading and configure modules of Apache 2.2 on Ubuntu. Now we should edit this configuration file.

On Suse we should edit /etc/apache2/httpd.conf file.

So we should add the next commands:

AddExternalAuth pwauth_auth /usr/local/bin/pwauth
SetExternalAuthMethod pwauth_auth pipe

<Directory "/path/to/www">
    AuthType Basic
    AuthBasicProvider external
    AuthExternal pwauth_auth
    AuthName "this text will be display on auth box"
    Require valid-user

For more details please read the mod_auth_external page.