Wednesday, December 2, 2009

Bash environment memo: profile, bash_profile & bashrc

Abstract: I'm trying to explain the process of bash setup in this article.

Keywords:bash, shell, environment

It is usually hard to answer the question where this variable is set, because there is SO many different files involved. So let's start with some concepts:

interactive login shell
The shell started after a successful login, using /bin/login, by reading the /etc/passwd file.
interactive non-login shell
It is normally started at the command-line using a shell program (eg.$/bin/bash) or by the /bin/su command. It is also started with a terminal program within a graphical environment.
non-interactive shell
It's usually used to execute a shell script

An interactive login shell generally initializes with the following configuration files:

/etc/profile -> [~/.bash_profile] -> [~/.bash_login] -> [~/.profile]

In some systems, the /etc/profile script will look for additional settings in /etc/profile.d/*.sh, and the ~/.bash_profile usually sources ~/.bashrc.

When an interactive non-login shell starts, it first copies the environment of its parent shell and then reads its settings in the following order:

/etc/bash.bashrc -> [~/.bashrc]

Remember that only exported variables of its parent shell is copied, functions and aliases of its parent are NOT inherited.

A non-interactive shell simply copies environment from its parent, and does not read any configuration files.

Thursday, November 5, 2009

Mount internal filesystem without passwd with udisk (formerly devicekit-disk)

Abstract: Use policykit to avoid root password for mount an internal filesystem.

Last week, I updated my Arch Linux laptop, and got the new deviekit-disk. Then I found that the authorizations set with gnome interface, which actually sets theorg.freedesktop.hal.storage..., stops work. I have to give the root passwd when I try to mount an internal filesystem, which is really bothering:(

Luckly it is not hard to solve this problem with Policykit, though there dosen't seem to be a sweet GUI to help. All you have to do is simply create a text file named anything with suffix .pkla in /etc/polkit-1/localauthority/50-local.d/ and fill it with the content below:

[filesystems mount internal privs]
Identity=unix-group:disk
Action=org.freedesktop.devicekit.disks.filesystem-mount-system-internal
ResultAny=no
ResultInactive=no
ResultActive=yes

In case you have udisks2 instead of udisks, the action should written like this:

Action=org.freedesktop.udisks2.filesystem-mount-system;org.freedesktop.udisks2.filesystem-mount

Here I allow all users in group disk to mount internal filesystem without root password. disk is the default group of storage devices, you should add yourself to this group. Otherwise you can specify your login name:

Identity=unix-user:yourname

To see all possible actions, check /usr/share/polkit-1/actions directory.
You can always man pklocalauthority for more info:)


Update (May 22nd 2012)

Udisks2 uses different action name.


Update (Oct 25th, 2011)

Latest udev will set storage devices of group disk. So I specify group disk instead of storage now.

PS: I moved to a new blog.


Update (April 3rd 2010)

People from freedesktop.org are making a great progress - by renaming devicekit-disk as udisk (Sounds talking about flash disk, isn't it?)... So you got to change Action=org.freedesktop.devicekit.disks.filesystem-mount-system-internal to Action=org.freedesktop.udisks.filesystem-mount-system-internal.

Dear sirs/madams from freedesktop.org, will you please stop renaming things? It's not fun, at all :(


Update (April 16th, 2010)

It seems /etc/polkit-1/localauthority/50-local.d/ is a better place to apply your own settings.


Update (Nov 2nd, 2010)

The configuration file has to be named with suffix .pkla to be recognized, for more details you can man pklocalauthority. And lots of thanks goes to Awebb who kindly points this out :)

Sunday, September 27, 2009

looking for a web software for knowlege sharing

I am currently looking for a web-based software for knowledge sharing in our lab, I've investigated some blog, cms and also wiki softwares, but none seems to fit :(

We just want something which is easy to setup and maintain, and can organize different kind of materials. CMS's are powerful, but relatively hard to used...Wiki seems to lack some kind of structure. Blogs are easy to use, but does not organize resources well.

So what do you use in your lab, my dear readers?

Wednesday, September 16, 2009

QEMU "host only network"

Abstract: Setup an LAN network of only the host and guest.

Recently I'm trying something on qemu and want to access the guest from the host more freely than user mod port redirect, but I do not want to expose my guest to the host network. All I did is to wrap everything into a script:

#!/bin/sh

HOSTIP="192.168.1.250"
GUESTIP="192.168.2.10"
NETMASK="255.255.255.0"
USERID=`whoami`
IFNAME=`sudo tunctl -b -u $USERID`

sudo ifconfig $IFNAME $HOSTIP netmask $NETMASK up

echo ":: Host IP is $HOSTIP"
echo ":: Run the follwing command in guest to set IP and gateway:"
echo " # ifconfig eth0 $GUESTIP"
echo " # route add default gw $HOSTIP"
echo ":: Starting QEMU with TUN/TAP network..."

qemu -hda webdev.qcow \
-boot c -m 256 \
-k en-us -localtime\
-net nic,vlan=0,model=rtl8139 \
-net tap,vlan=0,ifname=$IFNAME,script=no,downscript=no

sudo tunctl -d $IFNAME
HOSTIP="19

Btw, I find sticky note gadget on iGoogle a great tool to share a couple of lines between machines - I'm writing on my Windows laptop and the script is in my Linix desktop, just a few paste and copy brings the script here :)

Monday, September 7, 2009

VIM Modeline is your friend

Abstract: Add modeline to a file can help VIM remember file-specific options.

I've got to port some application to another arch these days, so I have to deal with a lot of files that does not follow my usual coding standards, and I don't want to edit my VIM settings now and then, so I turned to modeline for help.

To add modeline to all the files, I simply did a find & echo like this:

for f in `find . -name '*.cpp' -o -name '*.h' -print0| xargs -0i echo {}`;do echo -e "\n/* vim: set ts=2: */" >> $f;done

Saturday, September 5, 2009

How to insert formatted date in VIM

Abstract: Remember to escape the percent sign

I find that I cannot simply prefix a r! before plain date command to insert a date in VIM today, When I tried to insert date with

:r !date +%u

I got an error, the reason lies in the percent sign, which stands for the current file in VIM ex-mode. For example, you can count the current file like this:

:! wc %

Thus to insert a formatted date, you have to run:

:r !date +\%u

Thursday, August 13, 2009

Good Book: CERT C Secure Coding Standard

Abstract: a good reference book on coding in C

It's a good reference book on coding nicely in C, which is the most dangerous programming language I know except for brain-f*ck...

Another good point is that you can read it free online: https://www.securecoding.cert.org/confluence/display/seccode/CERT+C+Secure+Coding+Standard.