Ajitabh Pandey's Soul & Syntax

Exploring systems, souls, and stories – one post at a time

Category: FLOSS

About Free/Libre/Open Source Software

  • Raspberry Pi Wireless Device Power Off

    I use my device as headless as a home server and as a jump box. From past quite some time I was noticing that my Raspberry Pi was not accessible over the network after some period of in-activity. And since it is headless, the only way to get the network back was either take out the USB Wireless adapter or Reboot the Pi (through the power switch). I chose to take out the adapter and reseat it. However, this was causing my RPi to reboot also, may be because in the process the power connector was getting disturbed. I will revisit this point later.

    In order to switch off the power saving for my wireless device I tried

    $ sudo iwconfig wlan0 power off
    Error for wireless request "Set Power Management" (8B2C) :
        SET failed on device wlan0 ; Operation not permitted.

    I am using NetGear USB wireless adapter and it load the kernel module 8192cu. Checking

    $ cat /sys/module/8192cu/parameters/rtw_power_mgnt 
    1

    I realized that the power saving is still on (1 indicates minimum power saving). So I created a module config file as below to permanently set the power saving to 0 and rebooted the RPi.

    $ sudo vi /etc/modprobe.d/8192cu.conf 
    # Disable the power management during boot - Ajitabh
    options 8192cu rtw_power_mgnt=0
    $ sudo reboot

    Checking again confirmed that the power management has been disabled.

    $ cat /sys/module/8192cu/parameters/rtw_power_mgnt 
    0
  • Build An MP3 Catalogue System With Perl – Basics

    My mp3 collection was increasing and I wanted to build a catalogue for the same. There are various steps in having even a simple catalogue system. In this post and a few posts that will follow, I will be explaining how to write such a system using perl as the programming language.

    MP3 Format and ID3 Tags

    MP3 is an audio coding format for digital audio. The audio data in this file is in a compressed format. The compression is a lossy compression, meaning the sound quality is not very clear. In spite of being a lossy format, it is one of the most popular format for audio streaming and storage. The mp3 file has built in bibliographic information such as title, artist, album. This information is stored in a field inside the file known as ID3 tag. Using this information, the MP3 players are able to display the Song Title, Album name and Artist name(s) etc.

    There are couple of versions of these ID3 tags in use. ID3v1 (1.1 being the last in version 1 series) and ID3v2 (ID3v2.4 being the latest version).

    Perl MP3::Tag Module

    The MP3::Tag module of perl can be used to read and write both the versions of the ID3 tag. Here are few of the sample perl programs to do that. This will help in understanding the usage of the modules before we proceed to the next steps.

    Below are couple of examples showing how to read ID3v1 and ID3v2 tags.

    #!/usr/bin/perl
    #
    # id3v1_read.pl
    #
    use 5.010;
    use warnings;
    use strict;
    use MP3::Tag;
    
    # set filename of MP3 track
    my $filename = "your_mp3_file";
    
    # create new MP3-Tag object
    my $mp3 = MP3::Tag->new($filename);
    
    # get tag information
    $mp3->get_tags();
    
    # check to see if an ID3v1 tag exists
    # if it does, print track information
    if (exists $mp3->{ID3v1}) {
      #$mp3->{ID3v1}->remove_tag();exit;
    
      say "Filename: $filename";
      say "Artist: " . $mp3->{ID3v1}->artist;
      say "Title: " . $mp3->{ID3v1}->title;
      say "Album: " . $mp3->{ID3v1}->album;
      say "Year: ". $mp3->{ID3v1}->year;
      say "Genre: " . $mp3->{ID3v1}->genre;
    } else {
      say "$filename: ID3v1 tag not found";
    }
    
    # clean up
    $mp3->close();

    ID3v2 tags are a bit more complex as they allow a lot more information to be stored in the MP3 file such as the album artwork etc. If you run the following script on one of your MP3 files it will print all the ID3v2 information to the screen. I have used the getc() function in order to allow you to observe the output and press <Enter> to proceed to the next set of key-value pair. After couple of keystrokes you will see there are lot of junk characters printed. These junk characters are nothing but the album artwork and following the junk characters is the MIME type of the artwork. In my case the MIME types were all “image/jpeg”.

    #!/usr/bin/perl
    #
    # id3v2_read.pl
    #
    use 5.010;
    use warnings;
    use strict;
    use MP3::Tag;
    
    # set filename of MP3 track
    my $filename = "mp3_file_name;
    
    # create new MP3-Tag object
    my $mp3 = MP3::Tag->new($filename);
    
    # get tag information
    $mp3->get_tags();
    
    # check to see if an ID3v2 tag exists
    # if it does, print track information
    if (exists $mp3->{ID3v2}) {
      # get a list of frames as a hash reference
      my $frames = $mp3->{ID3v2}->get_frame_ids();
    
      # iterate over the hash, process each frame
      foreach my $frame (keys %$frames) {
        # for each frame get a key-value pair of content-description
        my ($value, $desc) = $mp3->{ID3v2}->get_frame($frame);
        if (defined($desc) and length $desc) {
          say "$frame $desc: "; 
        } else {
          say "$frame :";
        }
        # sometimes the value is itself a hash reference containing more values
        # deal with that here
        if (ref $value eq "HASH") {
          while (my ($k, $v) = each (%$value)) {
            say "\n     - $k: $v";
          }
        } else {
          say "$value";
        }
        # allows to view each iteration
        getc(STDIN);
      }
    } else {
      say "$filename: ID3v2 tag not found";
    }
    
    # clean up
    $mp3->close();

    Next Steps

    Most of the current MP3 players read ID3v2 tags. It will be good to understand the structure of the ID3v2 tags, using one of the links I provided above. This will help you prepare for understanding the further articles in this series. In next part we will see how to extract desired information quickly and how to extract the artwork data from the MP3 file. Stay tuned for more.

  • Setting up a Git Repository on A Remote Server

    Git is a wonderful distributed version control system and we can have a personal repository quickly set up without any technical difficulties. However at times we may need to work on shared development model where multiple people need commit access to the code. In such cases we would need a centralized repository store.

    By following  the given steps we will be able to set up a shared git repository accessible over SSH.

    • First a user needs to be created to hold the git repository (multiple repositories can also be set up under the same git user). Next a password need to be set up for the same user. Since this user will  never log on using shell, a password which is secure, random, very long string is a good option.
    $ sudo /usr/sbin/useradd -c "Git Version Control" -d /home/git -m -s /bin/bash git
    $ sudo passwd git
    • Next login as git user and create the project directory to hold the repository and then initialize that directory as a barebones (blank) git repository
    $ mkdir -p /home/git/projects/myproject1.git
    $ cd /home/projects/myprojects.git
    $ /usr/bin/git --bare init
    • Set up the .ssh directory for the git user to hold the authorized_keys file with the public ssh keys for the user allowed to log in a git user and access the repository
    $ cd /home/git
    $ mkdir .ssh
    $ chmod -R 700 .ssh
    $ touch .ssh/authorized_keys
    $ chmod 644 .ssh/authorized_keys
    • Finally change the shell of the git user to git-shell so that interactive logins for the user are not possible and only selected remote git commands are allowed.
    $ /usr/bin/chsh -s /usr/bin/git-shell

    Add the public key of the user(s) to the authorized key file. These users will be able to access the repository. Now all that needs to be done is to clone the repository and start working. Following command can be used to clone the repository on a *nix box. The 7652 is the port number where ssh daemon is listening. If your port is the default, 22, you can skip this port number.

    $ git clone ssh://git@myhost.mydomain.com:7652/home/git/projects/myproject1.git/
  • Time-zone Setting in Linux and BSDs from Shell

    Often the default time-zone in a linux and bsd system does not match our preferences. On a system which we have installed ourself, we may have selected the appropriate time-zone during installation, but as systems administrators we often get our hands on a system which is pre-installed and after taking over we want to change the time-zone to something which we are comfortable understanding and co-relating various system events in the time of our comfort.

    The time-zone of  the system is determined by a file called “/etc/localtime”, which a binary file. In order to change the time-zone, we need to replace this file with an appropriate file of our time-zone. All time-zone files are found in “/usr/share/zoneinfo”.

    On some systems “/etc/localtime” is a copy and in some cases a hard link of one of the time-zone found in “/usr/share/zoneinfo” directory. In OpenBSD, “/etc/localtime” is a symlink to one of the files in “/usr/share/zoneinfo”. I prefer the symlink approach, you can pick any of the methods to make appropriate “/etc/localtime” file available.

    In order to change the time-zone of my system from UTC to IST, I did the following.

    $ sudo ln -sf/usr/share/zoneinfo/Asia/Kolkata /etc/localtime

    In case you accidentally delete the “/etc/localtime” file, the timezone of the system reverts to UTC and upon having the correct file present again, it will reflect the correct timezone again. See below (I did this on a RHEL 6 machine) –

    $ date
    Wed Dec 18 13:56:05 IST 2013
    $ sudo rm -f /etc/localtime
    $ date
    Wed Dec 18 08:36:29 UTC 2013
    $ ls -l /etc/localtime
    ls: cannot access /etc/localtime: No such file or directory
    $ sudo ln -sf /usr/share/zoneinfo/Asia/Kolkata /etc/localtime
    $ date
    Wed Dec 18 14:07:03 IST 2013

     

  • FreeBSD 9.1 on Thinkpad T420

    beastieVery recently I was thinking of trying FreeBSD operating system on a laptop as a desktop system. FreeBSD is an excellent UNIX class of operating systems primarily used as a server on internet. Among other things the operating system posses excellent memory management features, ZFS and LLVM etc.

    I have installed various BSD class of operating systems earlier also, but when I installed FreeBSD 9.1 this time, I noticed that the default disk partitioning options have changed in the installer. By default if the automatic partitioning option is chosen, FreeBSD creates GPT (GUID Partition Table). I decided to proceed with all default options except the timezone settings were changed to IST.

    At the end of the install, the system refused to boot from the hard disk. I thought this is because the BIOS may be configured to use the UEFI (Unified Extensible Firmware Interface) being configured in BIOS as the laptop had windows 7 installed in it. I tried changing the setting to – disable, EUFI/legacy, legacy – but still the laptop did not boot. Upon my research on the internet I came across – https://wiki.freebsd.org/UEFI. Here it was mentioned that –

    Partitions not seen. When using GPT, FreeBSD will create a protective MBR. This MBR has one partition entry covering the whole disk. FreeBSD marks this partition active. This causes at least some UEFI implementations to ignore the GPT. To fix this the partition needs to be marked inactive. This should be fixed as of r251588, to be confirmed.

    Running

    fdisk /dev/sda0

    from the live mode in FreeBSD confirmed that there is an MBR partition created and the flag 80 (active) has been set on it. I booted from a linux CD and ran

    fdisk -l /dev/sda0

    to confirm that this is indeed the case. So I used linux fdisk to remove the active flag (toggle option), but this did not help me.

    At this moment I decided to drop this issue here and take the easy way out in order to get the operating system installed. During the installation this time I chose manual partitioning –

    • deleted all partition
    • changed the partitioning scheme to MBR (the default was GPT)
    • created a FreeBSD partition on it with further two partitions (slices) within the FreeBSD disk area
      • one as type freebsd-swap
      • other freebsd-ufs (mounted at /).

    During the end of the installation an option to go to shell for additional installation is provided. I selected that option and then ran the

    sysinstall

    utility and selected options as follows –

    sysinstall -> Configure -> Packages -> FTP -> Main Site

    Selected all the desired software and finished the installation. The reboot took me straight to FreeBSD.

    Enabling UTF-8

    The available locales can be checked using the locale command. In order to find out the UTF-8 locale which are installed –

    # locale -a | grep UTF-8|grep US
    en_US.UTF-8

    In order to add the support for this locale, following lines need to be added in /etc/login.conf –

    :charset=UTF-8:\
    :lang=en_US.UTF-8:

    Finally run the –

    #cap_mkdb /etc/login.conf

    Now in order to use these following two environment variables can be set –

    LANG=en_US.UTF-8
    GDM_LANG=en_US.UTF-8

    Installing X windows and Windows Managers

    Since I wanted to get a graphical desktop on the laptop, I need to install X windows, a window manager and some other graphical tools like browser etc.

    # pkg_add -r x11 ratpoison awesome stumpwm firefox

    Configuring X and starting is done as below –

    # X -configure
    # cp /root/xorg.conf.new /etc/xorg.conf
    # cat ~/.xinitrc
    export LC_ALL=en_US.UTF-8
    export LANGUAGE=en_US.UTF-8
    export LANG=en_US.UTF-8
    exec /usr/local/bin/ratpoison

    Following two lines needs to be added to

    /etc/rc.conf

    for the X to work correctly –

    hald_enable="YES"
    dbus_enable="YES"

    and then finally

    startx

    to launch the X. May be later on I will install one of the display managers to facilitate graphical login.

    So I am ON with freebsd and now I can start learning/exploring this further and may be try some of the other BSDs along with this as multiboot. I have not really worked on BSDs from quite sometime. I expect this to be a good refresher.