How to burn an image file to USB in Ubuntu Linux?

This is a quick tip. In Windows, we’ve lot of free and paid alternatives to write an image file to USB device(e.g ImgBurn, Win32DiskImager). Let me explain my requirement. If I get a bootable ISO image file of an operating system like Ubuntu or moblin how would you do that if you’re in a Linux environment? A byte-exact copy of the ISO image must be placed on the USB drive. It is not sufficient to simply copy the image file to the drive.

It’s quite simple to do that

unmount the USB device if it’s automatically mounted to the system.

Step 1

You can either use # umount <usb-drive> command or directly unplug using your nautilus file manager(windows shell equivalent)

Step 2

Image Writer is a small python executable script that detects your USB drive and writes the image to it. The advantage of using image writer is that it will not inadvertently overwrite your system hard drive. Download Image Writer. Navigate to the desired location where you kept the image writer and change its permission to execute.

# cd <directory with downloaded image-writer file>
# chmod a+x ./image-writer
# ./image-writer <image file>

Now you’re done!

There’s a alternative method using ‘dd’ which you can see in the following link. But it’s very risky. It may erase your whole system files if it’s not used carefully.

Source: moblin help

How to write your own trace function?

Most of us are using TRACE, ATLTRACE macros, OuputDebugString API for  debugging purpose during our development. [Go to end and check the function if you don't want to hear more about the basics of debug strings] Debug strings are used for debugging purpose and it’s displayed by debugger during debugging. You can see it in the output window of most of the debuggers (especially in Visual Studio) and also some smart utilities like DbgView captures the system wide debugging string and it displays in it’s own window without debugging any of the process.

Mainly OutputDebugString function provided by Kernel32 DLL is used for “outputing” debug strings. There are other altenatives available like TRACE, ATLTRACE2 macros, afxDump function etc. and most of these are actually wrapping OutputDebugString API and provides flavored functionalities like formatting like printf string, dumping MFC objects etc. The most widely used are TRACE macros. There are some more alternatives for TRACE macros like TRACE0, TRACE1, TRACE2 (deprecated now). It’s nothing but impossing restrictions on the number of parameters.

Trace macros are supposed to work with debug build of the applciation and in release mode, the traces will not be available. If we approach OutputDebugString API, we will have to first format string using sprintf or CString::Format function and then pass to API. But this adds the burden of declaring temporary buffer/CString objects every time we call this function as follows

[sourcecode language='cpp']
CString csMessage;
csMessage.Format(_T(“My Data:%d”), nData );
OutputDebugString(csMessage).
[/sourcecode]

You will always have to repeat the above code wherever you need a debug trace. Ofcourse TRACE macro can be used as printf like function, but only in debug mode. So it’s better to write our own trace functions. 

The following function is  variadic function which can be used as printf function with format specifierss

[sourcecode language='cpp']
void M4DTrace( LPCTSTR lpctFormat, … )
{
static TCHAR szBuffer[1000];
va_list ArgList;
va_start( ArgList, lpctFormat );
_vstprintf_s( szBuffer, lpctFormat, ArgList );
va_end( ArgList );
OutputDebugString( szBuffer );
}
[/sourcecode]

Instead of _vsntprintf_s, you can use another formating function called wvsprintf
but I found it has some problems formatting float variables in the list. The above function can runs under both UNICODE and MBCS environment.
If you need to know under the hood of OutputDebugString, please check this article
Updated[2009-Apr-17) – updated formatting with _vstprintf_s instead of _vsntprintf_s

How to check a given number is exact power of two?

When I was digging into OpenGL texture mapping, I was checking with some sample code (I’m not remembering from where I downloaded that), inside the code, I found a simple line to check whether the texture dimensions are power of two or not. If the texture is non power of two, the program had to check whether the graphics hardware is capable of supporting non-power of two textures or not by querying GL_ARB_texture_non_power_of_two string. Actually, I was much attracted with the single line of code which is checking the power of two using binary operators. It’s might be a simple logic for the programmers who deals with bits and bytes. But still it’s intresting. The sample code author had particularly mentioned the logic was adopted from SJ Baker’s Cool Code List.

Here’s baker’s method (??) for checking whether a number is exact power of two or not.

b = ((n&(n-1))==0) ;


(NB: This code sets ‘b’ to TRUE if ‘n’ is an integer power of two – in this context, both zero and one are considered to be powers of two.)

This code actually works by changing the least significant ’1′ bit of ‘n’ to a ’0′. If ‘n’ is a power of two, it only has one ’1′ bit – and zeroing it leaves you with zero as the answer. Hence, the inner expression is also a cute trick…

If we would make it as a simple function it may appears as follows.

bool IsPowerOfTwo( int n )
{
    // Function returns true if the number if power of two
    return ((n&(n-1))==0) ;
}

The logic is simply straight forward which can be interpreted by using Windows Calculator. Check it yourself. The link contains some other cool tips as well.

 

Sidebar: Bakers also shared his interesting “Cruel Code List” as well J