Setting Debian time zone with puppet

To manually change the time zone on a Debian install you would naturally use the interactive command dpkg-reconfigure tzdata.

In order to change all your puppet managed machines at once I use this simple recipe:

#
# Timezone -> America/Sao_Paulo
#

package {'tzdata':
  ensure  => 'present'
}

file {'/etc/localtime':
  require => Package['tzdata'],
  source  => 'file:///usr/share/zoneinfo/America/Sao_Paulo',
  notify  => Exec['reboot']
}

file {'/etc/timezone':
  require => Package['tzdata'],
  content => 'America/Sao_Paulo',
  notify  => Exec['reboot']
}

That’s it!

Local DNS resolution for in-house devices

2013-03-20 23.16.05

Three great articles on how to setup my DD-WRT router to take advantage of Local DNS resolution. No more typing IP addresses going forward, everything is ipad.home or server1.office.

As an added bonus I also modified the resolution of domains related to advertising to an invalid IP, less junk! Great!

Creating a customized Proxmox VE OpenVZ template

proxmox-create-custom-openvz-container

Every time I want to test some code or software I usually do it on a virtualized environment to keep it isolated from my main system and every time I setup a machine from scratch. I use Virtual Box when I am on the go but at home I have several single-purpose VMs running on Proxmox VE, a powerful open source virtualization platform, based on KVM and OpenVZ. Here is how to simplify the setup process creating a custom Debian-based OpenVZ template:

  1. Create a regular OpenVZ Container having debian-6.0-standard_6.0-6_i386 as base.
  2. With the VM up and running, log in and setup networking. In my case I am using DHCP, so I added the following lines to /etc/network/interfaces:

    auto eth0 
    iface eth0 inet dhcp
    

    and reseted the network stack with /etc/init.d/networking restart.

  3. Update the system to install the latest patches:

    apt-get update && apt-get upgrade
    
  4. Make sure sudo and openssh-server are installed:

    apt-get install sudo openssh-server
    
  5. Create the default admin user, add it to the sudoer’s list and setup your ssh-key:

    adduser USERNAME
    usermod -a -G sudo USERNAME 
    mkdir /home/USERNAME/.ssh 
    echo "YOURSSSHKEYHERE" > /home/USERNAME/.ssh/authorized_keys 
    chown -R USERNAME:USERNAME /home/USERNAME/.ssh
    
  6. Add PuppetLabs as a repository and install puppet:

    echo -e "deb http://apt.puppetlabs.com/ squeeze main\ndeb-src http://apt.puppetlabs.com/ squeeze main" >> /etc/apt/sources.list.d/puppet.list 
    apt-key adv --keyserver keyserver.ubuntu.com --recv 4BD6EC30 
    apt-get update 
    apt-get install puppet
    
  7. Cleanup!

    apt-get --purge clean
    rm -f /etc/hostname 
    cat /dev/null > /etc/resolv.conf
    

    Let’s remove the current host ssh keys and create a script to auto generate them on the next boot.watch full Stayin’ Alive: A Grammy Salute to the Music of the Bee Gees 2017 film online

    rm -f /etc/ssh/ssh_host_*
    vi /etc/init.d/ssh_gen_host_keys
    

    Paste the script, a modified version of the one shown on HowToForge:

    #!/bin/sh
    ### BEGIN INIT INFO
    # Provides:          Generates new ssh host keys on first boot
    # Required-Start:    $remote_fs $syslog
    # Required-Stop:     $remote_fs $syslog
    # Default-Start:     2 3 4 5
    # Default-Stop:
    # Short-Description: Generates new ssh host keys on first boot
    # Description:       Generates new ssh host keys on first boot
    ### END INIT INFO
    ssh-keygen -f /etc/ssh/ssh_host_rsa_key -t rsa -N ""
    ssh-keygen -f /etc/ssh/ssh_host_dsa_key -t dsa -N ""
    /etc/init.d/ssh restart
    insserv -r /etc/init.d/ssh_gen_host_keys
    rm -f \$0
    

    After editing the file, make it executable and install it:

    chmod a+x /etc/init.d/ssh_gen_host_keys
    insserv /etc/init.d/ssh_gen_host_keys
    
  8. Done setting up the VM but don’t turn it off yet! Now take note of your VM ID (CTID) and ssh into Proxmox then run:

    vzctl set CTID --ipdel all --save
    

    You might want to tweak the /etc/network/interfaces now. Before continuing is a good idea to create an /tmp/excludes file with the following:

    .bash_history
    lost+found
    /dev/*
    /mnt/*
    /tmp/*
    /proc/*
    /sys/*
    /usr/src/*
    /etc/ssh/ssh_host*
    

    Stop the VM and change directory to the VM root:

    vzctl stop CTID
    cd /var/lib/vz/private/CTID
    

    Then, tar the directory:

    tar --numeric-owner -czvf /var/lib/vz/template/cache/debian-6.0-YOURCUSTOMTEMPLATE\_6.0-6\_i386.tar.gz -X /tmp/excludes .
    

After that it will be available as a template for you to create new OpenVZ containers from. Please note that the template name should match one of the conf files on /etc/vz/dists (in your Proxmox box), otherwise you will have to write yourself your own.

This was heavily based on the OpenVZ Wiki, How to create a CentOS template and on Proxmox Forums.

That’s it!

TinyMCE please paste as plain text by default!

tinymce-paste-plain

There are some WordPress blogs where users don’t always have an understanding of HTML tags and styling. They usually write their posts on Microsoft Word or other similar software and then paste it on WordPress, carrying a plethora of ugly tags that break all templates.

Reasoning with them is of no use, they don’t see that God is in the details.The Lego Batman Movie (2017)

Here comes this plugin that I just wrote, encapsulating what was well demonstrated by Dariusz Lyson on Stack Exchange.

Check it out on the wordpress.org plugins repo.

Struggling with FreeNAS power consumption optmization

DisksView

I’ve been using FreeNAS 8.3.0 for several months now and it became such a work/entertainment hub that I simply started take it for granted. Yesterday I needed to create a new dataset and just out of curiosity I looked at the Reporting tab. My box has been running for over a month now, with all four disks spinning the entire time.

Ok, spinning disks evolved a long way but this is just a waste of energy. If it is in the middle of the night or during the day when no one is at home it should be in stand-by mode. I don’t want to manually turn it on and off because I will forget it and I don’t want to schedule power on and add a cron job to shut it down because I might be working late some day or just finishing a extra long movie and have to wait until it boots up again.Movie Fifty Shades Darker (2017)

It has to be automatic and seamless

Thankfully FreeNAS offers settings on the web ui to manage this. After a lot of experimentation and frustation I figured it out:

First of all, these settings seem to come into effect only after a reboot. A little note somewhere would have been helpful.

Second, on the View Disks page, the HDD Standby and the Advanced Power Management settings seem to be mutually exclusive. The first one just a “dumb” timer (that I don’t know if is in seconds of minutes, it’s behaviour is erratic) and the second one is a setting that is applied by the HD firmware. APM values lower than 128 allow the drive to spin down when idle.

After reading How to find out if a drive is spinning down properly and HDD standby times not as expected on the FreeNAS forum I did a couple more experiments.

All my drives have been set with HDD Standby = Always On and Advanced Power Management = 64.

Reboot and a improved check-spinning.sh:

#!/bin/sh

while [ 1 ]
do
    echo -n `date`    
    camcontrol devlist | awk -F\( '{print $2'} | awk -F\, '{print $1}' |while read LINE
    do
        CM=$(camcontrol cmd $LINE -a "E5 00 00 00 00 00 00 00 00 00 00 00" -r - | awk '{print $10}')
        if [ "$CM" = "FF" ] ; then
            echo -n " SPINNING  "
        elif [ "$CM" = "00" ] ; then
            echo -n "   IDLE    "
        else 
            echo -n "  UNKNOWN  "
        fi
    done    
    echo
    sleep 5
done

I let it run for a few hours and the output was something like:

Sat Mar 16 21:41:23 BRT 2013   IDLE       IDLE       IDLE       IDLE    
Sat Mar 16 21:41:28 BRT 2013   IDLE       IDLE       IDLE       IDLE    
Sat Mar 16 21:41:33 BRT 2013   IDLE       IDLE       IDLE       IDLE    
Sat Mar 16 21:41:38 BRT 2013 SPINNING   SPINNING   SPINNING   SPINNING  
Sat Mar 16 21:41:48 BRT 2013 SPINNING   SPINNING   SPINNING   SPINNING  
Sat Mar 16 21:41:53 BRT 2013 SPINNING   SPINNING   SPINNING   SPINNING  
Sat Mar 16 21:41:59 BRT 2013 SPINNING   SPINNING   SPINNING   SPINNING  
Sat Mar 16 21:42:04 BRT 2013 SPINNING   SPINNING   SPINNING   SPINNING  
Sat Mar 16 21:42:10 BRT 2013 SPINNING   SPINNING   SPINNING   SPINNING  
Sat Mar 16 21:42:15 BRT 2013 SPINNING   SPINNING   SPINNING   SPINNING  
Sat Mar 16 21:42:20 BRT 2013 SPINNING   SPINNING   SPINNING   SPINNING  
Sat Mar 16 21:42:26 BRT 2013 SPINNING   SPINNING   SPINNING   SPINNING  
Sat Mar 16 21:42:31 BRT 2013 SPINNING   SPINNING   SPINNING   SPINNING  
Sat Mar 16 21:42:36 BRT 2013 SPINNING     IDLE       IDLE     SPINNING  
Sat Mar 16 21:42:41 BRT 2013 SPINNING     IDLE       IDLE     SPINNING

The second script I wrote was to log all processes touching the disk at the time that they were being used.Watch Full Movie Online Streaming Online and Download

Following some suggestions from StackExchange, check-top.sh:

#!/bin/sh

while [ 1 ]
do
    echo -n `date`
    top -I -mio -b -n -a 2
    sleep 1
done

With the long output of this one I cross-referenced all times and found out that the culprit were three python scripts scheduled to run every minute: /usr/local/bin/graph.py, /usr/local/www/freenasUI/tools/alert.py and /usr/local/www/freenasUI/tools/autosnap.py.

They are responsible for updating the reports on the gui, for updating alerts and for generating automatic snapshots and replicating them to offsite FreeNAS boxes.

Don’t get me wrong, I use and love these three features, but I don’t need to have these scripts running every minute. Basically, if I am not connected to my NAS reading or writing files to it, I don’t need automatic snapshots or replication (nothing is changing right?). If there is no one connected, there is also no need to updating reports that no one is seeing.

How did I fixed it?

I wrote a bash script to check the state of my disks before invoking the passed command. Here is /conf/base/etc/runifspinning.sh:

#!/bin/bash

while read LINE; do
    CM=$(camcontrol cmd $LINE -a "E5 00 00 00 00 00 00 00 00 00 00 00" -r - | awk '{print $10}')
    if [ "$CM" = "00" ] ; then
        exit 0
    fi
done < <(camcontrol devlist | awk -F\( '{print $2'} | awk -F\, '{print $1}')

$*

Then, I changed the base crontab at /conf/base/etc/crontab from:

*/5 *   *   *   *   root    /usr/local/bin/python /usr/local/bin/graph.py
*/5 *   *   *   *   root    /usr/local/bin/python /usr/local/www/freenasUI/tools/alert.py > /dev/null 2>&1
*   *   *   *   *   root    /usr/local/bin/python /usr/local/www/freenasUI/tools/autosnap.py > /dev/null 2>&1

To:

*/5 *   *   *   *   root    /conf/base/etc/runifspinning.sh /usr/local/bin/python /usr/local/bin/graph.py
*/5 *   *   *   *   root    /conf/base/etc/runifspinning.sh /usr/local/bin/python /usr/local/www/freenasUI/tools/alert.py > /dev/null 2>&1
*   *   *   *   *   root    /conf/base/etc/runifspinning.sh /usr/local/bin/python /usr/local/www/freenasUI/tools/autosnap.py > /dev/null 2>&1

Rebooted and bingo! Now, I still have all the functionality but only when the disks are already spinning. No more waking from idle to perform repetitive tasks.

A few last notes

  • To change the base configuration you will have to ssh into your box and be able to run commands as root.
  • All these changes will probably be lost when upgrading since they are not persisted on the config database.
  • To make /conf writable you can use mount -uw /.

Enabling sudo and persisting the sudoer’s list on FreeNAS

Fiddling with FreeNAS for a while now it was bothering me to have to su before running a command that requires root.

This sound simple but there is one caveat: If you just edit /usr/local/etc/sudoers, your changes will be lost when your box reboots.

FreeNAS stores some of its base files in /conf/base. To modify it you have to make it writable first, ssh into your box and run:

su
mount -uw /

Now you can nano /conf/base/etc/local/sudoers and add the following line:

%wheel ALL=(ALL) ALL

Make sure that the users that require sudo will be on the wheel group (you can change this from the web gui).

Reboot your FreeNAS and the system will apply this setting. That’s it!

More info on DistroGeeks, Karl Keppner and Sudo.ws.

Update 2013-10-26: Trying to apply this to FreeNAS 9.1.1 I noticed that the correct path to the persistent sudoers file is /conf/base/etc/local/sudoers.

Typo on hostname

Have you had the experience of finishing a Debian install only to figure out latter that it would have been better to use another name?

To do that you need to run the following as root:watch full film I Don’t Feel at Home in This World Anymore

echo "NEWHOSTNAME" > /etc/hostname
/etc/init.d/hostname.sh start

The first line replaces the contents of the /etc/hostname file with the NEWHOSTNAME, the second line runs the script that reloads it.

Simple as that.