Burning an ISO to a USB Stick on Mac OSX

My kids have dropped my 10+ year old Sony Vaio laptop one final time, so it’s time to find another old laptop (they’ll do it again) & put a fresh Operating System (OS) on it. For the past 5 years the old Vaio had been running CrunchBang OS (Statler build) with no problems at all. It’s fast & minimalist, totally perfect for little monkeys (& significant others). Another big plus is the kids have previously downloaded a few viruses, but as CrunchBang is a variant of the Linux OS the viruses have had no effect as they are targeting Windows OS. Linux has it’s security right, with superuser’s only allowed to install software that may do any real damage… You can still get viruses for Linux, but there aren’t as many for sure.

Anyway you probably knew all that already? Last time I installed CrunchBang I burned a CD, but I wanted to get down with the cool kids and use one of those new fangled USB sticks to install it. Plus wowzas, the latest Waldorf build has an ISO that is bigger than a CD now! CrunchBang have some documentation on how to burn to USB, but from another Linux box. So I thought I’d put down what you need to do if you’re on OSX. My google-fu failed me when I tried to look for “Burn a ISO image to USB stick on OSX”

Firstly it’s really important to find out which device your USB stick is.

Side story: I once worked for a company whose multi-million pound virtualization system was wiped clean by one of our “IT staff” deciding it was a good idea to format their USB stick from the actual virtualisation machine. It had devastating effect; they also discovered that some important services were not being backed up! Oh my, the :shit: really hit the fan that day!! So please be careful.

If they’d just followed these following steps that should not have happened.

Find the correct device first:

  • Make sure the USB stick is removed from your machine

  • Open a terminal

  • Type diskutil list

    You then should see something similar to this:

1
2
3
4
5
6
7
/dev/disk0
   #:                       TYPE NAME                    SIZE       IDENTIFIER
   0:      GUID_partition_scheme                        *500.1 GB   disk0
   1:                        EFI                         209.7 MB   disk0s1
   2:                  Apple_HFS Macintosh HD            429.9 GB   disk0s2
   3:                 Apple_Boot Recovery HD             650.0 MB   disk0s3
   4:       Microsoft Basic Data BOOTCAMP                69.3 GB    disk0s4

This is the disk (& partitions) that I do not want to touch!!! You may have more than one?

  • now put the USB stick in and run diskutil list again
1
2
3
4
5
6
7
8
9
10
11
/dev/disk0
   #:                       TYPE NAME                    SIZE       IDENTIFIER
   0:      GUID_partition_scheme                        *500.1 GB   disk0
   1:                        EFI                         209.7 MB   disk0s1
   2:                  Apple_HFS Macintosh HD            429.9 GB   disk0s2
   3:                 Apple_Boot Recovery HD             650.0 MB   disk0s3
   4:       Microsoft Basic Data BOOTCAMP                69.3 GB    disk0s4
/dev/disk1
   #:                       TYPE NAME                    SIZE       IDENTIFIER
   0:     FDisk_partition_scheme                        *8.1 GB     disk1
   1:                 DOS_FAT_32 NO NAME                 8.1 GB     disk1s1

Now you can see there is a new device. That’s the USB stick!!! /dev/disk1 (You should also probably check that it’s big enough to take the ISO you want to burn.)

Create a DMG

  • Before you can burn to USB you have to convert the ISO to a dmg file:

hdiutil convert -format UDRW -o crunchbang crunchbang-11-20130119-i686.iso

Where crunchbang-11-20130119-i686.iso is the current download from CrunchBang, this could of course be *any* ISO.

Burn to the USB stick

  • When that’s complete run sudo dd if=crunchbang.dmg of=/dev/diskN bs=8192

    where the /dev/diskN is the USB disk we discovered earlier. In my case /dev/disk1. I’ll repeat it once more, incase you are stupid and you made it this far ;)

    => !!!!please make sure you reference the correct USB disk!!!! <==

    From now on I’ll reference it as diskN so you don’t copy and paste, and then access the wrong disk! :)

  • If you get the error message: dd: /dev/diskN: Resource busy

    Then run: diskutil unmount diskN and rerun the last command.

Winning!

  • When complete you should see something like:
1
2
3
100224+0 records in
100224+0 records out
821035008 bytes transferred in 132.323860 secs (6204739 bytes/sec)
  • Finally, to safely eject the usb stick run diskutil eject /dev/diskN

Now you have a USB stick (that you may boot from if the ISO is meant to be booted)!

BTW. You may have to change your BIOS setting to boot from the USB stick?

SHRD : A Ruby Interpretation of the Double Precision Shift Right Assembly Instruction

Following on from my last post on Assembly conversion, I also came across the SHRD instruction.

I already knew about the SHR & SHL command, logical shift right & left respectively, which translates rather easily:

AssemblyRuby
1
shr @ax, 5
1
@ax >>= 5
1
shl @ax, 5
1
@ax <<= 5

But I was initially stumped when I saw

1
shrd ax, dl, cl

in the Assembly code listing.

I’d figured out I needed the following test data, from the debugger

1
2
3
def test_shrd
  assert_equal 12927, shrd(40919, 140, 6)
end

And then this link gave me the insight I needed to code it up.

Voila:

1
2
3
4
5
6
#see https://en.wikibooks.org/wiki/X86_Assembly/Shift_and_Rotate#Extended_Shift_Instructions for more info on shrd
def shrd(rm16, r16, cl)
  rm16 >>= cl
  r16 <<= (16 - cl)
  rm16 | (r16 & 0xFFFF)
end

The test went from red to green :)

They will not control us. We will be victourious!!!!!!”

:D

Really Controlling the Flow: From Assembly to Ruby

I’ve recently been converting a bunch of Assembly code into Ruby. ZOMG Why, Why, Why? You may well ask. Well the Assembly code is Intel x86 flavoured, it’s never been run on Linux & we want to run on Linux. I could have converted to C, but everyone in my current team writes Ruby, and most importantly Ruby allows me (and you) to control the machine, rather than the other way around. We are the masters. They are the slaves!

My main issue with the conversion was figuring out what the JMP type conditional commands equivalent was in Ruby. From my 2 minutes of google searching I found there doesn’t seem to be a resource on the web that has a simple quick reference.

So without further ado, here’s my first stab at an Intel x86 Assembly to Ruby for JMP commands cheatsheet.

( Given there is a function/method called func0 & a register/variable named al )
AssemblyRuby
1
jmp func0
1
func0
1
2
cmp al, 07Bh
jc func0
1
func0 unless al - 0x7B >= 0
1
2
cmp al, 07Bh
jnc func0
1
func0 if al - 0x7B >= 0
1
2
cmp al, 07Bh
jnz func0
1
func0 unless al - 0x7B == 0
1
2
cmp al, 07Bh
jz func0
1
func0 if al - 0x7B == 0
1
2
test al, 07Bh
jnz func0
1
func0 unless al & 0x7B == 0
1
2
test al, 07Bh
jz func0
1
func0 if al & 0x7B == 0
1
2
and al, 07Bh
jnz func0
1
func0 unless al &= 0x7B == 0
1
2
and al, 07Bh
jz func0
1
func0 if al &= 0x7B == 0

If you see a mistake, have a query or a suggestion to update or just found this useful, please let me know.