Saturday, October 22, 2011

Shrink VirtualBox disk images

In Linux (Debian) guest:
  • apt-get install zerofree
    zerofree is a small program which uses the ext2fs library to get a list of all the blocks, checks if the current block is free but non-zero (i.e. has been used at some point) and fills it with zeroes again. The utility also works on ext3 or ext4 filesystems. This allows the VBoxManage tool to compact the VDI.
  • init 1
    to go to single mode.
  • mount -n -o remount,ro -t ext2 /dev/XXX /MOUNTPOINT"
    to remount all partitions read only (chec the available filesystem with df).
    • -n : not try to update "/etc/mtab" (which might be on the now read only disk)
    • -t ext2 : not need journaling.
  • fsck.ext2 -f /dev/XXX
    to check the filesystem (if necessary).
  • zerofree /dev/XXX
  • fsck.ext2 -f /dev/XXX
    again to check the filesystem (if necessary).
  • mount -t ext4 -o remount,rw /dev/XXX /MOUNTPOINT
    to remount the partitions back to normal.
  • Shutdown the virtual machine.
In Windows guest:
  • Perform hard disk defragmentation.
  • Download sdelete from http://technet.microsoft.com/en-us/sysinternals/bb897443.aspx
  • sdelete -c DRIVE:
  • Shutdown the virtual machine
In host:
  • VboxManage modifyhd /path/to/the/vmdiskimage.vdi --compact
Ref:
- http://www.virtualbox.org/manual/ch08.html#vboxmanage-modifyvdi
- http://intgat.tigress.co.uk/rmy/uml/sparsify.html
- http://forums.virtualbox.org/viewtopic.php?t=1198
- http://maketecheasier.com/shrink-your-virtualbox-vm/2009/04/06

Thursday, October 13, 2011

Access linux filesystems in Windows

There are some drivers available to access a linux filesystem from Windows, such as Ext2Fsd (read and write, for ext2, partly ext3/ext4), explore2fs (read-only, for ext2/ext3), Ext2read (read only, for ext2/ext3/ext4), DiskInternals Linux Reader (freeware, read-only, for ext2/ext3/ext4, HFS and ReiserFS), Ext2 Installable File System for Windows (freeware, read and write, for ext2), RFSTOOL (read-only, for ReiserFS), ReiserDriver (read-only, for ReiserFS), LTOOLS (read and write, for ext2/ext3 and ReiserFS). Some of them have stopped development for quite a long time.

An more "native" approach is to make use of a virtual machine to read and write the linux filesystems on internal and external hard disk drives. It seems to be the only choice for btrfs until now.

  • Install VirtualBox (with USB support enabled).
  • Create a Debian virtual machine. Get a Debian netinst image and install a base system (without any suggested groups of packages). It takes around 900 MB plus swap space at the time of this writing. Network of the virtual machine is set to bridged.
  • To access a linux partition on intrenal hard disk drive: create an image representing it and add it to VirtualBox
    • Run cmd.exe as administrator.
    • Change into the VirtualBox directory:
      cd "c:\Program Files\Oracle\VirtualBox"
    • List the partitions of the hard disk:
      VBoxManage.exe internalcommands listpartitions -rawdisk \\.\PhysicalDriveX
      with X is the hard disk drive number (the first one in Windows is numbered 0). Linux filesystem is 0x83. Note the number(s) of the needed partition(s).
    • Create the image of the partition(s):
      VBoxManage.exe internalcommands createrawvmdk -filename "\path\to\file.vmdk -rawdisk \\.\PhysicalDriveX -partitions Y,Z
      The image file name has to be a .vmdk. Y, Z are the partitions numbers found by the previous command.
    • Run VirtualBox as administrator. Go to Storage tab of the Settings of the machine. Add the image in SATA Controller.
  • To access a linux partition on usb hard disk drive, select it from the menu Devices -> USB Devices from the machine window.
  • Install samba package. Its current version on Debian testing is 3.5.11.
    • Check available samba users with pdbedit -L -a. Create a new samba user (if necessary) with smbpasswd -a username (the username should be added in Linux system by useradd before that).
    • The default smb.conf should work out of the box, or can be edited to meet appropriate share needs. By default, the home directory of the user will be shared and read-only is set. To enable writing, set read only = no in [homes] definition. File and directory creation mask can also be re-set. Run /etc/init.d/samba reload to reload smb.conf.
  • Mount the partitions on internal and/or usb hard disks to directories which can be accessed by samba.
  • In Windows 7, type the IP address of the virtual machine in search/run box (menu Start), like \\192.168.56.38 (found by running ifconfig in the virtual machine). Windows 7 uses DOMAIN/username to login, which fails to log into samba server. This can be bypassed by adding a \ before the username.

Notes:

  • There are reports that with old versions of Samba and Windows 7, it is necessary to modify the Local Security Policy to set the LAN Manager Authentication Level to “Send LM & NTLM responses”. In Windows 7 Professional, this can be done in Control Panel -> Administrative Tools or secpol.msc from the command line. Windows 7 Home versions don’t have this tool but the same thing can be done by edit the registry by hand: open registry with regedit, go to HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Lsa and add a DWORD value named "LMCompatibilityLevel" and set the value to "1".
  • To enable USB 2.0 (EHCI) controller, VirtualBox extension package must be installed separately.
  • Be careful with USB devices that are currently in use on the host! For example, if you allow your guest to connect to your USB hard disk that is currently mounted on the host, when the guest is activated, it will be disconnected from the host without a proper shutdown. This may cause data loss.
  • There are suggestions to use coLinux. But it does not support 64-bit system at the current time.

Links:

  • Ext2Fsd: http://www.ext2fsd.com
  • Explore2fs: http://www.chrysocome.net/explore2fs
  • ext2read: http://sourceforge.net/projects/ext2read/
  • DiskInternals Linux Reader: http://www.diskinternals.com/linux-reader
  • Ext2 Installable File System For Windows: http://www.fs-driver.org/index.html
  • RFSTOOL: http://freshmeat.net/projects/rfstool
  • ReiserDriver: http://rfsd.sourceforge.net
  • LTOOLS: http://www2.hs-esslingen.de/~zimmerma/software/ltools.html
  • VirtualBox: https://www.virtualbox.org
  • coLinux: http://www.colinux.org
Ref:
- http://www.virtualbox.org/manual/ch09.html#rawdisk
- http://www.ubuntugeek.com/howto-access-ext3-partition-from-windows.html