Citrix Xen: Command Line Tips & Tricks

root's picture

How to get the Citrix XenServer version:


[root@XenGz ~]# cat /etc/redhat-release
XenServer release 6.5.0-90233c (xenenterprise)

[root@XenGz ~]# xe-linux-distribution 
os_distro="xenserver"
os_majorver="6"
os_minorver="5"
os_uname="3.10.0+2"
os_name="XenServer release 6.5.0-90233c (xenenterprise)"

How to list the vhd partitions and get the corresponding snapshosts (if any):


[root@XenGz ~]# vhd-util scan -m "VHD-*" -f -c -l VG_XenStorage-c1a19876-fcb2-c530-798a-8dbe2857f318 -p -v
vhd=VHD-c1997097-d775-453a-8343-6411a23391c7 capacity=53687091200 size=20451426304 hidden=1 parent=none
   vhd=VHD-30694682-5fd9-4885-84e3-b1062719020a capacity=53687091200 size=8388608 hidden=0 parent=VHD-c1997097-d775-453a-8343-6411a23391c7
   vhd=VHD-61676ec7-d53f-4fdb-817d-77c0b23f8718 capacity=53687091200 size=53800337408 hidden=0 parent=VHD-c1997097-d775-453a-8343-6411a23391c7
   vhd=VHD-c6abf541-d410-4131-8b0b-8c184e19b495 capacity=53687091200 size=53800337408 hidden=0 parent=VHD-c1997097-d775-453a-8343-6411a23391c7
   vhd=VHD-cc670f39-88b8-4cc5-981f-082321a83f20 capacity=53687091200 size=8388608 hidden=0 parent=VHD-c1997097-d775-453a-8343-6411a23391c7
vhd=VHD-04a1ccdf-f7db-4328-8bbd-72230db0291a capacity=8388608 size=16777216 hidden=0 parent=none
vhd=VHD-5c10445f-a2df-42b4-a3c3-793847a78db5 capacity=42949672960 size=43041947648 hidden=0 parent=none
vhd=VHD-6501a327-8043-44e7-8583-e30ac7bcb0b5 capacity=112742891520 size=112969383936 hidden=0 parent=none
vhd=VHD-769cb8fa-1d0a-4a1b-ab8e-5d373c0fbca3 capacity=16106127360 size=16143876096 hidden=0 parent=none
vhd=VHD-8f2c7531-3a88-4d6c-973f-642bc19095fe capacity=64424509440 size=64558727168 hidden=0 parent=none
vhd=VHD-b8088503-71e3-49e6-8020-96933765e3e8 capacity=8589934592 size=8615100416 hidden=0 parent=none
vhd=VHD-f3c150a1-ec3e-416b-b237-a589ade8c092 capacity=54223962112 size=54337208320 hidden=0 parent=none

How to vaidate VHD partition with vhd-util while the VM is off:


As you can see below, you have to activate the logical volume first.
[root@XenGz ~]# vhd-util check -n /dev/VG_XenStorage-c1a19876-fcb2-c530-798a-8dbe2857f318/VHD-769cb8fa-1d0a-4a1b-ab8e-5d373c0fbca3
cannot stat /dev/VG_XenStorage-c1a19876-fcb2-c530-798a-8dbe2857f318/VHD-769cb8fa-1d0a-4a1b-ab8e-5d373c0fbca3: 2
[root@XenGz ~]# lvchange -ay /dev/VG_XenStorage-c1a19876-fcb2-c530-798a-8dbe2857f318/VHD-769cb8fa-1d0a-4a1b-ab8e-5d373c0fbca3
[root@XenGz ~]# lvscan | grep VHD-769cb8fa-1d0a-4a1b-ab8e-5d373c0fbca3
  ACTIVE            '/dev/VG_XenStorage-c1a19876-fcb2-c530-798a-8dbe2857f318/VHD-769cb8fa-1d0a-4a1b-ab8e-5d373c0fbca3' [15.04 GB] inherit
[root@XenGz ~]# vhd-util check -n /dev/VG_XenStorage-c1a19876-fcb2-c530-798a-8dbe2857f318/VHD-769cb8fa-1d0a-4a1b-ab8e-5d373c0fbca3
/dev/VG_XenStorage-c1a19876-fcb2-c530-798a-8dbe2857f318/VHD-769cb8fa-1d0a-4a1b-ab8e-5d373c0fbca3 is valid
[root@XenGz ~]# lvchange -an /dev/VG_XenStorage-c1a19876-fcb2-c530-798a-8dbe2857f318/VHD-769cb8fa-1d0a-4a1b-ab8e-5d373c0fbca3

How to create ISO storage repository:


In this example, the repository will be /tmp/hdd/iso.
[root@XenGz hdd]# xe sr-create name-label=ISOTMP type=iso device-config:location=/tmp/hdd/iso device-config:legacy_mode=true content-type=iso
0f804eac-defd-2727-4923-d33a39159ba1
[root@XenGz ~]# ls -la /tmp/hdd/iso/
total 642688
drwxr-xr-x 2 root root      4096 Dec 17 23:26 .
drwxr-xr-x 4 root root      4096 Dec 17 23:25 ..
-rw-r--r-- 1 root root 657457152 Dec 17 23:22 debian-8.2.0-amd64-CD-1.iso

How to list all OS templates


In the example below, I grepped only the Label Name because the full output is too much :)
[root@XenGz ~]# xe template-list | grep name-label
          name-label ( RW): Scientific Linux 5 (32-bit)
          name-label ( RW): SUSE Linux Enterprise Server 12 (64-bit)
          name-label ( RW): CentOS 4.7 (32-bit)
          name-label ( RW): SUSE Linux Enterprise Server 10 SP4 (32-bit)
          name-label ( RW): Ubuntu Precise Pangolin 12.04 (32-bit)
          name-label ( RW): SUSE Linux Enterprise Server 10 SP1 (32-bit)
          name-label ( RW): SUSE Linux Enterprise Server 10 SP4 (64-bit)
          name-label ( RW): Oracle Enterprise Linux 5 (32-bit)
          name-label ( RW): SUSE Linux Enterprise Server 10 SP3 (64-bit)
          name-label ( RW): Red Hat Enterprise Linux 7
          name-label ( RW): Oracle Enterprise Linux 6 (64-bit)
          name-label ( RW): Oracle Linux 7
          name-label ( RW): Windows Server 2008 (32-bit)
          name-label ( RW): Ubuntu Trusty Tahr 14.04
          name-label ( RW): CentOS 4.6 (32-bit)
          name-label ( RW): Scientific Linux 5 (64-bit)
          name-label ( RW): Red Hat Enterprise Linux 4.5 (32-bit)
          name-label ( RW): CentOS 6 (64-bit)
          name-label ( RW): CentOS 7
          name-label ( RW): Ubuntu Maverick Meerkat 10.10 (32-bit) (experimental)
          name-label ( RW): SUSE Linux Enterprise Server 11 SP1 (64-bit)
          name-label ( RW): SUSE Linux Enterprise Server 11 SP2 (64-bit)
          name-label ( RW): SUSE Linux Enterprise Server 11 SP3 (32-bit)
          name-label ( RW): Citrix XenApp on Windows Server 2008 (64-bit)
          name-label ( RW): Red Hat Enterprise Linux 4.8 (32-bit)
          name-label ( RW): SUSE Linux Enterprise Server 10 SP3 (32-bit)
          name-label ( RW): Scientific Linux 6 (32-bit)
          name-label ( RW): Red Hat Enterprise Linux 4.7 (32-bit)
          name-label ( RW): SUSE Linux Enterprise Server 10 SP2 (32-bit)
          name-label ( RW): CentOS 5 (64-bit)
          name-label ( RW): Windows Server 2012 R2 (64-bit)
          name-label ( RW): Citrix XenApp on Windows Server 2008 (32-bit)
          name-label ( RW): Windows 8 (32-bit)
          name-label ( RW): Windows 10 (32-bit)
          name-label ( RW): Ubuntu Maverick Meerkat 10.10 (64-bit) (experimental)
          name-label ( RW): CentOS 6 (32-bit)
          name-label ( RW): Citrix XenApp on Windows Server 2003 (32-bit)
          name-label ( RW): SUSE Linux Enterprise Server 10 SP2 (64-bit)
          name-label ( RW): SUSE Linux Enterprise Desktop 11 SP3 (64-bit)
          name-label ( RW): SUSE Linux Enterprise Server 11 (64-bit)
          name-label ( RW): Windows Server 2012 (64-bit)
          name-label ( RW): Red Hat Enterprise Linux 4.6 (32-bit)
          name-label ( RW): SUSE Linux Enterprise Server 11 SP1 (32-bit)
          name-label ( RW): Windows 10 (64-bit)
          name-label ( RW): Windows 7 (64-bit)
          name-label ( RW): Windows Server 2003 (32-bit)
          name-label ( RW): Xen API SDK
          name-label ( RW): Debian Squeeze 6.0 (64-bit)
          name-label ( RW): Windows 8 (64-bit)
          name-label ( RW): Red Hat Enterprise Linux 5 (64-bit)
          name-label ( RW): Ubuntu Precise Pangolin 12.04 (64-bit)
          name-label ( RW): Red Hat Enterprise Linux 6 (32-bit)
          name-label ( RW): Scientific Linux 7
          name-label ( RW): CentOS 4.5 (32-bit)
          name-label ( RW): Oracle Enterprise Linux 6 (32-bit)
          name-label ( RW): Windows Server 2008 R2 (64-bit)
          name-label ( RW): Windows XP SP3 (32-bit)
          name-label ( RW): Citrix XenApp on Windows Server 2003 (64-bit)
          name-label ( RW): SUSE Linux Enterprise Server 10 SP1 (64-bit)
          name-label ( RW): Debian Wheezy 7.0 (64-bit)
          name-label ( RW): CentOS 4.8 (32-bit)
          name-label ( RW): SUSE Linux Enterprise Server 11 SP2 (32-bit)
          name-label ( RW): CoreOS
          name-label ( RW): Citrix XenApp on Windows Server 2008 R2 (64-bit)
          name-label ( RW): Windows Server 10 Preview (64-bit) (experimental)
          name-label ( RW): Red Hat Enterprise Linux 5 (32-bit)
          name-label ( RW): Windows Vista (32-bit)
          name-label ( RW): SUSE Linux Enterprise Server 11 (32-bit)
          name-label ( RW): Other install media
          name-label ( RW): Red Hat Enterprise Linux 6 (64-bit)
          name-label ( RW): Scientific Linux 6 (64-bit)
          name-label ( RW): CentOS 5 (32-bit)
          name-label ( RW): Windows 7 (32-bit)
          name-label ( RW): Windows Server 2003 (64-bit)
          name-label ( RW): Ubuntu Lucid Lynx 10.04 (64-bit)
          name-label ( RW): SUSE Linux Enterprise Server 11 SP3 (64-bit)
          name-label ( RW): Oracle Enterprise Linux 5 (64-bit)
          name-label ( RW): Debian Wheezy 7.0 (32-bit)
          name-label ( RW): Debian Squeeze 6.0 (32-bit)
          name-label ( RW): Windows Server 2008 (64-bit)
          name-label ( RW): Ubuntu Lucid Lynx 10.04 (32-bit)

How to make an old fashion backup image of VHD logical volume while VM is off:


VHD="VHD-72d97c7e-ffb1-4f83-a48c-5e323500f958"
lvchange -ay /dev/VG_XenStorage-c1a19876-fcb2-c530-798a-8dbe2857f318/${VHD}
dd if=/dev/VG_XenStorage-c1a19876-fcb2-c530-798a-8dbe2857f318/${VHD} conv=sync,noerror | gzip -c > /backup/vhd/${VHD}.img.gz
lvchange -an /dev/VG_XenStorage-c1a19876-fcb2-c530-798a-8dbe2857f318/${VHD}

Execution output:

[root@XenGz ~]# VHD="VHD-72d97c7e-ffb1-4f83-a48c-5e323500f958"
[root@XenGz ~]# lvchange -ay /dev/VG_XenStorage-c1a19876-fcb2-c530-798a-8dbe2857f318/${VHD}
[root@XenGz ~]# dd if=/dev/VG_XenStorage-c1a19876-fcb2-c530-798a-8dbe2857f318/${VHD} conv=sync,noerror | gzip -c > /backup/vhd/${VHD}.img.gz

105078784+0 records in
105078784+0 records out
53800337408 bytes (54 GB) copied, 2445.34 seconds, 22.0 MB/s
[root@XenGz ~]# lvchange -an /dev/VG_XenStorage-c1a19876-fcb2-c530-798a-8dbe2857f318/${VHD}
[root@XenGz ~]#         <- no output!

How to export vm with compression enabled:


xe vm-export vm=gzvm64 filename=/backups/gzvm64.xva compress=true

How to eject the cd from a VM:


[root@XenGz ~]# xe vm-cd-list vm=gzvm64
CD 0 VBD:
uuid ( RO)             : 60a5842c-0a90-66e7-7c80-a6294ab5c620
    vm-name-label ( RO): gzvm64
            empty ( RO): false
       userdevice ( RW): 3


CD 0 VDI:
uuid ( RO)             : 7a66737c-547a-45e3-8e23-a4ec45ac1d1c
       name-label ( RW): xs-tools.iso
    sr-name-label ( RO): XenServer Tools
     virtual-size ( RO): 120027136

[root@XenGz ~]# xe vm-cd-eject vm=gzvm64 cd-name="xs-tools.iso"
Error: Unknown field 'cd-name'
[root@XenGz ~]# xe vm-cd-eject vm=gzvm64
[root@XenGz ~]# xe vm-cd-list vm=gzvm64
CD 0 VBD:
uuid ( RO)             : 60a5842c-0a90-66e7-7c80-a6294ab5c620
    vm-name-label ( RO): gzvm64
            empty ( RO): true
       userdevice ( RW): 3

This error happens if you delete (any) vdi while assigned to a vm:


[root@XenGz ~]# xe vm-start vm=debian
This operation cannot be performed because the specified VDI could not be found on the storage substrate
sr: 411e6ba5-af91-c875-500e-5b8ddcb0520e (ISOTMP)
vdi: 758ec20a-ce4a-481f-a582-1316c30101f7 (debian-8.2.0-amd64-CD-1.iso)

[root@XenGz ~]# xe vm-cd-list vm=debian
CD 0 VBD:
uuid ( RO)             : 41973dba-dece-0193-fa58-448837f85b6a
    vm-name-label ( RO): debian
            empty ( RO): false
       userdevice ( RW): 3


CD 0 VDI:
uuid ( RO)             : 758ec20a-ce4a-481f-a582-1316c30101f7
       name-label ( RW): debian-8.2.0-amd64-CD-1.iso
    sr-name-label ( RO): ISOTMP
     virtual-size ( RO): 657457152


[root@XenGz ~]# xe vm-cd-remove vm=debian cd-name=debian-8.2.0-amd64-CD-1.iso
[root@XenGz ~]# xe vm-cd-list vm=debian
[root@XenGz ~]#          <- no output means no cds configured for this vm!

How to change the VM's virtualization boot mode type to PV (paravirtualized):


Note, you do this also when you want to boot the VM from CD or DVD.
xe vm-param-set HVM-boot-policy="BIOS order" uuid=<vm_uuid>

How to change the VM's virtualization boot mode to HVM (fully virtualized):


xe vm-param-set HVM-boot-policy="" uuid=<vm_uuid>

Thou shalt not steal!

If you want to use this information on your own website, please remember: by doing copy/paste entirely it is always stealing and you should be ashamed of yourself!
Have at least the decency to create your own text and comments and run the commands on your own servers and provide your output, not what I did!

Or at least link back to this website.