Clone Ubuntu VMs in VirtualBox

Update: I found a permanent workaround for the udev ethernet interface problem. See this post.

I installed Ubuntu from mini.iso and I want to make several copies. What are the potential pitfalls? Well, the only real problem is that if we’re not careful they’ll all have the same MAC address. There’s a checkbox to avoid this in VirtualBox,

Reinitialize the MAC address checkbox

What a convenient checkbox!

which gets you halfway there, but when you boot your new system it will still remember that eth0 was that old MAC address. What’s worse is it will stall for over a minute while booting and tell you it’s waiting for network configuration…

Waiting up to 60 more seconds for network configuration

AAAAHHHHHHHHHHH!!!!

Update: I found a permanent workaround for the udev ethernet interface problem. See this post.

The easiest way to get through this is just to wait it out. Eventually you’ll get a login prompt. When you do, log in. Now you want to edit your udev rules so that you’re newly generated MAC address maps to eth0. That’ll fix everything.

sudo vi /etc/udev/rules.d/*-persistent-net.rules

Mine looks like this

# PCI device 0x8086:0x100e (e1000)
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="08:00:27:11:7c:8b", ATTR{dev_id}=="0x0", ATTR{type}=="1", KERNEL=="eth*", NAME="eth0"

# PCI device 0x8086:0x100e (e1000)
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="08:00:27:03:8e:5a", ATTR{dev_id}=="0x0", ATTR{type}=="1", KERNEL=="eth*", NAME="eth1"

I’m just going to delete the old eth0 entry (the first one) and change the name of the new one to eth0.

# PCI device 0x8086:0x100e (e1000)
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="08:00:27:03:8e:5a", ATTR{dev_id}=="0x0", ATTR{type}=="1", KERNEL=="eth*", NAME="eth0"

A quick reboot and everything’s shiny. :)

Posted in VirtualBox | Tagged , , , | 7 Comments

Depaginate Phoronix Articles

I sketched this for Phoronix, but you could easily adapt it to any site that uses pagination. I think it’s an interesting problem, so if you want to see another site depaginated, just ask me in a comment and I’ll check it out.

Here’s the script. It’s also available as a Gist if you want to tinker with it.

/**
 * Depaginate Phoronix articles
 *
 * Script by Justin Force, 2012. Released to the public domain.
 *
 * Stick this in whatever user scripts you have. I keep an unpacked Chrome
 * extension. You could also use this with GreaseMonkey or something as long as
 * you load jQuery first.
 */


$(function () {
  $.each($('.phxcms_navigation_format a').filter(function () {
    return $(this).attr('href').match(/&num=/) && !$(this).text().match(/Next Page/);
  }), function () {
    var div = $('<div>');
    div.load($(this).attr('href') + ' #phxcms_content_phx', function () {
      div.find('.phxcms_navigation_format').remove();
    });
    $('#phxcms_content_phx').append(div);
  });
});
Posted in javascript, quickies | Tagged , , , , , , , , | Leave a comment

Dropbox: Unable to monitor filesystem

Have you seen this while using Dropbox in Linux?

Unable to monitor filesystem
Please run: echo 100000 | sudo tee /proc/sys/fs/inotify/max_user_watches
and restart Dropbox to correct the problem.

Apparently, Linux is imposing some kind of default restriction of the number of files that you can monitor with inotify. This is not Dropbox’s fault. This is a system-level setting, so you’ll need root to fix it. The easiest way to fix it is to tell sysctl to increase the limit. Just run this command to add the sysctl configuration:

echo "fs.inotify.max_user_watches = 99999999999" | sudo tee /etc/sysctl.d/20-dropbox-inotify.conf

And to load the new configuration immediately, reboot or just run this:

sudo sysctl -p /etc/sysctl.d/20-dropbox-inotify.conf

That should permanently fix it.

Posted in Dropbox | Tagged , , , , | Leave a comment

extconf.rb:4:in `require’: no such file to load — mkmf (LoadError)

I’ve hit this a dozen times and I always forget the solution. When you get this on Ubuntu,

sudo apt-get install ruby-dev
Posted in ruby | Tagged , , , , , , , | 1 Comment

ssh Through Two Hosts in One Command

Say you want a shell on a box but you have to hop through another box to get it.

force@harold:~% ssh admin@maude
maude:~ force$ ssh root@whee
root@whee:~$ echo w00t
w00t

You can do that in one line using -t (pseudo-tty allocation).

force@harold:~% ssh -t admin@maude ssh root@whee
root@whee:~$ echo w00t
w00t

After playing with it, I can only get this to work for one hop. So yes, it’s of limited use. But I hop through another box every time I ssh at work, so I’m going to be getting a lot of mileage out of this little optimization. :)

Posted in quickies | Tagged , , , , , | Leave a comment

JavaScript Performance: Iterating over Arrays with Holes

I was sketching some code that basically took some data from a database and cached it in an array to be repeatedly reused. Conveniently, each row from the database had a unique ID, so I could use that as an array index. But is that a good idea? It just means that when I iterate over the array, I’ll need to check whether an element is defined before I try to use it. Is it better to code around this? Say, storing the id and other information in an object and simply pushing the objects onto the array? This would save needlessly iterating over undefined values later. Or is it better to use the id as an array index so that I can do quick and dirty iterations with checking? This sounds like a job for jsPerf! So, following are the details of my experiment. If you like, you can check it out in full and try the jsPerf yourself.

The Setup

Here are the simple and complex arrays, the select indexes for the entries that we want to look up later, and the pairs of IDs and names that comprise our data. At the end, there’s a get() function so that we can look up entries without a numerical index.

var simple = [], complex = [],
  selectIndexes = [1, 2, 10, 14, 300, 1000],
  pairs = [
    {"id": 1,    "name": "Albert"   },
    {"id": 2,    "name": "Bailey"   },
    {"id": 4,    "name": "Charlotte"},
    {"id": 5,    "name": "Darlene"  },
    {"id": 10,   "name": "Edna"     },
    {"id": 12,   "name": "Faron"    },
    {"id": 13,   "name": "Gary"     },
    {"id": 14,   "name": "Helen"    },
    {"id": 15,   "name": "Igor"     },
    {"id": 16,   "name": "Justin"   },
    {"id": 17,   "name": "Kyle"     },
    {"id": 300,  "name": "Lynette"  },
    {"id": 500,  "name": "Morgan"   },
    {"id": 1000, "name": "Nora"     }
  ],

  get = function (a, i) {
    var ret = null;
    $.each(a, function () {
      if (this.id === i.valueOf()) {
        ret = this;
      }
    });
    return ret;
  };

The Experiment

simple

A simple array with integer indexes and holes.

$.each(pairs, function () {
  simple[this.id] = this.name;
});

$.each(simple, function () {
  if (this && this !== window) { // this !== window is a workaround for IE
    this.charAt(0);
  }
});

$.each(selectIndexes, function () {
  simple[this].charAt(0);
});

complex

A complex array which holds objects with id and name attributes. Since we’re not using integer indices, we have to supply a get function (defined in Setup above).

$.each(pairs, function () {
  complex.push(this);
});

$.each(complex, function () {
  this.name.charAt(0);
});

$.each(selectIndexes, function () {
  get(complex, this).name.charAt(0);
});

The Results

Have a look at this graph:

Browser performance graph. Simple arrays with holes are the clear winner.

Simple arrays with holes are the clear winner.

Conclusion

The simple implementation just wipes the floor with complex one across the board. Not only is it easier to implement and read, but checking whether an element is undefined before you access it appears to be a trivially cheap operation. Arrays with holes are simple and safe to use.

Posted in javascript, Uncategorized | Tagged , , , , , , , | 3 Comments

Defining Functions Inline Is Just Fine

I put together this jsperf to see if there’s a difference between defining your function and then using it or defining it inline when looping with jQuery.each. You can try it yourself if you like, but here are the results:

I compared using a pre-defined function with using a function defined inline.

var body = $('body'); // cache this for use with both

//pre-defined
//
var doSomething = function () {
  body.append('<div class=' + this);
}
$.each([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], doSomething);

// inline
//
$.each([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], function () {
  body.append('<div class=' + this);
});

Here’s a graph of the results of different browsers performing the test:

Performance of different browsers running $.each with pre-defined and inline functions

Don't take this as an indication of the general performance of the browsers mentioned. Some were run on Linux, some on Mac, one in Wine on Linux, and some in Windows VMs on Linux.

The difference is negligible, so use whichever looks more readable to you. I prefer defining my functions inline, and will now do so with confidence.

Posted in javascript | Tagged , , , , | 1 Comment

Show Duplicate Filenames with Ruby

Here’s a quick one. I want to show just the names of files that have the same basename in a directory.

Code

require 'find'
require 'set'

all_files  = []
duplicates = {}
basenames  = Set.new

ARGV.each do |dir|
  Find.find dir do |file|
    basename = File.basename file
    all_files << file
    unless basenames.add? basename
      if duplicates[basename]
        duplicates[basename] << file
      else
        duplicates[basename] = [all_files.find{|f| File.basename(f) == basename}, file]
      end
    end
  end
end

puts duplicates.values.flatten.sort_by.with_index{|f,i| [File.basename(f), i]}

Example

Here’s some example terminal interaction.

force@zit:/tmp/demo% find .                                                                                                              .
./Pictures
./Pictures/February
./Pictures/February/family.jpg
./Pictures/February/fruit.jpg
./Pictures/index.html
./Pictures/January
./Pictures/January/car.jpg
./Pictures/January/family.jpg
./Pictures/March
./Pictures/March/dogs.jpg
./Pictures/March/cats.jpg
./Videos
./Videos/Aruba
./Videos/Aruba/scenic.mkv
./Videos/Aruba/water.mkv
./Videos/index.html
./Videos/Grand Canyon
./Videos/Grand Canyon/scenic.mkv
./Videos/Grand Canyon/water.avi
./Videos/Grand Canyon/walls.mkv

force@zit:/tmp/demo% ruby ~/show_duplicates.rb .                                                                                         ./Pictures/February/family.jpg
./Pictures/January/family.jpg
./Pictures/index.html
./Videos/index.html
./Videos/Aruba/scenic.mkv
./Videos/Grand Canyon/scenic.mkv
force@zit:/tmp/demo%
Posted in ruby | Tagged , , , | Leave a comment

Ruby instance_eval gotcha

I don’t know how much of a “gotcha” this is since it’s just a defined behavior and feature of the language, but I still found it a little counterintuitive when I encountered it, and I had to actually read the source of the Gem I was using to figure out what was going on. Specifically, I was doing this with Sinatra and Mail:

get '/' do
  @body = params[:body]
  Mail.new do
    to      'someone@example.com'
    from    'me@example.com'
    subject 'Gotcha!'
    body    @body
  end
end

That resulted in an

undefined method `length' for :Mail::Body

around that body @body line. Hrm? So I look at the source, and I find that after a bit of wrapping, eventually Mail’s message.rb is calling this.

instance_eval(&block)

And that’s how I learned about instance_eval. It takes a block, then executes that block in the context of the instance which calls it. So it effectively changes any references to self to be the instance where you write it. Therefore, my reference to @body was looking for @body in Message and not in my own app. This will probably be clearer with another example. Say we have these two classes:

class Foo

  def speak
    'arf!'
  end

  def act &block
    local = true
    external = 0
    instance_eval &block
  end
end

class Bar

  def speak
    'hello'
  end

  def dizzy?
    true
  end

  def do
    @name = 'dirt'
    age = 27
    local = false

    p speak    # "hello"
    p dizzy?   # true
    p @name    # "dirt"
    p age      # 27

    Foo.new.act do
      p speak    # "arf!" - because we are referring to Foo::speak
      p dizzy?   # Runtime error! Foo does not have a dizzy? method!
      p @name    # Runtime error! Foo does not have a @name variable!
      p external # Runtime error! This block doesn't have access to local variables in Foo::act!
      p age      # 27     - wait...that worked?
      p local    # false  - This local is used and not the one from Foo::act.
    end
  end

end

Bar.new.do

Gotcha again! Your local variables are still passed into the block!

So, instance_eval is why the rest of that block works with the calls to the to, from, and subject methods. Those methods aren’t defined in my context, and that was my first clue. Those methods are defined in Message‘s context, and instance_eval gives my block access to them, but that instance can now access my local variables (but not my instance variables), and my block cannot access the local variables of the context calling instance_eval.

Whew! I get it now, but it still sounds a bit complicated.

Posted in ruby | Tagged , , , , | 1 Comment

JavaScript Strings: instanceof vs. typeof

I’ve discovered some really interesting (read as “horrible”) behavior in Safari 5 and Internet Explorer 9. I was using this with great success in Chrome and Firefox.

if (typeof this === 'string') {
    doStuffWith(this);
}

Then I test in IE9, and it doesn’t work at all. Big surprise. But in Safari, it’s intermittent! So I start debugging, and I find that Internet Explorer is always returning false. But the weirdest thing is that Safari seems to be doing some kind of optimization in its JavaScript VM where it is true the first time, but false every time you hit reload!

My brain almost exploded.

So now I’ve settled on this:

if (this instanceof String || typeof this === 'string')
    doStuffWith(this.toString());
}

And now everything works great. Note that you can call “a string”.toString() and it just returns a copy of the string, i.e.

"a string".toString() === new String("a string").toString(); // true

So I’ll be using both from now on.

Posted in javascript | Tagged , , , , , , , , , , , , | Leave a comment