Tux

...making Linux just a little more fun!

Virtual Hard Disks

Michael SanAngelo [msanangelo at gmail.com]


Wed, 14 Jan 2009 13:05:20 -0600

Hi, I was wondering what are the possibilities of creating and using virtual disks for. I understand I can use dd to create it then mkfs.ext3 or something like it to format the disk. What purpose could they be used for besides serving as a foundation for creating a live cd?

I want to do this from the cli so no gui.

Thanks,

Michael S.


Top    Back


Lew Pitcher [lew.pitcher at digitalfreehold.ca]


Wed, 14 Jan 2009 14:19:36 -0500

On January 14, 2009 14:05:20 Michael SanAngelo wrote:

> Hi, I was wondering what are the possibilities of creating and using
> virtual disks for. I understand I can use dd to create it then mkfs.ext3 or
> something like it to format the disk. What purpose could they be used for
> besides serving as a foundation for creating a live cd?

I've used this "virtual hard disk" on occasion, for spot fixes to problems. I can't think of a "permenant" use for it, though, although others might be able to.

The biggest use I have is for "virtual cdroms". I take a dd image of a cdrom or data dvd, and save it on a hard disk. This gives me a library of images that I can pick and choose from; I can install Windows XP into a virtual machine without having to find the original CDROM, I can mount my Slackware install DVD and install new software (or upgrade old software), etc.

The second use I have for "virtual hard disks" is to place a Unixish file system (any one of the Linux fs that support unix file attribute bits, uid/gid, and mtime/ctime/utime) onto a USB thumb drive. Rather than reformat the drive (and thus make it unavailable to Windows machines), I create a VFAT file on the drive, and populate it with a Linux fs. On Linux, I can mount this file as a fs and have all the features you expect in a linux file system, and on MSWindows, I can run Linux in a VM, and give it access to the file through Windows.

> I want to do this from the cli so no gui.
dd if=/dev/zero of=/path/to/vfs/file bs=1024 count=<number of Kbytes in fs>
mkfs -t <filesystemtype> /path/to/vfs/file
mount -t <filesystemtype> -o loop /mount/point /path/to/vfs/file
... do your work...
umount /mount/point
-- 
Lew Pitcher


Top    Back


Thomas Adam [thomas.adam22 at gmail.com]


Wed, 14 Jan 2009 19:41:05 +0000

2009/1/14 Michael SanAngelo <msanangelo@gmail.com>:

> Hi, I was wondering what are the possibilities of creating and using virtual
> disks for. I understand I can use dd to create it then mkfs.ext3 or
> something like it to format the disk. What purpose could they be used for
> besides serving as a foundation for creating a live cd?
>
> I want to do this from the cli so no gui.

The term "virtual disk" is misleading here -- what you're referring to are essentially loopback files, a la:

dd if=/dev/zero of=/somefile bs=1024 count=300000

That will create a 30GiB zero-filled file called "somefile".

Then what you want to do with that is attach a loopback device to the file:

losetup /dev/loop0 /somefile

(It may well be that /dev/loop0 is in use -- there are up to 8 loopback devices:

/dev/loop{1,2,3,4}

Use whichever is available.)

Then create a filesystem on it:

mkfs -t ext3 -m 1 -v /dev/loop0

(Replacing "/dev/loop0 if applicable with the above.)

Then you can mount it somewhere:

mount -t ext3 /dev/loop0 /mnt

(Replacing "/dev/loop0 if applicable with the above.)

Do stuff under /mnt as you would with a normal directory then when you're done:

umount /mnt
losetup -d /dev/loop0

(Replacing /dev/loop0 if applicable.)

-- Thomas Adam


Top    Back


Jimmy O'Regan [joregan at gmail.com]


Wed, 14 Jan 2009 19:47:45 +0000

2009/1/14 Lew Pitcher <lew.pitcher@digitalfreehold.ca>:

> On January 14, 2009 14:05:20 Michael SanAngelo wrote:
>> Hi, I was wondering what are the possibilities of creating and using
>> virtual disks for. I understand I can use dd to create it then mkfs.ext3 or
>> something like it to format the disk. What purpose could they be used for
>> besides serving as a foundation for creating a live cd?
>
> I've used this "virtual hard disk" on occasion, for spot fixes to problems. I
> can't think of a "permenant" use for it, though, although others might be
> able to.
>
> The biggest use I have is for "virtual cdroms". I take a dd image of a cdrom
> or data dvd, and save it on a hard disk. This gives me a library of images
> that I can pick and choose from; I can install Windows XP into a virtual
> machine without having to find the original CDROM, I can mount my Slackware
> install DVD and install new software (or upgrade old software), etc.
>
> The second use I have for "virtual hard disks" is to place a Unixish file
> system (any one of the Linux fs that support unix file attribute bits,
> uid/gid, and mtime/ctime/utime) onto a USB thumb drive. Rather than reformat
> the drive (and thus make it unavailable to Windows machines), I create a VFAT
> file on the drive, and populate it with a Linux fs. On Linux, I can mount
> this file as a fs and have all the features you expect in a linux file
> system, and on MSWindows, I can run Linux in a VM, and give it access to the
> file through Windows.
>

Disk images are also widely used in forensics; it's the recommended practice in data recovery to work with a copy of the hard drive you want to recover from; PC emulators use virtual disks out of necessity (generally, whole disk images, but qemu for one can use 'partition' images, like this, coLinux uses them by default); if your kernel has support, you can use NB (http://nbd.sourceforge.net/) to serve disk images to diskless clients, who can then use them as though they were real disks; the list of possible uses is only limited by the imagination :)


Top    Back


René Pfeiffer [lynx at luchs.at]


Thu, 15 Jan 2009 01:18:57 +0100

On Jan 14, 2009 at 1941 +0000, Thomas Adam appeared and said:

> 2009/1/14 Michael SanAngelo <msanangelo@gmail.com>:
> > Hi, I was wondering what are the possibilities of creating and using virtual
> > disks for. I understand I can use dd to create it then mkfs.ext3 or
> > something like it to format the disk. What purpose could they be used for
> > besides serving as a foundation for creating a live cd?
> >
> > I want to do this from the cli so no gui.
> 
> The term "virtual disk" is misleading here -- what you're referring to
> are essentially loopback files, a la:
> 
> ```
> dd if=/dev/zero of=/somefile bs=1024 count=300000
> '''
> 
> That will create a 30GiB zero-filled file called "somefile".

This command will really write 30 GiB to disk. If you just want to create a file that can store up to 30 GiB "on demand", you can use this:

dd if=/dev/zero of=disk.img bs=1 count=0 seek=30GB

disk.img will show have a size of 30 GB, but it will only use the blocks that are actually used (when using it as a disk image for example). The file produced is called a sparse file.

http://en.wikipedia.org/wiki/Sparse_file

Sparse files are quite handy when you store the disks of virtual machines.

Best, René.


Top    Back