Archive for category Script

Tomb and Thin Provisioning

Tomb is a real nice encryption tool.

Unfortunately I had to store some data which might or might not grow “a lot” and where it’s stored there isn’t much space to begin with.. and Tomb doesn’t let you create volumes with, although less secure, non-randomized data source.

I made a very small patch to the latest version (2.1.1) to instead create/resize sparse files as disk images. This is clearly dangerous for data that must be absolutely protected as you give away some informations on the content of the image that might help an attacker.

If your use case doesn’t require such a high level of security you can use it and delay the space allocation. For instance a clean disk, allocated for 32GB of space occupies much less (when empty).

tomb dig -s 32768 secret.tomb
tomb lock secret.tomb -k secret.tomb.key

du -h secret.tomb
133M secret.tomb

The current 133M blocks are mostly used by the filesystem itself but you can get that down even more by first allocating a small disk, changing the reserved block count for the filesystem and then resize it to a bigger disk.

tomb dig -s 10 secret.tomb
tomb lock secret.tomb -k secret.tomb.key
tomb resize -s 10 secret.tomb -k secret.tomb.key

du -h secret.tomb
5.1M secret.tomb

The change is very simple but I don’t want to share it as a patch or send it upstream due to the weakened security. Hence I will just point out that instead of using count in the dd commands for dig and resize you should use seek.


, , , ,

Leave a comment

Concat videos with srt subtitle

As lately I am encountering a lot of split video files with .srt subtitle i just wrote a simple script that will concat the subtitles with the correct timing shift.
It just requires mencoder and mplayer installed to do the concatenation operation. The script does not support mkv files as they have embedded subtitles and it needs some mkv toll operation, but usually mkv files are not senslessly splitted anyway

All the code can be found on github

Usage is really simple.
If you have file1.avi file2.avi just do:

perl file1.avi file2.avi output.avi

and you’ll have output.avi and ready to be used, or even:

perl file1.avi file2.avi output.mp4

and the output will use .mp4 as container (check mplayer manuals for the formats)
Here is the script.

use strict;
use warnings;
use Carp;

sub add_time($);
sub shift_sub($$$);

croak "Wrong number of arguments" if (@ARGV != 3);

my $file1     = shift;
my $file2     = shift;
my $output    = shift;

$output =~ /(.*)\..*/;
my $srt_output = "$";
$file1 =~ /(.*)\..*/;
my $srt1 = "$";
$file2 =~ /(.*)\..*/;
my $srt2 = "$";

croak "Input $file1 non existent"    if (!-e$file1);
croak "Input $file2 non existent"    if (!-e$file2);
croak "Input $srt1 non existent"    if (!-e$srt1);
croak "Input $srt2 non existent"    if (!-e$srt2);
croak "Output $output exists"        if (-e$output);
croak "Output $srt_output exists"    if (-e$srt_output);

# get file1 length
my $file1_length = 0;
open(INFO,"mplayer -vo null -nosound -frames 0 -identify '$file1'|");
foreach() {
    $file1_length = $1 if(/^ID_LENGTH=(.*)\..*/);
close INFO;

# convert $file1_length in $h,$m,$s
my ($h,$m,$s);
$s = $file1_length % 60;
$m = int ($file1_length % (60*60))/60;
$h = int ($file1_length % (60*60*24))/(60*60);

# concatenate the subs

# concatenate the movie
system("mencoder -ovc copy -oac copy '$file1' '$file2' -o '$output'");

sub shift_sub($$$) {
    my ($file1,$file2,$output) = @_;

    open OUTPUT, ">$output" or croak "can't open $output";
    open FILE, $file1 or croak "can't open $file1";
    my $idx_shift = 0;
    while(!eof(FILE)) { # pass trough storing maximum starting index
        $_ = ;
        print OUTPUT;
        if (/^(\d+)\r\n$/) {
            $idx_shift = $_;
    close FILE;

    open FILE, $file2 or croak "can't open $file2";
    while(!eof(FILE)) {
        # srt idx
        my $num = ; # print index
        print OUTPUT $num+$idx_shift."\r\n";

        # srt time
        my $timing = ;
        my ($start,$end) = split(/\s-->\s/,$timing);
        print OUTPUT add_time($start)." --> ".add_time($end)."\r\n";

        # srt text
        my $line;
        do {
            $line = ;
            print OUTPUT $line;
        } while (!($line =~ m/^\r\n$/));
    close FILE;

sub add_time($) {
    my ($hh,$mm,$ss,$sss) = split(/[:,]/,$_[0]);
    $ss += $s; $mm += $m; $hh += $h;
    if ( $ss > 60 ) { $mm++; $ss -= 60; }
    if ( $mm > 60 ) { $hh++; $mm -= 60; }
    return sprintf("%03d:%02d:%02d,%03d",$hh,$mm,$ss,$sss);


Rtorrent web interface

Me and alka wrote a web application in ruby which serves as an interface to rtorrent. It has been written in ruby using the ramaze and sequel frameworks. It
is currently tested on debian stable with mostly apt-get installed gems,
for more info check the README file included (the only one external to
the distribution should be the xmlrpc gem which is on another post in
this blog).
It supports authentication and accesses rtorrent through the unix socket
interface, allowing to run the server as a user process.
An internal copy of the information from rtorrent is stored in a
in-memory sqlite database (maybe it can be totaly avoided, but that was how we planned it in the beggining) to avoid to access too much times to rtorrent
along with the caching of pages in ramaze.

Currently may lack some feature, but it’s doing it’s job, the main
missing feature is the user interface. If someone can write a
CSS and maybe do some cleanup in the interface any help would be

As usual in the code page everything can be found or directly here

It is currently no more developed as we switched torrent client. Patches or comment are welcomed

, , , , ,

Leave a comment

Convert Man pages to PDF

Always wanted to watch a man page in a pdf file? It is clearly needed sometimes, especially on mac, since it is the easiest way i know to print a man page or to read it not from a terminal.

The problem is solved easily for both Mac OS and linux. I had to add control for <code>pstopdf</code> too, since on mac it is called that way and has different parameters (but you may have <code>ps2pdf</code> installed with macports anyway).

#! /bin/bash

if [ $# -eq 1 ] ; then
    to_pdf=$(which ps2pdf)
    if [ -z "$to_pdf" ] ; then
        to_pdf=$(which pstopdf)

    case "$to_pdf"  in
        *pstopdf) man -t "$name" | "$to_pdf" -i -o "$fname.pdf" ;;
        *ps2pdf)  man -t "$name" | "$to_pdf" - "$name.pdf" ;;
        *)        man -t "$name" > "$"
    exit $?
echo "Wrong number of parameters"
exit 1

Just call it like man2pdf gcc and you’ll get gcc.pdf

, , , , ,

1 Comment

Parallelized Make

Since the introduction of dual core machines the number of mono core machines is declined and almost no computer of that kind are sold at all. Having such a machine is clearly a big advantage, but for many things you must tell programs that you are on a multi-cpu machines to make them use all the power. So i wrote a very little and simple script to replace the default make.

Just place this script in a path before the standard make. I made a folder ~/.bin in my home directory and modified the PATH accordingly export PATH=~/.bin:$PATH


# verbosity check
if [ "$VERBOSE" = "yes" ] ; then

# Get the number of cores
if [ -z $NCPU ] ; then
    tmp=$(sysctl hw.ncpu)
    NCPU=${tmp#hw.ncpu: }
	if [ "$VERBOSE" = "yes" ] ; then
		echo "Parallelization enabled with $NCPU threads"

if [ ! -z $NOPMAKE ] ; then
    echo "Parallelization Disabled" >&2
    /usr/bin/make $@
    exit $?

# Start the compilation in parallel
if [ ! -z "$DISTCC_HOSTS" ] ; then
    if [ -z "$HOST_COUNT" ] ; then
        export HOST_COUNT=$(echo $DISTCC_HOSTS | wc -w)
    /usr/bin/make CC='distcc /usr/bin/gcc' CXX='distcc /usr/bin/g++' -j$((j+$HOST_COUNT)) $@
    /usr/bin/make -j$j $@

To make it work on linux the sysctl command must be replaced with a count of the lines containing “processor” in the file /proc/cpuinfo. Something like grep processor /proc/cpuinfo | wc -l

, , ,

Leave a comment