www.hutsky.cz / blog / twitter /

Encrypt partition with dm-crypt

November 30th, 2011

Ever wnated to have your external disk or USB stick encrypted? This is a way to get there on a Debian-based system using dm-crypt and LUKS.

First, make sure dm-crypt module is loaded (see if /dev/mapper/ is present), and if not, load the module:

modprobe dm-crypt

Now you can use cryptsetup to encrypt your device (you’ll be asked for a password):

cryptsetup --verbose --key-size 256 --verify-passphrase luksFormat /dev/sdd1

After this step, you can open the encrypted disk.

cryptsetup luksOpen /dev/sbd1 mydisk

The disk is not ready yet, we need to format it first, just as if we connected an empty device. In theis example, I’ll format it using ext3 file system.

mkfs.ext3 -j -m 1 -O dir_index,filetype /dev/mapper/mydisk

Now you can finally mount and access your disk.

mount -t ext3 /dev/mapper/mydisk /media/mydisk

When you’re done and want to unplug the device, you unmount it the usual way:

umount /dev/mapper/encr-zipdisk

and then use LUKS to close the encrypted connection:

 cryptsetup luksClose encr-zipdisk

Now you can remove the device.

Skyrim

November 23rd, 2011

My Skyrim assasin career so far

Read the rest of this entry »

Add a keyboard layout in LXDE

November 6th, 2011

With the latast Ubuntu 11.10 out, I had to go for something that would be less resource hungry than Unity on my laptop. After considering a downgrade to one of the older releases, or switching to Mint or Debian, I chose Lubuntu as it seemed to combine up-to-date Ubuntu software sources that contain all I’m used to having on my laptop, and a more lightweight desktop environment.

I have to say I took to it immediately as there are no lags or glitches that I experienced before. All configuration was easy to find, with the only exception of adding a secondary keyboard layout. I added Keyboard Layout Switcher to the panel and then went to the general Preferences -> Keyboard and Mouse -> Keyboard section. But there was no way to add another layout, I could only go to LXKeymap application from there and change to another layout system-wide, but not to add a secondary layout.

After some time, I found help on this forum:

http://forums.linuxmint.com/viewtopic.php?f=175&t=62681

One has to go to this configuration file:

/etc/xdg/lxsession/LXDE/autostart

and add the following:

@setxkbmap -option grp:switch,grp:alt_shift_toggle,grp_led:scroll us,cz

Naturally, you add whatever locales you need instead of us,cz. You can switch between the layouts using Alt + Shift after you’ve logged out and in.

I also prefer qwerty version of the Czech layout so there was one minor thing to change:

@setxkbmap -option grp:switch,grp:alt_shift_toggle,grp_led:scroll us,cz_qwerty

Apart from this small drawback, I have to say I’ve become quite partial to LXDE so far.

Python os.sytem() and Windows Program Files directory

April 2nd, 2011

I ran across a problem when trying to run a program located in “Program Files” directory from within a Python application using os.system(). It was obvious that the problem was caused by the space character in the name of the directory. I didn’t know how to solve the issue as I’m not proficient in Python and also not very comfortable in the Windows environment. Luckily, once again, there were others before me, who had found the way:

 os.system( '""C:\\\\Program Files\\\\WinProgram\\\\PROGRAM.EXE" -params "')

Adding an extra pair of qotation marks did the trick.

MySql – get names of columns and put them in an array

March 26th, 2011

Sometimes I need to get all the names of the columns in an sql table, most often to use the names of the columns as names of variables as it’s easier to use than simple $row[1], $row[2], etc. Let’s take this WordPress table ‘wp_comments’ as an example:

Field Type Null Key Default Extra
comment_ID bigint(20) unsigned NO PRI NULL auto_increment
comment_post_ID int(11) NO MUL 0  
comment_author tinytext NO   NULL  
comment_author_email varchar(100) NO      
comment_author_url varchar(200) NO      
comment_author_IP varchar(100) NO      
comment_date datetime NO   0000-00-00 00:00:00  
comment_date_gmt datetime NO MUL 0000-00-00 00:00:00  
comment_content text NO   NULL  
comment_karma int(11) NO   0  
comment_approved varchar(20) NO MUL 1  
comment_agent varchar(255) NO      
comment_type varchar(20) NO      
comment_parent bigint(20) NO   0  
user_id bigint(20) NO   0  

If you want to use the names of the columns as the names of variables returned by a database query, you’d have to copy&paste them from the table one by one, which is time consuming and very inconvenient, of course. This simple code:



$column_names = array();
  1. $query = "SHOW COLUMNS FROM wp_comments";
  2. $result = mysql_query($query);
  3.  
  4. while($column = mysql_fetch_array($result)) {
  5.  $column_names[] = $column[0];
  6.  }
  7. $columns_string = implode(', $', $column_names);
  8. print $columns_string;

will print out a list of variables based on the column names of the given table. You can take all the names at once now and paste the list here instead of doing it one by one.

list($comment_ID,
  1.         $comment_post_ID,
  2.         $comment_author,
  3.         $comment_author_email,
  4.         $comment_author_url,
  5.         $comment_author_IP,
  6.         $comment_date,
  7.         $comment_date_gmt,
  8.         $comment_content,
  9.         $comment_karma,
  10.         $comment_approved,
  11.         $comment_agent,
  12.         $comment_type,
  13.         $comment_parent,
  14.         $user_id) = $row;

Firefox 4 and IE 9 – testing Javascript speed

March 24th, 2011

With Firefox 4 and Internet Explorer 9 out, I decided to do a small test of javascript speed. The tool used was SunSpider JavaScript Benchmark . The winner was Chrome run in Ubuntu, closely followed by IE 9 and Firefox 4 in Windows. The results came very close, however, so I think it’s fair to say that all three browsers performed more or less the same. As you can see n the following graph, the older versions of Firefox and Internet Explorer offered by far a much slower experience when running javascript. What was quite surprising was the performance of Chrome in Windows, which was rather poor.

Firefox 4 in Windows XP
results 458 ms

Chrome 11.0.696.16 beta in Ubuntu 9.10
results 378 ms

Firefox 3.6 in Ubuntu 9.10
results 1422 ms

IE 8 in Windows 7
results 7460 ms

Firefox 3.6 in Windows 7
results 1813 ms

IE 9 in Windows 7
results 431 ms

Firefox 4 in Windows 7
results 471 ms

Chrome in Windows 7
results 2488 ms

Chrome in Windows XP
results 1690 ms

Debian 6 is out – testing in Virtualbox

March 15th, 2011

It’s been a while since Debian 6 got released. Now I found some spare time to check it out. Before I get to installing it on my home server, I decided to have a peek using Virtualbox. At first I encountered a problem while while loading the system after the installation. The startup process would stop, saying:

BUG: soft lockup - CPU#0 stuck for 61s! [modprobe :90]

Luckily, I wasn’t the only one to come across this issue so after lowering the amount of memory allocated to the virtual machine, the system managed to start smoothly.

Read the rest of this entry »

Drag and drop to change position of elements

June 2nd, 2010

Long time ago, I was trying to figure out how to change position of items in a list. Let’s say there’s a list of product in an e-commerce solution and the user wants to choose what items go first and what go last. What I used back then was to create a simple form which called a function that takes the id of the previous or next item and swaps it for the current item’s id and vice versa. It was simple, but it worked. But now I needed something more user-friendly, after all it’s true that this solution was not very convenient for long lists. This is where the motto of jQuery “write less, do more” comes handy.

See demo here.

First, you need a table containing the items:

  1. <table id="positions">
  2.           <tr id="1"><td>item 1</td></tr>
  3.           <tr id="2"><td>item 2</td></tr>
  4.           <tr id="3"><td>item 3</td></tr>
  5.           <tr id="4"><td>item 4</td></tr>
  6.           <tr id="5"><td>item 5</td></tr>
  7.           <tr id="6"><td>item 6</td></tr>
  8. </table>

Now download jQuery and the Table Drag and Drop plugin (if you are new to jQuery, there’s great documentation on their page).

Having installed jQuery file and the TableDnD plugin, we need to get things working. Include this in the head section of the website:

Read the rest of this entry »

Half-life 2: Episode Two

June 2nd, 2010

To my great delight, I discovered that Half-life 2 is now available for something like 10 € nowadays. I know the game is quite old now, but I only played Episode One back then so I couldn’t resist to learn more about Episode Two. Even though it’s old, the graphics is still damn good when you use the highest details, and most importantly, it’s loads of fun. Read the rest of this entry »

Change all tags to lowercase in VIM

May 30th, 2010

Sometimes, you’ve got a whole chunk of HTML code that needs cleaning up a bit before you can use it. This week I got some materials in .doc format that had to be put on a website. There was so much of it that simple cut and paste was not possible. It would have been be particularly tiresome to try to get all the tables to HTML. On the other hand, it’s quite easy to save a word document as an html file. However, the generated HTML code is not very clean and definitely doesn’t comply with XHTML standards (though it seems to me that the result is much better when you use OpenOffice rather than MS Office).

Now that you have a very messy piece of HTML code, making it look better would mean even more toil than the copy and paste process. This is where a little bit of Vim magic comes handy.

1. First we change all tags to lowercase:

:%s/<\/\?\zs\(\a\+\)\ze[ >]/\L\1/g

2. Note that this will change tag names only. To change tag attributes to lowercase as well, use this command:

:%s/\(<[^>]*\)\@<=\<\(\a*\)\ze=['"]/\L\2/g

3. The above two expressions were found using google. Now I needed something that would wipe out all attributes to get rid of unnecessary styles attached to tags so I came up with this search and replace expression:

:%s/<\([a-z]*\)\s\([^>]*\)/<\1/g