Xen shell commands

Some more or less commonly used shell commands for managing Xen Virtual Machines (VM, also called Domains). Tested on Xen 3.0, should work also in other versions.
 
Basic commands       
Show the running Virtual Machines: xm list             
Start a virtual machine (myhost):   xm create myhost             
Gracefully shutdown (soft-reset) a virtual machine:  xm shutdown myhost             
Forced power Off (hard-reset) of a virtual machine:  xm destroy myhost             
Reboot a VM : xm reboot myhost            
 
Using the console       
Start a virtual machine and attach immediately a console:   xm create -c myhost             
Attach to the console of a VM (sample ID=4 for myhost, as shown in xm list):           
xm console myhost  or  xm console 4            
To leave / detach from the xen console press CTRL+5            
 
Resources management and usage       
To alter the RAM assigned to a VM (sample 256 Mb): xm mem-set myhost 256         
To alter the CPUs assigned to a VM (sample 2 CPUs or cores): xm vcpu-set myhost 2            
 
Shows resource utilization of the running hosts:  xm top  or  xentop             
Shows Virtual Machines uptimes:  xm uptime           
 
Saving and Restoring       
Virtual Machines state can be saved on a file and later restored (sample destination file /var/tmp/myhost.save)         
xm save myhost /var/tmp/myhost.save         
Note that after this command the VM is no longer active: you can reboot from start it with xm create myhost or restore the saved machine state with xm restore  /var/tmp/myhost.save         
Note also that the save file ( /var/tmp/myhost.save ) is only the dump on the VM memory, for backups it's of no use if you copy it to another physical host without copying also the  VM config file and the file/LVM/partition used for the disk) .         
 
Block Devices management       
It's is possible to add block devices (disks) to a running VM:       
To add a local file ( /var/tmp/data.disk ) as /dev/xvdc to the myhost paravirtualized VM, in write mode:       
xm block-attach myhost file://var/tmp/data.disk /dev/xvdc w       
Mode can be: w (read/write), r (read only), w! (shared read/write, when the samer device is attached to two different VMs).     
The above file can be created (here 4 Gb) with:     
dd if=/dev/zero of=/var/tmp/data.disk  bs=1024k count=4096     
 
To add a local physical disk, partition or LVM (for example an USB stick):       
xm block-attach myhost phy://dev/sda /dev/xvdc w       
Note that I/O performances are much better when using physical devices or LVM instead of plain files as block devices for the VMs.     
 
To list the currently attached block devices to a VM:       
xm block-list myhost --long or xm block-list myhost        
To detach a block device (51744 is the Vdev in xm block-list)       
xm block-detach myhost 51744       
       
Bridging management       
On Linux the bridge utils can be used to show and manage bridging:             
brctl show             
brctl showmacs xenbr0             
To manually create a new xen bridge interface and associate it a physical device.             
brctl addbr xenbr2             
brctl addif peth3