Saturday, November 9, 2013

Skipping Ad while playing video using Mplayer

To skip Advertisements/unwanted scenes, or to mute certain parts of a movie, the Edit Decision Lists (EDL) are very helpful.

Please read the following link for more details:-
http://www.mplayerhq.hu/DOCS/HTML/en/edl.html

Friday, November 8, 2013

Mounting Debian ISO files as Offline Software Repository

If you are not connected to internet, then still you can install packages from Debian Installer ISOs. For example, if Debian 7.0 (Wheezy) ISOs are in directory /media/FreeAgent GoFlex Drive/Softwares/Debian_7.0_Wheezy directory and we want to mount these iso in /mnt/Debian/ directory. The following command can be used:
$ sudo $(which mount_debian.pl) -d /mnt/Debian -g -r "/media/FreeAgent GoFlex Drive/Softwares/Debian_7.0_Wheezy"
Using iso(s) dir '/media/FreeAgent GoFlex Drive/Softwares/Debian_7.0_Wheezy'
Using mount point dir '/mnt/Debian'
...
Total 11 iso(s) mounted successfully.
Unmounting of the iso(s) can be achieved by the following command:
$ sudo $(which mount_debian.pl) -d /mnt/Debian -u -g -r
Running: umount "/mnt/Debian"/*.iso
Running: rmdir "/mnt/Debian"/*.iso
Running: cp "/etc/apt/sources.list.net" "/etc/apt/sources.list"
Running: apt-get update
...
Usage of the command:
$ sudo $(which mount_debian.pl)
/home/mitesh/Perl/mount_debian.pl: Please provide Dir where Debian installer iso(s) are present.
USAGE: /home/mitesh/Perl/mount_debian.pl [options] iso_dir
          -a apt_sources_dir        default  /etc/apt
          -d mount_base_dir         default  /mnt/Debian
          -g                        debug on 
          -r                        apt-get update 
          -u                        unmount 
          iso_dir                   Dir where Debian installer iso(s) are present.

PS: iso_dir is not required with -u command

The mount_debian.sh perl script is given as follows:
#!/usr/bin/perl -w
#===============================================================================
#
#         FILE:  mount_debian.pl
#
#        USAGE:  ./mount_debian.pl 
#
#  DESCRIPTION:  
#
#      OPTIONS:  ---
# REQUIREMENTS:  ---
#         BUGS:  ---
#        NOTES:  ---
#       AUTHOR:  Mitesh Singh Jat (mitesh)
#      COMPANY:  
#      VERSION:  1.0
#      CREATED:  11/07/2013 10:30:04 PM IST
#     REVISION:  ---
#===============================================================================

use strict;
use warnings;

use Getopt::Std;
my $apt_sources_dir = "/etc/apt";
my $mount_point_base = "/mnt/Debian";
my $umount = 0;
my $reload = 0;
my $debug = 0;

sub usage()
{
    print STDERR "USAGE: $0 [options] <iso_dir>\n";
    print STDERR "          -a <apt_sources_dir>    apt sources dir: default  $apt_sources_dir\n";
    print STDERR "          -d <mount_base_dir>     mount point dir: default  $mount_point_base\n";
    print STDERR "          -g                      debug on \n";
    print STDERR "          -r                      apt-get update \n";
    print STDERR "          -u                      unmount \n";
    print STDERR "          <iso_dir>               Dir where Debian installer iso(s) are present.\n";
}

sub run_cmd()
{
    my $cmd = $_[0];
    print "Running: $cmd\n" if $debug;
    system("$cmd");
    my $retval = $?; 
    if ($retval != 0)
    {   
        print STDERR "Error in running: $cmd\nExit code = $retval\n";
    }   
    return ($retval);
}

sub run_cmd_get()
{
    my $cmd = $_[0];
    print "Running: $cmd\n" if $debug;
    my @outs = `$cmd`;
    my $retval = $?;
    if ($retval < 0)
    {
        print STDERR "Error in running: $cmd\nExit code = $retval\n";
        exit($retval);
    }
    chomp(@outs);
    return (@outs);
}
sub take_backup() 
{
    my $file1 = $_[0];
    my $file2 = $_[1];
    my $cmd = "cp \"$file1\" \"$file2\"";
    my $retval = &run_cmd("$cmd");
    if ($retval != 0)
    {
        print STDERR "$0: Cannot copy \"$file1\" to \"$file2\"\n";
        exit(-1);
    }
    return 1;
}
sub reload_repo()
{
    my $file1 = $_[0];
    my $file2 = $_[1];

    my $cmd = "cp \"$file1\" \"$file2\"";
    my $retval = &run_cmd($cmd);
    exit(-1) if ($retval != 0);

    $cmd = "apt-get update";
    $retval = &run_cmd($cmd);
    exit(-1) if ($retval != 0);
}

my %opts;
getopt('a:d:', \%opts);

foreach my $opt (sort keys %opts)
{
    if (!defined($opts{$opt}))
    {
        print STDERR "$0: Requires value for option '$opt'\n";
        &usage();
        exit(-1);
    }
    else 
    {
        if ($opts{$opt} =~ m/^-/) 
        {
            print STDERR "$0: Requires value for option '$opt'\n";
            &usage();
            exit(-1);
        }
    }
}
$apt_sources_dir = $opts{"a"} if (defined($opts{"a"}));
$mount_point_base = $opts{"d"} if (defined($opts{"d"}));
$debug = 1 if (defined($opts{"g"}));
$reload = 1 if (defined($opts{"r"}));
$umount = 1 if (defined($opts{"u"}));

my $apt_dvd = "$apt_sources_dir/sources.list.dvd";
my $apt_net = "$apt_sources_dir/sources.list.net";
my $apt_orig = "$apt_sources_dir/sources.list";

my $retval = 0;
my $cmd;
$retval = `whoami`;
chomp($retval);
unless ($retval eq "root")
{
    print STDERR "$0: Please run this script as 'root' user.\n";
    &usage();
    exit(-1);
}

unless ($umount)
{

if (@ARGV != 1) 
{
    print STDERR "$0: Please provide Dir where Debian installer iso(s) are present.\n";
    &usage();
    exit(-1);
}
my $iso_dir = "$ARGV[0]";
## Check for iso dir presence
unless (-d "$iso_dir") 
{
    print STDERR "$0: Please make sure the iso(s) dir '$iso_dir' is present\n";
    exit(-1);
}
print "Using iso(s) dir '$iso_dir'\n" if ($debug);

## Check for destination dir
unless (-d "$mount_point_base") 
{
    $cmd = "mkdir -p \"$mount_point_base\"";
    $retval = &run_cmd("$cmd");
    if ($retval != 0) 
    {
        print STDERR "$0: Cannot create dir '$mount_point_base'\n";
        exit(-1);
    }
}
print "Using mount point dir '$mount_point_base'\n" if ($debug);

## Taking backup of ftp/http line sources.list
my $is_backed_up = 0;
if (-f "$apt_net") 
{
    ## Take backup only when ftp / http (internet)
    $cmd = "grep -v -e \"^#\" -e \"^\$\" \"$apt_orig\" | egrep -c \"(ftp|http)://\"";
    my @nets = &run_cmd_get($cmd);
    if ($nets[0] > 0) 
    {
        $is_backed_up = &take_backup("$apt_orig", "$apt_net");
    }
}
else
{
    $is_backed_up = &take_backup("$apt_orig", "$apt_net");
}

## Get loop devices
$cmd = "ls /dev/loop[0-9]*";
my @temps = &run_cmd_get($cmd);
my %loop_devices;
my $nloop_devices = @temps;
foreach my $temp (@temps) 
{
    $loop_devices{$temp} = 1;
}
$cmd = "ls \"$iso_dir\" | grep -i \"\.iso\$\"";
@temps = &run_cmd_get($cmd);
my %iso_images;
my $niso_images = @temps;
foreach my $temp (@temps) 
{
    $iso_images{$temp} = 1;
}
$cmd = "df | awk '{print \$1}' | grep \"/dev/loop\"";
@temps = &run_cmd_get($cmd);
my %mounted_loop_devices;
my $nmounted_loop_devices = @temps;
foreach my $temp (@temps) 
{
    $mounted_loop_devices{$temp} = 1;
}

## Check for free loop devices
my $nfree_loops = $nloop_devices - $nmounted_loop_devices;
if ($niso_images > $nfree_loops)
{
    print "Less available loop devices $nfree_loops than isos $niso_images\n" if ($debug);
    my $req_loops = $niso_images - $nfree_loops; 
    if ($nmounted_loop_devices == 0) 
    {
        print "Reloading loop kernel module with extra $req_loops loop devices\n" if $debug;
        $cmd = "rmmod loop";
        $retval = &run_cmd($cmd);
        print "$retval\n";
        exit(-1) if ($retval != 0);
        $cmd = "modprobe loop max_loop=$niso_images";
        $retval = &run_cmd($cmd);
        exit(-1) if ($retval != 0);
    }
    else 
    {
        print STDERR "Please reload 'loop' kernel module. After unloading following loop devices...\n";
        foreach my $temp (sort keys %mounted_loop_devices)
        {
            print STDERR "$temp\n";
        }
        print STDERR "\nrmmod loop\n";
        my $temp = $nmounted_loop_devices + $req_loops;
        print STDERR "\nmodprobe loop max_loop=$temp\n\n";
        exit(-2);
    }
}

## Create entries in sources.list file
open(SFH, ">$apt_orig") or die("$0: Cannot write in file '$apt_orig'\n");
foreach my $iso (sort keys %iso_images)
{
    print "Mounting iso $iso ...\n" if $debug;
    my $dest_dir = "$mount_point_base/$iso";
    unless (-d "$dest_dir") 
    {
        $cmd = "mkdir -p \"$dest_dir\"";
        $retval = &run_cmd($cmd);
        exit(-1) if ($retval != 0);
    }
    $cmd = "df \"$dest_dir\" | grep -c \"^/dev/loop.*\.iso\$\"";
    my @mounts = &run_cmd_get($cmd); 
    if ($mounts[0] == 0) ## Mount only if not mounted
    {
        $cmd = "mount -o loop,ro \"$iso_dir/$iso\" \"$dest_dir\"";
        $retval = &run_cmd($cmd);
        exit(-1) if ($retval != 0);

        ## read distro version dir in dists/
        $cmd = "ls -l \"$dest_dir/dists\" | grep \"^d\" | awk '{print \$(NF)}' | head -1";
        my $version = `$cmd`;
        if (defined($version) and ($version ne ""))
        {
            $cmd = "ls -l \"$dest_dir/dists\"/*/ | grep \"^d\" | sort -u | awk '{print \$(NF)}' | awk '{sec=sec\" \"\$1} END{print sec}'";
            my $sections = `$cmd`;
            if (defined($sections))
            {
                chomp($sections);
                chomp($version);
                print SFH "deb file://$dest_dir $version $sections\n";
            }
        }
    }
}
print SFH "\n## Automatically generated by $0 ##\n";
close(SFH);
print "Total $niso_images iso(s) mounted successfully.\n" if $debug;

## Reload repository if sources.list is updated
if ($is_backed_up and $reload)
{
    &reload_repo("$apt_orig", "$apt_dvd");
}
exit(0);
}
#######################################
## umount code                       ##
#######################################

$cmd = "umount \"$mount_point_base\"/*.iso";
$retval = &run_cmd($cmd);
exit(-1) if ($retval != 0);
$cmd = "rmdir \"$mount_point_base\"/*.iso";
$retval = &run_cmd($cmd);
exit(-1) if ($retval != 0);
if ($reload)
{
    &reload_repo("$apt_net", "$apt_orig");
}
exit(0);

PS: The above script can be used with any Debian Package Management related distros like Ubuntu, Linux Mint, etc.

Wednesday, April 3, 2013

Pidgin: Keyboard becomes unresponsive after Status change

In Fedora 17 (i386), after changing status in pidgin 2.10.x, it is not possible to use keyboard in chat window. Keyboard becomes unresponsive. Even, I tried to compile and install newer pidgin 2.10.7, the issue remains same.

Then, after searching, I came across this bug https://developer.pidgin.im/ticket/15220 in Pidgin (actually GTK+), which suggests to

Right Click inside typing input box of Chat window > Input Methods > (Select any input method)

After this, input from keyboard is again possible.

Wednesday, March 27, 2013

Emacs in Daemon mode

When I started to use emacs as Java IDE, I have installed CEDET and JDEE. This make start time of emacs longer, even when I am not opening any Java file. Therefore, it is required to start Emacs in daemon mode. So that only one time startup is longer, and subsequent startup of emacs (emacs client to be precise) will be much faster.

I have written the following shell script (em, please place it in your $PATH), which starts the emacs daemon on first time only, and connects emacsclient to it.
#!/bin/bash

usr=$(whoami)
emacs_daemon="emacs --daemon"
cmd="ps auxww | grep \"$usr.*$emacs_daemon\" | grep -v grep"
#echo "$cmd"
is_running=$(eval "$cmd")
if [ -z "$is_running" ]
then
    echo "Starting emacs daemon"
    eval "$emacs_daemon"
    if [ $? -ne 0 ]
    then
        echo "$0: Cannot start $emacs_daemon" >&2
        exit
    fi
fi
emacsclient -a "" -t "$@"
exit $?

First Run:
$ em Hello.java
Starting emacs daemon

Warning: due to a long standing Gtk+ bug
http://bugzilla.gnome.org/show_bug.cgi?id=85715
Emacs might crash when run in daemon mode and the X11 connection is unexpectedly lost.
Using an Emacs configured with --with-x-toolkit=lucid does not have this problem.
("emacs")
Loading /usr/share/emacs/site-lisp/site-start.d/auto-complete-init.el (source)...
Loading /usr/share/emacs/site-lisp/site-start.d/auto-complete-init.el (source)...done
Loading /usr/share/emacs/site-lisp/site-start.d/emacs-color-theme-init.el (source)...
Loading /usr/share/emacs/site-lisp/site-start.d/emacs-color-theme-init.el (source)...done
Loading /usr/share/emacs/site-lisp/site-start.d/emacs-goodies-loaddefs.el (source)...
Loading /usr/share/emacs/site-lisp/site-start.d/emacs-goodies-loaddefs.el (source)...done
Loading /usr/share/emacs/site-lisp/site-start.d/focus-init.el (source)...
Loading /usr/share/emacs/site-lisp/site-start.d/focus-init.el (source)...done
Loading /usr/share/emacs/site-lisp/site-start.d/gnuplot-init.el (source)...
Loading /usr/share/emacs/site-lisp/site-start.d/gnuplot-init.el (source)...done
Loading /usr/share/emacs/site-lisp/site-start.d/gnus-bonus-init.el (source)...
Loading /usr/share/emacs/site-lisp/site-start.d/gnus-bonus-init.el (source)...done
Loading /usr/share/emacs/site-lisp/site-start.d/php-mode-init.el (source)...
Loading /usr/share/emacs/site-lisp/site-start.d/php-mode-init.el (source)...done
Loading /usr/share/emacs/site-lisp/site-start.d/rpm-spec-mode-init.el (source)...
Loading /usr/share/emacs/site-lisp/site-start.d/rpm-spec-mode-init.el (source)...done
Loading /usr/share/emacs/site-lisp/site-start.d/rpmdev-init.el (source)...
Loading /usr/share/emacs/site-lisp/site-start.d/rpmdev-init.el (source)...done
Loading /usr/share/emacs/site-lisp/cedet-1.1/common/cedet.el (source)...
Setting up CEDET packages...
Warning: cedet-called-interactively-p called with 0 arguments, but requires 1
Warning: cedet-called-interactively-p called with 0 arguments, but requires 1
Setting up CEDET packages...done
Loading /usr/share/emacs/site-lisp/cedet-1.1/common/cedet.el (source)...done
Warning: cedet-called-interactively-p called with 0 arguments, but requires 1
Warning: cedet-called-interactively-p called with 0 arguments, but requires 1
Warning: cedet-called-interactively-p called with 0 arguments, but requires 1
Warning: cedet-called-interactively-p called with 0 arguments, but requires 1                                                                                                  
Warning: cedet-called-interactively-p called with 0 arguments, but requires 1                                                                                                  
Warning: cedet-called-interactively-p called with 0 arguments, but requires 1
Warning: cedet-called-interactively-p called with 0 arguments, but requires 1
jde-java-font-lock: building names cache...
jde-java-font-lock: building names cache...empty
Warning: cedet-called-interactively-p called with 0 arguments, but requires 1
Warning: cedet-called-interactively-p called with 0 arguments, but requires 1
Warning: cedet-called-interactively-p called with 0 arguments, but requires 1
Warning: cedet-called-interactively-p called with 0 arguments, but requires 1
Starting Emacs daemon.

Second and Subsequent Runs:
$ em Hello.java

Tuesday, February 19, 2013

Removing Empty Directories

We can remove empty directories inside a directory by using following command

$ ls -l * | grep -B1 "^total 0K" | grep -v -e "total 0K" -e "--" | cut -d: -f1 | xargs rmdir -v

The above command cannot delete recursively. Hence, using find command to delete recursively.
$ find . -type d -empty -delete

Friday, November 16, 2012

Stopping Tumblerd in XFCE


Tumbler is a D-Bus service for applications to request thumbnails for various URI schemes and MIME types. It is an implementation of the thumbnail management D-Bus specification described here.

  • Issues with Tumblerd: 
    • Using more memory and CPU
    • Preventing removable storage devices from unmounting(then, ejecting)
      • External HDD
      • USB Pendrive
      • CD/DVD 
    • Usually, locking of external devices occur with Video files
$ lsof /run/media/mitesh/PHONE\ CARD
COMMAND   PID   USER   FD   TYPE DEVICE  SIZE/OFF NODE NAME
tumblerd 1268 mitesh    9r   REG   8,17  34902492 1625 /run/media/mitesh/PHONE CARD/video/abc.mp4
tumblerd 1268 mitesh   10r   REG   8,17  69869584 1624 /run/media/mitesh/PHONE CARD/video/def.mp4
tumblerd 1268 mitesh   11r   REG   8,17  35883924 1627 /run/media/mitesh/PHONE CARD/video/ghi.avi
tumblerd 1268 mitesh   14r   REG   8,17 293435929 1629 /run/media/mitesh/PHONE CARD/video/jkl/mno.flv
tumblerd 1268 mitesh   15r   REG   8,17 240015268 1630 /run/media/mitesh/PHONE CARD/video/jkl/pqr.flv

So, in order to get rid of this blocking process tumblerd, we can kill the tumblerd commands given by lsof, as:
 
$ lsof /run/media/mitesh/PHONE\ CARD/ | awk '{print $2}' | grep -v PID | xargs -L 1 kill -TERM

PS: Please replace /run/media/mitesh/PHONE\ CARD/ with the mount point of the removable device, which is not allowed to be umounted.


If we want to remove this tumblerd, we can give following command:
  • Fedora
    • $ sudo yum remove tumbler
  • Debian/Ubuntu
    •  $ sudo apt-get remove tumbler

Friday, November 9, 2012

Encrypt Directory in Linux

Encrypt Directory

This article describes basic usage of eCryptfs. It guides us through the process of creating a private and secure encrypted directory within our $HOME directory, where we can store all our sensitive files and private data.

In implementation eCryptfs differs from dm-crypt, which provides a block device encryption layer, while eCryptfs is an actual file-system – a stacked cryptographic file system to be exact.

Preliminary Note

In this wiki, I will encrypt a directory /home/mitesh/Private, which is located in /home partition. i.e. /home/mitesh/Private is an ordinary directory and does not use a partition of its own.

Installing eCryptfs

  • eCryptfs can be installed as following:
    • Debian
      • $ sudo apt-get install ecryptfs-utils
    • Fedora
      • $ sudo yum install ecryptfs-utils

Setup

Setup (Simple)

As a user, run the following command:

$ ecryptfs-setup-private

and follow the instructions.

Setup (Detail)

  • First, we create private directories.
    $ mkdir /home/mitesh/Private
    $ mkdir /home/mitesh/.Private
    $ chmod 700 /home/mitesh/.Private
    $ chmod 500 /home/mitesh/Private
    
  • Let's summarize
    • Actual encrypted data will be stored in ~/.Private directory (lower directory)
    • While mounted, decrypted data will be available in ~/Private directory (upper directory)
      • While not mounted nothing can be written to this directory
      • While mounted it has the same permissions as the lower directory
  • eCryptfs can now be mounted on top of ~/Private.
    $ sudo mount -t ecryptfs /home/mitesh/.Private /home/mitesh/Private
    Passphrase: <-- some_passphrase
    Select cipher:
     1) aes: blocksize = 16; min keysize = 16; max keysize = 32 (not loaded)
     2) blowfish: blocksize = 16; min keysize = 16; max keysize = 56 (not loaded)
     3) des3_ede: blocksize = 8; min keysize = 24; max keysize = 24 (not loaded)
     4) twofish: blocksize = 16; min keysize = 16; max keysize = 32 (not loaded)
     5) cast6: blocksize = 16; min keysize = 16; max keysize = 32 (not loaded)
     6) cast5: blocksize = 8; min keysize = 5; max keysize = 16 (not loaded)
    Selection [aes]: <-- ENTER
    Select key bytes:
     1) 16
     2) 32
     3) 24
    Selection [16]: <-- ENTER
    Enable plaintext passthrough (y/n) [n]: <-- ENTER
    Enable filename encryption (y/n) [n]: <-- ENTER
    Attempting to mount with the following options:
      ecryptfs_unlink_sigs
      ecryptfs_key_bytes=16
      ecryptfs_cipher=aes
      ecryptfs_sig=bd28c38da9fc938b
    WARNING: Based on the contents of [/root/.ecryptfs/sig-cache.txt],
    it looks like you have never mounted with this key
    before. This could mean that you have typed your
    passphrase wrong.
    
    Would you like to proceed with the mount (yes/no)? : <-- yes
    Would you like to append sig [bd28c38da9fc938b] to
    [/root/.ecryptfs/sig-cache.txt]
    in order to avoid this warning in the future (yes/no)? : <-- yes
    Successfully appended new sig to user sig cache file
    Mounted eCryptfs 
    
  • Remember to take backup of /root/.ecryptfs/sig-cache.txt file, if /root partition gets reformatted
    $ sudo cp -r /root/.ecryptfs /home/mitesh/
    
  • Verifying mounting of eCryptfs
    $ mount | grep -i private
    /home/mitesh/.Private on /home/mitesh/Private type ecryptfs (rw,relatime,ecryptfs_sig=e8d08163d274c68f,ecryptfs_cipher=aes,ecryptfs_key_bytes=16,ecryptfs_unlink_sigs)
    
  • Verifying encryption
    $ cat ~/Private/test.txt
    Some secret text
    is present in this file.
    $ sudo umount /home/mitesh/Private
    $ cat ~/Private/test.txt
    cat: /home/mitesh/Private/test.txt: No such file or directory
    $ cat ~/.Private/test.txt
    -#@%!*^(junk characters)^*!%@#-
    
  • Mounting without interaction (so passphrase must be in some removable device like USB pendrive)
    $ sudo mount -t ecryptfs -o key=passphrase:passphrase_passwd_file=/mnt/usb/.ecryptfs-pass,ecryptfs_cipher=aes,ecryptfs_key_bytes=16,ecryptfs_passthrough=n,ecryptfs_enable_filename_crypto=n /home/mitesh/.Private /home/mitesh/Private
    $ cat /mnt/usb/.ecryptfs-pass
    passphrase_passwd=ThisIsAWeakPassword
    

Encrypting Other Directory

  • We can encrypt multiple directories, by putting those directories into /home/mitesh/Private and creating soft-links.
    $ mv ~/Documents ~/Private
    $ ln -s ~/Private/Documents ~/Documents
    
  • By following the above 2 steps, we have encrypted directory ~/Documents .

Unmount

$ sudo umount /home/mitesh/Private