Friday, March 11, 2011

God save the VIM!: My personal .vimrc

set number            <-- A must 
set ignorecase        <-- quite useful
set encoding=utf-8
set tabstop=4          
set shiftwidth=4      <-- 8 is too much and annoying
set expandtab         <-- replace tabs with spaces. That makes layout independent of tab's setup.
set foldmethod=indent <-- Ummm, depends on taste. Automatic folding based on indentation
Once a file is open it may be better to change it to foldmethod=manul
set cursorline        <-- Visual help (cursorcolumn is useful sometimes) 
syntax off            <-- Syntax highlighting never works to me.
set cindent           <-- C indent rules                                           
set smartindent       <-- Make vim smart about indentation
set autoindent        <-- Enable autoindent 
 
" associate file extension (lp) with syntax (lua)
au BufNewFile,BufRead *.lp setlocal ft=lua
 
" shorcut for if-then-else in C-style languages
ab ite 
\if () {
\
\} else if () {
\
\} else if () {
\
\} else {
\
\} end  

 
" shorcut for if-then-else in lua
ab itel 
\if ()  then -- {
\
\elseif () then -- } { 
\
\elseif () then -- } { 
\
\else -- } { 
\
\end -- }
 

" 
fu! SaveSess()
    execute '!mkdir .vim'
    execute 'mksession! .vim/session.vim'
endfunction

fu! RestoreSess()
execute 'so .vim/session.vim'
if bufexists(1)
    for l in range(1, bufnr('$'))
        if bufwinnr(l) == -1
            exec 'sbuffer ' . l
        endif
    endfor
endif
endfunction

autocmd VimLeave * call SaveSess()
autocmd VimEnter * call RestoreSess()
 
" indentation rules for FFmpeg
set shiftwidth=4
set softtabstop=4
"set cindent         < -- useful for C alike languages
"set cinoptions=(0   < -- useful for C alike languages

autocmd FileType make,automake set noexpandtab shiftwidth=8 softtabstop=8 < -- " Allow tabs in Makefiles.
highlight ForbiddenWhitespace ctermbg=red guibg=red < -- hihglight forbidden Trailing whitespace and tabs
match ForbiddenWhitespace /\s\+$\|\t/
autocmd InsertEnter * match ForbiddenWhitespace /\t\|\s\+\%#\@<!$/ < -- Do not highlight spaces at the end of line while typing on that line.
 
 
Update 2012-04: The RestoreSess/SaveSess (Grotty copy&paste from http://stackoverflow.com/questions/5142099/auto-save-vim-session-on-quit-and-auto-reload-session-on-start) is a really great addition. It saves and automatically restore all the vim session (open file, tabs, window layouts,...). One more reason to say Eclipse "Good Bye!".

Update 2014-05: Unashamedly copied Makefile related stuff from ffmpeg documentation project.

Update 2014-11:  associating file extensions with language syntax and shorcuts for if-then-else.

External Links:

Tuesday, December 21, 2010

Add skip-name-resolve to default my.cnf

Copying and pasting myself from an entry in the bugzilla system:

Description of problem:

The default mysql setup (/etc/my.cnf) indicates to make an inverse dns lookup
for new network connections. Mostly this is a quite primitive way to strength
database security (IMHO it provides no added security at all) but promotes
buggy behaviour and is quite risky. For example mysql connections will fail if
DNS fails. Even if it works, connections will really slow down (around x100
times slower!!) if using an external DNS or the DNS is overloaded.

Other potencial problems. After granting access with the command:

GRANT ALL PRIVILEGES ON *.* TO 'myuser'@'%' IDENTIFIED BY 'MyPassword' WITH
GRANT OPTION;

connections will be allowed to 'myuser' from any host, EXCEPT from localhost
(127.0.0.1). An extra command will be needed.

GRANT ALL PRIVILEGES ON *.* TO 'myuser'@'%' IDENTIFIED BY 'MyPassword' WITH
GRANT OPTION;

What's much worse. Allowed connections can stop working if suddenly /etc/hosts
is altered. (That really hurts in production environments).

Friday, December 17, 2010

KVM weekly backup, the easy way

For the joy of all my readers here it comes the master-of-the-universe weekly script backup for KVM:

# Redirect stdout/stderr to custom log.

exec 1>> /var/log/custom_$(basename ${0}).$(whoami).$(date '+%Y%m%d').log 2>&1
pushd /media/backup/MyServer1/
# Keep copies of the VM for the last 4 weeks.
mv kvm.qemu.gz.1 kvm.qemu.gz.2
mv kvm.qemu.gz.0 kvm.qemu.gz.1
mv kvm.qemu.gz kvm.qemu.gz.0

# Stop KVM instance through telnet.
# KVM has to be started with the option:
# -monitor telnet:127.0.0.1:9942,server,nowait
echo "stop" | nc -q 10 127.0.0.1 9942
# Freeze KVM instance

#Here we do the real backup of the KVM instance

cat /VM-Images/kvm.qemu | gzip > /media/backup/MyServer1/kvm.qemu.gz
if [ $? != 0 ]; then
echo "WARN: KVM backup failed"
fi
echo "c" | nc -q 10 127.0.0.1 9942 # Continue KVM instance
popd

For the previous script to work correctly we have to start the KVM instance with the option -monitor telnet:127.0.0.1:9942.

In my particular system I start KVM instances at startup in /etc/rc.local. This is script is also quite interesting so I extracted all the kvm related stuff:

/etc/rc.local:
# Setting up the bridge (tip: apt-get install bridge-utils)
brctl addbr ofi1
brctl addif ofi1 eth1
ifconfig eth1 0.0.0.0 promisc up
ifconfig ofi1 192.168.2.100 netmask 255.255.0.0 up
ifconfig ofi1:1 172.16.1.3

...
kvm -net nic,macaddr=52:54:00:19:34:56 \
-net tap,script=/etc/qemu-ifup -hda /VM-Images/kvm.qemu \
-boot c -vnc :5 1>/var/log/custom_kvm.qemu.log \
-monitor telnet:127.0.0.1:9942,server,nowait 2>&1 &
...
# ionice/renice down our virtual machine
PID=$(sof /VM-Images/kvm.qemu | grep -v ^COMMAND | while read cmd pid staff; do echo $pid; done)
ionice -c 3 -p ${PID} &
renice 10 -p
${PID} &


/etc/qemu-ifup:
#!/bin/sh
# ofi1 is the choosen name in /etc/rc.local
/usr/sbin/brctl addif ofi1 $1 ;
ifconfig $1 0.0.0.0 up;

TIP: KVM console has lot of interesting options not shown here, including those for migrating a virtual machine.

Monday, December 6, 2010

eternal spirit of contradiction

Visual/graphics applications tends to be developed with text based programming languages, while text/voice applications tends to be developed with visual based ones.

Another probe of our natural bias toward self-contradiction.

Saturday, October 9, 2010

A mysql inmigrant in an Oracle world

Last few years I've been living in MySQL. A quiet city full of good food and nice people.

Two months ago I had to relocate to Oracle, a "fast-food-city" where people forget what they are and become slaves of projects, budgets, estimates and more parasitic MBA vocabulary.

Life style is quite different, even if both cities are close to each other. Just one switch away.

Beeing I MySQL citizen the first thing I tried to do was to go to the Oracle's city council and sign up for a new database. What a surprise when the public servant ignored me. I was so upset that I didn't known what to do. Fortunatly a gentle citizien next to me laughted and heading to me with a smile in his face said:

- "I couldn't avoid hearing your conversation. You are new in the city, right?"

- "Yes am I" replied. "I just arrived in the city a few hours ago and ..."

- "and you tried to sign up for a new database as you ussually did in MySQL", interrupted him with an increasing smile and then continued ...
- "you can't do that in Oracle. People here don't like it. Don't worry about that, just do next thing: Tell the officer you want a new user for the existing database. That will automatically provide you with your private schema. The schema will have the same name than your user and you will be able to do whatever you like in it as you previosly could in your private MySQL database."

-"Thanks you, I was completly lost!" I replied.

-"You are wellcome. I came time ago from the beatiful PostgreSQL countryside and it was taugh to get used to a bloated big poluted city like this. Still, it's not that bad once you get used to it. have a nice day"

Now it's time to find a good server to hire! Servers in Oracle are quite expensive when compared to the prices in MySQL. I don't want an luxury one with swimming pool and a garden just something small to pass the night and good neighbours, you known, I don't like conflictive hard-disks next door. Guess me good luck!



Tuesday, March 30, 2010

Windows Licences comming to a dead-end

The image above reflect real prices for the Zotac ION. The snapshot is dated 2010-03-25.
Prices start at 164.37Eu (220Dolars) and with such price we get a noise-free nettop with an 1.6Ghz Atom and an Nvidia ION able to play 3D content and FullHD videos. It comes also with a useful handle to hang it on the back of any VESA monitor or TV with HDMI input so in practice, it wastes no space at all.

The Atom is not big deal but is more than just enough for 90% of common office tasks and the Nvidia ION chipset can do magic.

Another "feature" is that these equipments come without CD/DVD. Just an internal laptop HD with 160Gb. Enough to install any OS. Anything else must go into the SD slot or conected through USB. The price is so adjusted that it doesn't even come with a Windows Licence. When you go bellow the 200€ price per unit that's not possible. A bunch of extra euros is too much when you have put all the efforts in getting a final low-price.

This is an start. But something that is going to be more and more normal. Prices will drop down the 150 barrier, then 100. Finally, PCs will be for free for final consumers in maybe one or two years, and most people will get them from its Internet provider.

People will still be able to buy an expensive quad-core and waste 200 Watts power to write a letter or check their emails, paying a "funny" extra bill each month to its power-supply company, bear with the annoying fan sounds and the hot air comming from the inside of the bloated cages that reminds of a old diesel engine. Yes, the will, but most will not. Today is even possible to get Atom equipments for around 100€ by dropping all the goods of the Nvidia ION.

In this context I see no place for the Microsoft tax. Whether they like it or not, it's something not in their hands. Hard times are comming for the old Windows monopoly. Microsoft will need to fetch money from anywhere else. I guess they will try to get it increasing licences fees from their loyal (and somethinglike naïve) customers. They will raise server licences, SQL licences, "whatever" licences. Loyal customers who bet for Microsoft and ignored opensource alternatives, just because Microsoft was a giant in the stock market will now have to confront a world dominated by opensource alternatives. Loyal customers will pay more and more for less and less.

A final note. When using Linux, it's quite easy to make the Zotak ION start through the PXE network protocol. Since network swithches with 48x100Mbs + 2x1Gbs Uplink ports are around 300€ (and droping) that mean we can drop the internal HD and get even a more adjusted price per unit. And having all network based means software updates and central administration comes for free, even for a farm of hundreds (maybe thousands) of PCs.

Just one extra piece is needed. A central NFS server acting as root file system. And today such server can even be placed in a virtual machine. Great, isn't it?