Wednesday, September 30, 2009

Recovering a broken Linux Operating System part (2/3)

In Suse and Redhat the installation cd has a few but powerful tools, lets call them powertools.  These are memcheck86, a very complete and easy to use RAM health check, with this tool i have diagnosed and resolved a lot of memory problems.

The other powertool is the "Rescue Mode", it looks like this:













And after booting, type "root" and you get in without being asked for a password.





This option do not mounts any disk partition, local or remote.  Once in the machine mount the desired partition:

# mount /dev/sda1 /disc1
# vi /disk1/etc/fstab

The fstab file has the partition table of the system, it is common that the disks changed and the system is not able to find some partition.

Tuesday, September 29, 2009

Recovering a broken Linux Operating System part (1/3)

Multiple methods exist to recover a broken Linux Operating System.  Like any problem, there is a method for every problem and a broken operating system could have multiple causes.  To recover a Linux do it with a Senior Administrator by you if you don't know what you are doing.

1) Enter in Single user mode, for this you would need to remember the root password but if you do, you can correct any problem related with networking or some drivers because the initialization scripts are not executed.

In the boot screen put "1" as boot parameter.

Then enter the root password and start analyzing logs, filesystem.  Do not forget to check the memory with a live cd and memcheck86






2) Suppose that you forgot the root password or a BOFH has changed it, you can still use this trick to enter without the root password:
Enter "init=/bin/bash rw"

Monday, September 28, 2009

Working with text files in Unix/Linux (part 3/3)

Another complex combination.  To list line numbers on a file the cat command has an option, but the "nl" command has multiple formating options, something that the "cat" command doesnt.

The -n specifies a format and "rz" right justifies with not leading zeros.

cat sourcefile.php | nl -n rz

To see the last 3 lines of any text file use tail:
cat sourcefile.php | tail -n1

To see how many lines a file has, combine them both:
cat sourcefile.php | nl -n rz | tail -n1

Friday, September 25, 2009

Working with text files in Unix/Linux (part 2/3)

The scenario is that you have a compressed file with ZIP, and this is a raw text log file that you have to unzip and then to cat.

$ zcat messages.gz

You want to see only the first lines of a text file, for that is the head command.

$ head messages.log

And if you want to see the line number of a file, use the cat with the -n argument like:
$ cat -n file1.txt

Everything together: 
$ zcat messages.gz | head | cat -n

Thursday, September 24, 2009

Working with text files in Unix/Linux (part 1/3)

Removing repeated lines in a text, the strategy to remove repeated lines in a text is not very

intuitive at the first, the operation is divided in parts:


1) convert if possible all the lines to lowercase.
2) sort the lines so repeated lines are together.
3) remove repeated lines.



$ cat file1.txt file2.txt file3.txt | tr [A-Z] [a-z] | sort | uniq


View only the lines that are repeated:
$ cat file1.txt file2.txt file3.txt | tr [A-Z] [a-z] | sort | uniq -D


View the amount of uniq files:
$ cat file1.txt file2.txt file3.txt | tr [A-Z] [a-z] | sort | uniq | wc


Print a file from end to start:
$ tac file1.txt file2.txt

Tuesday, September 15, 2009

Resizing an LVM partition in Linux

First check how much space you have left in the Volume Group that contains that Logical Volume:

lvextend -L +300 /dev/system_vg/usr_lv
resize_reiserfs -s+300m /dev/system_vg/usr_lv

1) Without unmounting
2) The /usr partition, which holds kernel modules and is being used.
3) Zero downtime.

Friday, September 11, 2009

Using Bash History (part 2/2)

The part 1 of this text is (click here). To view the last 7 lines typed in the command interpreter, write:
[root@server1 upload]# history 7
 1854  clear
 1855  ls
 1856  vi ajaxfileupload_instructions.txt
 1857  clear
 1858  history
 1859  echo $HISTFILE $HISTSIZE $HISTFILESIZE
 1860  history 7


With the "history" command you can see the entire history.

With the up and down arrows it is possible to see the individual commands, modify and reexecute them.

Other ways to execute previous commands:
To execute the immediately command used before:
[root@server1 upload]# !!

 
To execute the command 1860 from the history, in my history it 
is the "history 7" command.
[root@server1 upload]# !1860
history 7
 
The command is: 
1864  ls -la
This command appends "/tmp" to the command 1864. 
[root@server1 upload]# !1864 /tmp
 
Run the previous command that containst the "tmp" string: 
[root@server1 upload]# !?tmp?
 
Run the previous ls command: 
[root@server1 upload]# !ls
ls -la /tmp
Replace in the previous ls command the "homel" for "home" string.
[root@server1 upload]# !ls:s/homel/home
ls -la /home

Thursday, September 10, 2009

Using Bash History (part 1/2)

Linux has the bash shell set as default (Bourne Again Shell).  This shell (like others) has a history special command that can be used to view, modify and reuse commands used in the past.
Bash reads the ~/.bash_history file when it starts, and this file is loaded into memory, and updated just there, when you logoff this file is updated with the new commands.  This filename can be changed with the $HISTFILE variable, the number of command lines in history during a bash session is ustomized by the $HISTSIZE variable and the number of command lines recorded in the history file is set by the $HISTFILESIZE variable.
Viewing the history setup:
[root@server1 upload]# echo $HISTFILE $HISTSIZE $HISTFILESIZE
/root/.bash_history 1000 1000

Monday, September 07, 2009

A simple backup script

I am need to modify files frequently and always need a backup to rollback if i need to.  One old way to do this was doing a copy of the actual file to a file with a new extension like ".orig".

But as a passionate that i am, i need something better, so i created a simple but effective backup script:

1) Create a script and place it in /usr/bin so all the users can access it.
#!/bin/bash
FILENAME=$1
NUM=`date +%s`
echo "Copying $FILENAME to $FILENAME.$NUM";
cp $FILENAME $FILENAME.$NUM;

2) Then setup an environment variable for your shell, i am working with bash so i use the "alias" in the .bashrc startup script, but depending on the shell you are using, it will differ.
alias backup='/usr/bin/backupScript.bash'

That is all ;-)

Friday, September 04, 2009

SuSe 10 - Choosing an installation method

Graphical install - Insert the CD or DVD and select "Installation". Press F2 to modify the language, F3 to change the vide mode, F5 to set some kernel options and F6 to include a special driver for some special hardware. With F4 it is possible to set from where should the packages be downloaded rather than the CD/DVD, this can be a local repository available through FTP,HTTP,NFS or SMB/CIFS.

For more info about installation methods go
to http://en.opensuse.org/Installation

Boot prompt install - The installation can be done starting at the boot prompt. You have to press ESC in the boot screen and select OK in the message that appears. Then you can specify if
you want to install using ssh or vnc for example.

Some boot prompt options

boot: linux
This start a normal installation.

boot: linux ssh=1
This starts an ssh server.

boot: linux vnc=1
This start a vnc server.

Between ssh and vnc, ssh is more secure because the vnc method transfers all in plain text, including the root password.

boot: linux rescue
With this method you can repair a crashed system, logging in as root.

boot: memtest
Starts the memtest86+, when a machine is crashing without explanation.

Wednesday, September 02, 2009

Linux Strategic Strengths

To answer this question i had to look at the market figures and forecasts, this figure displays that the Server Market prefers Linux more than the client os market. In Year 2012 the revenues from the Linux OS as a Server are expected to decrease to 84%, this effect is caused by the decrease of Linux OS as a workstation.

This report ignores Linux running on mobile devices such as smartphones and PDAs, it considers Linux on enterprises.

The Virtualisation Battle

Some years ago Microsoft and Red Hat were competing in one of their business units for the same segment. Now the competition is between Microsoft and VMWare. Linux is doing a good job leading virtualisation technology but Windows is moving slowly, trying to follow. Linux is adopted more frequently in the companies because of this, with Virtualisation it is easier to migrate to Linux also.

Tuesday, September 01, 2009

A postgres tip

I needed to do some calculation on a postgres function so i had to use plpgsql. I also had to return a set of records. To do this i instintivelly used a 'return setof record' and defined a variable
'ret_row record' but i got this error:

Query failed: ERROR: a column definition list is required for functions returning "record"

The solution is to define the function like this:

CREATE OR REPLACE FUNCTION data_from_user(id_user bigint,
lati double precision, longi double precision, z integer)
RETURNS SETOF markas AS
$BODY$
DECLARE
ret_row markas%rowtype;
diffa double precision := 0;
diffb double precision := 0;
sumar double precision := 0;
BEGIN
if z = 15 then
sumar := 0.03;
end if;
diffa := lati + sumar;
diffb := longi + sumar;

for ret_row in
select * from markas where id_owner = $1 and lat <>
union
select * from markas where id_owner in (
select id_solicitado from solicitudes where
id_solicitante = $1 and id_estado = 2)
and lat <>
union
select * from markas where id_owner in (
select id_solicitante from solicitudes where
id_solicitado = $1 and id_estado = 2)
and lat <>
loop
return next ret_row;
end loop;
return;
END;
$BODY$
LANGUAGE 'plpgsql' VOLATILE;


I hope it was helpfull.