Showing posts with label windows. Show all posts
Showing posts with label windows. Show all posts

Tuesday, November 13, 2012

Resizing a VirtualBox hard drive image under Windows

Every once in a while I run out of hard drive space on one of my virtual machines. I used to go through wild gyrations to add an additional hard drive to the machine. It's actually much simpler to just expand the hard drive.

In my case, I'm using a Windows 7 guest OS inside a Windows 7 host OS. I do the following:

  1. Shutdown the guest machine
  2. Start a command prompt and type the following:
    1. "c:\Program Files\Oracle\VirtualBox"\vboxmanage modifyhd Win7CppDev.vdi --resize 30720
  3. You should be able to confirm that the drive is larger in File > Media Manager.
  4. Boot the guest VM
  5. Start Computer Management > Storage > Disk Management
  6. Use "Extend Volume..." on C: partition to use the newly unallocated partition space.
  7. Restart guest OS for good measure, even though I don't think it is needed.
Congratulations, you now have more space.

Thursday, July 12, 2012

Using Dell Smart Card Reader in VirtualBox guest via RDP

I've had a bunch of issues getting my VirtualBox guest machine to use the built-in Dell Latitude E6520 smart card reader. It seems to be a known issue with VBox, but basically the internal guest OS just doesn't see the device, or if it does, it can't use it. I've been working around the problem by purchasing an external USB smart card reader (SCM Microsystems SCR3310) to read my CAC.

My setup is as follows:

  • Host: Dell Latitude 6520 laptop running Windows 7 64-bit, 8GB RAM, dual-core/dual-thread Core i5 to provide 4 virtual processors. Using latest copy of VirtualBox.
  • Guest: Windows 2008 R2 Server, 4GB RAM, 3 processors.

I found a new trick that works. Instead of using the graphical console that VirtualBox gives me, I boot the VM into headless most (using command line), then use Remote Desktop Protocol to connect directly to the virtual machine. Note that I'm using the RDP service provided by Windows Server, NOT the RDP service provided by VirtualBox. That is, I RDP into the virtual machine and NOT to the RDP service offered on the host laptop that VBox intercepts. My exact steps were as follows.

  • Configure the VM with a Bridged network adapter so it gets a similar address on your local network as your laptop. This could also probably be done by adding a "local only" network adapter. I don't think it will work properly with a NAT network. (I run both a NAT and a Bridged at the same time sometimes)
  • Boot the Windows Server VM. Do NOT enable "Remote Display" in the display settings. You can either use the command line "VBoxHeadless -startvm my_vm" command, or just start it from the GUI and ignore the console.
  • Make sure RDP is enabled in your Windows Server. Use the Server Manager and/or initial configuration wizard to do it, or just go through the control panel.
  • Use CMD.exe with 'ipconfig' to make note of your IP address on the bridged network. (Example: 192.168.40.2)
  • From the host laptop, start an RDP session
    • Use host: 192.168.40.2 (or whatever the bridged address was)
    • On the Local Resources tab, use "More..." and turn on "Smart Cards" check box
When it connects, you should now have access to your Smart Card. Additionally, it will detect the insert/remove events if you re-insert it (another thing that it doesn't do, even when I have my external smart card reader attached).

NOTE: In my case, I also had ActivClient installed on my Windows Server VM so that I could manipulate the smart card. I'm not sure if you need that or not.

Wednesday, October 5, 2011

DOS batch file stderr redirection

I never remember the whether the syntax of DOS file redirection is similar to normal Unix file redirection. It is. So I'm writing it down where I can find it.

CALL .\somescript.bat >> logfile.log 2>&1

Of note, I recently learned that redirecting output from a PowerShell script does NOT work very well and gives file handle errors. That's another story, entirely.

Common DOS batch file error handling mistakes I make

I write a decent amount of batch scripts now days to automate things on windows server boxes. Unfortunately, I don't do it quite frequently enough to remember all the crazy syntax of DOS commands. One of these errors is error handling. These are the things I commonly need to deal with.

I found this page (and site) very helpful for all DOS related stuff: http://www.robvanderwoude.com/errorlevel.php

  1. Use "IF %ERRORLEVEL% NEQ 0 SET MYERROR=1" to record whether the previous command resulted in an error code. Alternately, you can check for "IF ERRORLEVEL 1 ..." if you want to look for a specific error level.
  2. Be cautious of manipulations with ERRORLEVEL. It's not really an environment variable like other variables. In particular, do NOT ever use "set ERRORLEVEL=5" or similar. It will corrupt any further use of %ERRORLEVEL% syntax by fixing it at a value.
  3. Use "CALL FOO.BAT" instead of just "FOO.BAT" when calling subscripts. Otherwise, when that script completes it will not return to the current script.
  4. Use "EXIT /B 1" to return an exit code from your script. If you use "EXIT 1" it will exit the entire command shell, including closing your current window if it's running in one.
  5. Consider using SETLOCAL and ENDLOCAL within your script to prevent temporary environment variables from carrying through to outer shells.

Below is an example script.


:: Sample script with some error handling
SETLOCAL
SET MYPARAM=%1
if "%MYPARAM%"=="" goto :USAGE

CALL .\childscript.bat %MYPARAM%
if %ERRORLEVEL% NEQ 0 set MYERROR=1

echo.
echo Finished child script. Handling errors now...

if %MYERROR%==1 GOTO :ERROR

echo It worked!
ENDLOCAL
set SOME_EXTERNAL_VARIABLE=1


GOTO :EOF
:USAGE
echo Please provide a command line parameter.
EXIT /B 2


:ERROR
echo It didn't work, dude.
EXIT /B 1 

Update: I was wrong about the ERRORLEVEL syntax earlier, so I updated after some testing.

Monday, October 3, 2011

Change Windows 7 default logon screen background

When you change your desktop background in Windows 7, it does not change the background for the logon screen. Since I have multiple machines connected to a KVM on my setup, I like to have different colors to signify which machine I'm actually looking at.

Windows 7 doesn't provide a good mechanism through the UI for changing this. To do it, you need to change a registry key and store your background as the correct file.

I found this post which was helpful: http://www.kodyaz.com/articles/change-windows-7-logon-screen-background-using-registry-oobe.aspx

Bascially,
  1. Navigate to this registry key path with regedit.exe: Computer\HKEY_LOCAL_MACHINE\SOFTWARE\
    Microsoft\Windows\CurrentVersion\Authentication\LogonUI\Background
  2. Make sure the OEMBackground DWORD key is set to 1.
  3. Navigate to C:\Windows\System32\oobe\info\backgrounds directory
  4. There will probably already be a bunch of backgrounds there. Pick the one you want and save it as 'backgrounddefault.jpg'
That's it. You're done. Hit Windows-L to lock your screen and you should see the new background file.

Wednesday, February 2, 2011

Upgrade and clone hard drive in laptop

I recently bought an upgraded hard drive to put into my laptop.

Side note: Momentus XT 500 GB "hybrid" hard drive. This is a 7200 RPM drive with 4GB of solid-state disk internal to act as a write-through cache. It's supposed to drastically speed up the performance of the drive for executing common things (like booting, swapping, running apps).

Goal: clone current hard drive, swap, then boot.

Approach 1: purchase Apricorn EZ Gig product that comes with a cable and a software package to clone the drive. Price: $39. Unfortunately, they were out of stock locally, so I attempted the poor-man's approach.

Approach 2: Use an adapter kit I had laying around to hook up the drive to USB, then use CloneZilla to do it.


  1. Visit http://clonezilla.org/clonezilla-live.php and download clonezilla ISO. (I got the AMD64 arch for my E5500 laptop)
  2. Visit http://tuxboot.org/download.php so I can burn the ISO to a USB stick.
    1. Follow to source forge, go into the latest date directory, and download the tuxboot-windows.exe version.
  3. Run tuxboot.exe (it wants admin access to write to the device)
  4. Burn the image to the USB device. Don't worry about rebooting since you won't be booting to this machine immediately.
  5. Reboot onto the USB stick
  6. Follow prompts to start up clonezilla
  7. Choose drive-to-drive clone
  8. Follow prompts, pick the right drives for source and destination
  9. Let it churn. My machine was doing approximately 2-3 GB/min, so it took a few hours.
When it finished, I simply swapped the drives and the system magically booted up. Easy, peazy, lemon squeezy.