Tuesday, September 25, 2012

Clearing partitions on a USB stick using Windows 7

I've been trying to access the entire size of an 8GB USB stick.  At one point I must have created multiple partitions on the USB stick when using it with Linux machines.  I didn't know that Windows can only access the first partition on a stick.  The primary partition I had created was rather small, about 250MB.  I wanted to use my USB stick to copy a file that was several GB so this amount of space wasn't going to work.  I looked and looked for answers and finally found out a way to do it without installing 3rd party software.

WARNING: These steps will delete the entire contents of the USB stick!

Open a command line and run the following:
  >diskpart
      (accept the request for privileges)
 DISKPART> list disk
      (determine which disk is your USB stick, for me it was Disk 1)
 DISKPART> select Disk 1
 DISKPART> clean
 DISKPART> create partition primary

This cleared the disk and gave me one large partition that can now be formatted.  Instead of trying to partition it from diskpart, I decided to just use the Disk Management gui using these steps:
  1. Start -> Run : diskmgmt.msc
  2. Right click on the usb disk in the gui and select Format
  3. Select the file system you want to use, I used FAT32 so it would also work in Linux
  4. Press OK
I now have a working USB stick that has all disk space available and no partitions hidden from Windows.

Tuesday, September 4, 2012

Remote Desktop to a machine behind a NAT

It is nice to be able to connect to someone else's Linux machine remotely so you can help them with issues.  The problem I run into frequently is that the remote user is not very technical.  So when a remote machine is behind a NAT stepping them through configuring their router to do port forwarding is not easy.  The difficulty is compounded when you are unfamiliar with their router.  Here is a solution I came up with that just requires the remote user to run a quick script that will reverse tunnel into my machine so I can connect via SSH or VNC into theirs.  The VNC is nice because then they can watch what you're doing or you can help them with something they're doing.

Setup Local Machine:

  1. You'll need to setup your local machine to be running an ssh server.  Then setup your router to port forward port 22 to your local machine.
  2. Create a guest user on your local machine for the remote machine to login to.  I created the guest so someone smart on the other end doesn't have too many permissions on my machine.  You could just use your login if you don't mind giving the remote user your password or them possibly using the password-less login to your machine.

Setup Remote Machine:

  1. Setup VNC server for remote desktop viewing.  Setup steps I used can be found here: http://ubuntuforums.org/showthread.php?p=10744047
  2. Create an executable shell script that can easily be run by novice users on the far side.  This shell script will do 2 reverse ssh tunnels into the local machine.  The first tunnel is for ssh access and the second is for VNC.  Script contents:
    • vi reverseSSH.sh
    • Paste in the following:
    #! /bin/bash
    echo "Setting up secure tunnels for Remote Desktop help."
    echo "Press Ctrl + C when you are finished to close connections."
    ssh -R 12323:localhost:22 -R 12324:localhost:5900 guest@$1 -N
    • Run chmod +x on the newly created script

Connecting the local and remote machines

  1. Determine the local machine's external IP address.  This can easily be done by visiting this site: http://www.whatismyip.com/
  2. Run the script created above from the remote machine.
    • ./reverseSSH.sh [ip address found in previous step]
  3. Now you should be able to ssh into the remote machine from the local machine using:
    • ssh username@localhost -p 12323
    • This will require you to know a username and password to login on the remote machine
  4. In addition you can connect via VNC to the machine with these: localhost: 12324
    • The way I have it setup, a remote user will need to be logged in and acknowledge the remote desktop request.  You can change this in the remote desktop settings if you prefer.
  5. (Optional) Setup password-less login from the remote machine to the local by running the following while ssh'd on the remote machine.
    # ssh-keygen -t rsa
        Press "Enter" for the next 3 prompts and accept the defaults
    # ssh-copy-id -i ~/.ssh/id_rsa.pub guest@local.server

Friday, August 10, 2012

I'm Officially an Inventor

I just received notification that a patent I helped work on was officially issued by the US Patent office: http://www.google.com/patents/US8218532

Pretty exciting!

Wednesday, February 8, 2012

Using Gradle with Ubuntu 11.10 (Oneiric Ocelot)

I am a big fan of dependency management. After learning about Maven and working with it for several projects I have really bought into the idea of using build repositories and having projects be dependent on build artifacts that reside in repositories rather than storing all the dependency jars in the SCM. Even worse is when a team has a bunch of modules but only a single build that constantly builds them all. This seems like such a waste of time and resources to be constantly building something that may not have changed recently. Anyway, I've heard a log of good things about Gradle, so today I wanted to check it out.

Since I'm running with Ubuntu, I first searched the Synaptic Package Manager to see if Gradle was available there.  It was so I quickly installed it.  There were a lot of dependencies, so it took a few minutes to download and install everything.
From there I went to the Gradle site and started the Hello World tutorial and quickly had it running.  Since I am familiar with Java projects, I wanted to start comparing the Java capabilities of Gradle to Maven.  I was happy to see that Gradle seems to be following the same directory structure that Maven uses, so that makes life easier.  I created a very simple Java application that would just have a single HelloWorld class with a JUnit test case to test it.  I started by following the Gradle Java tutorial

Here is the basic project:
TestGradleJava
    build.gradle
    src/main/java
        HelloWorld.java
    src/test/java
        TestHelloWorld.java

build.gradle
apply plugin: 'eclipse'
apply plugin: 'java'

repositories {
    mavenCentral()
}

dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.+'
}

HelloWorld.java
public class HelloWorld {

    public String getHello() {
        return "Hello World";
    }   
}

TestHelloWorld.java
import static org.junit.Assert.assertEquals;

import org.junit.Test;

public class TestHelloWorld {

    @Test
    public void test() {
        assertEquals("Hello World", new HelloWorld().getHello());
    }

}

On a side note, I was able to get Gradle to create the Eclipse project files by adding apply plugin: 'eclipse' to the build.gradle file.  Then just running 'gradle eclipse' from the command line created the .project and .classpath files needed for Eclipse.

Following the instructions from the tutorial it states to run 'gradle build'.  Doing so I got the following output:

brian@torreys:~/workspace/TestGradleJava$ gradle build
:compileJava UP-TO-DATE
:processResources UP-TO-DATE
:classes UP-TO-DATE
:jar
:assemble
:compileTestJava UP-TO-DATE
:processTestResources UP-TO-DATE
:testClasses UP-TO-DATE
:test

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':test'.
Cause: Could not generate test report to '/home/brian/workspace/TestGradleJava/build/reports/tests'.
Cause: java.lang.NullPointerException (no error message)

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

BUILD FAILED

Total time: 15.675 secs

Doing some research online I found some information that stated there is an issue with the Gradle installation done by the Ubuntu package, (http://www.mail-archive.com/s4-dev@incubator.apache.org/msg00220.html). The resolution is to manually install it. Here are the steps I used to install Gradle manually, after using Synaptic Package Manager to uninstall first of course.
  1. Download the latest version of Gradle from here: http://www.gradle.org/downloads.  I grabbed gradle-1.0-milestone-7-bin.zip
  2. Move the download to /opt
  3. Unzip the archive
    >sudo unzip gradle-1.0-milestone-7-bin.zip
  4. Create a symbolic link to create a gradle dir that points to the dir created by unzipping the archive.  I like to plan for easy upgrades of tools, so I create a symbolic link to the actual gradle directory.
    >sudo ln -s /opt/gradle-1.0-milestone-7 gradle
  5. Create another symbolic link to put the gradle executable into /usr/sbin.  I find this easier than modifying the PATH adding /opt/gradle/bin, but you can do that too.
    >sudo ln -s /opt/gradle/bin/gradle /usr/sbin/gradle
  6. Try it to make sure it works
    >gradle -v
Here are the instructions that Gradle publishes: http://www.gradle.org/docs/current/userguide/installation.html

Now we head back to our TestGradleJava directory and we'll try running 'gradle build' again:
brian@torreys:~/workspace/TestGradleJava$ gradle build
:compileJava UP-TO-DATE
:processResources UP-TO-DATE
:classes UP-TO-DATE
:jar
:assemble
:compileTestJava
Download http://repo1.maven.org/maven2/junit/junit/4.10/junit-4.10.pom
Download http://repo1.maven.org/maven2/org/hamcrest/hamcrest-core/1.1/hamcrest-core-1.1.pom
Download http://repo1.maven.org/maven2/org/hamcrest/hamcrest-parent/1.1/hamcrest-parent-1.1.pom
:processTestResources UP-TO-DATE
:testClasses
:test
:check
:build

BUILD SUCCESSFUL

Total time: 24.657 secs

This is great, now the JUnit targets are running and passing!

My overall conclusion at this point is Gradle looks really nice.  A very simple build file can easily build a simple Java project with dependency management.  I'll have to see how it does with more complex build files as I progress the simple application into something more complicated.