Creating a VM from a Linux System

Create a VM with similar characteristics i.e. Same OS & HDD setup as real machine.

Real Machine

  • IBM HS20 Blade
  • 2x Xeon Processors
  • Filesystem Size Used Avail Use% Mounted on
    /dev/sda5 48G 3.5G 43G 8% /
    /dev/sda1 99M 53M 41M 57% /boot
    /dev/sdb5 49G 4.5G 42G 10% /db2data
    none 2.0G 0 2.0G 0% /dev/shm
    /dev/sdb2 7.7G 1.5G 5.9G 21% /home

For this example I have created a VM system as follows

  • RHEL4-U4 VM
  • 100MB /dev/sda1 (/boot)
  • 10 GB /dev/sda2 (/)
  • 2GB /dev/sda3 (sawp)
  • 25 GB /dev/ sdb1 (/db2data)

Boot VM using Knoppix

Fix Network – Menu -> KNOPPIX -> Network/Internet -> Network Card Configuration

Enable SSH

  • cd /etc/ssh
  • sudo ssh-keygen -t rsa -f ssh_host_rsa_key -C ‘ ‘ -N ‘ ‘
  • sudo chmod 600 ssh_host_rsa_key
  • sudo chmod 644 ssh_host_rsa_key.pub
  • sudo ssh-keygen -t dsa -f ssh_host_dsa_key -C ‘ ‘ -N ‘ ‘
  • sudo chmod 600 ssh_host_dsa_key
  • sudo chmod 644 ssh_host_dsa_key.pub
  • sudo /etc/init.d/ssh restart

Set the root password on the VM System

  • sudo passwd
  • <somepassword>
  • <somepassword>

Check you can now SSH into the VM from the real Linux system

  • ssh root@x.x.x.x
    The authenticity of host ‘x.x.x.x (x.x.x.x)’ can’t be established.
    RSA key fingerprint is 46:cf:45:0f:40:9d:47:e8:f1:45:97:27:86:f4:3e:04.
    Are you sure you want to continue connecting (yes/no)? yes
    Warning: Permanently added ‘x.x.x.x’ (RSA) to the list of known hosts.
    Password:
    Welcome to Knoppix!

Check the VM discs are mounted with Knoppix. On my system it appears as follows

  • sudo mount /dev/sda1 /mnt/sda1
  • sudo mount /dev/sda2 /mnt/sda2
  • sudo mount /dev/sdb1 /mnt/sdb1

Work out what filesystems you need to copy across.

# df -vh
Filesystem Size Used Avail Use% Mounted on
/dev/sda5 48G 3.4G 43G 8% /
/dev/sda1 99M 66M 29M 70% /boot
/dev/sdb5 49G 18G 29G 38% /db2data
none 2.0G 0 2.0G 0% /dev/shm
/dev/sdb2 7.7G 51M 7.3G 1% /home

Obviously this section depends on how your Linux system is setup.
On my real system /home is on a separate drive but for when I move it to the VM
I am going to make this just part of the system disc as its not really used.

Attempt to Copy across some of the file systems i.e.

  • rsync -ave ssh –delete –exclude=/db2data/* –exclude=/u/* –exclude=/boot/* / root@x.x.x.x:/mnt/sda2/
    • –exclude=/db2data/* is used so it does not copy across the files stored in /db2data as I want them to be stored on a different disk
    • –exclude=/u/* is used as this directory is mounted from a remote machine
    • –exclude=/boot/* is used as this directory is mounted to another partition
  • rsync -ave ssh –delete /boot/ root@x.x.x.x:/mnt/sda1/
  • rsync -ave ssh –delete /home root@x.x.x.x:/mnt/sda2/home
  • rsync -ave ssh –delete /db2data/ root@x.x.x.x:/mnt/sdb1/

The next step is to exit the fstab file to make sure the files are mounted correctly

Next step – reboot the VM and see what happens.

Issues…..

  • Depending on how grub is setup you might need to reinstall grub onto the main disk. If you need to do this follow the instructions below
    1. Boot from a Live CD, like Ubuntu Live, Knoppix, Mepis, or similar.
    2. Open a Terminal. Go SuperUser (that is, type “su”). Enter root passwords as necessary.
      Type “grub” which makes a GRUB prompt appear.
    3. Type “find /boot/grub/stage1”. You’ll get a response like “(hd0)” or in my case “(hd0,0)”. Use whatever your computer spits out for the following lines.
    4. Type “root (hd0,0)”.
    5. Type “setup (hd0,0)”. This is key. Other instructions say to use “(hd0)”, and that’s fine if you want to write GRUB to the MBR. If you want to write it to your linux root partition, then you want the number after the comma, such as “(hd0,3)”.
    6. Type “quit”.
  • If you have changes /boot from being a partition to mounted within / you will find that GRUB cannot find the kernel files. The easiest solution is to reboot the vm using the Knoppix CD and then mount the filesystem with /boot on it i.e. sudo mount /dev/sda2 /mnt/sda2 then edit the /boot/grub.conf file from something like
    • title Red Hat Linux (2.4.18-0.12)
      root (hd0,0)
      kernel /vmlinuz-2.4.18-0.12 ro root=LABEL=/
      initrd /initrd-2.4.18-0.12.img

to

  • title Red Hat Linux (2.4.18-0.12)
    root (hd0,0)
    kernel /boot/vmlinuz-2.4.18-0.12 ro root=/dev/sda1
    initrd /boot/initrd-2.4.18-0.12.img

Leave a comment

Your email address will not be published. Required fields are marked *