1

The basic idea is like this: run another Linux installation as if it was in VirturalBox or VM ware.

With "chroot", I can do something similar. For example, I can mount the partition where Arch Linux was installed, and chroot to it. I then can execute some commands.

But there are also many restrictions. For instance, "pacman -Syu" cannot work properly (maybe I get something wrong).

Is there any better way to do the similar work?

2

For chroot to work, you also need to bind the kernels file systems, such as procfs, sysfs and /dev.

To do this, you typically execute before chrooting:

cd /path/to/chroot/destination
mount -o bind /proc proc
mount -o bind /sys sys
mount -o bind /dev dev

And only then you chroot into /path/to/chroot/destination.

However, this is not equivalent to running the linux under a VM, because it does not have its own kernel. There are two more layers of isolation you can use, one is LXC Containers (also without its own kernel) and one is an actual VM.

Both can be managed by libvirt, and I suggest that you have a look at libvirt for that purpose, with the qemu driver and the lxc driver.

With libvirt, you can also mount host file systems as guest file systems (see the domain configuration¹). However, in general it might be wiser to just hand the whole partition to libvirt and let it do the rest. This has the restriction that you cannot access the files directly from the host system while the VM is running².

A simple example configuration would be:

<domain type='qemu'> <!-- if you have kvm, put kvm here instead of qemu -->
  <name>my_fancy_vm</name>
  <memory unit='KiB'>524288</memory>
  <vcpu placement='static'>1</vcpu>
  <features>
    <acpi/>
    <apic/>
  </features>
  <os>
    <type arch='x86_64' machine='pc-i440fx-1.6'>hvm</type>
    <boot dev='cdrom' />
    <boot dev='hd' />
  </os>
  <cpu mode='custom'>
    <model>kvm64</model>
  </cpu>
  <pm>
    <suspend-to-mem enabled='no' />
    <suspend-to-disk enabled='no' />
  </pm>
  <devices>
    <!-- for kvm, you have to put /usr/bin/qemu-kvm here -->
    <emulator>/usr/bin/qemu-system-x86_64</emulator>
    <!-- to directly use a filesystem here, please see the documentation -->
    <disk type='block' device='disk'>
      <driver name='qemu' type='raw' />
      <target dev='vda' bus='virtio' />
      <source dev='/dev/partition_used_by_the_other_linux' />
    </disk>
    <serial type='pty'>
      <target port='0' />
    </serial>
    <console type='pty'>
      <target type='serial' port='0' />
    </console>
  </devices>
  <seclabel type='none' />
</domain>

If you have multiple partitions to use, create multiple ones by duplicating the disk section. For more details please see the documentation on the domain XML format. It also contains information on how to set up networking.

After setting up the domain, you can define it in libvirt with:

virsh define path/to/domain.xml

And start it with:

virsh start my_fancy_vm  # that is the name chosen in the xml

To connect to the fake serial console defined in the xml, you can use:

virsh console my_fancy_vm

Note that nothing might show up – you may have to configure your archlinux to use it. For lxc guests there might be an easier way than binding a console to get a shell, but I have not tried it. There are tutorials for using libvirt with lxc though.


  1. One domain is one virtual machine, in case you don’t know.
  2. Well, you can, but you really, really should not. You can of course mount it via sshfs to the guest.
  • That's exactly what I needed. Very detailed and instructive. Thanks a lot! – Lagrange Feb 12 '14 at 7:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.