Sunday, July 29, 2012

Migration to systemd on Arch

Today I migrated to systemd on my Arch system. All the mortals unaware of the word, please allow me the privilege to introduce systemd - the latest replacement PID 1 in your linux box. As you might be aware that after the kernel loads up, it launches init in the inter-stellar space with the process ID of 1 which is responsible for forking other payloads into the user space. The traditional SysV Init boot method was conceived decades ago and has been used almost unaltered in most of the linux distros. It was excogitated in the age when processors were babies with single cores which could compute ONLY a few million instructions a second, the computers had only crossed the stone age and had just stepped into the semiconductor age(the second part of this statement is technically wrong, take it just as a humorous piece). SysV Init starts the userspace processes one by one. But today in the age of multicore monsters with each of the 4(or 6) cores capable of simultaneously computing at a staggering speed of  more than a couple of billion instructions per second coupled with architectural innovations like simultaneous multithreading(SMT i.e. hyper threading in intel terms), advance tournament branch predictioning, added with speculative execution and deep pipelining that makes sure an application cleverly programmed can run almost as fast as the demarcation set by Amdahl's law; Sys V init is very inefficient since it's coders hadn't dreamt of these features in their weirdest of day(night)dreams.
Then some bored lazy guy named Lennard Pottering at Fedora fed up of waiting for the age old system boot process to load his OS had a mesmerizing realization of an Archimedes like enlightening idea - to boot a system fast, start less; and start more parallelly. And he came up with systemd along with some helping hands in the community. Although Ubuntu gave up on SysV Init long ago and took the upstart way, but systemd is any day any moment better than upstart.
systemd organises services and other things(beyond the scope of this post) into units. An assortment of one or more units can be grouped into a target, which is similar to the concept of runlevels but is not exactly run level. A target can be a requirement for another target(i.e. latter inherits from former). And systemd starts the units as parallelly as possible by making sure independent units can start together and a unit dependent on another is held on, until its dependency is started.
Enough of background story and theory! Lets see how easy it is to set it up on Arch. Archwiki has an awesome page for it, but n00bs are n00bs and are born to make mistakes and do things that they are not supposed to. Even non-n00bs can make silly mistakes :P
Install the packages systemd, systemd-sysvcompat, systemd-arch-units after removing the packages sysvinit and initscripts. But before removing initscripts, you may want to take a backup of /etc/rc.conf. Afterwards you should make a few files which are expected by systemd. They are /etc/hostname, /etc/vconsole.conf, /etc/locale.conf, /etc/timezone
# cp /etc/rc.conf /etc/rc.conf.bak
# pacman -Rns sysvinit initscripts
# pacman -S systemd systemd-sysvcompat systemd-arch-units
# echo `hostname` > /etc/hostname
In the file /etc/vconsole.conf write
KEYMAP=us
FONT=lat9w-16
FONT_MAP=8859-1_to_uni

and in the file /etc/locale.conf write

LANG=en_US.UTF-8
LC_COLLATE=C

and lastly in the file /etc/timezone enter your timezone, for example:

Asia/Calcutta

If you dual boot with Windows then to keep your linux system showing localtime write the following in /etc/adjtime

0.0 0.0 0.0
0
LOCAL
You should also uncomment the en_US-UTF8 UTF8 and en_US-ISO8859-1 lines from /etc/locale-gen and then run the locale-gen command on the terminal as root user to avoid locale warnings while next time an mkinitcpio is done or something else that requires it.
If you load some modules at boot time by adding them in /etc/rc.conf then to do the same with systemd, you can create a file named /etc/modules-load.d/modules.conf and add all your modules names each in a single line.
For example:

vboxdrv
vboxnetflt

If you run some services from /etc/rc.conf at boot time, then you can use:

# systemctl enable sshd.service

to keep starting them at boot. These service configs are in /usr/lib/systemd/system/ directory and are the units as introduced somewhere above in this post. All the files with extension .service are daemon units that you can run at boot or for certain targets, or later by the systemctl command.

In case of some services, for example networkmanager you will see an  error if you try to do a systemctl enable over it:
Failed to issue method call: No such file or directory

Don't panic. Just create a symlink in /etc/systemd/system for that file in multi-user.target wants.

Example:
# ln -s /usr/lib/systemd/system/networkmanager.service /etc/systemd/system/multi-user.target.wants/networkmanager.service

This may not exactly work in some cases if the daemon you want to start requires other daemons to be enabled too. In that case you can enable the required daemon first, or the required target first.

Hope, this should get you up and running with systemd on your arch box.
One more thing, systemd provides a nifty tool to analyse your boot time. Try these commands, and you will know what I mean:

$ systemd-analyze
$ systemd-analyze blame | less
$ systemd-analyze plot > ~/bootplot.svg
By the way my Fujitsu Siemens Amilo Si3655 with Core2Duo P8400 2.26GHz, 320GB hard disk, 4 gigs of RAM and pae kernel boots in less than 13 seconds. Here is my output from systemd-analyze.
Startup finished in 656ms (kernel) + 2521ms (initramfs) + 9648ms (userspace) = 12826ms
And here is my plot svg file.

References:
https://wiki.archlinux.org/index.php/Systemd
http://0pointer.de/blog/projects/systemd.html
http://0pointer.de/blog/projects/why.html
http://freedesktop.org/wiki/Software/systemd
http://distrowatch.com/table.php?distribution=arch

Notes:
1. Commands prefixed with # are to be run as root user, and ones prefixed with $ are to be run as normal user.
2. If you are not an arch user, please don't try to install systemd by following this article word by word. Fedora and OpenSuse come with systemd by default. Other distro users should check corresponding wiki and forums.

Monday, July 2, 2012

Fix disfunctional shutdown/restart button in Arch Linux

Arch Linux is one awesomely awesome operating system! You get a chance to build your own Linux system from scratch, right from installing the kernel, then installing core-utils, followed by editing config files and then setting up X and a Desktop Environment. I will post sometime later an step by step verbose article on building a system with Arch + XFCE + General utilities. For now I'll focus on the title of this post.

Those of you who run Arch and start your GUI with:

exec ck-launch-session dbus-launch startxfce4

in xinitrc and use SLIM might have noticed that you can't shutdown with the buttons available in your Desktop Env.

The reason is that the updated version of SLIM already launches a console kit session(the thing responsible to execute commands that require root access). So there are two console kit sessions and the conflict.
You can check the number of console kit sessions yourself by:
ck-list-sessions
To solve it, remove the portion ck-launch-session dbus-launch from the exec line.
Here is my .xinitrc, that you may use.

#!/bin/sh
# ~/.xinitrc
# Executed by startx (run your window manager from here)
if [ -d /etc/X11/xinit/xinitrc.d ]; then
  for f in /etc/X11/xinit/xinitrc.d/*; do
    [ -x "$f" ] && . "$f"
  done
  unset f
fi

DEFAULT_SESSION=startxfce4
case $1 in
  lxde)
  exec startlxde
  ;;
  xfce4)
  exec startxfce4
  ;;
  *)
  exec $DEFAULT_SESSION
  ;;
esac


/usr/bin/xscreensaver -no-splash &

Friday, October 28, 2011

Setup transparent net access over insti proxies

If you are a student at an institute that forces you to cross a damn proxy server for accessing internet, you must be fed up of entering your username and password time and again in browsers, as well as configuring each new application you install, to use the proxy. And if an application doesn't have any proxy configuration option, then you can't use that application! For example you can't install the Android Development plugin for eclipse. This is very irritating. And, some campuses add to this torture further by using crappy NTLM authentication.
Here in IIT Madras exactly this condition prevails. But here I present a step by step walk through to get rid of this situation. Welcome to transparent proxying!

What is transparent proxy?
A transparent proxy(correctly called intercepting proxy) intercepts your requests and forwards to the destination without letting your applications know that there is a proxy between you and internet. This is done with some intervention from iptables in Linux. So what are the advantages? You need not configure each and every application with the proxy address, port, username and passwords. You can keep the setting "Direct connection to internet" in every application and every darn-so-ever app will work flawlessly!!!! Cool?? Eh?? Lets start.

Configure a transparent proxy on your notebook
I assume you are using a recent version of Ubuntu. Even older ones will do :) The steps should work for any Linux distro provided you use appropriate distro specific commands.

Before installing software you should have a working internet connection. You may setup proxy in /etc/apt/apt.conf for the time being to install required software for now. You need to put the proxy details in apt.conf as follows for https, https and ftp:

Acquire::http::Proxy "http://<username>:<password>@<proxy-address>:<port>


An example:

Acquire::http::Proxy "http://cs11m039:bleeeep@proxy.iitm.ac.in:3128";
Acquire::https::Proxy "https://cs11m039:bleeeep@proxy.iitm.ac.in:3128";
Acquire::ftp::Proxy "ftp://cs11m039:bleeeep@proxy.iitm.ac.in:3128";


If you are not from computer science background then I should tell you that, to edit /etc/apt/apt.conf you should open terminal then type sudo gedit /etc/apt/apt.conf

Step 1: 
If your campus doesn't require NTLM authentication then you can simply install and configure squid to setup transparent proxying. The details for that are beyond the scope of this post and I may cover that sometime later or you may search the net for other writers who might have posted the same.
IIT Madras campus network has NTLM. So I need to install cntlm. It works as a proxy and performs NTLM authentication on your behalf so you don't need to enter username and password everytime ypu lauch the browser. Type the following command and press enter:
sudo apt-get install cntlm 
Now edit /etc/cntlm.conf file and edit these lines to reflect your settings:

Username cs11m039
Domain iitm
Password password
If you have different proxies in institute and hostels then you can specify them all in these lines



Proxy 10.93.0.38:3128
Proxy 10.93.0.34:3128
Edit socks proxy details too

SOCKS5Proxy 3128
SOCKS5User cs11m039:password

Leave all other settings as default. Instead of cs11m039, put your username and instead of password put your password. Instead of domain as iitm put your institutes NTLM domain. For SOCKS5Proxy put the port number of proxy.

Now restart cntlm by typing:

sudo service cntlm restart

Now open any browser and set the http proxy as 127.0.0.1 and port 3128. Try opening google.com. If it works, means you got cntlm installed perfectly. Proceed to next step. Else, double check your configuration. Make sure cntlm got installed. Also make sure if your computer is even turned on!!!!! If all else fails, write a comment here and I may reply if I feel like.(:P)

Step 2:
Install tinyproxy.
sudo apt-get install tinyproxy

Open its configuration file

sudo gedit /etc/tinyproxy.conf 

Edit these lines:
Port 8888
Upstream 127.0.0.1:3128
Comment the line that says Allow 127.0.0.1 by adding a leading # symbol before Allow.
Don't touch any other line.

Step 3:
Open /etc/default/tinyproxy and put this inside and close the file:


case "$1" in
  start)
    iptables -t nat -A OUTPUT -p tcp --dport 80 -j REDIRECT --to 8888
    ;;
  stop)
    iptables -t nat -F OUTPUT
    ;;
  restart)
    start-stop-daemon --stop --quiet -t --exec $DAEMON > /dev/null || exit 0

    iptables -t nat -F OUTPUT
    iptables -t nat -A OUTPUT -p tcp --dport 80 -j REDIRECT --to 8888
    ;;
esac


Now restart tinyproxy by typing sudo service tinyproxy restart 
Disable proxy settings in the browser and try opening a website. If you followed all the above steps correctly then the website should open. Also, you may now safely truncate the /etc/apt/apt.conf file and it will work. All your apps like installing software in Eclipse will work too without any problems.

This is how your internet is working now:

Your application requests for internet access which is received by Linux kernel and due to the iptables rule setup above the request is redirected to port 8888 where tinyproxy is running. Tinyproxy intercepts the non-proxyified request and makes it http-proxy request compliant then forwards it to upstream proxy which is cntlm running on port 3128. Cntlm then performs NTLM authentication and contacts the insti proxy server to serve your request.

User application<==>Kernel(iptables)<==>tinyproxy intercepting proxy<==>cntlm authentication proxy<==>insti squid proxy<==>The internet

Happy browsing!

Update
On 3rd November, IIT Madras ditched the NTLM authentication and implemented rather insecure HTTP Basic authentication with OpenLDAP. Now you don't require cntlm if you want to setup transparent proxy.

In step 1 above don't install cntlm. Just install polipo by:
sudo apt-get install polipo

Then open its configuration file by:
sudo gedit /etc/polipo/config

And set these values:
parentProxy = "hproxy.iitm.ac.in:3128"
parentAuthCredentials = "username:password"

where username and password should be your username and your password for OpenLDAP.Also make sure to comment the lines that stand for SOCKS proxy.
Also set its listening port to 3128, so that tinyproxy doesn't need to be reconfigured. And don't forget to uninstall or disable cntlm(if it is installed) from startup because both polipo and cntlm will try to capture port 3128 for listening and you may face issues.




Thursday, September 29, 2011

My experience with CentOS 6

And finally I decided to switch to 64 bit computing! Motivated by my Parallel Computer Architecture course (in which I'm underperforming) I decided to install CentOS 6 X86_64 version. IIT gives enough internet speed in the campus (if only you know how to tap into :P) that I ended up downloading the two bin-DVD ISOs as well as LiveCD and LiveDVD ISOs totalling around 8GB in less than 2 hours from http://ftp.jaist.ac.jp/pub/Linux/CentOS/6.0/isos/x86_64/. Then I used the cool dd command to replicate the LiveDVD ISO onto my pen drive.

dd if=CentOS-6.0-x86_64-LiveDVD.iso of=/dev/sdb bs=1K

Don't forget to
umount /dev/sdb1
before issuing the above command.

So, after getting the bootable pen drive I restarted my notebook and pressed F12 to alter the boot order to select the pen drive instead of hard disk. The LiveDVD environment booted and I clicked on autologin letting it boot to GNOME by default. The interface was pretty fast. In fact much faster than Ubuntu because there is not much of eye candy at your disposal in CentOS by default. One irritation I had, was that I couldn't click by tapping the touchpad. I had to press the touchpad's left button to make the clicks. After some fiddling I found out in System>Preferences>Mouse in the Touchpad tab the option to Enable mouse clicks with touchpad. Then I enabled Desktop Effects and tested the Desktop on a cube of Compiz that pumps in zeal in linux fanboys and jeal in Windoze fans! Everything seemed to work perfectly, and I decided to go ahead and replace my Backtrack 5 installation with CentOS6. I launched the Install to hard drive installer and chose create a cutom layout in disk partitioning to specify my own partition to install and selected the Backtrack 5 partition and formatted it. After clicking the final install button, to my utter dismay the installer blurted something very nasty! The installation media is corrupt and installer can't proceed! Damn!!!!!!!!!!
I rebooted to find that my Backtrack 5 partition was wiped out. I booted into Ubuntu and used dd to replicate the bin-DVD1.iso image into my USB pen drive. After a wait of around 15 minutes my bin-DVD1 installer was ready. I rebooted and chose USB boot after F12 but this time it said: Operating system not found!!! Damn again!
After booting back to Ubuntu I replicated the LiveCD.iso into USB disk and re-booted into the LiveCD environment without any issues. I checked my system information and was proud to see 4GB of installed RAM! Both my 32-bit OSes viz. Windoze and Ubuntu 11.04 let me use only 3GB(approx.) RAM because of that damn memory mapping for I/O devices. Anyways I launched the installer and started following the wizard. During the bootloader setup screen I saw only Windoze was detected and named as Other. I added an entry for Ubuntu as well and edited the Other to read Windoze. I proceeded with the installer and after setting up all configs pressed the final install button. To my surprise, the install took less than 3 minutes!!!! Yes!! Thats right!
Then I rebooted and after a couple of formalities booted straight into lean and thin and clean upto the brim desktop environment of GNOME with very basic set of apps like GIMP, OpenOffice etc. Next thing I did was to install the multimedia codecs. I issued the following command
yum install gstreamer-plugins-base gstreamer-plugins-good gstreamer-plugins-bad gstreamer-plugins-ugly gstreamer-ffmpeg
For a user switching from Ubuntu it was nice to see that the package names are consistent across distros. Anyways I noticed that the gstreamer plugins from the base set and good set were already installed and yum started downloding the rest of them.
Then I installed vlc media player by
yum install vlc
When I tried typing gcc and g++ on the terminal I got command not found. Same happened for java but python was installed and worked. They don't provide everything in the CD due to space constraint. So I went ahead to install these myself. yum install g++ didn't work.
After some fiddling I noticed that I could hear sound when I plugged in the earphones but after plugging them out I couldn't hear any sound through speakers. Exactly same problem I faced in last october with Ubuntu and had posted at http://www.techbite.in/2010/10/realtek-alc888-sound-issues-in-ubuntu.html but I couldn't use the same solution for CentOS because there was no /etc/modprobe.d/alsa-base.conf in CentOS. I tried the locate command but in vain. If you know how to solve this issue please comment here.
Next I updated /etc/fstab and entered the entries for auto mounting of my Windoze partitions and Ubuntu partition. But before that I had to installl ntfs-3g because that would let my CentOS access NTFS partitions.
Anyways I went ahead with my installation streak and downloaded flash plugin 11 rc from Adobe's site for 64 bit architecture and extracted it and copied the libflahplayer.so to ~/.mozilla/plugins directory and my firefox started to show YouTube videos after a restart. My installation streak is still going on and the apps waiting in the queue for installation are:
compiz
w3m
aria2c
chrome
opera
nmap
g++
lftp
openssh
ettercap
apache
php
mysql
audacious
skype
gnuplot
vidalia

After the installation of these I'll have a relaxed sleep because I have a 4 day holiday!! That will be enough for me to spend nice time with CentOS6.

Update(Sep29 9:30AM IST):
I added these two lines to /etc/modprobe.d/dist-alsa.conf to got the sound from speakers working after a restart.
alias snd-card-0 snd-hda-intel
options snd-hda-intel model=auto
Other issues I faced so far are
1. Upon trying to install byobu-3.21-1.el6.noarch.rpm it says it needs python 2.6. But I already have python 2.6.5 installed
2. To run the ADT plugin for eclipse I require 32 bit libs support. Can't figure out how to do that. In Ubuntu 64 bit, one just have to install ia32-libs using apt-get.
3. Can't install audacious through yum. It says audacious-plugins package not found.
4. Installed compiz but can't find ccsm
5. Can't find aria2 through yum. I guess I've to hunt for a manual download of rpm instead.

Update(Oct 1 07:30PM IST)
I downloaded JDK 7 from here http://www.oracle.com/technetwork/java/javase/downloads/java-se-jdk-7-download-432154.html and installed it. It worked perfectly. But after that I used yum to install Eclipse IDE which in turn installed OpenJDK and made it the default java. I hate OpenJDK and want only Sun JAVA. I tried:
update-alternatives --config java
But Sun Java wasn't listed there. There were two javas listed there. One was Open JDK 1.6 and other was gcj 1.5. I played some hide n seek and found out that Sun Java was installed in /usr/java/ and there were three directories there named jdk1.7.0, latest and default. Then after some fiddling with the man page of update-alternatives command I issued these three commands:

update-alternatives --install "/usr/bin/java" "java" "/usr/java/default/bin/java" 3
update-alternatives --install "/usr/bin/javac" "javac" "/usr/java/default/bin/javac" 3
update-alternatives --install "/usr/bin/javaws" "javaws" "/usr/java/default/bin/javaws" 3

And then running this:
update-alternatives --config java
showed me Sun Java as the third option. Then I set it as the default java and my problem was solved.

To get started with server administration in CentOS if you have some basic Linux background, you should read the CentOS Server Administration Cookbook.