Skip to content

Archive for

12
Dec

Mount KVM images on Linux

Just a quick little script I use once in a while, which I need to mount my KVM images, to access the files manually.

The way it works is to calculate where the data starts, then simply pass that as a paramter to the mount command.

simple, but very useful! 😉


#!/bin/bash
if [ $# -lt 2 ]
        then
        echo "Usage $0 image mountpoint"
        exit
fi

IMAGEFILE=$1
MOUNTPOINT=$2

SECTORSIZE=$(parted -s $IMAGEFILE unit s print | grep "Sector size" | awk -F": " {'print $2'} | awk -F"B/" {'print $1'})
OFFSETBLOCKS=$(parted -s $IMAGEFILE unit s print | grep "^ 3" | awk {'print $2'} | awk -Fs {'print $1'})
OFFSETSECTORS=$(echo "$SECTORSIZE * $OFFSETBLOCKS"  | bc)
mount -o loop,offset=$OFFSETSECTORS $IMAGEFILE $MOUNTPOINT

10
Dec

How to write a simple ajax game loop in Javascript

A friend of mine from the Java world has been trying to get into JS lately.

He understood the basics of how JS works – Functions look (on the outside) superficially similar to Java, it has standard loops/variables, etc.
Canvas functions are very straight forward- The part that confused him was “How do I create a game loop”, and “How do I update the page based on JSON responses from my server?”

Luckily, both of those are REALLY easy in Javascript!

I’m certainly not a Javascript expert, but I put together a simple demonstration, with an over-the-top number of comments.
GameShot

We’ll start with our very simple HTML page.
Anyone who’s done even “Hello World” should understand most of what we’re doing here.

We’re loading our JS, then three divs – Header, Game, and Footer.

<html>
<head>
    <title>This is an example</title>

    <!-- 
         Include are our JS scripts. The "defer" line just tells it to run this after the page loads.
         It's not actually needed, but it makes the initial load faster 
         We're loading in jQuery via a CDN, but copying it locally would work just as well.
         This is just one less file to deal with.
     -->

    <script defer src="http://code.jquery.com/jquery.js"></script>
    <script defer src="exampleCode.js"></script>
</head>

    <!-- 
        In the body (below), define the game however you want it to look.
        I'm using 3 divs, just for the sake of showing we're only showing/changing the game div.
        There's nothing special about this setup.
     -->

<body>
    <div id="Intro">
        This is the beginning of the game.
    </div>
    <div id="Game">
        The GAME GOES HERE.
    </div>
    <div id="Footer">
        This is the bottom.
    </div>
</body>

Now this is calling our JS, exampleCode.js –
That’s also very simple. We’re just going to do two “game loops” to update the page.
One of which updates the color, and the other uses an AJAX request


// This function does the logic of updating our game div.
// Obviously, in a real game, this would be more interesting.
function ChangeColor()
{ 

  // in jQuery, the $ character gives you the root jQuery object.
  // You can use the $(FOO) notation gives you a reference to any element
  // Any element you want. It's just like a CSS selector.
  // So-
  // $('#foo') gives you a HTML element with id="foo"
  // $('.foo') gives you every element with the class="foo" (loop through them)

  // The next line creates an array of random colors.
  bgcolorlist= Array("#DFDFFF", "#FFFFBF", "#80FF80", "#EAEAFF", "#C9FFA8", "#F7F7F7", "#FFFFFF", "#DDDD00");

  // Now, Randomly choose one, by picking a random number (between 1 and arraysize), and then get that array element.
  randombgcolor = bgcolorlist[Math.floor(Math.random()*bgcolorlist.length)];

  // Now, in jQuery, you can set a CSS property, by using the .css function.
  // I'm using it to set the background for the HTML element #Game.

  $("#Game").css('background',randombgcolor);

}

// Let's try a slightly more interesting function. We'll add an external call.
function UpdateTime()
{ 

  // Now, let's do something Ajaxy-
  // There is a URL endpoint for Tavern that gives us the server status, including the time.
  // We can use that to display the current time on our page.

  // The $.ajax() function doesn't return the JSON.. It RUNS the success value as a function.
  // That function will save the variable for us.

  // Normally, we could use $.get(), or $.getJSON(), but these just call $.ajax() under the hood.
  // By calling $.ajax() directly, we can set more options.

  // set the result variable ahead of time, so it's scope isn't only inside the function.
  var result;


  // make the AJAX call
  $.ajax({
    // async defaults to True- This means that the result will wait for a return before running anything else.
    async: false,
    // Setting the dataType to JSON is a convenience. It automatically parses it into a JSON object for us.
    dataType:"json",
    // This is the URL we're pulling from. I set this up to be accessible from any domain.
    // Normally, you can only make requests to the SAME DOMAIN as the HTML runs from.
    // But you can bypass this with a header (Access-Control-Allow-Origin), which I've set for you.
    url: "http://gettavern.com:8090/status",

    // This is just our super-simple function that gets called on success.
    // ALL it's doing is setting the result back to that variable we declared before.

    // Normally, in JS, you'd just do whatever it is you actually needed with your drawing/etc, right in the inline function..
    // Or have it call a longer function, that's defined normally (like the one we're in!)
    // But in this case, I wanted to a) illustrate Inline functions.
    //                           and b) make it short ;)
    success: function(data) {
        result = data;
    }
  });




  // Now, make that into something printable. Notice how we pull values out of the JS call as if it were a dictionary/hashmap?
  timeStampString = "The current timestamp is " + result['timestamp'] + ".";

  // Now, let's DO something with this data-
  // create a new javascript Date object based on the timestamp
  // multiplied by 1000 so that the argument is in milliseconds, not seconds
  var date = new Date(result['timestamp']*1000);
  // hours part from the timestamp
  var hours = date.getHours();
  // minutes part from the timestamp
  var minutes = date.getMinutes();
  // seconds part from the timestamp
  var seconds = date.getSeconds();

  // will display time in 10:30:23 format
  var formattedTime = "
This is equal to " + hours + ":" + minutes + ":" + seconds; // This redefines the HTML inside the game object to whatever we pass it. In this case, it's the time. // Get the Game's DOM object // This gets the instance of the Game, just like we were in the first function. game = $("#Game"); // Replace the Game's HTML. // Just like used the background function in the first example (above), here we're overwriting the HTML that's in the Game object. // Everytime this runs, everthing that is in the game div is erased, and replaced with whatever string we pass. // In this case, we're passing in our two time-based strings we just created. game.html(timeStampString + formattedTime); } // Run the code above, every 500ms self.setInterval("ChangeColor()",500); self.setInterval("UpdateTime()",1000);

You can see this in action at jsfiddle.

It’s all very very straightforward code, but it occured to me that it might be useful to others, so I’ve archived it here for anyone who finds it 😉

5
Dec

iStat Menus demotes user’s licenses with sneaky upgrade

For the last few years, I’ve been using a cute little tool called iStat menus to graph my CPU/Memory in my address bar.
It doesn’t do that much, but it works, it stays out of my way, and the UI is better than the nearest free alternative.

Menu2

I don’t think about the tool very much – Once in a while I’ll glance up when the system is running slow, or use the clock function to see the time in multiple time-zones.

Every so often, there’s a new version available, and the software prompts me to upgrade –

Upg1 2

Upg2 3
I generally go ahead and agree to the change, and go about my business.

Although my 1999 self would cry to hear me say it, I don’t always read through the changelogs-

We get them everywhere now – On my Android Phone (Yep, go ahead, update), iPhone (Sure. Update), OSX (OK, add the new release), etc.

In my professional life managing servers, I review the changes, weigh the pros and cons, and decide if I want to upgrade – But for a simple Menu app, that seems overkill.

This week, iStat released another upgrade, and once more I upgraded without really paying much attention.
I probably should have, but realistically, I don’t think most users will.
Upg3

As you can see in the screenshot however, this upgrade to iStat Pro 4 is a paid upgrade.
Accepting this upgrade downgraded my paid software to 14-day trial status.

Trial

I’m not the only one who felt that wasn’t cool.

iStat is offering upgrade pricing ($9), and the app isn’t that expensive in and of itself, but the entire process felt very sketchy.[1]

Users are used to accepting in-app upgrades, and switching one out for a paid-upgrade feels very disingenuous, regardless of their justifications.
Bjango (the creators) could have handled things differently,by setting the “Remind me Later” Dialog option to be the blue/rightmost option, or even installing as a different name in the Applications folder, so you could still easily fire up v3.

Admittedly, It’s a minor issue, $9 isn’t much, and I don’t begrudge an indie shop trying to raise money for a new version and stand in business, but the sense of fairplay has been violated- It’s one more reason for me to try to avoid buying non App Store applications in the future.

For anyone else who runs into this, You can uninstall iStat 4 using the menu item-
Uninstall
And then reinstall Version 3.


[1]For what it’s worth, I did end up buying the upgrade. The UI is a bit nicer.
This isn’t about the $9. It’s about the process.