Tuesday, April 10, 2012

ATI Mobility Radeon HD 5xxx Series in Linux (kernel 3.2.14)

I will try to explain how I got to install the AMD Catalyst™ 12.3 Proprietary Linux x86 Display Driver and avoid all the installation problems with the Kernel 3.2.14.

In the beggining I was getting this error:

# vim /usr/share/ati/fglrx-install.log
1
2 Creating symlink /var/lib/dkms/fglrx/8.85/source ->
3 /usr/src/fglrx-8.85
4
5 DKMS: add Completed.
6
7 Kernel preparation unnecessary for this kernel. Skipping...
8
9 Building module:
10 cleaning build area....
11 cd /var/lib/dkms/fglrx/8.85/build; sh make.sh --nohints --uname_r=2.6.38-2-6 86 --norootcheck.....(bad exit status: 1)
12 0
13 0
14 [Error] Kernel Module : Failed to build fglrx-8.85 with DKMS
15 [Error] Kernel Module : Removing fglrx-8.85 from DKMS
16
17 ------------------------------
18 Deleting module version: 8.85
19 completely from the DKMS tree.
20 ------------------------------
21 Done.
22 [Reboot] Kernel Module : update-initramfs

To avoid this problem I uninstalled the program dkms. I have to say that I don't know what is for this program and if you need it you can try to reinstall it after finishing the installation of the ATI driver.

Saturday, January 21, 2012

Caps lock indicator in Linux / Gnome2

If we want to add a caps lock indicator in the Gnome2 panel (that one that shows the clock) there is one easy alternative. We can install the package lock-keys-applet.

$ sudo apt-get install lock-keys-applet
$ killall gnome-panel


Once we have done this, we can add to the panel the new element doing right click in the panel, "Add to panel" and then choose the "Lock Keys" applet.

Source
http://ubuntuforums.org/showthread.php?t=1224918

Sunday, January 1, 2012

Subversion quick configuration

1.1 Create a folder to hold all repositories
$ mkdir ~/.svnroot

1.2 Create a repository
$ svnadmin create ~/.svnroot/project

2.1 Import the repository to work with it (only necessary to do it once)
$ svn checkout file:///home/pron/.svnroot/project

2.2 Create the main structure (only necessary to do it once)
$ cd project
$ mkdir trunk tags branches

3. Activate daemon to allow access to Subversion repositories using the svn network protocol
$ svnserve -d -r ~/.svnroot

4.1 Make a backup of a repository
$ svnadmin dump -q ~/.svnroot/project > project.dump

4.2 Restore a backup
$ svnadmin create project
svnadmin load project < project.dump

5.1 Creating a version
$ svn copy file:///home/pron/.svnroot/project/trunk \
> file:///home/pron/.svnroot/project/tags/v0.1 \
> -m "Creating v0.1 with the right command"

6.1 Recover the last version of a file that has been saved but no commited
$ rm file
$ svn up file


6.2 SVN Diff against changes in the remote repository 
$ svn diff -r HEAD

6.3 How to ignore a directory with SVN
$ svn propedit svn:ignore .

If you have multiple things to ignore, separate by newlines in the property value. In that case it's easier to edit the property value using an external editor:
$ svn propset svn:ignore dirname .

6.4 Getting SVN revision number into a program automatically
Add this code to your file (i.e. file.py)
VERSION='$Rev$'

After this, in the command line write this:
$ svn propset svn:keywords "Revision" file.py


Resources
http://svnbook.red-bean.com/en/1.1/ch04s06.html
http://stackoverflow.com/questions/116074/how-to-ignore-a-directory-with-svn http://stackoverflow.com/questions/1449935/getting-svn-revision-number-into-a-program-automatically

Unixtime quick usage - Seconds to date

1.1 Get unixtime in the console
$ date +%s


1.2 Get unixtime in python
>>> import time
>>> int(time.time())


2 Unixtime to date in the console
$ date -d "UTC 1970-01-01 1294779314 secs"

or
$ date -ud @1294779314


Resources
http://en.wikipedia.org/wiki/Date_(Unix)
http://www.linuxhowtos.org/Tips%20and%20Tricks/converting_unixtimestamp.htm

GPG quick usage

1. Encrypt a file with public key
$ gpg --encrypt --recipient 'Your Name' foo.txt


2.1 Decrypt a file with private key (using the password in the commandline)
$ gpg --output foo.txt --passphrase "$_PASS" --decrypt foo.txt.gpg


2.2 Decrypt a file with private key (not using the password in the commandline)
$ gpg --output foo.txt --decrypt foo.txt.gpg


Resources
http://www.madboa.com/geek/gpg-quickstart/
http://aperiodic.net/phil/pgp/tutorial.html

Add a existing user to existing group (usermod)

If we want to add the existing user pron to the existing group vboxusers, we can execute this command:

$ usermod -Ga vboxusers pron


Is neccessary to put -a, otherwise other groups will be erased. To see changes type:

$ id


Resources
http://www.cyberciti.biz/faq/howto-linux-add-user-to-group/

Thursday, June 23, 2011

Aliasing cd and ls

In the Linux console, is a common action to list directory content (ls), once entered into that folder (cd). Thus, it would be more efficient to join those 2 command into 1.

We can do this adding the next code into ~/.bashrc file:

function cl {
if [ "$1" == "" ]
then
cd
else
cd $1
fi
ls
}
alias cd='cl'


Therefore if we open a new console and we type 'cd <directory>', it will execute 'cd <directory>' and 'ls', respectively.

Resources
http://www.unix.com/shell-programming-scripting/16318-cd-then-ls-l.html
http://www.linuxforums.org/forum/newbie/161327-aliasing-cd-ls.html

Wednesday, June 9, 2010

ATI Mobility Radeon HD 5xxx Series in Linux (kernel 2.6.34)

The aim of this writting is to show how I got to install the ATI driver in Linux, using kernel 2.6.34.

First of all, I tried to download the driver from http://ati.amd.com/support/driver.HTML. Although there is not any driver for 5xxx series, we can use the 4xxx series driver: http://support.amd.com/us/gpudownload/linux/Pages/radeon_linux.aspx?type=2.4.2&product=2.4.2.3.32&lang=English

After downloaded the driver I gave execution permissions to the driver and just in case, put out xserver:

$ chmod +x ati-driver-installer-10-5-x86.x86_64.run
$/etc/init.d/gdm stop


Friday, May 14, 2010

MySQL fast configuration

Creating User:

create user pron@192.168.1.13 identified by 'pass';

This will create an user called pron that could connect from the host 192.168.1.13 to the MySQL server.

If we want to create an user that could connect only from localhost, we won't espicify the IP address.

Creating DataBase:

create database mydb;

Adding user to DataBase:

grant all on mydb.* to pron@192.168.1.13;

Monday, January 11, 2010

Pointers to functions in C

I'm going to show a silly example to describe how to use pointers to functions. They are very usefull. Imagine that you are in a loop and want to execute a function when the index's are odd numbers and another function in even numbers.

#include 
#include

void (*fp)(int);
void fun1(int a);
void fun2(int a);


main () {
int i=0;
fp=&fun1;
for(i;i<5;i++) {
(*fp)(i);
}
}


void fun1(int a) {
printf("Even number: %d\n",a);
fp=fun2;
}

void fun2(int a) {
printf("Odd number: %d\n",a);
fp=fun1;
}


Output:
Even number: 0
Odd number: 1
Even number: 2
Odd number: 3
Even number: 4


In the main method we set the first function to use. Then when we call a function, each function changes the function address to the other function. This way, we can avoid and if condition inside the loop.

Friday, December 25, 2009

Secure a server avoiding ARP Spoofing (ARP Poisoning)

Problem
We want to isolate our server in our local LAN, so any computer in our LAN couldn't connect to it meanwhile server MAC address is unknown.

Facts
Router IP: 192.168.1.1 MAC: 00:14:6c:4f:66:70
Server IP: 192.168.1.12 MAC: 00:0d:61:00:85:2f
Server OS: Debian 5.0

Server network configuration (/etc/network/interfaces)
We'll see two cases. In the first case we can configure the router ARP table, so in its ARP table exist Server IP and Server MAC. In the second case we can't configure router ARP table.

The first case is more secure, because we don't spread ARP Requests and Reply between router and server:

Case 1
Configure the router ARP table like that:
Server IP: 192.168.1.12 MAC: 00:0d:61:00:85:2f

Server network configuration:

auto lo eth0
iface lo inet loopback
iface eth0 inet static
address 192.168.1.12
netmask 255.255.255.0
network 192.168.1.0
# Disable the use of the ARP protocol on this interface.
# So eth0 will not create ARP packets.
# This hack works because ifup configure network like that:
# ifconfig ${DEVICE} ${IPADDR} \
# netmask ${NETMASK} broadcast \
# ${BROADCAST} ${ARP:+arp}
broadcast 192.168.1.255 -arp
gateway 192.168.1.1

# Trusted IP and ARP (In this case only the router)
up arp -s 192.168.1.1 00:14:6c:4f:66:70


Case 2

auto lo eth0
iface lo inet loopback
iface eth0 inet static
address 192.168.1.12
netmask 255.255.255.0
network 192.168.1.0
broadcast 192.168.1.255
gateway 192.168.1.1

up arptables -F
up arptables -P INPUT DROP
up arptables -P OUTPUT DROP
up arptables -P FORWARD DROP
# Trusted IP and ARP (In this case only the router)
# Only ACCEPT connections from trusted gateway.
up arptables -A INPUT -s 192.168.1.1 \
--source-mac 00:14:6c:4f:66:70 -j ACCEPT
# Send replies only to the trusted hosts.
up arptables -A OUTPUT -d 192.168.1.1 \
--destination-mac 00:14:6c:4f:66:70 -j ACCEPT
# Add static entry into the ARP table to link your trusted host to its own MAC.
up arp -s 192.168.1.1 00:14:6c:4f:66:70


Conclusion
The first configuration is more secure and we don't have to use arptables. However we have to configure statically router and server ARP tables. If in our LAN we trust another PC, we have to edit server configuration adding PC's MAC. In PC computer we have to add Server's MAC address statically too. Remember Server can't create ARP replies (we have disabled server ARP).

In the second configuration, we don't have to configure statically the router. Moreover, if we add a new trusted PC, we only have to edit Server configuration (add PC MAC in ARP table and append chaing in arptables). However we are spreading ARP Reply and Requests, so any computer can see them.


Sources:
http://www.experts-exchange.com/Networking/Linux_Networking/Q_21428737.html
https://bugzilla.redhat.com/show_bug.cgi?id=12111
http://abulmagd.blogspot.com/2008/08/arptables-and-arp-poisoningnetcut.html

Wednesday, December 23, 2009

autocd: invalid option name (bash)

It is posible to add some extra options to the bash like autocd option. This option allows us to change the actual directory without using cd command.

Without autocd 
pron@debian:~$ cd Desktop
pron@debian:~/Desktop$

With autocd
pron@debian:~$ Desktop
cd Desktop
pron@debian:~/Desktop$

To add this option to our bash:
$ shopt -s autocd
or
$ bash -O autocd (this last, we'll create new bash enviroment)

But in my Debian 5.0 /bin/bash version was 3.2.39 that didn't work autocd option. So when i tried to activate the autocd option i get this error:
$ shopt -s autocd
bash: shopt: autocd: invalid shell option name

The shopt command is a SHELL BUILTIN COMMAND. They are commands contained within the shell itself. I realized that my bash version did't support autocd option. So, i installed a new bash version (4.0.0) from GNU web page.

Once installed the new bash version, it's necessary to change your user default shell. My new version of shell was installed in /usr/local/bin/bash. So the first step was to edit (like root) /etc/shells to add the new shell location (/usr/local/bin/bash). After that, you could change your user bash using chsh command. It prompts for your user password, and then you type your shell location (in this case /usr/local/bin/bash). Finally, to set all changes, it was necessary to exit from you account. If you are in a gnome or KDE enviroment, logout from your user account and login again.

After done all this you can use the autocd option. To set it permanently, you can edit your ~/.bashr file and add 'shopt -s autocd' command.

Source:
http://www.linux-magazine.com/w3/issue/111/088-090_command.pdf
http://www.gnu.org/software/bash/manual/
http://linux.about.com/od/bgb_guide/a/gdebgb16t01.htm

Wednesday, November 18, 2009

Color highlight in Vim

First of all we'll edit or create ~/.vimrc file:

Code:" Set syntax highlighting to always on
syntax enable

" Set the background to dark and the colorscheme to murphy
set background=dark
colorscheme murphy

" Set automatic filetype detection to on
filetype on


From now, by default highlight would be enabled and the colorscheme we'll be "murphy". If we wanted to use another color highlight we can download it from this site:

http://www.cs.cmu.edu/%7Emaverick/VimColorSchemeTest/index-pl.html

After downloaded the file, save it in ~/.vim/colors/ directory.

Sources:
http://www.linuxquestions.org/questions/linux-software-2/color-highlight-in-vim-howyoudothat-565358/

Saturday, November 7, 2009

Hide the windows command prompt

To execute a program in a hidden windows command prompt, we can create a JScript file (.js extension) and then execute it:

JScript code
var WindowStyle_Hidden = 0
var objShell = WScript.CreateObject("WScript.Shell")
var result = objShell.Run("cmd.exe /c putty.exe 192.168.1.14 -l tv", WindowStyle_Hidden)


In this case, this script we'll execute putty.exe in a hidden cmd prompt.

Source:
http://www.geekstogo.com/forum/Hide-command-prompt-windows-t56092.html

Wednesday, October 21, 2009

Renaming huge amount of files

There are a lot of scripts to renames files, but these are two choices:

for & sedfor F in * ; do mv “$F” `echo “$F” | sed ’s/{search_string}/{replace_string}/’ ` ; done


The other choice is to use the command 'mmv'. I never used it, but seems that it works.

Monday, October 12, 2009

Diff two directories

If we wanted to find the differences between 2 directories we can use this commands:

ls -R$ ls -R 'directory1' > ls_dir1
$ ls -R 'directory2' > ls_dir2

Then we will change each file directory names for the same name. This way we could get better diff file.
sed$ sed -e 's/directory1/diff_name/' ls_dir1 > ls_dir1_sed
$ sed -e 's/directory2/diff_name/' ls_dir2 > ls_dir2_sed

And finally we'll do the diff, to see the differences:
diff
$ diff ls_dir1_sed ls_dir2_sed | more


Sed: http://www.gentoo.org/doc/es/articles/l-sed2.xml

Tuesday, May 12, 2009

Extract audio tracks from video

To extract audio tracks from videos:
ffmpeg -i video.aviInput #0, avi, from 'video.avi':
Duration: 00:55:21.8, start: 0.000000, bitrate: 1380 kb/s
Stream #0.0: Video: mpeg4, yuv420p, 704x384, 25.00 fps(r)
Stream #0.1: Audio: mp3, 48000 Hz, stereo, 160 kb/s
Stream #0.2: Audio: mp3, 48000 Hz, stereo, 160 kb/s
Must supply at least one output file


With that command, we don't extract anything, but we can see the audio track amount.
We see, that the video has 2 audio tracks. To extract one:

ffmpeg -i video.avi -map 0.2 audio.mp3Input #0, avi, from 'video.avi':
Duration: 00:55:21.8, start: 0.000000, bitrate: 1380 kb/s
Stream #0.0: Video: mpeg4, yuv420p, 704x384, 25.00 fps(r)
Stream #0.1: Audio: mp3, 48000 Hz, stereo, 160 kb/s
Stream #0.2: Audio: mp3, 48000 Hz, stereo, 160 kb/s
Must supply at least one output file


We'll get the second audio track into audio.mp3 file.

In the case that we want to extract all audio's from all video's:
j=1;for i in *.avi;
do ffmpeg -i "$i" -map 0.2 /home/pron/SubWorkspace/Audio$j.mp3;
((j++));done;


For each avi file, we'll get an Audio?.mp3 file.

Sunday, May 10, 2009

OpenOffice Tips

Starting Page Numbering on Page 2, and Other Ways to Mix Page Styles in a Document

This is very useful tip, due to you learn creating page styles for the index's pages and for the body. This way you separate different sections of your document.

http://openoffice.blogs.com/openoffice/2005/12/different_page_.html

Page numbering
Another useful tip. I got resolve a problem with the index table. Never use the Offset feature you can see in the page number field dialog! The ToC (Table of Contents) is based on the real page numbering, it doesn't take into account the offset feature. Moreover, your last page won't have any numbering if the correction is positive.

* Go to the first paragraph of the page to be renumbered, right click and select Paragraph.
* Go to the Text Flow tab, in the Breaks section, adjust the page number.

http://user.services.openoffice.org/en/forum/viewtopic.php?f=71&t=1221

Adding Captions
You can add captions (names) to images, tables and other objects in a document. The, you can create a index of table using this captions. For that, in the type combo box select Index of Tables, and in Categories select Captions.

http://wiki.services.openoffice.org/wiki/Documentation/OOo3_User_Guides/Writer_Guide/Adding_captions

Subtitle synchronizer

I have written a program to synchronize subtitle's file (srt format). But what i mean when i say synchronize? The reality is that the videos are recorded with differents fps (Frames per second). So if our film has been recorded in 24 fps, but the subtitle is created with a film with 25 fps, our subtitle we'll desynchronize each second. This happen because the film with 24 fps is reproducing faster (less frames in the same time). So to realize that our subtitles are desynchronized with the film, we'll see that the film is more desynchronized as long as time is passing.

The program needs to know when stars and ends the conversation in the video. So you have to look for in the video when is the first and last conversation. It would be more precise to see which are the first and last conversations in the subtitle file, and then you we'll find easier the star/end times in the video.

The way to use the program is the next:

./subsincro desynchronized_file.srt new_start_time new_end_time > synchronized_file.srt

For example:

Suppose that in my_serie.3x05.srt we have this:

1
00:00:18,268 --> 00:00:20,498
Let's keep those hammers working!

(...)

422
01:28:22,428 --> 01:28:26,057
How will you live, John?
- Day by day.

Then, we search this conversations in the video, and make a note of the real times in the video. Finally we execute the program with the new start/end time:

./subsincro my_serie.3x05.srt 00:00:05,520 01:24:31,120 > my_serie.3x05.syn.srt

Program code:
 /*
Copyright 2010 djtalo85@gmail.com
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/


/*****************************
SUBTITLES SYNCHRONIZER

usage: ./subsincro sub_file.str new_start_time new_end_time
example: ./subsincro Pron.3x01.srt 00:00:01,000 00:54:31,000

To create a new subtitle file, redirect the output.
example: ./subsincro Pron.3x01.srt 00:00:01,000 00:54:31,000 > Pron.3x01.new.srt

*****************************/


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int new_start_time,new_end_time,old_start_time,old_end_time;
int aux_start_time,aux_end_time;
int gap=0, subIndex=1;
float time_desync;


char *timeToString(int time) {
char *outTime;
int hour,min,sec,ms;
div_t d;

outTime = (char *) malloc (13*sizeof(char));

d=div(time,1000*60*60);
hour = d.quot;
time= d.rem;

d=div(time,1000*60);
min= d.quot;
time = d.rem;

d=div(time,1000);
sec= d.quot;
ms = d.rem;

sprintf(outTime,"%02i:%02i:%02i,%03i",hour,min,sec,ms);

return outTime;


}
printTime(int time) {
printf("%s",timeToString(time));
}

int strTimeToInt(char str[13]) {
int hour,min,sec,ms,error;
int time;
char aux[4];

strncpy(aux,str,2);
aux[2]='\0';
hour = atoi(aux);

strncpy(aux,&str[3],2);
aux[2]='\0';
min = atoi(aux);

strncpy(aux,&str[6],2);
aux[2]='\0';
sec = atoi(aux);

strncpy(aux,&str[9],3);
aux[3]='\0';
ms = atoi(aux);

time= ms + 1000*sec + 1000*60*min + 1000*60*60*hour;

//printf("%i ",error);
//printf("( %02i:%02i:%02i,%03i )",hour,min,sec,ms);

return time;
}
getTime(char str[1024]) {
char str_time[13];
char end_time[13];

sscanf(str,"%s --> %s",str_time,end_time);
aux_start_time = strTimeToInt(str_time);
aux_end_time = strTimeToInt(end_time);

}
update_time(char str[1024]) {
getTime(str);

/* Apply sync. */
aux_start_time = abs((float)(aux_start_time * 100) / time_desync);
aux_end_time = abs((float)(aux_end_time * 100) / time_desync);

/* Apply pad */
aux_start_time = aux_start_time - gap;
aux_end_time = aux_end_time - gap;

printTime(aux_start_time);
printf(" --> ");
printTime(aux_end_time);
printf("%s",&str[29]);

}

isInteger(char str[1024]) {
int i=0;
while(i<(strlen(str)-2)) {
if(str[i]>47 && str[i]<58) {
i++;
} else {

return -1;
}
}
if(i==0) {
return -1;
} else {
return 0;
}
}

main(int argc, char *argv[]) {
char line[1024];
FILE *f;
int index,startIndex;
int sync_start_time,sync_end_time;

if (argc < 4) {

fprintf(stderr,"Subsincro 0.3 by Pron\n\n");

fprintf(stderr,"Usage: %s file.srt new_start_time new_end_time\n", argv[0]);

fprintf(stderr,"Time format: hh:mm:ss,mmm\n\n");

return 1;

}

if(strlen(argv[2])!=12 || strlen(argv[3])!=12) {
fprintf(stderr,"Time argument format have to be 00:00:00,000\n");

return 1;
}

new_start_time= strTimeToInt(argv[2]);
new_end_time=strTimeToInt(argv[3]);


if((f = fopen( argv[1], "r" ))==NULL) {
fprintf(stderr,"File '%s' not exist\n",argv[1]);
return 1;
}

while((fgets(line, 1024, f))!=NULL) {
if(isInteger(line)==0) {
index=atoi(line);
fgets(line, 1024, f);
getTime(line);
if(subIndex==1) {
startIndex=index;
old_start_time = aux_start_time;
}
subIndex++;
}
}
old_end_time = aux_start_time;

//time_desync = (old_end_time *100.0)/(old_end_time - ((old_end_time - old_start_time) - (new_end_time - new_start_time)));
time_desync = (((float)old_end_time - (float)old_start_time) * 100.0) / ((float)new_end_time - (float)new_start_time);


/* gap*/
sync_start_time = ((float)(old_start_time * 100) / time_desync);
sync_end_time = ((float)(old_end_time * 100) / time_desync);

gap = sync_start_time - new_start_time;

fprintf(stderr,"Desynchronization: %f\n",time_desync);


fprintf(stderr,"Sub. Index\tOld Time\tSub. Synchronized\n",timeToString(gap));
fprintf(stderr,"%i\t\t%s\t%s\n",startIndex,timeToString(old_start_time),timeToString(sync_start_time - gap));
fprintf(stderr,"[...]\n");
fprintf(stderr,"%i\t\t%s\t%s\n",index,timeToString(old_end_time),timeToString(sync_end_time - gap));
fseek(f,0,0);
subIndex=1;
while((fgets(line, 1024, f))!=NULL) {
if(isInteger(line)==0) {
printf("%s",line);
fgets(line, 1024, f);
update_time(line);
subIndex++;
} else {
printf("%s",line);
}
}
fclose(f);
return 0;
}
}

Tuesday, April 14, 2009

How to disable ASLR (Address Space Layout Randomization)

$ sysctl -w kernel.randomize_va_space=0