Free DNS provides easy shared DNS hosting & URL forwarding
Showing posts with label linux. Show all posts
Showing posts with label linux. Show all posts

Wednesday, July 2, 2014

Starting Skype in invisible mode

I recently installed Audio Recorder which has a very cool feature: to automatically start recording Skype calls. So that I don't waste time or forget about it, I set Audio Recorder to automatically start when I log in. There is one downside: when Audio Recorder starts, it automatically starts Skype, which starts with its last online status. This is very annoying, as it shows me online in Skype right away I log in Ubuntu, even if I don't want to be online or I don't want to be visible.

After googleing a bit, I run into this Skype forum thread of users complaining about this feature (or lack of feature to set startup status). One of the posts explains how to manually change the SQLite database that Skype uses for accounts. I tested that and worked great so in order to integrate this into Audio Recorder, I wrote the following bash script located saved as ~/bin/skype:

#!/bin/bash
sqlite3 ~/.Skype/YOURSKYPEIDHERE/main.db 'UPDATE Accounts SET set_availability=6'
/usr/bin/skype $*

Note that ~/bin/skype has to be executable and ~/bin/ has to come in $PATH before /usr/bin for this to work.

Thursday, April 17, 2014

Udev tricks - How to toggle built-in wi-fi on/off

My Toshiba Satellite laptop has a RTL8192CE built-in wifi, which is a total joke. It only works if I'm within a few meters range of the router and without any big obstacles between. For example, if I'm in my home office I can't even use the router that is in the next room. All other devices in our house can connect and mark it as strong signal. If by any luck my laptop manages to connect, the connection is unusable, with network speeds of less than 1Mb/s and most of the packages failing their CRC checksum.

I googled and test various solutions. None of them worked. I found many others which have same problem. Different OSes, different laptop models, same card type, same problem. After loosing (in total) more than 3 days on this, I decided it's just not worth it and bought and Atheros external USB wifi dongle (model TL-WN721N). It works perfectly and without any problems.

The problem I had now is that when I plug the USB wifi, Network Manager in Ubuntu clutters its menu and dialog boxes with various options to select which wifi card to use. For example, when I want to connect to a hidden network, it asked me to select which card to use for connection. It also used to keep trying to reconnect to the home network on the faulty card (but that I fixed by deleting Wi-fi network settings). This is annoying and overall makes me waste a lot of time. Therefore, I read udev man pages to find out how can I disable the built-in wifi whenever I plug in the USB one. Eventually I came up with these rules:

SUBSYSTEM=="net", ACTION=="add", ENV{INTERFACE}=="wlan1", RUN+="/sbin/rmmod rtl8192ce"
SUBSYSTEM=="net", ACTION=="remove", ENV{INTERFACE}=="wlan1", RUN+="/sbin/modprobe --quiet rtl8192ce"

The major time waster was ENV{INTERFACE}=="wlan1" because NAME=="wlan1" works on add event, but not on remove. I also lost some valueable time trying to get it work with INTERFACE=="wlan1", before I actually understood that INTERFACE is not a property.

These rules go under: /etc/udev/rules.d/99-disable-builtin-wlan.rules.

Later on, I decided to do something similar for the built-in soundcard (for similar reasons). It took me some time to find out the proper commands to disabled it. First I tried to remove its modules (like i did with the wifi), but that option failed because they were always in use by PulseAudio. So I started to look into PulseAudio options. I found the pactl (and pacmd) which seemed to do what I needed. It took another 30 minutes of testing to find out how to use them for this task and another 15 minutes to find out that they don't work as root, but should be run as the current user. After that, writing the udev rules was a piece of cake:

SUBSYSTEM=="sound", ACTION=="add", ENV{ID_TYPE}=="audio", RUN+="/bin/su abautu -c '/usr/bin/pactl set-card-profile alsa_card.pci-0000_00_1b.0 off'"
SUBSYSTEM=="sound", ACTION=="remove", ENV{ID_TYPE}=="audio", RUN+="/bin/su abautu -c '/usr/bin/pactl set-card-profile alsa_card.pci-0000_00_1b.0 output:analog-stereo+input:analog-stereo'"

Note that you need to replace "su abautu" with your own username or it won't work.

Saturday, November 9, 2013

Openssl - setting up a custom CA certificate, requesting and approving certificates with Subject Alternative Name (SAN)

I spent last couple of hours reading web resources, man pages and config files to get what I need: a custom CA setup that allows me to sign certificate request which include Subject Alternative Name (SAN) extensions.

Setup custom CA certificate

I started with the Ubuntu certificates guide. It explains how to setup the custom CA. I used the instructions in there, but I decided to use the default Ubuntu CA setup (which used /etc/ssl/demoCA directory).
First I edited /etc/ssl/openssl.cnf. In the CA_default section:
- I changed dir = ./demoCA to dir = /etc/ssl/demoCA (this is so that I can run openssl ca from any directory without entering full paths)
- I uncommented copy_extensions = copy  (this is required so that I can include in certificates SANs from their certificate requests)

In the req section:
- I uncommented req_extensions = v3_req

In the v3_req section
- I added subjectAltName = $ENV::subjectAltName so that I can pass SAN content via environmental variables (I found this trick somewhere on the internet, in order to avoid writing them in config files).


Finally I ran following commands to create the required files:
sudo mkdir /etc/ssl/demoCA
cd /etc/ssl/demoCA
sudo sh -c "echo '01' > serial"
sudo touch index.txt
sudo mkdir private newcerts
sudo chown 700 private newcerts
sudo openssl req -new -x509 -extensions v3_ca -keyout private/cakey.pem -out cacert.pem -days 3650 -newkey rsa:2048

Create a certificate with SAN

As indicated in the Ubuntu docs (and lots of other places):
openssl genrsa -des3 -out server.key 2048
openssl rsa -in server.key -out server.key.insecure
mv server.key server.key.secure
mv server.key.insecure server.key
env 'subjectAltName=DNS:testbox.local' openssl req -reqexts v3_req -new -key server.key -out server.csr
openssl req -in server.csr -noout -text
sudo env 'subjectAltName=DNS:testbox.local' openssl ca -in server.csr 
The signed certificate will be saved in /etc/ssl/demoCA/newcerts/.

Saturday, July 6, 2013

Improving OpenVPN thoughput

I recently faced an unusual problem (for me): an OpenVPN connection going via TCP over a fast network, which had transfered speeds of less than 2KB/s. Although I tested some other options (compression on/off, UDP, etc), none of them proved significantly faster. Moreover, I has hoping to fix the problem with as few as possible changes to clients.
Some time ago, I red about optimizing OpenVPN over Gbit networks. That post covers optimizations related to CPU bottleneck (hardware SSL support, different ciphers, etc). This was certainly not the case for me, since boxes linked via OpenVPN were mostly idle. However, that post has a very brief and accurate explanation and statement about how OpenVPN works, i.e. the data flow between source and destination.
So based on that post, I decided to try mssfix 0 (ie. disable OpenVPN packet fragmenting and leave the kernel/driver do that) and to increase tun-mtu parameter to help test speed improvements. I increased it to 32000 and I got about 150KB/s speed, then to 48000 and I got about 250KB/s, then to 60000 and I got 300KB/s. I thought "what if ...?" and I raised it to 65500. Amazingly, I got average speeds of 1.25MB/s with top speeds above 2.5MB/s.
That was cool, but had one downside: the VPN connection become unstable, with the tun0 device disappearing from the server (probably because the process that managed it crashed).
Reading further in OpenVPN man page and other posts, I decided that I should try to use OpenVPN's mssfix feature. Default value is 1500, but OpenVPN automatically reduces that in order to allow for the SSL data overhead. I decided to be on the safe size and use mssfix 1440. Heuristically, I thought I should use a tun-mtu that is a multiple of that (to help split the data in even fully-filled packets). So I used tun-mtu 64800. With these two settings on, the server proved stable and average transfer speeds went to about 850KB/s (good enough for my needs).
I should not that these transfer speeds were obtained by applying the same settings:
tun-mtu 64800
mssfix 1440
in the client config. If no changes are applied to the client configs, the transfer speed will be still unacceptable low, but still higher than before (about 5KB/s).

Friday, November 2, 2012

Solr replication monitoring

I am using Solr replication on some projects for load balancing (not that Solr really needs that ;) ) and high-availability. Recently, due to a network problem, replication was stalled on one of the slave without me noticing it, so I wrote a small script to check the index version difference between master and slave and which admins can integrate with current monitoring system (nagios, zenoss, munin, whatever).
#!/bin/bash
if [ $# -lt 2 ]; then
 echo Usage: `basename $0` SolrMasterIP[:port] SolrSlaveIP[:port] [AlertThreshold]
 exit 1
fi
# read command params
MASTER=$1
SLAVE=$2
THRESHOLD=${3:-1}

# read index version from master and slave
MASTER_INDEX=`wget -qO - http://$1/solr/admin/replication/index.jsp | awk '/Index Version/ {print substr($3,0,length($3)-1)}'` 
[ -z "$MASTER_INDEX" ] && echo Error reading Master index && exit 2
SLAVE_INDEX=`wget -qO - http://$2/solr/admin/replication/index.jsp | awk '/Index Version/ {print substr($3,0,length($3)-1)}'`
[ -z "$SLAVE_INDEX" ] && echo Error reading Master index && exit 3

# check if master-slave index difference is within acceptable threshold
let "$MASTER_INDEX - $SLAVE_INDEX < $THRESHOLD" && echo Slave index version \($SLAVE_INDEX\) is behind master \($MASTER_INDEX\) && exit 4
echo Slave index version \($SLAVE_INDEX\) is in sync with master \($MASTER_INDEX\)

Sunday, October 28, 2012

Speed up Ubuntu Quantal Quetzal

I recently upgraded my Ubuntu 12.04 Precise to Ubuntu 12.10. Overall, I didn't see much difference, probably because I didn't run/notice the bugs/limitations that were fixed in this new release. One thing that I (subjectively) noticed is that boot up and application startup is slower, with the UI experiencing occasional freezes. I guess these slowness/freezes were already in 12.04, but they become more obvious after the upgrade. To the point that they become frustrating. So I looked into some ways that might speed up the system (not minor or complete tweakings, but just a few to get over the current situation).

Mount options

First thing I tried was adding nodiratime and noatime options to the mount options of / and /home partitions. To do that, just edit /etc/fstab and add these options alongside the existing ones. My fstab looks something like this now:
/dev/sda1 / ext4 nodiratime,noatime,errors=remount-ro 0 1
UUID=..... /home ext4 defaults,nodiratime,noatime 0 2
I didn't notice any major speedup after these changes, but they do seem common sense and lack of speedup might be due to my particular setup (some VMs I'm using).

Install ureadahead

Next I installed the ureadahead package which is supposed to lower boot up times. Install was easy:
sudo apt-get install ureadahead
According to what I read, on first run, ureadahead logs bootup files accesses and then aggregates these files into cached file that will be used in next bootups to speed things up. It also optimizes this cached file according to the type of disk that you have (SSD or HDD). After the system boots, ureadahead process seems to terminate and just gets out of the way.
I didn't notice any major speedup on this either. It could be some, but bootup time is still several times more of the Ubuntu advertised "10 seconds bootup".

Install preload

Preload is a daemon that logs statistics about programs usage and requirements and use these statistics to speed up programs startup. I guess this is what Windows has on by default in that prefetched folder. As mentioned in the man pages, the speed up is not immediately, because preload first needs to collect the stats and create some caches before it can use them. Even so, I did see programs starting faster pretty soon after installing preload. I just installed using:
sudo apt-get install preload

Install prelink

Preling is a tool that improves memory usage and load times by optimizing programs and libraries. Install it using:
sudo apt-get install prelink
After install, you need to start the optimization process. Before I did that, first I edited /etc/prelink.conf to include /opt directory in the optimization process because I have some custome software there. After that, just run

sudo prelink -avmR
The option v is for verbose so that you can see what's processing. You could remove that if you don't care. I guess I'll need to run such a command when I update/install new software, but that's not a big issue.
Although I installed it mostly for the "memory optimizations" promise, Prelink also seemed to help with speed. I'm not sure how much since I installed it after preload which already helped in that area.

Monday, December 7, 2009

National Geographic Wallpapers

National Geographic is offering wallpaper-sized variants of many of their beautiful photo collections. One of the collections that I follow in iGoogle is the Photo of the day. I like it mostly because it doesn't have a particular theme and includes photos from many places around the world.
From time to time, I like some picture so much that I "go through all the trouble" (a 5-clicks process) and save it as my wallpaper.
I wanted something automated to do this (i.e. save the POD as my wallpaper). I know there is Opal (both for Windows & Linux), but I don't want to use it for two reasons:
  1. when I last tested it, it failed to work (i.e. it did not set any wallpaper)
  2. I fail to see the purpose of running a program full time (even if it's using almost none CPU & memory) just to update a file one time a day
Thus, I decided to practice my bash scripting & linux command skills to write a script that will save the National Geographic POG and set it as wallpaper in Gnome. This is what I came up with (it is not and will not be optimized nor use error control):
#!/bin/bash

# the URL of the photo feed
RSS_FEED=http://www.nationalgeographic.com/rss/photography/photo-of-the-day

# the prefix of the photos URL (used to find the newest photo)
IMAGE_URL_PREFIX=http://photography.nationalgeographic.com/staticfiles/

# format of the photo
# in = 270x179, sw = 800x600, lw = 1024x768, xl=1280x1024
IMAGE_FORMAT=sw

# directory where to save the images
IMAGE_DIRECTORY=~/Pictures

# retrieve the RSS feed, grep for the latest image and
# replace it's name to match the required format
IMAGE_URL=`wget -qO /dev/stdout $RSS_FEED | egrep -m 1 -o "$IMAGE_URL_PREFIX[^\"]*" | sed -e s/-in\.jpg\$/-$IMAGE_FORMAT\.jpg/`

# retrive the image
wget -q -nc -P $IMAGE_DIRECTORY $IMAGE_URL

# get the image file name
IMAGE_FILE=`basename $IMAGE_URL`

# set the image as background
gconftool -s -t string /desktop/gnome/background/picture_filename $IMAGE_DIRECTORY/$IMAGE_FILE
gconftool -s -t string /desktop/gnome/background/picture_options scaled

Wednesday, November 11, 2009

Karmic Koala doesn't like my display

Yesterday I installed Karmic Koala on my office PC. I had a Jaunty installation before that I did not used/customize much so I figured there's not much for me to gain in an upgrade. Thus, I did a fresh install from a 4GB USB stick. The installation went very well, taking less than 15 minutes boot-to-reboot. Another thing I liked is that there was no need to unplug & replug the USB stick during the installation (this was required by Jaunty and earlier versions).
The first things I noticed is that overall the system is much nicer than Jaunty:
  • the icon theme has greater details and nicer design,
  • it has up-to-date packages not available in Jaunty (like Octave 3.2),
  • start up time has lowered (not at much as others say, but then again, my PC is not cutting edge),
  • the new IM client is empathy (I'm using it right now, but I'm not sure I like it better than Pidgin).
This means that upgrading my home PC will be a step forward (that I can't do right now because of other work).
One thing that I then noticed as annoying was the slow resolution (800x600) and the incapacity to make it larger. I then realized this happens in Xubuntu 9.10, too. Therefore it's not related to Xubuntu (as I thought before).
I spend some time on forums, trying to find a direct solution for this problem only to find out that something changed in X and something else changed in the kernel, and one is using the other, and there's another link point to another 5 pages discussion and then another link, and so on.
One thing that I got by browsing though all these forum posts was this: X in Ubuntu Karmic Koala comes with no xorg.conf because it's suppose to autodetect the video card and display settings. It does not happen in my case. I blamed the video card.
Next thing I found useful is that I can make X write a configuration for me using the command:
X -configure
This is new stuff for me, because I used x86cfg (or something similar) to do this way back (like 2-3 years ago). Anyway, this creates a file called xorg.cfg.new in my home directory which I then moved into /etc/X11, renamed it to xorg.cfg, and restarted the Gnome Display Manager (gdm) service.

This did not solve the problem either. So I went and looked into /var/log/Xorg.0.log to see if I get some pointers on whats happening. I saw a lot of messages like this:
(II) intel(0): EDID for output VGA1
(II) intel(0): Not using default mode "640x350" (vrefresh out of range)
(II) intel(0): Not using default mode "640x400" (vrefresh out of range)
(II) intel(0): Not using default mode "720x400" (vrefresh out of range)
... and so on

Then I realized that it's not the video card, but the display that is not being detected correctly. So I added the following lines to my /etc/X11/xorg.conf (I found something similar on forums) inside the "Monitor" section:
HorizSync 28-64
VertRefresh 43-75

This did the trick and after restarting gdm, I got a wonderful big resolution (which I trimmed down to 1024x768).

Sunday, November 8, 2009

I'm hooked to Linux

These days I "celebrate" 1 year since I switched to Linux as my main operating system. Before I continue, I have to say that I still use Windows XP at my office (older PC) and occasionaly at home (mostly for existing Texnic Center projects and Microsoft Excel).

I started using Ubuntu on a daily basis about 1 year ago (by upgrading from the until-then unfrequently used 8.04 to 8.10). I used Linux before but just to see what's new in it:

  • starting in 2002 with Mandrake 8 (I started by formatting by mistake the entire disk instead of the future home partition);
  • moving then to Mandriva Free 2004 (former Mandrake);
  • tried Ubuntu 6 (that I got from Tudor);
  • returned to Mandriva Free (2006, I believe);
  • played with Fedora (which I didn't like because I wasn't fond of Gnome);
  • tested Kubuntu 7.10 (because of KDE);
  • moved to Kubuntu 8.04 (which I didn't like because of KDE 4);
  • installed Ubuntu 8.04 (but didn't used it much);
  • upgraded to Ubuntu 8.10 (and switched to Linux).

I liked Ubuntu because I found it very easy to install applications on it. Since I do a lot of development, I also use the terminal quite frequently and the command line completion features also helped me a lot in my first days (and they still do). Overall, I'm very happy with it, although it requires more RAM than Windows XP to run smoothly (especially when you start Office applications). Currently, my 1GB RAM (don't laugh!) is doing it's job, but I ordered more RAM on eBay (that's also a premier for me). I had my ups-and-downs with Ubuntu (like getting Skype to work), but it's been fun to learn. Some things (like getting the webcam to work), I didn't figure out until now and I'm still wasting time trying to fix it.

Since I switched to Ubuntu, I kept on looking to other distributions, too, but I always returned to Ubuntu:

  • Xubuntu - I like it (especially because it's simple UI), but i find it limited (e.g. mousepad vs. gedit) and buggy (on my Dell Inspiron laptop it doesn't allow dual-displays, on my Maguay office PC is allows 800x600 pixels, etc).
  • ArchLinux - didn't finish installing it: I'm a techie user and I used more or less many OSs (including CP/M, MSDOS 5, Windows 3.1, Windows 95, QNX, etc), but I never liked doing more task that are required (these days, this means typing long cryptic commands that are hard to figure out or reproduce unless you use them daily or you have a wiki page in front of you)
  • LinuxMint - I liked it a lot especially because the launch menu (Ubuntu System Panel), but except for that, it felt too close to Ubuntu to make the switch. Instead I installed its launch menu in Ubuntu.
  • openSuse - nice but less friendly than Ubuntu. It also has a nice launch menu which looked cool the first time I tested it, but last week I checked it again and it felt just annoying (just like Vista and KDE4's menu). Too many clicks to open an application.
  • OpenSolaris - No, no, no. I don't have that much RAM to throw at the OS. I need RAM for my applications. Unfortunatelly, it's like comparing Visual Studio with Eclipse. One is fast and memory efficient, the other is not (but I use Eclipse).

Sunday, August 2, 2009

Octave files misdetected as Objective-C

As you may noticed from the previous post's screenshot, GEdit misdetects my Octave .m files as Objective-C code files (because both share the same file extension). Because I don't use Objective-C, I tried to fix this problems like this:

  1. I edited the file /usr/share/mime/packages/freedesktop.org.xml, commenting out the mime-type tag with type text/x-objcsrc (just search for text/x-objcsrc and you'll find it).
  2. After saving this file, I ran the following command:
    sudo update-mime-database /usr/share/mime/
  3. Last, I restarted GEdit.

It didn't seem to work, so I did the following thing:
sudo rm /usr/share/gtksourceview-2.0/language-specs/objc.lang.
This worked fine.

Octave taking over Matlab

I'm trying to replace my Matlab R12/Windows habit with Octave 3/Linux. I installed the QtOctave IDE, but it seems to bulky (especially because I use Gnome). I didn't liked it, so I uninstalled it.

Next I tried another approach: I installed the gedit-plugins package which contains the embeded terminal plugin and enabled the filesystem and embeded terminal plugins. My GEdit window setup looks like similar to Matlab's default view, except for the workspace and history sidepanes (which I didn't use much anyway).

I also added the file .octaverc in my home directory and added this line to it:
edit EDITOR 'gedit %s'
This command makes Octave's edit command to open files in the gedit (instead of default emacs).