Mounting partitions contained in a disk image in Linux

Say you have a disk image created using ‘dd’. You want to mount a partition contained on it but you realize it’s not as simple as just mounting the image and exploring the path.

The kpartx utility can help with that:

sudo kpartx -av disk.img

This should have created some partition mappings to /dev/mapper. Now, just mount the correct mapping the same way you would mount anything else. For instance:

sudo mount -t ntfs -o ro,noexec /dev/mapper/loop0p1 mounted

Drawing and connecting nodes with SVG

This is something I created over a year ago and I’ve decided to share the code in case someone finds it useful.

nodes

It’s nothing more than an example of using JavaScript to dynamically add connectable SVG objects.

Usage: 

  • Click a node and drag
  • Double-click to generate a node
  • Click a node, hold down shift, then release over another node to connect

Demo (Should be supported by most modern browsers)

Download ZIP

Detecting Smith numbers

After learning about Smith numbers today I cooked up this script fairly quickly. There are many places where it could be optimized and cleaned up but that wasn’t really the point – I just needed something that was accurate, and I’m pretty sure this is.

It also helped me to learn about the ulimit command after running it with a HUGE number that had taken up about 7GB of swap before I had to kill the process remotely via SSH…


#!/usr/bin/python
# smith number detector - TWS 1-12-13

def detectSmith(num):
  if(sumFactors(num) == sumDigits(num)):
    return True
  else:
    return False

# calculate the sum of the factors
def sumFactors(num):
  factorList = factors(num)
  sum = 0

  for factor in factorList:
    if factor > 9:
      # if a number (base ten) has multiple digits it has to be broken down
      sum = sum + sumDigits(factor)
    else:
      sum = sum + factor

  return sum

# sum the digits in a number with two or more
def sumDigits(num):
  sum = 0  

  if num > 9:
    while num > 9:
      sum = sum + num % 10
      num = num / 10
    sum = sum + num
    return sum
  else:
    # should never reach this
    return num

def factors(num):
  primeFactorList = []

  factor = lowestPrimeFactor(num)
  currentNum = num

  while factor != -1:
    primeFactorList.append(factor)
    currentNum = currentNum / factor
    factor = lowestPrimeFactor(currentNum)

  if(currentNum != num):
    # preventing returning the number passed in
    primeFactorList.append(currentNum)

  return primeFactorList

def lowestPrimeFactor(num):
  for x in range(2, (num / 2) + 1):
    if(num % x == 0):
      return x
  return -1

for number in range(1, 1000):
  if(detectSmith(number)):
    print "The number", number, "is a Smith number."

Ubuntu, PHP, S3, and AWS SDK2 Example

A super quick, super simple little example of how to get started with the AWS SDK2 for PHP. It connects to the S3 service with your credentials and checks to see if a file exists.

  1. Assuming you already have a LAMP setup, you may need to install php-pear:
    sudo apt-get install php-pear
  2. Add the AWS channel and auto discover dependencies:
    pear channel-discover pear.amazonwebservices.com
    pear -D auto_discover=1 install pear.amazonwebservices.com/sdk
  3. Download the .phar file here and throw it on your server in this script’s directory (for this example).
  4. Replace the items in brackets and run the script. The [OBJECT KEY] is just the name of the file, so ‘mypicture.jpg’ in the root of the bucket would have the key ‘mypicture.jpg’.
 <?php

require 'aws.phar';

use Aws\Common\Aws;

$aws = Aws::factory(array(
   'key' => '[KEY HERE]',
   'secret' => '[SECRET HERE]'
));

$client = $aws->get('s3');

echo $client->doesObjectExist('[BUCKET NAME]', '[OBJECT KEY]');

?>

You’ll get back the boolean response of that call (assuming it makes it that far without error). Refer to the API documentation for the other methods and their responses.

A simple Linux content search script

A simple script I use for work to search the contents of files. It just wraps up ‘find’ and ‘grep’ to recursively search and display the location of the file.

#!/bin/bash
# Tanner Stokes - 8-23-12
# Search contents of files

if [ -z "$1" ] || [ -z "$2" ]
then
echo "Usage: csearch \"filetype\" \"content\""
echo "ex: csearch \"*.php\" \"my_function()\""
exit 1
fi

find . -name "$1" -exec grep -Hn "$2" {} \;

Some derpy numbers

Now that this site has basically turned into ‘herp derp‘ central, I thought I’d post some numbers up regarding downloads. Running this command on my Apache access logs provided me with the download numbers for each browser version (in this case Firefox):

cat access.log | grep xpi | colrm 16 | sort | uniq | wc -l

 

Note: This method isn’t exactly super precise, it would only count once per IP, but it does the job. It also doesn’t account for other sites like download.com.

As of this post:

Chrome: 4722
Firefox: 2564
Safari: 531
Opera: 278

Update: 7-12-12

Shortly after creating this post I got picked up by Boing Boing, Gizmodo, and Reddit. New numbers:

Chrome: 8026
Firefox: 4025
Safari: 919
Opera: 415

Go Herp Derp! 

Herp Derp updated

I got around to fixing some annoying bugs in Herp Derp tonight:

  • When loaded via HTTP Secure (HTTPS) Herp Derp now works (thanks for reminding me @Funshine__Bear)
  • Firefox no longer freaks out when there is only one comment
  • YouTube Feather Mode is now supported (thanks @_sjs)

The latest version is available for all browsers and can be downloaded here.