Sunday, January 29, 2006

Running Cygwin from USB

Normally, installing cygwin on a PC means that you'll need administrative privileges on the system. This is needed because the cygwin startup needs several mount points to be set, which are stored in the Windows registry.
However, it is possible to install cygwin on a USB stick, and run it on any host PC without the need for admin privileges and/or registry hacks. The following batch script will figure out the correct drive letter for the stick, create the mount points, and start cygwin, and has been tested in Windows 2000 and XP; you only need to have write access to c:\temp on the host machine.

@echo off
for /F "delims=\: usebackq" %%i in (`cd`) do SET USB_DRIVE=%%i
echo USB driveletter is %USB_DRIVE%
REM pause
SET CYGWIN=tty title
SET PATH=%USB_DRIVE%:\cygwin\bin;%PATH%
echo PATH has been set to %PATH%
echo.
@echo on
mount -f -u -b %USB_DRIVE%:/cygwin /
mount -f -u -b %USB_DRIVE%:/cygwin/bin /usr/bin
mount -f -u -b %USB_DRIVE%:/cygwin/lib /usr/lib
@echo off
echo User mount points have been created.
echo.
echo @ECHO OFF > c:\temp\usbcygwin.bat
echo %USB_DRIVE%: >> c:\temp\usbcygwin.bat
echo CHDIR \cygwin\bin >> c:\temp\usbcygwin.bat
echo bash --login -i >> c:\temp\usbcygwin.bat
echo created c:\temp\usbcygwin.bat, starting cygwin.
echo.
c:\temp\usbcygwin.bat

6 comments:

Anonymous said...

thanks...its working ;-)

Anonymous said...

Very good post. Thanks.
One question. If i already have an installation
of cygwin in my local drive it will corrupt the settings?

Ton said...

I'm not sure which setting you mean; I have done exactly what you want to try and have not noticed any adverse affects.
However, cygwin will pick up your $HOME from your home directory in Windows and will pick up stuff like .profile, .bashrc etc from there. So those settings will be shared if you run cygwin from a USB drive.

Anonymous said...

Dunno... the following is simpler and seems to do the same.

@echo off
for /F "usebackq" %%i in (`cd`) do SET CYGROOT=%%i
SET PATH=%CYGROOT%\bin;%PATH%
mount -f -u -b %CYGROOT% /
mount -f -u -b %CYGROOT%/bin /usr/bin
mount -f -u -b %CYGROOT%/lib /usr/lib
for /F "delims=\:" %%i in ("%CYGROOT%") do SET CYGDRV=%%i
%CYGDRV%:
bash --login -i

Anonymous said...

Sloppy, sloppy me... the batch file above leaves some registry entries behind; if Cygwin is installed on the machine and run from USB, the installed version might break.

The fix is simple, though: just add

umount -U

at the end of the script. Enjoy!

Anonymous said...

Hi,

"you only need to have write access to c:\temp on the host machine."

It is not necessary.
Simply put, instead of C:\temp , the directory path that contains the installation of cygwin on the USB key, like this %USB_DRIVE%:\cygwin

In this case you don't need to have write access to C: