2011. november 14., hétfő

Git info in bash prompt

I modified an existing script to have coloured git info on bash prompt. The original version:

https://gist.github.com/738048

The result (I use black background in gnome-terminal):

kecsap@sakura:~/something/something[master ]$

My script:

RED="\[\033[0;31m\]"
YELLOW="\[\033[0;33m\]"
GREEN="\[\033[0;32m\]"
BLUE="\[\033[0;34m\]"
LIGHT_BLUE="\[\033[1;34m\]"
LIGHT_RED="\[\033[1;31m\]"
LIGHT_GREEN="\[\033[1;32m\]"
WHITE="\[\033[1;37m\]"
LIGHT_WHITE="\[\033[1;37m\]"
LIGHT_GRAY="\[\033[0;37m\]"
COLOR_NONE="\[\e[0m\]"

function parse_git_branch {

git rev-parse --git-dir &> /dev/null
git_status="$(git status 2> /dev/null)"
branch_pattern="^# On branch ([^${IFS}]*)"
remote_pattern="# Your branch is (.*) of"
diverge_pattern="# Your branch and (.*) have diverged"
if [[ ! ${git_status}} =~ "working directory clean" ]]; then
state="${LIGHT_RED}⚡"
fi
# add an else if or two here if you want to get more specific
if [[ ${git_status} =~ ${remote_pattern} ]]; then
if [[ ${BASH_REMATCH[1]} == "ahead" ]]; then
remote="${YELLOW}↑"
else
remote="${YELLOW}↓"
fi
fi
if [[ ${git_status} =~ ${diverge_pattern} ]]; then
remote="${YELLOW}↕"
fi
if [[ ${git_status} =~ ${branch_pattern} ]]; then
branch=${BASH_REMATCH[1]}
echo "${LIGHT_WHITE}[${COLOR_NONE}${branch} ${remote}${state}${LIGHT_WHITE}]${COLOR_NONE}"
fi
}

function prompt_func() {
previous_return_value=$?;
prompt="${USER}@${HOSTNAME}:${TITLEBAR}${LIGHT_GRAY}\w${GREEN}$(parse_git_branch)${COLOR_NONE}"
if test $previous_return_value -eq 0
then
PS1="${prompt}\$ "
else
PS1="${prompt}${LIGHT_RED}\$${COLOR_NONE} "
fi
}

PROMPT_COMMAND=prompt_func

2011. november 13., vasárnap

Moving LVM/LUKS partitions into a new hard drive

I received a new hard drive with bigger capacity and I wanted to move my old Ubuntu installation to the new drive. I connected both disks to a computer and boot from a LiveCD. I tried to use a simple dd command to copy the whole disk:

dd if=/dev/sda of=/dev/sdb

...but it failed to copy the encrypted (LVM/LUKS) partitions. I could unlock the partitions, but the new copy of the encrypted ext4 file system was half-destroyed during the copy process. I changed strategy since the new drive is bigger than the old one, I booted from the LiveCD and installed a new encrypted system with the maximal available space. I booted with the LiveCD again, mounted both disks, deleted the content of the new system and copied the old content to the new, unlocked partitions. I changed the grub/fstab and other config files to use the new UUIDs of the partitions, but the new hard drive did not boot. After updating the grub+initramfs, a strange thing happened. The system did not ask for passphrase to unlock the LVM, but I was dropped to the initramfs debug shell. I could unlock the encrypted partition and leave the debug shell, but the system wanted to ask for the passphrase after that. I found the solution on this Mint Linux forum topic: http://forums.linuxmint.com/viewtopic.php?f=189&t=83763

The solution was pretty easy: Delete the /etc/crypttab and create a new file /etc/initramfs-tools/conf.d/cryptroot with the following content:

CRYPTOPTS=target=sda5_crypt,source=/dev/sda5,lvm=LogicalVolumesOnEncryptedPartition-Root


It rocks now.

2011. november 1., kedd

Changing the Dropbox folder to a normal directory on Ubuntu

EDIT: The newer releases of the Dropbox client (1.2.x and above) has changed the database format into encrypted. The process of the blog post does not work with this newer releases anymore. However, there is a workaround: download the latest available stable 1.1.x version (1.1.35) from here, extract to the empty .dropbox_dist folder and make the the folder read-only for your dropbox user by making root the owner (eg. "sudo chown root:root ~/.dropbox_dist"). This method prevents the dropbox to update itself for newer versions.

I do not like the feature in the Dropbox client: it is not possible to setup a plain directory as DropBox folder, only "../something/Dropbox".

Here the tweak comes. The Dropbox uses sqlite to store the settings:

(0. Install sqlite3 package.)
1. Close the Dropbox client.
2. Go to your Dropbox folder:
cd ~/.dropbox
3. Convert the config.db to plain text
echo '.dump' | sqlite3 config.db > dump.txt
4. Search in dump.txt and edit the dropbox_path field to your desired directory location.
5. Close the dump.txt, delete or rename the old config.db to something else.
6. Convert the database back:
sqlite3 config.db < dump.txt
7. Restart the client and voilá, works.

Multiple DropBox accounts for one user on Ubuntu

I like the DropBox, but I have separate personal and work accounts. The old trick does not work any more, but after some tweaking I was able to set up two accounts in KDE/Ubuntu. How it works: for a secondary DropBox account, a "fake" home directory is needed for a "fake" user (-> it is not needed to create new users on the system). Here are the (not so exact) steps:

REPLACE the "USER_NAME" to your own user name in Ubuntu in the commands!!

(0. Create two DropBox accounts.)
1. Install the DropBox client on Ubuntu, start and setup an account.
2. Disable the automatic start in the Dropbox settings and close the sync client from the system tray.
3. Create a new "fake" home directory for your DropBox account:
sudo mkdir /home/USER_NAME-dropbox
4. Give your permissions to the directory:
sudo chown USER_NAME:USER_NAME /home/USER_NAME-dropbox
5. Copy the ~/.dropbox-dist to the new "fake" home directory:
cp ~/.dropbox-dist /home/USER_NAME-dropbox
6. Move the current DropBox user settings to the "fake" home dir:
mv ~/.dropbox /home/USER_NAME-dropbox
7. Create symlinks to the .kde and .config directories:
ln -s /home/USER_NAME/.config /home/USER_NAME-dropbox/.config
ln -s /home/USER_NAME/.kde /home/USER_NAME-dropbox/.kde

9. Start the Dropbox client again, setup the secondary account and disable the automatic start in the settings again.
10. Create a bash script somewhere to start two DropBox clients (don't forget to add executable permission!):
# First client
~/.dropbox-dist/dropboxd 2> /dev/null &
# Secondary client
USER="$USER-dropbox"
HOME="/home/$USER"
$HOME/.dropbox-dist/dropboxd 2> /dev/null &
10. In KDE: Go to System Settings/Startup and Shutdown. In the Autostart menu, add the freshly created bash script to start the client on computer startup.
(11. It is a good practice to change the icon colors in one of the ".dropbox-dist/icons/hicolor/16x16" directories to distinguish the clients in the system tray.)

Probably, there are nicer ways, but it works for me. Using the same principles, any number of clients can be run under the same user account.