~ubuntu-branches/ubuntu/quantal/virtinst/quantal-proposed

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
#
# Common code for all guests
#
# Copyright 2006-2009  Red Hat, Inc.
# Jeremy Katz <katzj@redhat.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free  Software Foundation; either version 2 of the License, or
# (at your option)  any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301 USA.

import os
import time
import logging
import signal

import urlgrabber.progress as progress
import libvirt
import libxml2

import _util
import CapabilitiesParser
import VirtualGraphics
import support
import XMLBuilderDomain
import virtinst
from XMLBuilderDomain import _xml_property
import DistroInstaller
from VirtualDevice import VirtualDevice
from VirtualDisk import VirtualDisk
from VirtualInputDevice import VirtualInputDevice
from VirtualCharDevice import VirtualCharDevice
from VirtualController import VirtualControllerUSB
from Clock import Clock
from Seclabel import Seclabel
from CPU import CPU
from DomainNumatune import DomainNumatune
from DomainFeatures import DomainFeatures

import osdict
from virtinst import _gettext as _


class Guest(XMLBuilderDomain.XMLBuilderDomain):

    # OS Dictionary static variables and methods
    _DEFAULTS = osdict.DEFAULTS
    _OS_TYPES = osdict.OS_TYPES

    _default_os_type = None

    @staticmethod
    def pretty_os_list():
        """
        Return a strip representation of OS list for printing
        """
        ret = ""
        for t in Guest.list_os_types():
            for v in Guest.list_os_variants(t):
                ret += "%-20s : %s\n" % (v, Guest.get_os_variant_label(t, v))
        return ret

    @staticmethod
    def list_os_types(supported=False, filtervars=None):
        """
        @param filtervars: List of only variants we want to show by default
        """
        vals = osdict.sort_helper(Guest._OS_TYPES)
        for t in vals[:]:
            if not Guest.list_os_variants(t, supported=supported,
                                          filtervars=filtervars):
                vals.remove(t)
        return vals

    @staticmethod
    def list_os_variants(type, sortpref=None, supported=False, filtervars=None):
        """
        Return a list of sorted os variants for the passed distro type

        @param sortpref: An option list of osdict 'distro' tags to
        prioritize in the returned list, e.g. passing ["fedora"] will make
        the sorted list have all fedora distros first
        @param filtervars: List of only variants we want to show by default
        """
        vals = osdict.sort_helper(Guest._OS_TYPES[type]["variants"],
                                  sortpref)
        ret = []
        for v in vals:
            if filtervars:
                if v not in filtervars:
                    continue
            elif supported:
                if not osdict.lookup_osdict_key(None, None,
                                                type, v, "supported"):
                    continue

            ret.append(v)
        return ret

    @staticmethod
    def get_os_type_label(type):
        return Guest._OS_TYPES[type]["label"]

    @staticmethod
    def get_os_variant_label(type, variant):
        return Guest._OS_TYPES[type]["variants"][variant]["label"]

    @staticmethod
    def cpuset_str_to_tuple(conn, cpuset):
        return DomainNumatune.cpuset_str_to_tuple(conn, cpuset)

    @staticmethod
    def generate_cpuset(conn, mem):
        """
        Generates a cpu pinning string based on host NUMA configuration.

        If host doesn't have a suitable NUMA configuration, a RuntimeError
        is thrown.
        """
        caps = CapabilitiesParser.parse(conn.getCapabilities())

        if caps.host.topology is None:
            raise RuntimeError(_("No topology section in capabilities xml."))

        cells = caps.host.topology.cells
        if len(cells) <= 1:
            raise RuntimeError(_("Capabilities only show <= 1 cell. "
                                 "Not NUMA capable"))

        # Capabilities tells us about the available memory 'cells' on the
        # system. Each 'cell' has associated 'cpu's.
        #
        # Use getCellsFreeMemory to determine which 'cell' has the smallest
        # amount of memory which fits the requested VM memory amount, then
        # pin the VM to that 'cell's associated 'cpu's

        cell_mem = conn.getCellsFreeMemory(0, len(cells))
        cell_id = -1
        mem = mem * 1024
        for i in range(len(cells)):
            if cell_mem[i] < mem:
                # Cell doesn't have enough mem to fit, skip it
                continue

            if len(cells[i].cpus) == 0:
                # No cpus to use for the cell
                continue

            # Find smallest cell that fits
            if cell_id < 0 or cell_mem[i] < cell_mem[cell_id]:
                cell_id = i

        if cell_id < 0:
            raise RuntimeError(_("Could not find any usable NUMA "
                                 "cell/cpu combinations."))

        # Build cpuset string
        cpustr = ""
        for cpu in cells[cell_id].cpus:
            if cpustr != "":
                cpustr += ","
            cpustr += str(cpu.id)

        return cpustr

    def __init__(self, type=None, connection=None, hypervisorURI=None,
                 installer=None, parsexml=None, caps=None, conn=None):

        # Set up the connection, since it is fundamental for other init
        conn = conn or connection
        if conn == None:
            logging.debug("No conn passed to Guest, opening URI '%s'" %
                          hypervisorURI)
            conn = self._open_uri(hypervisorURI)

        if conn == None:
            raise RuntimeError(_("Unable to connect to hypervisor, aborting "
                                 "installation!"))

        self._name = None
        self._uuid = None
        self._memory = None
        self._maxmemory = None
        self._hugepage = None
        self._vcpus = 1
        self._maxvcpus = 1
        self._cpuset = None
        self._autostart = False
        self._clock = None
        self._seclabel = None
        self._description = None
        self._features = None
        self._replace = None
        self._emulator = None
        self._installer = installer

        self._os_type = None
        self._os_variant = None
        self._os_autodetect = False

        # DEPRECATED: Public device lists unaltered by install process
        self.disks = []
        self.nics = []
        self.sound_devs = []
        self.hostdevs = []

        # General device list. Only access through API calls (even internally)
        self._devices = []

        # Device list to use/alter during install process. Don't access
        # directly, use internal APIs
        self._install_devices = []

        # The libvirt virDomain object we 'Create'
        self.domain = None
        self._consolechild = None

        self._default_input_device = None
        self._default_console_device = None

        caps = caps or (self._installer and self._installer._get_caps())
        XMLBuilderDomain.XMLBuilderDomain.__init__(self, conn, parsexml,
                                                   caps=caps)
        if self._is_parse():
            return

        if not self.installer:
            i = DistroInstaller.DistroInstaller(type=type,
                                                conn=conn,
                                                os_type=self._default_os_type,
                                                caps=self._get_caps())
            self.installer = i

        # Add default devices (if applicable)
        inp = self._get_default_input_device()
        if inp:
            self.add_device(inp)
        self._default_input_device = inp

        con = self._get_default_console_device()
        con.virtinst_default = True
        self.add_device(con)
        self._default_console_device = con

        # Need to do this after all parameter init
        self._features = DomainFeatures(self.conn)
        self._clock = Clock(self.conn)
        self._seclabel = Seclabel(self.conn)
        self._cpu = CPU(self.conn)
        self._numatune = DomainNumatune(self.conn)

    def _open_uri(self, uri):
        # This is here so test suite can overwrite it, to make sure
        # Guest is never opening anything
        return libvirt.open(uri)

    ######################
    # Property accessors #
    ######################

    def get_installer(self):
        return self._installer
    def set_installer(self, val):
        self._installer = val
    installer = property(get_installer, set_installer)

    def get_clock(self):
        return self._clock
    clock = property(get_clock)
    def get_seclabel(self):
        return self._seclabel
    seclabel = property(get_seclabel)
    def get_cpu(self):
        return self._cpu
    cpu = property(get_cpu)
    def get_numatune(self):
        return self._numatune
    numatune = property(get_numatune)

    def _get_features(self):
        return self._features
    features = property(_get_features)

    # Domain name of the guest
    def get_name(self):
        return self._name
    def set_name(self, val):
        _util.validate_name(_("Guest"), val, lencheck=True)

        do_fail = False
        if self.replace != True:
            try:
                self.conn.lookupByName(val)
                do_fail = True
            except:
                # Name not found
                pass

        if do_fail:
            raise ValueError(_("Guest name '%s' is already in use.") % val)

        self._name = val
    name = _xml_property(get_name, set_name,
                         xpath="./name")

    # Memory allocated to the guest.  Should be given in MB
    def get_memory(self):
        return self._memory
    def set_memory(self, val):
        if (type(val) is not type(1) or val <= 0):
            raise ValueError(_("Memory value must be an integer greater "
                               "than 0"))
        self._memory = val

        if self.maxmemory is None or self.maxmemory < val:
            self.maxmemory = val
    def _xml_memory_value(self):
        return int(self.memory) * 1024
    memory = _xml_property(get_memory, set_memory,
                           xpath="./currentMemory",
                           get_converter=lambda s, x: int(x) / 1024,
                           set_converter=lambda s, x: int(x) * 1024)

    # Memory allocated to the guest.  Should be given in MB
    def get_maxmemory(self):
        return self._maxmemory
    def set_maxmemory(self, val):
        if (type(val) is not type(1) or val <= 0):
            raise ValueError(_("Max Memory value must be an integer greater "
                               "than 0"))
        self._maxmemory = val
    def _xml_maxmemory_value(self):
        return int(self.maxmemory) * 1024
    maxmemory = _xml_property(get_maxmemory, set_maxmemory,
                              xpath="./memory",
                              get_converter=lambda s, x: int(x) / 1024,
                              set_converter=lambda s, x: int(x) * 1024)
    def get_hugepage(self):
        return self._hugepage
    def set_hugepage(self, val):
        if val is None:
            return val
        self._hugepage = bool(val)
    hugepage = _xml_property(get_hugepage, set_hugepage,
                             xpath="./memoryBacking/hugepages", is_bool=True)

    # UUID for the guest
    def get_uuid(self):
        return self._uuid
    def set_uuid(self, val):
        val = _util.validate_uuid(val)
        self._uuid = val
    uuid = _xml_property(get_uuid, set_uuid,
                         xpath="./uuid")

    def __validate_cpus(self, val):
        maxvcpus = _util.get_max_vcpus(self.conn, self.type)
        val = int(val)
        if val < 1:
            raise ValueError(_("Number of vcpus must be a positive integer."))
        if val > maxvcpus:
            raise ValueError(_("Number of vcpus must be no greater than %d "
                               "for this vm type.") % maxvcpus)
        return val

    # number of vcpus for the guest
    def get_vcpus(self):
        return self._vcpus
    def set_vcpus(self, val):
        val = self.__validate_cpus(val)
        self._vcpus = val

        # Don't force set maxvcpus unless already specified
        if self.maxvcpus is not None and self.maxvcpus < val:
            self.maxvcpus = val
    def _vcpus_get_converter(self, val):
        # If no current VCPUs, return maxvcpus
        if not val:
            val = self.maxvcpus
        return int(val)
    vcpus = _xml_property(get_vcpus, set_vcpus,
                          xpath="./vcpu/@current",
                          get_converter=_vcpus_get_converter)

    def _get_maxvcpus(self):
        return self._maxvcpus
    def _set_maxvcpus(self, val):
        val = self.__validate_cpus(val)
        self._maxvcpus = val
    maxvcpus = _xml_property(_get_maxvcpus, _set_maxvcpus,
                             xpath="./vcpu",
                             get_converter=lambda s, x: int(x))

    # set phy-cpus for the guest
    def get_cpuset(self):
        return self._cpuset
    def set_cpuset(self, val):
        if val is None or val == "":
            self._cpuset = None
            return

        DomainNumatune.validate_cpuset(self.conn, val)
        self._cpuset = val
    cpuset = _xml_property(get_cpuset, set_cpuset,
                           xpath="./vcpu/@cpuset")

    def get_graphics_dev(self):
        gdevs = self.get_devices(VirtualDevice.VIRTUAL_DEV_GRAPHICS)
        return (gdevs and gdevs[0] or None)
    def set_graphics_dev(self, val):
        gdev = self.graphics_dev
        if val:
            self.add_device(val)
        if gdev:
            self.remove_device(gdev)
    graphics_dev = property(get_graphics_dev, set_graphics_dev)

    # GAH! - installer.os_type = "hvm" or "xen" (aka xen paravirt)
    #        guest.os_type     = "Solaris", "Windows", "Linux"
    # FIXME: We should really rename this property to something else,
    #        change it throughout the codebase for readability sake, but
    #        maintain back compat.
    def get_os_type(self):
        return self._os_type
    def set_os_type(self, val):
        if type(val) is not str:
            raise ValueError(_("OS type must be a string."))
        val = val.lower()

        if val in self._OS_TYPES:
            if self._os_type != val:
                # Invalidate variant, since it may not apply to the new os type
                self._os_type = val
                self._os_variant = None
        else:
            raise ValueError(_("OS type '%s' does not exist in our "
                                "dictionary") % val)

    os_type = property(get_os_type, set_os_type)

    def get_os_variant(self):
        return self._os_variant
    def set_os_variant(self, val):
        if type(val) is not str:
            raise ValueError(_("OS variant must be a string."))
        val = val.lower()

        if self.os_type:
            if val in self._OS_TYPES[self.os_type]["variants"]:
                self._os_variant = val
            else:
                raise ValueError(_("OS variant '%(var)s' does not exist in "
                                   "our dictionary for OS type '%(ty)s'") %
                                   {'var' : val, 'ty' : self._os_type})
        else:
            found = False
            for ostype in self.list_os_types():
                if (val in self._OS_TYPES[ostype]["variants"] and
                    not self._OS_TYPES[ostype]["variants"][val].get("skip")):
                    logging.debug("Setting os type to '%s' for variant '%s'",
                                  ostype, val)
                    self.os_type = ostype
                    self._os_variant = val
                    found = True

            if not found:
                raise ValueError(_("Unknown OS variant '%s'" % val))

    os_variant = property(get_os_variant, set_os_variant)

    def set_os_autodetect(self, val):
        self._os_autodetect = bool(val)
    def get_os_autodetect(self):
        return self._os_autodetect
    os_autodetect = property(get_os_autodetect, set_os_autodetect)

    # Get the current variants 'distro' tag: 'rhel', 'fedora', etc.
    def get_os_distro(self):
        return self._lookup_osdict_key("distro")
    os_distro = property(get_os_distro)

    def get_autostart(self):
        return self._autostart
    def set_autostart(self, val):
        self._autostart = bool(val)
    autostart = property(get_autostart, set_autostart,
                         doc="Have domain autostart when the host boots.")

    def _get_description(self):
        return self._description
    def _set_description(self, val):
        self._description = val
    description = _xml_property(_get_description, _set_description,
                                xpath="./description")

    def _get_emulator(self):
        return self._emulator
    def _set_emulator(self, val):
        self._emulator = val
    emulator = _xml_property(_get_emulator, _set_emulator,
                             xpath="./devices/emulator")

    def _get_replace(self):
        return self._replace
    def _set_replace(self, val):
        self._replace = bool(val)
    replace = property(_get_replace, _set_replace,
                       doc=_("Whether we should overwrite an existing guest "
                             "with the same name."))

    #########################
    # DEPRECATED PROPERTIES #
    #########################

    # Deprecated: Should set graphics_dev.keymap directly
    def get_keymap(self):
        if self.graphics_dev is None:
            return None
        return self.graphics_dev.keymap
    def set_keymap(self, val):
        if self.graphics_dev is not None:
            self.graphics_dev.keymap = val
    keymap = property(get_keymap, set_keymap)

    # Deprecated: Should set guest.graphics_dev = VirtualGraphics(...)
    def get_graphics(self):
        if self.graphics_dev is None:
            return { "enabled" : False }
        return { "enabled" : True, "type" : self.graphics_dev, \
                 "keymap"  : self.graphics_dev.keymap}
    def set_graphics(self, val):

        # val can be:
        #   a dictionary with keys:  enabled, type, port, keymap
        #   a tuple of the form   : (enabled, type, port, keymap)
        #                            last 2 optional
        #                         : "vnc", "sdl", or false
        port = None
        gtype = None
        enabled = False
        keymap = None
        gdev = None
        if type(val) == dict:
            if "enabled" not in val:
                raise ValueError(_("Must specify whether graphics are enabled"))

            enabled = val["enabled"]
            if "type" in val:
                gtype = val["type"]
                if "opts" in val:
                    port = val["opts"]

        elif type(val) == tuple:
            if len(val) >= 1:
                enabled = val[0]
            if len(val) >= 2:
                gtype = val[1]
            if len(val) >= 3:
                port = val[2]
            if len(val) >= 4:
                keymap = val[3]

        else:
            if val in ("vnc", "sdl"):
                gtype = val
                enabled = True
            else:
                enabled = val

        if enabled not in (True, False):
            raise ValueError(_("Graphics enabled must be True or False"))

        if enabled:
            gdev = VirtualGraphics.VirtualGraphics(type=gtype)
            if port:
                gdev.port = port
            if keymap:
                gdev.keymap = keymap
        self.graphics_dev = gdev

    graphics = property(get_graphics, set_graphics)

    # Hypervisor name (qemu, xen, kvm, etc.)
    # Deprecated: should be pulled directly from the installer
    def get_type(self):
        return self._installer.type
    def set_type(self, val):
        self._installer.type = val
    type = property(get_type, set_type)

    # Deprecated: should be pulled directly from the installer
    def get_arch(self):
        return self.installer.arch
    def set_arch(self, val):
        self.installer.arch = val
    arch = property(get_arch, set_arch)

    # Deprecated: Should be called from the installer directly
    def get_location(self):
        return self._installer.location
    def set_location(self, val):
        self._installer.location = val
    location = property(get_location, set_location)

    # Deprecated: Should be called from the installer directly
    def get_scratchdir(self):
        return self._installer.scratchdir
    scratchdir = property(get_scratchdir)

    # Deprecated: Should be called from the installer directly
    def get_boot(self):
        return self._installer.boot
    def set_boot(self, val):
        self._installer.boot = val
    boot = property(get_boot, set_boot)

    # Deprecated: Should be called from the installer directly
    def get_extraargs(self):
        return self._installer.extraargs
    def set_extraargs(self, val):
        self._installer.extraargs = val
    extraargs = property(get_extraargs, set_extraargs)

    # Deprecated: Should set the installer values directly
    def get_cdrom(self):
        return self._installer.location
    def set_cdrom(self, val):
        self._installer.location = val
        self._installer.cdrom = True
    cdrom = property(get_cdrom, set_cdrom)


    ########################################
    # Device Add/Remove Public API methods #
    ########################################

    def _dev_build_list(self, devtype, devlist=None):
        if not devlist:
            devlist = self._devices

        newlist = []
        for i in devlist:
            if i.virtual_device_type == devtype:
                newlist.append(i)
        return newlist

    def add_device(self, dev):
        """
        Add the passed device to the guest's device list.

        @param dev: VirtualDevice instance to attach to guest
        """
        if not isinstance(dev, VirtualDevice):
            raise ValueError(_("Must pass a VirtualDevice instance."))

        if self._is_parse():
            xml = dev.get_xml_config()
            node = libxml2.parseDoc(xml).children
            dev.set_xml_node(node)
            self._add_child_node("./devices", node)

        return self._add_device(dev)

    def _add_device(self, dev):
        devtype = dev.virtual_device_type

        # If user adds a device conflicting with a default assigned device
        # remove the default
        if (dev.virtual_device_type == VirtualDevice.VIRTUAL_DEV_INPUT and
            self._default_input_device):
            if self._default_input_device in self.get_all_devices():
                self.remove_device(self._default_input_device)
            self._default_input_device = None

        if (dev.virtual_device_type in [VirtualDevice.VIRTUAL_DEV_CONSOLE,
                                        VirtualDevice.VIRTUAL_DEV_SERIAL] and
            self._default_console_device):
            if self._default_console_device in self.get_all_devices():
                self.remove_device(self._default_console_device)
            self._default_console_device = None

        # Actually add the device
        if   devtype == VirtualDevice.VIRTUAL_DEV_DISK:
            self.disks.append(dev)
        elif devtype == VirtualDevice.VIRTUAL_DEV_NET:
            self.nics.append(dev)
        elif devtype == VirtualDevice.VIRTUAL_DEV_AUDIO:
            self.sound_devs.append(dev)
        elif devtype == VirtualDevice.VIRTUAL_DEV_HOSTDEV:
            self.hostdevs.append(dev)
        else:
            self._devices.append(dev)


    def get_devices(self, devtype):
        """
        Return a list of devices of type 'devtype' that will installed on
        the guest.

        @param devtype: Device type to search for (one of
                        VirtualDevice.virtual_device_types)
        """
        if   devtype == VirtualDevice.VIRTUAL_DEV_DISK:
            devlist = self.disks[:]
        elif devtype == VirtualDevice.VIRTUAL_DEV_NET:
            devlist = self.nics[:]
        elif devtype == VirtualDevice.VIRTUAL_DEV_AUDIO:
            devlist = self.sound_devs[:]
        elif devtype == VirtualDevice.VIRTUAL_DEV_HOSTDEV:
            devlist = self.hostdevs[:]
        else:
            devlist = self._dev_build_list(devtype)

        devlist.extend(self._install_devices)
        return self._dev_build_list(devtype, devlist)

    def get_all_devices(self):
        """
        Return a list of all devices being installed with the guest
        """
        retlist = []
        for devtype in VirtualDevice.virtual_device_types:
            retlist.extend(self.get_devices(devtype))
        return retlist

    def remove_device(self, dev):
        """
        Remove the passed device from the guest's device list

        @param dev: VirtualDevice instance
        """
        found = False
        for devlist in [self.disks, self.nics, self.sound_devs, self.hostdevs,
                        self._devices, self._install_devices]:
            if found:
                break

            if dev in devlist:
                devlist.remove(dev)
                found = True
                break

        if not found:
            raise ValueError(_("Did not find device %s") % str(dev))

        if self._is_parse():
            xpath = dev.get_xml_node_path()
            if xpath:
                self._remove_child_xpath(xpath)


    ################################
    # Private xml building methods #
    ################################

    def _parsexml(self, xml, node):
        XMLBuilderDomain.XMLBuilderDomain._parsexml(self, xml, node)

        device_mappings = {
            "disk"      : virtinst.VirtualDisk,
            "interface" : virtinst.VirtualNetworkInterface,
            "sound"     : virtinst.VirtualAudio,
            "hostdev"   : virtinst.VirtualHostDevice,
            "input"     : virtinst.VirtualInputDevice,
            "serial"    : virtinst.VirtualCharDevice,
            "parallel"  : virtinst.VirtualCharDevice,
            "console"   : virtinst.VirtualCharDevice,
            "channel"   : virtinst.VirtualCharDevice,
            "graphics"  : virtinst.VirtualGraphics,
            "video"     : virtinst.VirtualVideoDevice,
            "watchdog"  : virtinst.VirtualWatchdog,
            "controller": virtinst.VirtualController,
            "filesystem": virtinst.VirtualFilesystem,
            "smartcard" : virtinst.VirtualSmartCardDevice,
            "redirdev"  : virtinst.VirtualRedirDevice,
            "memballoon": virtinst.VirtualMemballoon,
        }

        # Hand off all child element parsing to relevant classes
        caps = self._get_caps()
        for node in self._xml_node.children:
            if node.name != "devices":
                continue

            children = filter(lambda x: (x.name in device_mappings and
                                         x.parent == node),
                              node.children)
            for devnode in children:
                objclass = device_mappings.get(devnode.name)

                if objclass == virtinst.VirtualCharDevice:
                    dev = objclass(self.conn, devnode.name,
                                   parsexmlnode=devnode, caps=caps)
                else:
                    dev = objclass(conn=self.conn,
                                   parsexmlnode=devnode, caps=caps)
                self._add_device(dev)

        self._installer = virtinst.Installer.Installer(self.conn,
                                                   parsexmlnode=self._xml_node,
                                                   caps=caps)
        self._features = DomainFeatures(self.conn,
                                        parsexmlnode=self._xml_node,
                                        caps=caps)
        self._clock = Clock(self.conn, parsexmlnode=self._xml_node, caps=caps)
        self._seclabel = Seclabel(self.conn, parsexmlnode=self._xml_node,
                                  caps=caps)
        self._cpu = CPU(self.conn, parsexmlnode=self._xml_node, caps=caps)
        self._numatune = DomainNumatune(self.conn,
                                        parsexmlnode=self._xml_node, caps=caps)

    def _get_default_input_device(self):
        """
        Return a VirtualInputDevice.
        """
        if self.installer and self.installer.is_container():
            return None
        dev = VirtualInputDevice(self.conn)
        return dev

    def _get_default_console_device(self):
        """
        Only implemented for FullVirtGuest
        """
        dev = VirtualCharDevice.get_dev_instance(self.conn,
                                                 VirtualCharDevice.DEV_CONSOLE,
                                                 VirtualCharDevice.CHAR_PTY)
        return dev

    def _get_device_xml(self, devs, install=True):

        def do_remove_media(d):
            # Keep cdrom around, but with no media attached,
            # But only if we are a distro that doesn't have a multi
            # stage install (aka not Windows)
            return (d.virtual_device_type == VirtualDevice.VIRTUAL_DEV_DISK and
                    d.device == VirtualDisk.DEVICE_CDROM
                    and d.transient
                    and not install and
                    not self.get_continue_inst())

        def do_skip_disk(d):
            # Skip transient labeled non-media disks
            return (d.virtual_device_type == VirtualDevice.VIRTUAL_DEV_DISK and
                    d.device == VirtualDisk.DEVICE_DISK
                    and d.transient
                    and not install)

        # Wrapper for building disk XML, handling transient CDROMs
        def get_dev_xml(dev):
            origpath = None
            try:
                if do_skip_disk(dev):
                    return ""

                if do_remove_media(dev):
                    origpath = dev.path
                    dev.path = None

                return dev.get_xml_config()
            finally:
                if origpath:
                    dev.path = origpath
        def get_vscsi_ctrl_xml():
            vscsi_class = virtinst.VirtualController.get_class_for_type(
                          virtinst.VirtualController.CONTROLLER_TYPE_SCSI)
            ctrl = vscsi_class(self.conn)
            ctrl.set_address("spapr-vio")
            return ctrl.get_xml_config()

        xml = self._get_emulator_xml()
        # Build XML
        for dev in devs:
            xml = _util.xml_append(xml, get_dev_xml(dev))
            if (dev.address.type == "spapr-vio" and
                  dev.virtual_device_type == virtinst.VirtualDevice.VIRTUAL_DEV_DISK):
                xml = _util.xml_append(xml, get_vscsi_ctrl_xml())

        return xml

    def _get_emulator_xml(self):
        emulator = self.emulator
        if self.installer.is_xenpv():
            return ""

        if (not self.emulator and
            self.installer.is_hvm() and
            self.type == "xen"):
            emulator = "/usr/lib/xen-default/bin/qemu-dm"

        emu_xml = ""
        if emulator is not None:
            emu_xml = "    <emulator>%s</emulator>" % emulator

        return emu_xml

    def _get_features_xml(self, features):
        """
        Return features (pae, acpi, apic) xml
        """
        if self.installer and self.installer.is_container():
            return ""
        return features.get_xml_config()

    def _get_cpu_xml(self):
        """
        Return <cpu> XML
        """
        self.cpu.set_topology_defaults(self.vcpus)
        return self.cpu.get_xml_config()

    def _get_clock_xml(self):
        """
        Return <clock/> xml
        """
        return self.clock.get_xml_config()

    def _get_seclabel_xml(self):
        """
        Return <seclabel> XML
        """
        xml = ""
        if self.seclabel:
            xml = self.seclabel.get_xml_config()

        return xml

    def _get_osblob(self, install):
        """
        Return os, features, and clock xml (Implemented in subclass)
        """
        xml = ""

        osxml = self.installer.get_xml_config(self, install)
        if not osxml:
            return None

        xml = _util.xml_append(xml,
                               self.installer.get_xml_config(self, install))
        return xml

    def _get_vcpu_xml(self):
        curvcpus_supported = virtinst.support.check_conn_support(
                                    self.conn,
                                    virtinst.support.SUPPORT_CONN_MAXVCPUS_XML)
        cpuset = ""
        if self.cpuset is not None:
            cpuset = " cpuset='%s'" % self.cpuset

        maxv = self.maxvcpus
        curv = self.vcpus

        curxml = ""
        if maxv != curv and curvcpus_supported:
            curxml = " current='%s'" % curv
        else:
            maxv = curv

        return "  <vcpu%s%s>%s</vcpu>" % (cpuset, curxml, maxv)

    ############################
    # Install Helper functions #
    ############################

    def _prepare_install(self, meter, dry=False):
        self._install_devices = []
        ignore = dry

        # Fetch install media, prepare installer devices
        self._installer.prepare(guest=self,
                                meter=meter)

        # Initialize install device list
        for dev in self._installer.install_devices:
            self._install_devices.append(dev)

    def _cleanup_install(self):
        self._installer.cleanup()

    def _create_devices(self, progresscb):
        """
        Ensure that devices are setup
        """
        for dev in self.get_all_devices():
            dev.setup_dev(self.conn, progresscb)

    ##############
    # Public API #
    ##############

    def _get_xml_config(self, install=True, disk_boot=False):
        """
        Return the full Guest xml configuration.

        @param install: Whether we want the 'OS install' configuration or
                        the 'post-install' configuration. (Some Installers,
                        like the LiveCDInstaller may not have an 'install'
                        config.)
        @type install: C{bool}
        @param disk_boot: Whether we should boot off the harddisk, regardless
                          of our position in the install process (this is
                          used for 2 stage installs, where the second stage
                          boots off the disk. You probably don't need to touch
                          this.)
        @type disk_boot: C{bool}
        """
        # We do a shallow copy of the device list here, and set the defaults.
        # This way, default changes aren't persistent, and we don't need
        # to worry about when to call set_defaults
        origdevs = self.get_all_devices()
        devs = []
        for dev in origdevs:
            devs.append(dev.copy())
        tmpfeat = self.features.copy()

        def get_transient_devices(devtype):
            return self._dev_build_list(devtype, devs)
        def remove_transient_device(device):
            devs.remove(device)

        # Set device defaults so we can validly generate XML
        self._set_defaults(get_transient_devices,
                           remove_transient_device,
                           tmpfeat)

        if install:
            action = "destroy"
        else:
            action = "restart"

        osblob_install = install
        if disk_boot:
            osblob_install = False

        osblob = self._get_osblob(osblob_install)
        if osblob_install and not self.installer.has_install_phase():
            return None

        desc_xml = ""
        if self.description is not None:
            desc = str(self.description)
            desc_xml = ("  <description>%s</description>" %
                        _util.xml_escape(desc))

        xml = ""
        add = lambda x: _util.xml_append(xml, x)

        xml = add("<domain type='%s'>" % self.type)
        xml = add("  <name>%s</name>" % self.name)
        xml = add("  <uuid>%s</uuid>" % self.uuid)
        xml = add(desc_xml)
        xml = add("  <memory>%s</memory>" % (self.maxmemory * 1024))
        xml = add("  <currentMemory>%s</currentMemory>" % (self.memory * 1024))

        # <blkiotune>
        # <memtune>
        if self.hugepage is True:
            xml = add("  <memoryBacking>")
            xml = add("    <hugepages/>")
            xml = add("  </memoryBacking>")

        xml = add(self._get_vcpu_xml())
        # <cputune>
        xml = add(self.numatune.get_xml_config())
        # <sysinfo>
        # XXX: <bootloader> goes here, not in installer XML
        xml = add("  %s" % osblob)
        xml = add(self._get_features_xml(tmpfeat))
        xml = add(self._get_cpu_xml())
        xml = add(self._get_clock_xml())
        xml = add("  <on_poweroff>destroy</on_poweroff>")
        xml = add("  <on_reboot>%s</on_reboot>" % action)
        xml = add("  <on_crash>%s</on_crash>" % action)
        xml = add("  <devices>")
        xml = add(self._get_device_xml(devs, install))
        xml = add("  </devices>")
        xml = add(self._get_seclabel_xml())
        xml = add("</domain>\n")

        return xml

    def post_install_check(self):
        """
        Back compat mapping to installer post_install_check
        """
        return self.installer.post_install_check(self)

    def get_continue_inst(self):
        """
        Return True if this guest requires a call to 'continue_install',
        which means the OS requires a 2 stage install (windows)
        """
        # If we are doing an 'import' or 'liveCD' install, there is
        # no true install process, so continue install has no meaning
        if not self.installer.has_install_phase():
            return False

        return self._lookup_osdict_key("continue")

    def validate_parms(self):
        """
        Do some pre-install domain validation
        """
        if self.domain is not None:
            raise RuntimeError(_("Domain has already been started!"))

        if self.name is None or self.memory is None:
            raise RuntimeError(_("Name and memory must be specified for "
                                 "all guests!"))

        if _util.vm_uuid_collision(self.conn, self.uuid):
            raise RuntimeError(_("The UUID you entered is already in "
                                 "use by another guest!"))

    def connect_console(self, consolecb, wait=True):
        """
        Launched the passed console callback for the already defined
        domain. If domain isn't running, return an error.
        """
        (self.domain,
         self._consolechild) = self._wait_and_connect_console(consolecb)

        # If we connected the console, wait for it to finish
        self._waitpid_console(self._consolechild, wait)

    def terminate_console(self):
        """
        Kill guest console if it is open (and actually exists), otherwise
        do nothing
        """
        if self._consolechild:
            try:
                os.kill(self._consolechild, signal.SIGKILL)
            except:
                pass

    def domain_is_shutdown(self):
        """
        Return True if the created domain object is shutdown
        """
        dom = self.domain
        if not dom:
            return False

        dominfo = dom.info()

        state    = dominfo[0]
        cpu_time = dominfo[4]

        if state == libvirt.VIR_DOMAIN_SHUTOFF:
            return True

        # If 'wait' was specified, the dom object we have was looked up
        # before initially shutting down, which seems to bogus up the
        # info data (all 0's). So, if it is bogus, assume the domain is
        # shutdown. We will catch the error later.
        return state == libvirt.VIR_DOMAIN_NOSTATE and cpu_time == 0

    def domain_is_crashed(self):
        """
        Return True if the created domain object is in a crashed state
        """
        if not self.domain:
            return False

        dominfo = self.domain.info()
        state = dominfo[0]

        return state == libvirt.VIR_DOMAIN_CRASHED

    ##########################
    # Actual install methods #
    ##########################

    def remove_original_vm(self, force=None):
        """
        Remove the existing VM with the same name if requested, or error
        if there is a collision.
        """
        if force == None:
            force = self.replace

        vm = None
        try:
            vm = self.conn.lookupByName(self.name)
        except libvirt.libvirtError:
            pass

        if vm is None:
            return

        if not force:
            raise RuntimeError(_("Domain named %s already exists!") %
                               self.name)

        try:
            logging.debug("Explicitly replacing guest '%s'", self.name)
            if vm.ID() != -1:
                logging.info("Destroying guest '%s'", self.name)
                vm.destroy()

            logging.info("Undefining guest '%s'", self.name)
            vm.undefine()
        except libvirt.libvirtError, e:
            raise RuntimeError(_("Could not remove old vm '%s': %s") %
                               (self.name, str(e)))

    def start_install(self, consolecb=None, meter=None, removeOld=None,
                      wait=True, dry=False, return_xml=False, noboot=False):
        """
        Begin the guest install (stage1).
        @param return_xml: Don't create the guest, just return generated XML
        """
        is_initial = True

        self.validate_parms()
        self._consolechild = None

        self._prepare_install(meter, dry)
        try:
            # Create devices if required (disk images, etc.)
            if not dry:
                self._create_devices(meter)

            start_xml, final_xml = self._build_xml(is_initial)
            if return_xml:
                return (start_xml, final_xml)
            if dry:
                return

            # Remove existing VM if requested
            self.remove_original_vm(removeOld)

            self.domain = self._create_guest(consolecb, meter, wait,
                                             start_xml, final_xml, is_initial,
                                             noboot)

            # Set domain autostart flag if requested
            self._flag_autostart()

            return self.domain
        finally:
            self._cleanup_install()

    def continue_install(self, consolecb=None, meter=None, wait=True,
                         dry=False, return_xml=False):
        """
        Continue with stage 2 of a guest install. Only required for
        guests which have the 'continue' flag set (accessed via
        get_continue_inst)
        """
        is_initial = False
        start_xml, final_xml = self._build_xml(is_initial)
        if return_xml:
            return (start_xml, final_xml)
        if dry:
            return

        return self._create_guest(consolecb, meter, wait,
                                  start_xml, final_xml, is_initial, False)

    def _build_meter(self, meter, is_initial):
        if is_initial:
            meter_label = _("Creating domain...")
        else:
            meter_label = _("Starting domain...")

        if meter == None:
            meter = progress.BaseMeter()
        meter.start(size=None, text=meter_label)

        return meter

    def _build_xml(self, is_initial):
        log_label = is_initial and "install" or "continue"
        disk_boot = not is_initial

        start_xml = self.get_xml_config(install=True, disk_boot=disk_boot)
        final_xml = self.get_xml_config(install=False)

        logging.debug("Generated %s XML: %s",
                      log_label,
                      (start_xml and ("\n" + start_xml) or "None required"))
        logging.debug("Generated boot XML: \n%s", final_xml)

        return start_xml, final_xml

    def _create_guest(self, consolecb, meter, wait,
                      start_xml, final_xml, is_initial, noboot):
        """
        Actually do the XML logging, guest defining/creating, console
        launching and waiting

        @param is_initial: If running initial guest creation, else we
                           are continuing the install
        @param noboot: Don't boot guest if no install phase
        """
        meter = self._build_meter(meter, is_initial)
        doboot = not noboot or self.installer.has_install_phase()
        if not doboot:
            consolecb = None

        if is_initial and doboot:
            dom = self.conn.createLinux(start_xml or final_xml, 0)
        else:
            dom = self.conn.defineXML(start_xml or final_xml)
            if doboot:
                dom.create()

        self.domain = dom
        meter.end(0)

        if doboot:
            logging.debug("Started guest, connecting to console if requested")
            (self.domain,
             self._consolechild) = self._wait_and_connect_console(consolecb)

        self.domain = self.conn.defineXML(final_xml)
        if is_initial:
            try:
                logging.debug("XML fetched from libvirt object:\n%s",
                              dom.XMLDesc(0))
            except Exception, e:
                logging.debug("Error fetching XML from libvirt object: %s", e)

        # if we connected the console, wait for it to finish
        self._waitpid_console(self._consolechild, wait)

        return self.conn.lookupByName(self.name)


    def _wait_and_connect_console(self, consolecb):
        """
        Wait for domain to appear and be running, then connect to
        the console if necessary
        """
        child = None
        dom = _wait_for_domain(self.conn, self.name)

        if dom is None:
            raise RuntimeError(_("Domain has not existed.  You should be "
                                 "able to find more information in the logs"))
        elif dom.ID() == -1:
            raise RuntimeError(_("Domain has not run yet.  You should be "
                                 "able to find more information in the logs"))

        if consolecb:
            child = consolecb(dom)

        return dom, child

    def _waitpid_console(self, console_child, do_wait):
        """
        Wait for console to close if it was launched
        """
        if not console_child or not do_wait:
            return

        try:
            os.waitpid(console_child, 0)
        except OSError, (err_no, msg):
            logging.debug("waitpid: %s: %s", err_no, msg)

        # ensure there's time for the domain to finish destroying if the
        # install has finished or the guest crashed
        time.sleep(1)

    def _flag_autostart(self):
        """
        Set the autostart flag for self.domain if the user requested it
        """
        if not self.autostart:
            return

        try:
            self.domain.setAutostart(True)
        except libvirt.libvirtError, e:
            if support.is_error_nosupport(e):
                logging.warn("Could not set autostart flag: libvirt "
                             "connection does not support autostart.")
            else:
                raise e


    ###################
    # Device defaults #
    ###################

    def set_defaults(self):
        """
        Public function to set guest defaults. Things like preferred
        disk bus (unless one is specified). These changes are persistent.
        The install process will call a non-persistent version, so calling
        this manually isn't required.
        """
        self._set_defaults(self.get_devices, self.remove_device,
                           self.features)

    def _set_hvm_defaults(self, devlist_func, features):
        disktype = VirtualDevice.VIRTUAL_DEV_DISK
        nettype = VirtualDevice.VIRTUAL_DEV_NET
        disk_bus  = self._lookup_device_param(disktype, "bus")
        net_model = self._lookup_device_param(nettype, "model")

        # Only overwrite params if they weren't already specified
        for net in devlist_func(nettype):
            if net_model and not net.model:
                net.model = net_model

        for disk in devlist_func(disktype):
            if (disk_bus and not disk.bus and
                disk.device == VirtualDisk.DEVICE_DISK):
                disk.bus = disk_bus

        if self.clock.offset == None:
            self.clock.offset = self._lookup_osdict_key("clock")

        if features["acpi"] is None:
            features["acpi"] = self._lookup_osdict_key("acpi")
        if features["apic"] is None:
            features["apic"] = self._lookup_osdict_key("apic")
        if features["pae"] is None and self._get_caps():
            features["pae"] = self._get_caps().support_pae()


    def _set_pv_defaults(self, devlist_func, remove_func):
        # Default file backed PV guests to tap driver
        for d in devlist_func(VirtualDevice.VIRTUAL_DEV_DISK):
            if (d.type == VirtualDisk.TYPE_FILE
                and _util.is_blktap_capable()
                and d.driver_name == None):
                d.driver_name = VirtualDisk.DRIVER_TAP

        for d in devlist_func(VirtualDevice.VIRTUAL_DEV_INPUT):
            if d.type == d.INPUT_TYPE_DEFAULT:
                d.type = d.INPUT_TYPE_MOUSE
            if d.bus == d.INPUT_BUS_DEFAULT:
                d.bus = d.INPUT_BUS_XEN

        for d in devlist_func(VirtualDevice.VIRTUAL_DEV_CONSOLE):
            if hasattr(d, "virtinst_default"):
                remove_func(d)

    def add_usb_ich9_controllers(self):
        ctrl = VirtualControllerUSB(self.conn,
                                    model="ich9-ehci1")
        self.add_device(ctrl)

        ctrl = VirtualControllerUSB(self.conn,
                                    model="ich9-uhci1")
        ctrl.get_master().startport = 0
        self.add_device(ctrl)

        ctrl = VirtualControllerUSB(self.conn,
                                    model="ich9-uhci2")
        ctrl.get_master().startport = 2
        self.add_device(ctrl)

        ctrl = VirtualControllerUSB(self.conn,
                                    model="ich9-uhci3")
        ctrl.get_master().startport = 4
        self.add_device(ctrl)

    def _set_defaults(self, devlist_func, remove_func, features):
        if self.installer.is_hvm():
            self._set_hvm_defaults(devlist_func, features)
        if self.installer.is_xenpv():
            self._set_pv_defaults(devlist_func, remove_func)

        soundtype = VirtualDevice.VIRTUAL_DEV_AUDIO
        videotype = VirtualDevice.VIRTUAL_DEV_VIDEO
        inputtype = VirtualDevice.VIRTUAL_DEV_INPUT
        gfxtype = VirtualDevice.VIRTUAL_DEV_GRAPHICS
        channeltype = VirtualDevice.VIRTUAL_DEV_CHANNEL

        # Set default input values
        input_type = self._lookup_device_param(inputtype, "type")
        input_bus = self._lookup_device_param(inputtype, "bus")
        for inp in devlist_func(inputtype):
            if (inp.type == inp.INPUT_TYPE_DEFAULT and
                inp.bus  == inp.INPUT_BUS_DEFAULT):
                inp.type = input_type
                inp.bus  = input_bus

        # Generate disk targets, and set preferred disk bus
        used_targets = []
        for disk in devlist_func(VirtualDevice.VIRTUAL_DEV_DISK):
            if not disk.bus:
                if disk.device == disk.DEVICE_FLOPPY:
                    disk.bus = "fdc"
                else:
                    if self.installer.is_hvm():
                        if (self.installer.type == "kvm" and
                            self.installer.machine == "pseries"):
                            disk.bus = "scsi"
                        else:
                            disk.bus = "ide"
                    elif self.installer.is_xenpv():
                        disk.bus = "xen"
            used_targets.append(disk.generate_target(used_targets))

        # Set sound device model
        sound_model  = self._lookup_device_param(soundtype, "model")
        for sound in devlist_func(soundtype):
            if sound.model == sound.MODEL_DEFAULT:
                sound.model = sound_model

        # Set video device model
        # QXL device (only if we use spice) - safe even if guest is VGA only
        def has_spice():
            for gfx in devlist_func(gfxtype):
                if gfx.type == gfx.TYPE_SPICE:
                    return True
        if has_spice():
            video_model  = "qxl"
        else:
            video_model  = self._lookup_device_param(videotype, "model_type")

        for video in devlist_func(videotype):
            if video.model_type == video.MODEL_DEFAULT:
                video.model_type = video_model

        # Spice agent channel (only if we use spice)
        def has_spice_agent():
            for chn in devlist_func(channeltype):
                if chn.char_type == chn.CHAR_SPICEVMC:
                    return True

        if (has_spice() and
            not has_spice_agent() and
            support.check_conn_support(self.conn,
                                       support.SUPPORT_CONN_HV_CHAR_SPICEVMC)):
            agentdev = VirtualCharDevice.get_dev_instance(self.conn,
                                            VirtualCharDevice.DEV_CHANNEL,
                                            VirtualCharDevice.CHAR_SPICEVMC)
            self.add_device(agentdev)

        # Generate UUID
        if self.uuid is None:
            self.uuid = _util.generate_uuid(self.conn)


    ###################################
    # Guest Dictionary Helper methods #
    ###################################

    def _is_rhel6(self):
        emulator = self.emulator or ""

        return (self.type in ["qemu", "kvm"] and
                emulator.startswith("/usr/libexec/qemu"))

    def _lookup_osdict_key(self, key):
        """
        Using self.os_type and self.os_variant to find key in OSTYPES
        @returns: dict value, or None if os_type/variant wasn't set
        """
        return osdict.lookup_osdict_key(self.conn, self.type,
                                        self.os_type, self.os_variant,
                                        key)

    def _lookup_device_param(self, device_key, param):
        """
        Check the OS dictionary for the prefered device setting for passed
        device type and param (bus, model, etc.)
        """
        try:
            support._set_rhel6(self._is_rhel6())
            return osdict.lookup_device_param(self.conn, self.type,
                                              self.os_type, self.os_variant,
                                              device_key, param)
        finally:
            support._set_rhel6(False)


def _wait_for_domain(conn, name):
    # sleep in .25 second increments until either a) we get running
    # domain ID or b) it's been 5 seconds.  this is so that
    # we can try to gracefully handle domain restarting failures
    dom = None
    for ignore in range(1, int(5 / .25)): # 5 seconds, .25 second sleeps
        try:
            dom = conn.lookupByName(name)
            if dom and dom.ID() != -1:
                break
        except libvirt.libvirtError, e:
            logging.debug("No guest running yet: " + str(e))
            dom = None
        time.sleep(0.25)

    return dom

# Back compat class to avoid ABI break
XenGuest = Guest
Guest.get_config_xml = Guest.get_xml_config