~canonical-platform-qa/ubuntu-qa-tools/practisubunit

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
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
#!/usr/bin/python3

# Copyright (C) 2012-2014 Canonical Ltd.
# Author: Marc Deslauriers <marc.deslauriers@ubuntu.com>
#
# License: GPLv3
#
# apt-get install python3-distro-info
#
# TODO: Add Debian iso support
# TODO: Add disk space check before doing the iso mangling
# TODO: don't download and install language packs (is it only fetching off the cd?)
#       also see: https://bugs.launchpad.net/ubuntu/+source/ubiquity/+bug/462379
# TODO: re-enable preseed_iso_path when using --with-ovmf-uefi
# TODO: determine the best network driver when using --with-ovmf-uefi
#
# IMPORTANT: If you modify this file, you need to increment the
# following script_version variable so the cached iso files get
# regenerated. Format is YYYYMMDD + 2 digit daily revision.
script_version="2014102701"

import optparse
import os
import re
import subprocess
import sys
import time
import xml.etree.ElementTree as ET

try:
    import distro_info
except:
    print("Couldn't import distro_info!\n" +
          "Install it with: 'sudo apt-get install python3-distro-info'\n", file=sys.stderr)
    sys.exit(1)

#
# Command functions
#

def cmd_new():
    '''Install a new virtual machine'''

    usage = "usage: %prog new [options] release arch prefix"
    epilog = "\n" + \
             "  release - Specify release to install (hardy|lucid|natty|oneiric|precise...)\n" + \
             "  arch    - Specify arch to install (i386|amd64|...)\n" + \
             "  prefix  - Specify prefix for the machine (eg, 'sec' or 'clean')\n\n" + \
             "Eg, 'uvt new precise amd64 clean' will create a new vm called 'clean-precise-amd64'\n"

    optparse.OptionParser.format_epilog = lambda self, formatter: self.epilog
    parser = optparse.OptionParser(usage = usage, epilog = epilog)

    parser.add_option("-f", "--force", dest="force", default=False, action='store_true',
                      help="Don't prompt for confirmation")

    parser.add_option("-n", "--no-snapshot", dest="snapshot", default=True, action='store_false',
                      help="Don't create initial pristine snapshot")

    parser.add_option("-b", "--bridge", dest="bridge", default=None, metavar="BRIDGE",
                      help="Use interface BRIDGE for bridged networking")

    parser.add_option("-t", "--type", dest="type", default='desktop', metavar="TYPE",
                      help="desktop|server|mini|alternate (default: %default)")

    parser.add_option("--name", dest="name", default=None, metavar="Name",
                      help="Specify name of VM instead of autogenerating one")

    parser.add_option("-c", "--cache", dest="cache", default=False, action='store_true',
                      help="Attempt to cache preseeded iso files")

    parser.add_option("-v", "--no-vnc", dest="vnc", default=True, action='store_false',
                      help="Do not launch VNC viewer while creating VMs")

    parser.add_option("-V", "--verbose", dest="verbose", default=False, action='store_true',
                      help="Print extra debugging information")

    parser.add_option("--loader", dest="loader", default=None, metavar="LOADER",
                      help="Specify full path to non-default os loader")

    parser.add_option("--with-ovmf-uefi", dest="ovmf_uefi", default=False, action='store_true',
                      help="Configure for use with OVMF UEFI")

    (opt, args) = parser.parse_args()

    if opt.loader:
        loader = os.path.join("/usr/share/qemu", opt.loader)
        if not os.path.isfile(loader):
            print("Error: could not find '%s'\n" % loader, file=sys.stderr)
            sys.exit(1)

    if opt.ovmf_uefi and not opt.loader:
         print("Error: must specify --loader with --with-ovmf-uefi\n", file=sys.stderr)
         sys.exit(1)

    if opt.name and len(args) < 2:
        print("Error: Need to specify release, arch with --name!\n", file=sys.stderr)
        parser.print_help()
        sys.exit(1)
    elif not opt.name and len(args) < 3:
        print("Error: Need to specify release, arch and prefix!\n", file=sys.stderr)
        parser.print_help()
        sys.exit(1)

    if opt.name: # prefix not needed when using --name
        release, arch = args
    else:
        release, arch, prefix = args

    check_release(release)
    release_num = get_numerical_release(release)

    release_iso = locate_release_iso(release, release_num, arch, opt.type, opt.force)

    if opt.name:
        if not re.search(r'^[a-zA-Z0-9\-]+$', opt.name):
            print("Invalid name. Please use alphanumerics or '-'. Aborting!\n", file=sys.stderr)
            sys.exit(1)
        vm_name = opt.name
    elif opt.type == "desktop":
        vm_name = "%s-%s-%s" % (prefix, release, arch)
    else:
        vm_name = "%s-%s-%s-%s" % (prefix, release, opt.type, arch)

    check_vm_exists(vm_name, fail=True)

    print("Creating '%s' VM with:" % vm_name)
    print("Memory: %s" % uvt_conf['vm_memory'])
    print("Image size: %sGB" % uvt_conf['vm_image_size'])
    print("Release: %s - %s" % (release, release_num))
    print("Release ISO file: %s" % release_iso)
    print("Pristine snapshot: %s" % opt.snapshot)
    if opt.loader:
        print("OS boot loader: %s" % opt.loader)
    if opt.ovmf_uefi:
        print("Configure for OVMF UEFI: yes")
        print(" - Forcing vnc with OVMF")
        opt.vnc = True
        print(" - Automatic partitioning is recommended")
        print(" - System won't reboot automatically")
        print(" - On boot/reboot, need to 'Boot from file' in EFI configure")
        print(" - See https://wiki.ubuntu.com/SecurityTeam/SecureBoot "
              "for details")

    if uvt_conf['vm_extra_packages'] != "":
        print("Install extra packages: %s" % uvt_conf['vm_extra_packages'])

    if opt.force == False:
        if not confirm("Confirm?"):
            print("Aborting!\n", file=sys.stderr)
            sys.exit(1)

    create_preseeded_iso(release_iso,
                         opt.cache,
                         opt.type,
                         vm_name,
                         release,
                         ovmf=opt.ovmf_uefi)
    create_vm(release_iso,
              vm_name,
              arch,
              opt.bridge,
              release,
              opt.vnc,
              opt.loader,
              ovmf_uefi=opt.ovmf_uefi,
              verbose=opt.verbose)

    # Check for when the machine is added. virt-install can sometimes be slow
    # so try a few times
    if not check_dumpxml_exists(vm_name, tries=5):
        print("Problem creating virtual machine '%s' (doesn't exist). Aborting." % vm_name)
        sys.exit(1)

    # Wait for it to start before we wait for it to shut down...
    for timeout in range(120):
        if vm_running(vm_name) == True:
            break
        time.sleep(1)

    # Raring isos don't shut down properly when pre-seeded
    if release == "raring":
        print("Waiting up to 20 minutes for '%s' to finish install and shut down..." % vm_name)
        # Wait a while to make sure we don't shutdown while it's installing
        # Hopefully 20 minutes is enough. It's a shame we have to wait that
        # long before killing it, but I can't think of a better way at the
        # moment. We can't ping it, and can't ssh into it.
        vm_stop_wait(vm_name, 1200)
        # Now kill it
        print("Forcing shut down (workaround for Raring)...")
        vm_destroy(vm_name)
    else:
        print("Waiting up to 120 minutes for '%s' to finish install and shut down..." % vm_name)
        if not vm_stop_wait(vm_name, 7200):
            print(" timed out waiting for '%s' to finish install and shut down. Aborting." % vm_name)
            sys.exit(1)

    remove_ssh_keys(vm_name)

    # TODO: remove this once the preseed ISO works
    if opt.ovmf_uefi:
        f = "/tmp/%s_first-ovmf-boot.sh" % vm_name
        print("Skipping postinstall when using --with-ovmf-uefi")
        print("It is recommened you perform these steps manually:")
        print("- install openssh-server")
        print("- scp %s %s:/tmp" % (f, vm_name))
        print("- ssh %s chmod 755 %s" % (vm_name, f))
        print("- ssh -tt %s sudo %s" % (vm_name, f))
        print("- ssh -tt %s sudo reboot # triggers running /postinstall.sh" %
                (vm_name))
        print("- ssh -tt %s sudo tail -f /var/log/vm-new.log" % vm_name)
        print("- When /var/log/vm-new.log reports it is done, reboot, login")
        print("  via lightdm (to finish user configuration), then shutdown")
        print("- commit the snapshot with 'uvt snapshot %s'" % vm_name)
        sys.exit(0)

    print("Verifying lsb_release and waiting 30 minutes for post-installation to finish...")
    print("(in case of problems, see /var/log/vm-new.log in the guest)")

    command = "lsb_release -c | grep -q " + release + " || " + \
              "echo '*** WARNING *** LSB release is not " + release + "' ;" + \
              "while [ -r /etc/network/if-up.d/postinstall ]; do sleep 5; done"

    if vm_run_command(vm_name, command, root = True, start = True,
                      force_keys = True, start_timeout = 3600,
                      quiet = not opt.verbose) == False:
        print("Could not verify lsb_release. Aborting.")
        sys.exit(1)

    if opt.snapshot == True:
        print("Creating initial pristine snapshot...")
        vm_create_snapshot(vm_name)

    remove_preseeded_iso(release_iso, opt.cache, vm_name)
    print("VM installation complete.")

def cmd_start():
    '''Start a virtual machine'''

    usage = "usage: %prog start [options] <vm1> <vm2>..."

    parser = optparse.OptionParser(usage = usage)

    parser.add_option("-w", "--wait", dest="wait", default=False, action='store_true',
                      help="Wait for WMs to be SSH-available before exiting")

    parser.add_option("-t", "--time", dest="time", default=5, metavar="SECONDS",
                      help="Time to wait between starting VMs (default %default seconds)")

    parser.add_option("-p", "--prefix", dest="prefix", default=None, metavar="PREFIX",
                      help="Start all VMs with the PREFIX prefix")

    parser.add_option("-a", "--arch", dest="arch", default=None, metavar="ARCH",
                      help="Start all VMs with the ARCH arch")

    parser.add_option("-v", "--no-vnc", dest="vnc", default=True, action='store_false',
                      help="Do not launch VNC viewer on started VMs")

    parser.add_option("-r", "--revert", dest="revert", default=False, action='store_true',
                      help="Revert VM to pristine snapshot before starting")

    parser.add_option("-f", "--force", dest="force", default=False, action='store_true',
                      help="Do not ask for confirmation before reverting")

    (opt, args) = parser.parse_args()

    if opt.prefix == None and len(args) < 1:
        print("Error: Need to specify at least one VM (or prefix)!\n", file=sys.stderr)
        parser.print_help()
        sys.exit(1)

    machines = []
    if opt.prefix is not None:
        machines = vm_list(prefix=opt.prefix, arch=opt.arch)
    else:
        machines = args

    for machine in machines:
        if check_vm_exists(machine) == False:
            print("Error: VM '%s' does not exist, skipping." % machine, file=sys.stderr)
            continue

        if opt.revert:
            if not opt.force:
                print("\nWARNING! The '%s' VM will be reverted to the pristine snapshot!" % machine)
                if not confirm("Do you want to continue?"):
                    continue

            if vm_running(machine) == True:
                print("VM '%s' is running, stopping." % machine)
                vm_stop(machine)
                if not vm_stop_wait(machine):
                    print("Error: VM '%s' could not be stopped, skipping." % machine, file=sys.stderr)
                    continue

            print("Reverting '%s' to pristine snapshot..." % machine)
            vm_pristine_snapshot(machine)

        if vm_running(machine) == True:
            print("VM '%s' is already running, skipping." % machine)
            continue

        vm_start(machine)
        if opt.vnc:
            # Give the vm time to initialize before starting the viewer, or
            # we get a weird screen size
            time.sleep(5)
            vm_view(machine)
        if opt.wait:
            vm_start_wait(machine)

        print("Sleeping %s seconds to give '%s' a chance to start" % (opt.time, machine))
        time.sleep(opt.time)


def cmd_stop():
    '''Stop a virtual machine'''

    usage = "usage: %prog stop [options] <vm1> <vm2>..."

    parser = optparse.OptionParser(usage = usage)

    parser.add_option("-f", "--force", dest="force", default=False, action='store_true',
                      help="destroy rather than gracefully shutdown")

    parser.add_option("-p", "--prefix", dest="prefix", default=None, metavar="PREFIX",
                      help="Stop all VMs with the PREFIX prefix")

    parser.add_option("-a", "--arch", dest="arch", default=None, metavar="ARCH",
                      help="Stop all VMs with the ARCH arch")

    parser.add_option("-r", "--revert", dest="revert", default=False, action='store_true',
                      help="Revert VM to pristine snapshot after stopping")

    (opt, args) = parser.parse_args()

    if opt.prefix == None and len(args) < 1:
        print("Error: Need to specify at least one VM (or prefix)!\n", file=sys.stderr)
        parser.print_help()
        sys.exit(1)

    machines = []
    if opt.prefix is not None:
        machines = vm_list(prefix=opt.prefix, arch=opt.arch)
    else:
        machines = args

    for machine in machines:
        if check_vm_exists(machine) == False:
            print("Error: VM '%s' does not exist, skipping." % machine, file=sys.stderr)
            continue

        if vm_running(machine) == False:
            print("VM '%s' is not running, skipping." % machine)
            continue

        if opt.force == True:
            vm_destroy(machine)
        else:
            vm_stop(machine)

        if opt.revert:
            if not opt.force:
                print("\nWARNING! The '%s' VM will be reverted to the pristine snapshot!" % machine)
                if not confirm("Do you want to continue?"):
                    continue

            if vm_running(machine) == True:
                print("Error: VM '%s' is still running...skipping." % machine, file=sys.stderr)
                continue

            print("Reverting '%s' to pristine snapshot..." % machine)
            vm_pristine_snapshot(machine)

def cmd_cmd():
    '''Run a command inside a virtual machine'''

    usage = "usage: %prog cmd [options] <vm> <command>"

    epilog = "\n" + \
             "Eg:\n" + \
             "$ uvt cmd -p sec 'sudo reboot'\n\n" + \
             "This will reboot (via ssh) all sec-<release>-<arch> machines\n" + \
             "that are running with the 'sec' prefix\n\n" + \
             "$ uvt cmd clean-precise-i386 'sudo apt-get update'\n\n" + \
             "This will run 'apt-get update' on the single VM named 'clean-precise-i386'\n"

    optparse.OptionParser.format_epilog = lambda self, formatter: self.epilog
    parser = optparse.OptionParser(usage = usage, epilog = epilog)

    parser.add_option("-p", "--prefix", dest="prefix", default=None, metavar="PREFIX",
                      help="Run command on all VMs with the PREFIX prefix")

    parser.add_option("-a", "--arch", dest="arch", default=None, metavar="ARCH",
                      help="Run command on all VMs with the ARCH arch")

    parser.add_option("-s", "--start", dest="start", default=False, action='store_true',
                      help="Start the VM (and shutdown if it wasn't running")

    parser.add_option("-t", "--timeout", dest="timeout", default=90, metavar="TIMEOUT",
                      help="wait TIMEOUT seconds for VM to come up if -s is used (default: %default)")

    parser.add_option("-f", "--force-ssh", dest="force_ssh", default=False, action='store_true',
                      help="force the SSH keys to be taken")

    parser.add_option("-c", "--continue", dest="cont", default=False, action='store_true',
                      help="continue if command fails")

    parser.add_option("-r", "--root", dest="root", default=False, action='store_true',
                      help="login to the VM as root")

    parser.add_option("-q", "--quiet", dest="quiet", default=False, action='store_true',
                      help="only report hostnames and output")

    (opt, args) = parser.parse_args()

    if opt.prefix == None and len(args) < 2:
        print("Error: Need to specify at least one VM (or prefix) and command!\n", file=sys.stderr)
        parser.print_help()
        sys.exit(1)

    if opt.prefix != None and len(args) < 1:
        print("Error: Need to specify command!\n", file=sys.stderr)
        parser.print_help()
        sys.exit(1)

    machines = []
    if opt.prefix is not None:
        machines = vm_list(prefix=opt.prefix, arch=opt.arch)
        command = args[0]
    else:
        machines.append(args[0])
        command = args[1]

    for machine in machines:
        print("----- %s -----" % machine)
        if check_vm_exists(machine) == False:
            print("Error: VM '%s' does not exist, skipping." % machine, file=sys.stderr)
            continue

        result = vm_run_command(machine, command, root=opt.root, start=opt.start,
                                start_timeout=opt.timeout, force_keys=opt.force_ssh,
                                quiet=opt.quiet, output=True, interactive=True)

        if result == False and opt.cont == False:
            print("Error: VM '%s' command failed. Aborting." % machine, file=sys.stderr)
            sys.exit(1)

def cmd_repo():
    '''Adds or removes a local repo to a VM'''

    usage = "usage: %prog repo [options] <vm>"

    epilog = "\n" + \
             "Eg:\n" + \
             "$ uvt repo -e -p sec\n\n" + \
             "This will add the following to /etc/apt/sources.list.d/uvt-repo.list:\n" + \
             "deb %s <release>/\n" % uvt_conf['vm_repo_url'] + \
             "deb-src %s <release>/\n" % uvt_conf['vm_repo_url']

    optparse.OptionParser.format_epilog = lambda self, formatter: self.epilog
    parser = optparse.OptionParser(usage = usage, epilog = epilog)

    parser.add_option("-p", "--prefix", dest="prefix", default=None, metavar="PREFIX",
                      help="Modify repo on all VMs with the PREFIX prefix")

    parser.add_option("-a", "--arch", dest="arch", default=None, metavar="ARCH",
                      help="Modify repo on all VMs with the ARCH arch")

    parser.add_option("-d", "--disable", dest="disable", default=False, action='store_true',
                      help="Disable the local repo")

    parser.add_option("-e", "--enable", dest="enable", default=False, action='store_true',
                      help="Enable the local repo")

    parser.add_option("-r", "--release", dest="release", default=None, metavar="RELEASE",
                      help="Force repo to the RELEASE release")

    (opt, args) = parser.parse_args()

    if opt.prefix == None and len(args) < 1:
        print("Error: Need to specify at least one VM (or prefix)!\n", file=sys.stderr)
        parser.print_help()
        sys.exit(1)

    if opt.enable == True and opt.disable == True:
        print("Error: Need to specify either -d or -e, not both!\n", file=sys.stderr)
        parser.print_help()
        sys.exit(1)

    if opt.enable == False and opt.disable == False:
        print("Error: Need to specify either -d or -e!\n", file=sys.stderr)
        parser.print_help()
        sys.exit(1)

    machines = []
    if opt.prefix is not None:
        machines = vm_list(prefix=opt.prefix, arch=opt.arch)
    else:
        machines = args

    for machine in machines:
        if check_vm_exists(machine) == False:
            print("Error: VM '%s' does not exist, skipping." % machine, file=sys.stderr)
            continue

        if vm_running(machine) == False:
            print("INFO: VM '%s' is not running, skipping" % machine, file=sys.stdout)
            continue

        repo_release = None
        if opt.release != None:
            check_release(opt.release)
            repo_release = opt.release
        else:
            if '-' in machine:
                repo_release = machine.split("-")[1]

        if repo_release == None:
            print("Error: Could not determine valid release for '%s', skipping." % machine, file=sys.stderr)
            continue

        result = vm_update_local_repo(machine, release=repo_release,
                                      enable=opt.enable)

        if result == False:
            print("Error: could not update VM '%s'. skipping." % machine, file=sys.stderr)
            continue

def cmd_update():
    '''Does a dist-upgrade inside a VM'''

    usage = "usage: %prog update [options] <vm1> <vm2>"

    parser = optparse.OptionParser(usage = usage)

    parser.add_option("-p", "--prefix", dest="prefix", default=None, metavar="PREFIX",
                      help="Update all non-running VMs with the PREFIX prefix")

    parser.add_option("-a", "--arch", dest="arch", default=None, metavar="ARCH",
                      help="Update all non-running VMs with the ARCH arch")

    parser.add_option("-r", "--autoremove", dest="autoremove", default=False,
                      action='store_true', help="Autoremove packages")

    parser.add_option("-n", "--nosnapshot", dest="snapshot", default=True,
                      action='store_false', help="Don't revert or update snapshot")

    parser.add_option("-f", "--force", dest="force", default=False, action='store_true',
                      help="Do not ask for confirmation before reverting")

    (opt, args) = parser.parse_args()

    if opt.prefix == None and len(args) < 1:
        print("Error: Need to specify at least one VM (or prefix)!\n", file=sys.stderr)
        parser.print_help()
        sys.exit(1)

    machines = []
    if opt.prefix is not None:
        machines = vm_list(prefix=opt.prefix, arch=opt.arch)
    else:
        machines = args

    for machine in machines:
        if check_vm_exists(machine) == False:
            print("Error: VM '%s' does not exist, skipping." % machine, file=sys.stderr)
            continue

        if vm_running(machine) == True:
            print("VM '%s' is running, skipping." % machine)
            continue

        if opt.snapshot:
            if not opt.force:
                print("\nWARNING! The '%s' VM will be reverted to the pristine snapshot!" % machine)
                if not confirm("Do you want to continue?"):
                    continue

        print("Updating '%s'..." % machine)
        result = vm_update_packages(machine, autoremove = opt.autoremove,
                                             snapshot = opt.snapshot)

        if result == False:
            print("Error: could not update VM '%s'. skipping." % machine, file=sys.stderr)
            continue

def cmd_clone():
    '''Clones a VM into a new one'''

    usage = "usage: %prog clone [options] <original> <new>\n" + \
            "usage: %prog clone [options] -p PREFIX NEWPREFIX"

    parser = optparse.OptionParser(usage = usage)

    parser.add_option("-p", "--prefix", dest="prefix", default=None, metavar="PREFIX",
                      help="Clone all non-running VMs with the PREFIX prefix")

    parser.add_option("-a", "--arch", dest="arch", default=None, metavar="ARCH",
                      help="Clone all non-running VMs with the ARCH arch")

    parser.add_option("-r", "--revert", dest="revert", default=False, action='store_true',
                      help="Revert VM to pristine snapshot before cloning")

    parser.add_option("-n", "--nosnapshot", dest="snapshot", default=True,
                      action='store_false', help="Don't create clone's pristine snapshot")

    parser.add_option("-f", "--force", dest="force", default=False, action='store_true',
                      help="Do not ask for confirmation")

    (opt, args) = parser.parse_args()

    if opt.prefix == None and len(args) < 2:
        print("Error: Need to specify original and new VM names (or prefix)!\n", file=sys.stderr)
        parser.print_help()
        sys.exit(1)

    if opt.prefix != None and len(args) < 1:
        print("Error: Need to specify new VM prefix!\n", file=sys.stderr)
        parser.print_help()
        sys.exit(1)

    machines = []
    new_name = ""

    if opt.prefix is not None:
        machines = vm_list(prefix=opt.prefix, arch=opt.arch)
        new_name = args[0]
    else:
        machines.append(args[0])
        new_name = args[1]

    for machine in machines:
        if check_vm_exists(machine) == False:
            print("Error: VM '%s' does not exist, skipping." % machine, file=sys.stderr)
            continue

        if vm_running(machine) == True:
            print("VM '%s' is running, skipping." % machine)
            continue

        if opt.revert:
            if not opt.force:
                print("\nWARNING! The '%s' VM will be reverted to the pristine snapshot!" % machine)
                if not confirm("Do you want to continue?"):
                    continue

            print("Reverting '%s' to pristine snapshot..." % machine)
            vm_pristine_snapshot(machine)

        result = vm_clone(machine, prefix = opt.prefix, new_name = new_name,
                          snapshot=opt.snapshot)

        if result == False:
            print("Error: could not clone VM '%s'. skipping." % machine, file=sys.stderr)
            continue

def cmd_remove():
    '''Remove a virtual machine'''

    usage = "usage: %prog remove [options] <vm1> <vm2>..."

    parser = optparse.OptionParser(usage = usage)

    parser.add_option("-f", "--force", dest="force", default=False, action='store_true',
                      help="Do not ask for confirmation before removing")

    parser.add_option("-p", "--prefix", dest="prefix", default=None, metavar="PREFIX",
                      help="Stop all VMs with the PREFIX prefix")

    (opt, args) = parser.parse_args()

    if opt.prefix == None and len(args) < 1:
        print("Error: Need to specify at least one VM (or prefix)!\n", file=sys.stderr)
        parser.print_help()
        sys.exit(1)

    machines = []
    if opt.prefix is not None:
        machines = vm_list(prefix=opt.prefix)
    else:
        machines = args

    for machine in machines:
        if check_vm_exists(machine) == False:
            print("Error: VM '%s' does not exist, skipping." % machine, file=sys.stderr)
            continue

        disks = vm_get_disks(machine)

        if not opt.force:
            print("\nWARNING! The '%s' VM and the following disks will be DELETED:" % machine)
            for disk in disks:
                 print(disk)
            print("")
            if not confirm("Do you want to continue?"):
                continue

        if vm_running(machine) == True:
            print("Destroying '%s'" % machine)
            vm_destroy(machine)

        print("Undefining '%s'" % machine)
        vm_remove(machine)

        if check_vm_exists(machine) == True:
            print("Error: VM '%s' could not be deleted, skipping." % machine, file=sys.stderr)
            continue

        remove_ssh_keys(machine)

def cmd_snapshot():
    '''Create or update a virtual machine snapshot'''

    usage = "usage: %prog snapshot [options] <vm1> <vm2>..."
    parser = optparse.OptionParser(usage = usage)

    # Disable for now...not sure if we want to support this
    #parser.add_option("-n", "--name", dest="name", default=None, metavar='NAME',
    #                  help="Specify NAME for snapshot. If no name is specified, " +
    #                       "if will default to 'pristine' for the first one")

    parser.add_option("-p", "--prefix", dest="prefix", default=None, metavar="PREFIX",
                      help="Snapshot all VMs with the PREFIX prefix")

    parser.add_option("-f", "--force", dest="force", default=False, action='store_true',
                      help="Do not ask for confirmation before updating existing snapshots")

    (opt, args) = parser.parse_args()

    if opt.prefix == None and len(args) < 1:
        print("Error: Need to specify at least one VM (or prefix)!\n", file=sys.stderr)
        parser.print_help()
        sys.exit(1)

    machines = []
    if opt.prefix is not None:
        machines = vm_list(prefix=opt.prefix)
    else:
        machines = args

    for machine in machines:
        if check_vm_exists(machine) == False:
            print("Error: VM '%s' does not exist, skipping." % machine, file=sys.stderr)
            continue

        if vm_running(machine) == True:
            print("VM '%s' is running, stopping." % machine)
            vm_stop(machine)
            if not vm_stop_wait(machine):
                print("Error: VM '%s' could not be stopped, skipping." % machine, file=sys.stderr)
                continue

        # TODO: If we support multiple snapshots at some point, this needs
        # to be updated
        if vm_get_snapshot(machine) != None:
            if not opt.force:
                print("\nWARNING! The '%s' VM already has a snapshot!" % machine)
                print("Continuing will update the snapshot to the current VM state.")
                if not confirm("Do you want to continue?"):
                    continue

            if vm_delete_snapshot(machine) == False:
                print("Error: Could not delete VM '%s' current snapshot, skipping." % machine, file=sys.stderr)
                continue

        vm_create_snapshot(machine)

def cmd_revert():
    '''Revert virtual machine to pristine snapshot'''

    usage = "usage: %prog revert [options] <vm1> <vm2>..."
    parser = optparse.OptionParser(usage = usage)

    parser.add_option("-f", "--force", dest="force", default=False, action='store_true',
                      help="Do not ask for confirmation before reverting")

    parser.add_option("-p", "--prefix", dest="prefix", default=None, metavar="PREFIX",
                      help="Revert all VMs with the PREFIX prefix")

    (opt, args) = parser.parse_args()

    if opt.prefix == None and len(args) < 1:
        print("Error: Need to specify at least one VM (or prefix)!\n", file=sys.stderr)
        parser.print_help()
        sys.exit(1)

    machines = []
    if opt.prefix is not None:
        machines = vm_list(prefix=opt.prefix)
    else:
        machines = args

    for machine in machines:
        if check_vm_exists(machine) == False:
            print("Error: VM '%s' does not exist, skipping." % machine, file=sys.stderr)
            continue

        if not opt.force:
            print("\nWARNING! The '%s' VM will be reverted to the pristine snapshot!" % machine)
            if not confirm("Do you want to continue?"):
                continue

        if vm_running(machine) == True:
            print("VM '%s' is running, stopping." % machine)
            vm_stop(machine)
            if not vm_stop_wait(machine):
                print("Error: VM '%s' could not be stopped, skipping." % machine, file=sys.stderr)
                continue

        print("Reverting VM '%s' to pristine snapshot..." % machine)
        vm_pristine_snapshot(machine)

def cmd_list():
    '''List virtual machines'''

    usage = "usage: %prog list [options]"

    parser = optparse.OptionParser(usage = usage)

    parser.add_option("-p", "--prefix", dest="prefix", default=None, metavar="PREFIX",
                      help="List only VMs with the PREFIX prefix")

    parser.add_option("-a", "--arch", dest="arch", default=None, metavar="ARCH",
                      help="List only VMs with the ARCH arch")

    (opt, args) = parser.parse_args()

    machines = vm_list(prefix=opt.prefix, arch=opt.arch)

    machines.sort()
    for vm in machines:
        print(vm)

def cmd_config():
    '''Create a default config file template'''
    create_uvt_config()

def cmd_dump():
    '''Dumps the config for debugging purposes'''
    print("Config is:\n")
    keys = list(uvt_conf.keys())
    keys.sort()
    for each in keys:
        print("%s='%s'" % (each, uvt_conf[each]))

def cmd_view():
    '''Launch virt-viewer for specified VMs'''

    usage = "usage: %prog list [options]"

    parser = optparse.OptionParser(usage = usage)

    parser.add_option("-p", "--prefix", dest="prefix", default=None, metavar="PREFIX",
                      help="List only VMs with the PREFIX prefix")

    parser.add_option("-a", "--arch", dest="arch", default=None, metavar="ARCH",
                      help="List only VMs with the ARCH arch")

    (opt, args) = parser.parse_args()

    machines = []
    if opt.prefix is not None or opt.arch is not None:
        machines = vm_list(prefix=opt.prefix, arch=opt.arch)
    else:
        machines = args

    machines.sort()
    for vm in machines:
        if vm_running(vm) == True:
            vm_view(vm)


def cmd_test():
    '''Test command for dev purposes'''

#
# Misc functions
#
def vm_clone(vm_name, prefix, new_name, snapshot=True):
    '''Clones a VM'''

    # TODO: ionice
    # (ionice -c 2 -n 7 -p $$)

    if prefix == None:
        clone = new_name
    elif '-' in prefix:
        print("Error: Can't specify a multiple part prefix when cloning. Skipping.", file=sys.stderr)
        return False
    else:
        if prefix + '-' in vm_name:
            clone = new_name + vm_name[vm_name.find('-'):]
        else:
            print("Error: could not determine clone VM name for '%s', skipping." % vm_name, file=sys.stderr)
            return False

    if check_vm_exists(vm_name) == False:
       print("Error: VM '%s' does not exist, skipping." % vm_name, file=sys.stderr)
       return False

    if check_vm_exists(clone) == True:
       print("Error: New VM name '%s' already exists, skipping." % clone, file=sys.stderr)
       return False

    cmd = ['virt-clone', '--connect', uvt_conf["vm_connect"],
           '--original', vm_name, '--name', clone ]

    # Get a list of disks, and mangle the names to the new clone
    ori_disks = vm_get_disks(vm_name)
    for disk in ori_disks:
        cmd.append('--file')
        if vm_name in disk:
            cmd.append(disk.replace(vm_name, clone, 1))
        else:
            # Better than nothing
            cmd.append(disk + "-" + clone)

    print ("Cloning VM '%s' to '%s'..." % (vm_name, clone))
    rc, out = runcmd(cmd)
    if rc != 0:
        return False

    if vm_clone_update_config(vm_name, clone) != True:
        return False

    # Update clone snapshot
    if snapshot == True:
        vm_create_snapshot(clone)

    print ("Clone successful.")
    return True

def vm_clone_update_config(ori_name, clone_name):
    '''Updated the config in a VM'''

    # The old vm-tools used to mount the qcow2 image and manipulate files
    # inside it, resulting in a requirement for root access, having random
    # nautilus windows pop open when the disks gets mounted, and was
    # susceptible to sporadic failures.
    # 
    # Since the original VM needs to be shut down for the clone to work,
    # I can't think of any reason why we can't just fire up the clone and
    # perform whatever we need to do inside it.

    print ("Updating clone config...")

    # Change hostname, hosts file
    # Remove persistent udev net config since the MAC address changed
    # Regenerate ssh keys
    cmd = 'echo %s > /etc/hostname;' % clone_name + \
          'sed -i "s/%s/%s/g" /etc/hosts;' % (ori_name, clone_name) + \
          'rm -f /etc/udev/rules.d/70-persistent-net.rules;' + \
          'rm -f /etc/ssh/*_key /etc/ssh/*_key.pub;' + \
          'ssh-keygen -q -f "/etc/ssh/ssh_host_rsa_key" -N "" -t rsa;' + \
          'ssh-keygen -q -f "/etc/ssh/ssh_host_dsa_key" -N "" -t dsa;' + \
          'shutdown -h now'

    remove_ssh_keys(ori_name)

    print("Starting clone: %s" % clone_name)
    vm_start(clone_name)
    if vm_start_wait(ori_name, clone_name = clone_name) == False:
        print("Could not start clone: %s" % clone_name)
        return False

    result = vm_run_command(ori_name, cmd, root=True, start=False,
                            force_keys=True, quiet=True, output=False)

    remove_ssh_keys(ori_name)
    remove_ssh_keys(clone_name)

    print("Stopping clone: %s" % clone_name)
    if vm_stop_wait(clone_name) == False:
        print("Clone did not shutdown: %s" % clone_name)
        return False

    if result == False:
        print ("Could not update clone config.")
        return False

    print ("Clone config updated successfully.")
    return True

def vm_update_packages(vm_name, autoremove=False, snapshot=True):
    '''Does a dist-upgrade inside a VM'''

    command = "apt-get update && apt-get -y --force-yes upgrade && apt-get -y --force-yes dist-upgrade ; apt-get clean"

    if vm_running(vm_name) == True:
        print("VM '%s' is running, stopping." % vm_name)
        vm_stop(vm_name)
        if not vm_stop_wait(vm_name, quiet=True):
            print("Error: VM '%s' could not be stopped." % vm_name, file=sys.stderr)
            return False

    # Revert to the pristine snapshot
    if snapshot == True and vm_get_snapshot(vm_name) != None:
        vm_pristine_snapshot(vm_name)

    # Start it and update it
    vm_start(vm_name)
    if vm_start_wait(vm_name, quiet=True) == False:
        print("Could not start VM: %s" % vm_name)
        return False

    result = vm_run_command(vm_name, command, root=True, force_keys=True,
                            output=True)

    if result == True and autoremove == True:
        result = vm_run_command(vm_name,
                                "apt-get -y --force-yes autoremove --purge",
                                root=True, force_keys=True, output=True)

    # Remove any crash reports before we take a new snapshot
    if result == True:
        result = vm_run_command(vm_name,
                                "sh -c 'rm -f /var/crash/*'",
                                root=True, force_keys=True, output=True)

    vm_stop(vm_name)
    if not vm_stop_wait(vm_name, quiet=True):
        print("Error: VM '%s' could not be stopped." % vm_name, file=sys.stderr)
        return False

    if result == True:
        # TODO: If we support multiple snapshots at some point, this needs
        # to be updated
        if snapshot == True and vm_get_snapshot(vm_name) != None:
            if vm_delete_snapshot(vm_name) == False:
                print("Error: Could not delete VM '%s' current snapshot." % vm_name, file=sys.stderr)
                return False

            vm_create_snapshot(vm_name)

    return result

def vm_update_local_repo(vm_name, release, enable=True):
    '''Enables or disables a local repo'''

    if vm_ping(vm_name) == False:
        return False

    sources_file = "/etc/apt/sources.list.d/uvt-repo.list"

    if enable == True:
        print("Enabling %s for %s" % (uvt_conf['vm_repo_url'], vm_name))
        command = "bash -c 'echo -e \"deb %(repo_url)s %(release)s/\ndeb-src %(repo_url)s %(release)s/\" > %(sources)s'" \
                   % { 'repo_url': uvt_conf['vm_repo_url'],
                       'release': release,
                       'sources': sources_file }
    else:
        print("Disabling %s for %s" % (uvt_conf['vm_repo_url'], vm_name))
        command = "rm -f %s" % sources_file

    result = vm_run_command(vm_name, command, root=True, force_keys=True)
    if result == True:
        vm_run_command(vm_name, "apt-get update", root=True,
                       force_keys=True, output=False)

    return result

def vm_check_pool(vm_path):
    '''Configures libvirt storage pool for uvt'''

    # Is is already there?
    if 'uvt' in vm_get_pools():
        pool_path = vm_get_pool_path('uvt')
        if pool_path == vm_path:
            return
        else:
            print("The current libvirt 'uvt' pool is set to:")
            print(pool_path + "\n")
            print("The uvt configuration wants it to be located here:")
            print(vm_path + "\n")
            print("Changing the pool path may break existing VMs.")
            print("")

            if not confirm("Would you like to change the pool path?"):
                print("Aborting. Please fix pool path manually.")
                sys.exit(1)

            vm_delete_pool('uvt')

    vm_create_pool('uvt', vm_path)

def vm_create_pool(pool_name, path):
    '''Creates a storage pool'''

    # 'pool-create-as' is only for the session. Must use 'pool-define-as'
    # for it to be permanent.
    rc, out = runcmd(["virsh", "--connect", uvt_conf["vm_connect"],
                      "pool-define-as", pool_name, "dir",
                      "-", "-", "-", "-", path])

    if rc != 0:
        return False

    # Turn on autostart
    rc, out = runcmd(["virsh", "--connect", uvt_conf["vm_connect"],
                      "pool-autostart", pool_name])

    if rc != 0:
        return False

    # Start it now
    rc, out = runcmd(["virsh", "--connect", uvt_conf["vm_connect"],
                      "pool-start", pool_name])

    if rc != 0:
        return False

    return True

def vm_delete_pool(pool_name):
    '''Deletes a storage pool'''

    rc, out = runcmd(["virsh", "--connect", uvt_conf["vm_connect"],
                      "pool-destroy", pool_name])

    if rc != 0:
        return False

    rc, out = runcmd(["virsh", "--connect", uvt_conf["vm_connect"],
                      "pool-undefine", pool_name])

    if rc != 0:
        return False

    return True

def vm_get_pool_path(pool_name):
    '''Returns the path for a given storage pool'''

    rc, out = runcmd(["virsh", "--connect", uvt_conf["vm_connect"],
                      "pool-dumpxml", pool_name])
    if rc is not 0:
        return None

    element = ET.fromstring(out[out.find("<"):])
    pool = ET.ElementTree(element).getroot()
    return pool.findtext('target/path')

def vm_create_snapshot(vm_name, multiple = False, snapshot=None):
    '''Creates a new VM snapshot'''

    # If we didn't ask for multiple snapshots, and we already
    # have a snapshot, bail out.
    if multiple == False and vm_get_snapshot(vm_name) != None:
        print("ERROR: VM already has a snapshot, skipping!")
        return False

    new_snap = snapshot
    if snapshot == None:
        if vm_get_snapshot(vm_name) != None:
            new_snap = time.strftime("%Y%m%d%H%M", time.localtime(time.time()))
        else:
            new_snap = "pristine"

    rc, out = runcmd(["virsh", "--connect", uvt_conf["vm_connect"],
                      "snapshot-create-as", vm_name, new_snap, "uvt snapshot"])

    if rc == 0:
        # For some reason, libvirt doesn't write out the xml file after
        # manipulating snapshots.
        # Work around the issue by updating the description
        vm_set_description(vm_name)
        return True
    else:
        return False

def vm_pristine_snapshot(vm_name):
    '''Revert a VM to the pristine snapshot'''

    rc, out = runcmd(["virsh", "--connect", uvt_conf["vm_connect"],
                      "snapshot-revert", vm_name, 'pristine'])

    if rc == 0:
        # For some reason, libvirt doesn't write out the xml file after
        # manipulating snapshots.
        # Work around the issue by updating the description
        vm_set_description(vm_name)
        return True
    else:
        print("ERROR: Could not revert %s to pristine snapshot, skipping!" % vm_name)
        return False

def vm_delete_snapshot(vm_name, snapshot='pristine'):
    '''Delete a VM snapshot'''

    rc, out = runcmd(["virsh", "--connect", uvt_conf["vm_connect"],
                      "snapshot-delete", vm_name, snapshot, '--children'])

    if rc == 0:
        return True
    else:
        return False

def vm_set_description(vm_name, desc='Created by UVT'):
    '''Sets a VM description'''

    rc, out = runcmd(["virsh", "--connect", uvt_conf["vm_connect"],
                      "desc", vm_name, '--new-desc', desc])

    if rc == 0:
        return True
    else:
        return False

def vm_get_snapshot(vm_name):
    '''Gets the name of the current VM snapshot'''

    rc, out = runcmd(["virsh", "--connect", uvt_conf["vm_connect"],
                      "snapshot-current", "--name", vm_name])

    if rc is not 0 or "no current snapshot" in out:
        return None

    return out.strip()

def vm_get_disks(vm_name):
    '''Gets a list of disks belonging to the vm'''

    rc, out = runcmd(["virsh", "--connect", uvt_conf["vm_connect"],
                      "dumpxml", vm_name])
    if rc is not 0:
        return None

    disks = []
    element = ET.fromstring(out[out.find("<"):])
    domain = ET.ElementTree(element).getroot()
    for disk in domain.findall('devices/disk'):
        # Make sure it's a disk device, so we don't list CDs
        if disk.get('device') == 'disk':
            disks.append(disk.find('source').get('file'))

    return disks

def vm_view(vm_name):
    '''Launches a viewer for a VM'''

    try:
        subprocess.Popen(["nohup", "virt-viewer",
                          "-c", uvt_conf["vm_connect"], vm_name],
                          stdin=None, stdout=subprocess.PIPE,
                          stderr=subprocess.STDOUT, close_fds=True,
                          shell=False)
    except:
        print("ERROR: Something went wrong with virt-viewer for '%s'!" % vm_name, file=sys.stderr)
        return

def vm_get_vnc(vm_name):
    '''Gets the VNC address for a running VM'''
    rc, out = runcmd(["virsh", "--connect", uvt_conf["vm_connect"],
                      "vncdisplay", vm_name])
    if rc is not 0:
        return None

    match = re.match('^[0-9:]+', out)
    if match:
        return match.group(0)
    else:
        return None

def vm_list(prefix=None, arch=None, supported=True):
    '''Get a list of virtual machines'''
    # TODO: see if there's a python library for this
    rc, out = runcmd(["virsh", "--connect", uvt_conf["vm_connect"],
                      "list", "--all"])
    machines = []
    if rc is not 0:
        return []
    for line in out.split("\n"):
        # skip the two first lines
        if re.search(r'Id\s+Name', line):
            continue
        if "-----------------" in line:
            continue

        try:
            good_prefix = True
            good_arch = True
            name = line.strip().split()[1]
            if prefix != None:
                if not name.startswith(prefix):
                    good_prefix = False
            if arch != None:
                if arch != name.split("-")[-1]:
                    good_arch = False
            if good_prefix and good_arch:
                machines.append(name)
        except:
            pass

    return machines

def vm_get_pools():
    '''Get a list of storage pools'''

    rc, out = runcmd(["virsh", "--connect", uvt_conf["vm_connect"],
                      "pool-list", "--all"])
    pools = []
    if rc is not 0:
        return []
    for line in out.split("\n"):
        # skip the two first lines
        if "Autostart" in line:
            continue
        if "-----------------" in line:
            continue

        try:
            pools.append(line.strip().split()[0])
        except:
            pass

    return pools

def vm_run_command(vm_name, command, root=False, start=False,
                   start_timeout=1800, force_keys=False, quiet=True,
                   output=True, interactive=False):
    '''Runs a command on a VM'''

    was_running = vm_running(vm_name)
    if not start and not was_running:
        if not quiet:
            print("Skipping (not running)")
            return True

    if start and not was_running:
        if not quiet:
            print("Starting VM: %s" % vm_name)
        vm_start(vm_name)
        if vm_start_wait(vm_name, start_timeout) == False:
            print("Could not start VM: %s" % vm_name)
            return False

    ssh_command = ['ssh']
    if root:
        ssh_command += ['-l', 'root']
    else:
        ssh_command += ['-l', uvt_conf['vm_username']]
    if force_keys:
        ssh_command += ['-o', 'StrictHostKeyChecking=no']
    if interactive:
        ssh_command += ['-t']
    if quiet:
        ssh_command += ['-q']
    ssh_command += ['-o', 'BatchMode=yes']

    ssh_command += [vm_name, command]

    if interactive:
        rc, out = runcmd(ssh_command, stderr = None, stdout = None, stdin = None)
    elif output:
        rc, out = runcmd(ssh_command, stderr = None, stdout = None)
    else:
        rc, out = runcmd(ssh_command)

    if rc != 0:
        print("SSH command failed.")

    if start and not was_running:
        if not quiet:
            print("Stopping VM: %s" % vm_name)
        vm_stop(vm_name)
        if vm_stop_wait(vm_name, start_timeout) == False:
            print("Could not stop VM: %s" % vm_name)
            return False

    return True

def remove_ssh_keys(vm_name):
    '''Removes SSH keys for a host'''
    for host in [vm_name, vm_name + ".", vm_name + ".local"]:
        runcmd(["ssh-keygen", "-R", host])

def vm_start(vm_name):
    '''Starts a VM'''
    rc, out = runcmd(["virsh", "--connect", uvt_conf["vm_connect"],
                      "start", vm_name])

    # Make sure libvirt says we're running, or else try again
    for first in range(30):
        if vm_running(vm_name) == True:
            break
        time.sleep(1)

    rc, out = runcmd(["virsh", "--connect", uvt_conf["vm_connect"],
                      "start", vm_name])


def vm_destroy(vm_name):
    '''Powers off a VM'''
    rc, out = runcmd(["virsh", "--connect", uvt_conf["vm_connect"],
                      "destroy", vm_name])

def vm_remove(vm_name):
    '''Removes a VM'''

    # Get a list of the VM's disks
    disks = vm_get_disks(vm_name)

    rc, out = runcmd(["virsh", "--connect", uvt_conf["vm_connect"],
                      "undefine", vm_name, "--snapshots-metadata"])

    # Now erase the disks from the pool
    for disk in disks:
        rc, out = runcmd(["virsh", "--connect", uvt_conf["vm_connect"],
                          "vol-delete", disk])

    # We need to refresh the pool now, or we can't create a new
    # vm with the same name as one we just removed
    rc, out = runcmd(["virsh", "--connect", uvt_conf["vm_connect"],
                      "pool-refresh", 'uvt'])


def vm_stop(vm_name):
    '''Stops a VM'''

    # We can't just do a virsh shutdown, as that only sends an ACPI
    # event, and newer Ubuntu releases either ignore them, or display
    # a shutdown menu.

    # If we can connect using ssh, issue a shutdown command
    if ssh_connect(vm_name):
        vm_run_command(vm_name, "shutdown -h now", root=True,
                                force_keys=True, output=False)
    else:
        rc, out = runcmd(["virsh", "--connect", uvt_conf["vm_connect"],
                          "shutdown", vm_name])


def vm_start_wait(vm_name, timeout=1800, quiet=False, clone_name=None):
    '''Waits until a VM is started'''

    # See if we're waiting for a clone to start
    if clone_name != None:
        libvirt_host = clone_name
    else:
        libvirt_host = vm_name

    # First step, make sure libvirt says we're running
    for first in range(timeout):
        if vm_running(libvirt_host) == True:
            break
        if quiet == False:
            sys.stdout.write(".")
            sys.stdout.flush()
        time.sleep(1)

    # Second step, make sure the ssh port is reachable
    for second in range(timeout-first):
        if vm_ping(vm_name) == True:
            if quiet == False:
                sys.stdout.write("\n")
            return True
        if quiet == False:
            sys.stdout.write(".")
            sys.stdout.flush()
        time.sleep(1)

    if quiet == False:
        sys.stdout.write("\n")
    return False

def vm_ping(vm_name):
    '''Attempts to ping a VM'''
    hostnames = [ vm_name + ".", vm_name ]
    if uvt_conf["vm_host_use_avahi"].lower() is not "no":
        hostnames += [ vm_name + ".local" ]

    for host in hostnames:
        rc, out = runcmd(["ping", "-c1", "-w1", host])
        if rc == 0 and ssh_connect(host) == True:
            return True

    return False

def ssh_connect(host):
    '''Attempts a connection on port 22'''
    import socket
    try:
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        s.connect((host, 22))
        data = s.recv(4096)
        s.close()
        if b"SSH-2" in data:
            return True
    except:
        pass

    return False

def vm_running(vm_name):
    '''Checks if a vm is running or not'''
    rc, out = runcmd(["virsh", "--connect", uvt_conf["vm_connect"],
                      "domstate", vm_name])
    if "running" in out:
        return True
    elif "shut off" in out:
        return False

    # Something went wrong
    return None

def vm_stop_wait(vm_name, timeout=1800, quiet=False):
    '''Waits until a VM is stopped'''
    # All we need to do is wait until libvirt says we're stopped
    for second in range(timeout):
        if vm_running(vm_name) == False:
            if quiet == False:
                sys.stdout.write("\n")
            return True
        if quiet == False:
            sys.stdout.write(".")
            sys.stdout.flush()
        time.sleep(1)

    return False

def create_vm_img(name, size, disk_format, verbose=False):

    args = ['qemu-img',
            'create',
            '-f',
            disk_format,
            '-o',
            "size=" + size + "G",
            name ]

    if verbose:
        print("Running:")
        print(" ".join(args))

    rc, out = runcmd(args)

    if rc != 0:
        print("ERROR: Something went wrong with qemu-img create!", file=sys.stderr)
        sys.exit(1)

    args = ['qemu-img',
            'check',
            '-f',
            disk_format,
            name ]

    if verbose:
        print("Running:")
        print(" ".join(args))

    rc, out = runcmd(args)

    if rc != 0:
        print("ERROR: Something went wrong with qemu-img check!", file=sys.stderr)
        sys.exit(1)

def create_vm(release_iso, vm_name, arch, bridge, release, vnc, loader, ovmf_uefi=False, verbose=False):
    '''Creates a new vm with virt-install'''

    if arch == "amd64":
        virtinst_arch = "x86_64"
    else:
        virtinst_arch = arch

    if bridge:
        network_type = "bridge=%s" % bridge
    else:
        network_type = "network=default"

    disk_path = uvt_conf["vm_path"] + '/' + vm_name + '.' + uvt_conf['vm_disk_format']
    preseed_iso_path = uvt_conf['vm_dir_iso_cache'] + '/' + "vmtools-" + vm_name + \
                       '-' + release_iso

    create_vm_img(disk_path, uvt_conf['vm_image_size'], uvt_conf['vm_disk_format'], verbose)

    vm_disk_driver = get_vm_disk_driver(release)
    vm_video = get_vm_video(release)
    vm_net = get_vm_net(release)

    # OVMF UEFI files are limited in what they support. Downgrade to
    # known good values when specifying --with-ovmf-uefi
    if ovmf_uefi:
        vm_disk_driver = "ide"
        vm_video = "cirrus"
        # TODO: once UEFI pxeboot is supported, may want to set this
        #vm_net = ...
        # TODO: why doesn't preseed_iso_path work even with UEFI
        print("WARNING: ignoring preseed iso when using --with-ovmf-uefi", file=sys.stderr)
        preseed_iso_path = os.path.join(uvt_conf['vm_dir_iso'], release_iso)

    args = [ 'virt-install',
             '--quiet',
             '--connect=qemu:///system',
             '--name=' + vm_name,
             '--arch=' + virtinst_arch,
             '--cpu=host',
             '--ram=' + uvt_conf['vm_memory'],
             '--disk=path=' + disk_path + ',' + \
                    'size=' + uvt_conf['vm_image_size'] + ',' + \
                    'format=' + uvt_conf['vm_disk_format'] + ',' + \
                    'sparse=true,' + \
                    'bus=' + vm_disk_driver,
             '--virt-type=' + get_hypervisor(),
             '--accelerate',
             '--hvm',
             '--cdrom=' + preseed_iso_path,
             '--os-type=linux',
             '--os-variant=generic26',
             '--graphics=vnc',
             '--network=' + network_type + ',model=' + vm_net,
             '--video=' + vm_video,
             '--noreboot' ]

    if vnc == False or 'DISPLAY' not in os.environ:
        args.append('--noautoconsole')

    if loader:
        args.append('--boot=loader=' + loader)

    if verbose:
        print("Running:")
        print(" ".join(args))

    try:
        subprocess.Popen(args, stdin=None, stdout=subprocess.PIPE,
                         stderr=subprocess.STDOUT,
                         close_fds=True, shell=False)
    except:
        print("ERROR: Something went wrong with virt-install!", file=sys.stderr)
        sys.exit(1)

    # We need to wait a while for virt-install to start, otherwise things will
    # fail later
    time.sleep(30)

def get_vm_disk_driver(release):
    '''Figures out the appropriate disk driver type'''

    if release[0] < "jaunty"[0]:
        return "ide"
    else:
        return "virtio"

def get_vm_disk_device(release):
    '''Figures out the appropriate disk device'''

    if release[0] < "jaunty"[0]:
        return "/dev/sda"
    else:
        return "/dev/vda"

def get_vm_net(release):
    '''Figures out the appropriate net driver'''

    # hardy is the first release to support virtio networking, but
    # due to bug #331128, only intrepid and higher is supported
    if release[0] < "intrepid"[0]:
        return "e1000"
    else:
        return "virtio"

def get_vm_video(release):
    '''Figures out the appropriate video driver'''

    # vmvga is broken for precise guests. (LP: #893366)
    # vmvga is broken for precise hosts. (LP: #918791)
    # cirrus is currently broken for raring guests (LP: #1080674)
    # cirrus often crashes in quantal guests when running openjdk qrt test
    #
    # Generally, cirrus is the best option for now.

    if release[0] < "quantal"[0]:
        return "cirrus"
    else:
        return "vmvga"

def get_vm_keyboard(release):
    '''Figures out the appropriate keyboard config location'''

    # natty changed the keyboard settings location
    if release[0] < "natty"[0]:
        return "/etc/default/console-setup"
    else:
        return "/etc/default/keyboard"

def get_hypervisor():
    '''Finds an appropriate hypervisor'''
    ret, out = runcmd(['kvm-ok'])
    if ret == 0:
        return 'kvm'
    else:
        return 'qemu'

def create_preseeded_iso(release_iso, cache, iso_type, vm_name, release,
                         ovmf=False):
    import shutil
    import tempfile
    '''Adds preseed to iso'''

    release_iso_preseed="vmtools-%s-%s" % (vm_name, release_iso)

    # See if there's a cached one first
    cache_ver_file = os.path.join(uvt_conf['vm_dir_iso_cache'], 'cache_version')
    if os.path.exists(cache_ver_file):
        cache_info = parse_config_file(cache_ver_file)
        if 'script' not in cache_info \
           or 'config' not in cache_info \
           or cache_info['script'] != script_version \
           or cache_info['config'] != uvt_conf_hash:
            print("Clearing out cached iso directory...")

            names = os.listdir(uvt_conf['vm_dir_iso_cache'])
            for name in names:
                if name.startswith('vmtools-'):
                    path = os.path.join(uvt_conf['vm_dir_iso_cache'], name)
                    os.unlink(path)
            print("Done.")

    release_iso_preseed_path = os.path.join(uvt_conf['vm_dir_iso_cache'], release_iso_preseed)
    if os.path.exists(release_iso_preseed_path):
        if cache == True:
            print("Found cached preseeded iso file: %s" % release_iso_preseed)
            return
        else:
            os.unlink(release_iso_preseed_path)

    print("Creating preseeded iso...")
    # Create a temp dir to store the iso contents
    temp_dir = tempfile.mkdtemp(prefix="vm-new-setup-")

    # Extract the release iso with xorriso
    iso_path = os.path.join(uvt_conf['vm_dir_iso'], release_iso)
    runcmd(['xorriso', '-osirrox', 'on', '-indev', iso_path,
            '-extract', '/', temp_dir])

    # xorriso leaves this unwritable
    runcmd(['chmod', '-R', 'u+w', temp_dir])

    create_preseed_file(temp_dir, iso_type, release, vm_name)
    create_isolinux_cfg(temp_dir, iso_type, release)
    create_latecommand_file(temp_dir, iso_type, release, vm_name, ovmf=ovmf)

    # Now, recreate an iso file
    if iso_type == "mini":
        # If type mini, then stuff preseed into initrd
        # thanks to smoser for this gem
        initrd = os.path.join(temp_dir, "initrd.gz")

        current_dir = os.getcwd()
        os.chdir(temp_dir)

        runcmd(['gunzip', initrd])
        runcmd(['cpio', '-o', '--format=newc', '--append', '-F', 'initrd'],
               stdin = subprocess.PIPE,
               input="./preseed.cfg\n./latecommand.sh\n")
        runcmd(['gzip', '-9', 'initrd'])
        os.chdir(current_dir)

        isolinux_prefix = ""
    else:
        isolinux_prefix = "isolinux/"

    rc, out = runcmd(['mkisofs', '-r', '-V', 'VMTools', '-J', '-l',
                      '-b', isolinux_prefix + "isolinux.bin",
                      '-c', isolinux_prefix + "boot.cat",
                      '-no-emul-boot', '-boot-load-size', '4',
                      '-cache-inodes', '-boot-info-table',
                      '-v', '-v', '-o', release_iso_preseed_path,
                      temp_dir])

    if rc != 0:
        print("ERROR: Something went wrong trying to generate iso file!", file=sys.stderr)
        sys.exit(1)

    cache_ver_contents = "script=%s\nconfig=%s\n" % (script_version, uvt_conf_hash)
    open(cache_ver_file, 'w').write(cache_ver_contents)

    # Remove temp dir (with sanity check!)
    if os.path.isdir(temp_dir):
        shutil.rmtree(temp_dir, ignore_errors=True)

    if os.path.isfile(release_iso_preseed_path):
        print("Preseeded iso file successfully created: %s" % release_iso_preseed)
    else:
        print("ERROR: Something went wrong creating preseeded iso file!", file=sys.stderr)
        sys.exit(1)

def remove_preseeded_iso(release_iso, cache, vm_name):
    '''Removes preseeded iso file'''

    if cache == True:
        return

    release_iso_preseed="vmtools-%s-%s" % (vm_name, release_iso)
    release_iso_preseed_path = os.path.join(uvt_conf['vm_dir_iso_cache'], release_iso_preseed)

    if os.path.exists(release_iso_preseed_path):
            os.unlink(release_iso_preseed_path)

def create_preseed_file(temp_dir, iso_type, release, vm_name):
    '''Creates an appropriate preseed file'''

    if iso_type == "mini":
        preseed_name = "preseed.cfg"
        preseed_contents = '''
d-i debian-installer/locale string en_US
d-i console-setup/ask_detect boolean false
d-i console-setup/layoutcode string us
console-setup console-setup/layoutcode string	us
console-setup console-setup/layout select	U.S. English
console-setup console-setup/variantcode select	U.S. English
console-setup console-setup/codeset select	. Combined - Latin; Slavic Cyrillic; Hebrew; basic Arabic
d-i netcfg/choose_interface select auto
d-i netcfg/get_hostname string vmtools
d-i netcfg/get_domain string defaultdomain
d-i netcfg/wireless_wep string
d-i hw-detect/load_firmware boolean true
d-i clock-setup/utc boolean true
d-i time/zone string %(vm_timezone)s
d-i partman-auto/method string regular
d-i partman-lvm/device_remove_lvm boolean true
d-i partman-md/device_remove_md boolean true
d-i partman-lvm/confirm boolean true
d-i partman-auto/choose_recipe select atomic
d-i partman-partitioning/confirm_write_new_label boolean true
d-i partman/choose_partition select finish
d-i partman/confirm boolean true
d-i partman/confirm_nooverwrite boolean true
d-i passwd/user-fullname string Ubuntu
d-i passwd/username string %(vm_username)s
d-i passwd/user-password password %(vm_password)s
d-i passwd/user-password-again password %(vm_password)s
d-i user-setup/allow-password-weak boolean true
d-i base-installer/kernel/override-image string linux-server
d-i apt-setup/restricted boolean true
d-i apt-setup/universe boolean true
tasksel	tasksel/force-tasks string server
d-i grub-installer/only_debian boolean true
d-i grub-installer/with_other_os boolean true
d-i finish-install/reboot_in_progress note
d-i debian-installer/exit/poweroff boolean true
d-i debian-installer/splash boolean false
d-i pkgsel/language-pack-patterns string
d-i pkgsel/install-language-support boolean false
d-i pkgsel/upgrade select none
d-i mirror/http/proxy string %(vm_aptproxy)s
d-i preseed/late_command string cp /latecommand.sh /target/root/; chroot /target chmod +x /root/latecommand.sh; chroot /target bash /root/latecommand.sh;%(vm_latecmd)s
''' % uvt_conf

        if 'vm_mirror_host' in uvt_conf:
            if 'vm_mirror_dir' in uvt_conf:
                mirror_dir = uvt_conf['vm_mirror_dir']
            else:
                mirror_dir = "/ubuntu"
            preseed_contents += '''d-i mirror/country string manual
d-i mirror/http/hostname string %s
d-i mirror/http/directory string %s
''' % (uvt_conf['vm_mirror_host'], mirror_dir)

    elif iso_type == "server":

        # lucid and over defaults to LVM
        if release == "hardy":
            partitioning = 'regular'
        else:
            partitioning = 'lvm'

        preseed_name = "preseed/vmtools.seed"
        preseed_contents = '''
d-i debian-installer/locale string en_US
d-i console-setup/ask_detect boolean false
d-i console-setup/layoutcode string us
console-setup console-setup/layoutcode string	us
console-setup console-setup/layout select	U.S. English
console-setup console-setup/variantcode select	U.S. English
console-setup console-setup/codeset select	. Combined - Latin; Slavic Cyrillic; Hebrew; basic Arabic
d-i netcfg/choose_interface select auto
d-i netcfg/get_hostname string vmtools
d-i netcfg/get_domain string defaultdomain
d-i netcfg/wireless_wep string
d-i hw-detect/load_firmware boolean true
d-i clock-setup/utc boolean true
d-i time/zone string %(vm_timezone)s
d-i partman-auto/method string %(partitioning)s
d-i partman-lvm/device_remove_lvm boolean true
d-i partman-md/device_remove_md boolean true
d-i partman-lvm/confirm boolean true
d-i partman-lvm/confirm_nooverwrite true
d-i partman-lvm/confirm_changes boolean true
d-i partman-partitioning/confirm_write_new_label boolean true
d-i partman/choose_partition select finish
d-i partman/confirm boolean true
d-i partman/confirm_nooverwrite boolean true
d-i partman-auto/choose_recipe select atomic
d-i passwd/user-fullname string Ubuntu
d-i passwd/username string %(vm_username)s
d-i passwd/user-password password %(vm_password)s
d-i passwd/user-password-again password %(vm_password)s
d-i user-setup/allow-password-weak boolean true
d-i base-installer/kernel/override-image string linux-server
d-i apt-setup/restricted boolean true
d-i apt-setup/universe boolean true
tasksel	tasksel/force-tasks string server
d-i grub-installer/only_debian boolean true
d-i grub-installer/with_other_os boolean true
d-i finish-install/reboot_in_progress note
d-i debian-installer/exit/poweroff boolean true
d-i debian-installer/splash boolean false
d-i pkgsel/language-pack-patterns string
d-i pkgsel/install-language-support boolean false
d-i pkgsel/upgrade select none
d-i mirror/http/proxy string %(vm_aptproxy)s
d-i preseed/late_command string cp /cdrom/preseed/latecommand.sh /target/root/; chroot /target chmod +x /root/latecommand.sh; chroot /target bash /root/latecommand.sh;%(vm_latecmd)s
''' % { 'vm_timezone': uvt_conf['vm_timezone'],
        'partitioning': partitioning,
        'vm_username': uvt_conf['vm_username'],
        'vm_password': uvt_conf['vm_password'],
        'vm_aptproxy': uvt_conf['vm_aptproxy'],
        'vm_latecmd': uvt_conf['vm_latecmd'] }

    elif iso_type == "alternate":
        preseed_name = "preseed/vmtools.seed"
        preseed_contents = '''
d-i debian-installer/locale string en_US
d-i console-setup/ask_detect boolean false
d-i console-setup/layoutcode string us
console-setup console-setup/layoutcode string	us
console-setup console-setup/layout select	U.S. English
console-setup console-setup/variantcode select	U.S. English
console-setup console-setup/codeset select	. Combined - Latin; Slavic Cyrillic; Hebrew; basic Arabic
d-i netcfg/choose_interface select auto
d-i netcfg/get_hostname string vmtools
d-i netcfg/get_domain string defaultdomain
d-i netcfg/wireless_wep string
d-i hw-detect/load_firmware boolean true
d-i clock-setup/utc boolean true
d-i time/zone string %(vm_timezone)s
d-i partman-auto/method string regular
d-i partman-lvm/device_remove_lvm boolean true
d-i partman-md/device_remove_md boolean true
d-i partman-lvm/confirm boolean true
d-i partman-auto/choose_recipe select atomic
d-i partman-partitioning/confirm_write_new_label boolean true
d-i partman/choose_partition select finish
d-i partman/confirm boolean true
d-i partman/confirm_nooverwrite boolean true
d-i passwd/user-fullname string Ubuntu
d-i passwd/username string %(vm_username)s
d-i passwd/user-password password %(vm_password)s
d-i passwd/user-password-again password %(vm_password)s
d-i user-setup/allow-password-weak boolean true
d-i apt-setup/restricted boolean true
d-i apt-setup/universe boolean true
tasksel	tasksel/force-tasks string ubuntu-desktop
d-i grub-installer/only_debian boolean true
d-i grub-installer/with_other_os boolean true
d-i finish-install/reboot_in_progress note
d-i debian-installer/exit/poweroff boolean true
d-i debian-installer/splash boolean false
d-i pkgsel/language-pack-patterns string
d-i pkgsel/install-language-support boolean false
d-i pkgsel/upgrade select none
d-i mirror/http/proxy string %(vm_aptproxy)s
d-i preseed/late_command string cp /cdrom/preseed/latecommand.sh /target/root/; chroot /target chmod +x /root/latecommand.sh; chroot /target bash /root/latecommand.sh;%(vm_latecmd)s
''' % uvt_conf

    else:
        preseed_name = "preseed/vmtools.seed"
        preseed_contents = '''
ubiquity	languagechooser/language-name	select	English
ubiquity	countrychooser/shortlist	select	US
ubiquity	time/zone	select	%(vm_timezone)s
ubiquity	debian-installer/locale	select	en_US.UTF-8
ubiquity	localechooser/supported-locales	multiselect	en_US.UTF-8
console-setup	console-setup/layoutcode	string	us
console-setup	console-setup/layout	select	U.S. English
console-setup	console-setup/variantcode	select	U.S. English
console-setup	console-setup/codeset	select	. Combined - Latin; Slavic Cyrillic; Hebrew; basic Arabic
ubiquity	partman-auto/init_automatically_partition	select	Guided - use entire disk
ubiquity	partman-auto/disk	string	%(disk_device)s
ubiquity	partman-auto/method	string	regular
ubiquity	partman-auto/choose_recipe	select	All files in one partition (recommended for new users)
ubiquity	partman/confirm_write_new_label	boolean	true
ubiquity	partman/choose_partition	select	Finish partitioning and write changes to disk
ubiquity	partman/confirm	boolean	true
ubiquity	partman/confirm_nooverwrite	boolean	true
ubiquity	passwd/user-fullname	string	Ubuntu
user-setup	passwd/user-fullname	string	Ubuntu
ubiquity	passwd/username	string	%(vm_username)s
user-setup	passwd/username	string	%(vm_username)s
ubiquity	passwd/user-password password %(vm_password)s
ubiquity	passwd/user-password-again password %(vm_password)s
user-setup	passwd/user-password password %(vm_password)s
user-setup	passwd/user-password-again password %(vm_password)s
d-i		netcfg/get_hostname	string	vmtools
d-i		netcfg/get_domain	string	defaultdomain
ubiquity	migration-assistant/partitions	multiselect
ubiquity	ubiquity/summary	note
ubiquity	ubiquity/reboot	string	true
d-i             mirror/http/proxy       string  %(vm_aptproxy)s
ubiquity	ubiquity/success_command	string	cp /cdrom/preseed/latecommand.sh /target/root/; chroot /target chmod +x /root/latecommand.sh; chroot /target bash /root/latecommand.sh;%(vm_latecmd)s
''' % { 'vm_timezone': uvt_conf['vm_timezone'],
        'disk_device': get_vm_disk_device(release),
        'vm_username': uvt_conf['vm_username'],
        'vm_password': uvt_conf['vm_password'],
        'vm_aptproxy': uvt_conf['vm_aptproxy'],
        'vm_latecmd': uvt_conf['vm_latecmd'] }

    open(os.path.join(temp_dir, preseed_name), 'w').write(preseed_contents)

def create_isolinux_cfg(temp_dir, iso_type, release):
    '''Creates an appropriate isolinux config'''

    initrd_name = get_initrd_name(release, iso_type)
    iso_kernel_name = get_iso_kernel_name(release, iso_type, temp_dir)

    if iso_type == "server" or iso_type == "alternate":
        isolinux_name = "isolinux/isolinux.cfg"
        isolinux_contents = '''
default vmtools
timeout 1
prompt 0
label vmtools
  menu label vmtools
  menu default
  kernel /install/%s
  append auto=true priority=critical console-setup/ask_detect=false file=/cdrom/preseed/vmtools.seed initrd=/install/%s quiet --
''' % (iso_kernel_name, initrd_name)
    elif iso_type == "mini":
        isolinux_name = "isolinux.cfg"
        isolinux_contents = '''
default vmtools
timeout 1
prompt 0
label vmtools
  menu label vmtools
  menu default
  kernel /%s
  append auto=true priority=critical locale=en_US console-setup/ask_detect=false initrd=/%s quiet --
''' % (iso_kernel_name, initrd_name)
    else:
        isolinux_name = "isolinux/isolinux.cfg"
        isolinux_contents = '''
default vmtools
timeout 1
prompt 0
label vmtools
  menu label vmtools
  menu default
  kernel /casper/%s
  append file=/cdrom/preseed/vmtools.seed boot=casper automatic-ubiquity noprompt keyboard-configuration/layoutcode=us initrd=/casper/%s quiet splash --
''' % (iso_kernel_name, initrd_name)

    open(os.path.join(temp_dir, isolinux_name), 'w').write(isolinux_contents)

def get_iso_kernel_name(release, iso_type, temp_dir):
    '''Figures out the appropriate iso kernel name'''

    if iso_type in ["server", "alternate"]:
        return "vmlinuz"

    if iso_type in ["mini"]:
        return "linux"

    if os.path.exists(os.path.join(temp_dir, "casper", "vmlinuz.efi")):
        return "vmlinuz.efi"
    else:
        return "vmlinuz"

def get_initrd_name(release, iso_type):
    '''Figures out the appropriate initrd name'''

    if iso_type in ["server", "alternate", "mini"]:
        return "initrd.gz"

    if release[0] < "lucid"[0]:
        return "initrd.gz"
    else:
        return "initrd.lz"

def create_latecommand_file(temp_dir, iso_type, release, vm_name, ovmf=False):
    '''Creates an appropriate latecommand file'''

    # Read user's ssh public key
    ssh_key = open(uvt_conf['vm_ssh_key']).read().strip()

    if iso_type == "mini":
        latecommand_file = os.path.join(temp_dir, "latecommand.sh")
    elif ovmf == True:
        latecommand_file = os.path.join(temp_dir,
                "%s_first-ovmf-boot.sh" % vm_name)
    else:
        latecommand_file = os.path.join(temp_dir, "preseed/latecommand.sh")

    print("Creating %s..." % os.path.basename(latecommand_file))

    post_script = "/postinstall.sh"
    xsession_script = "/etc/X11/Xsession.d/90vm-tools"
    gui_first_login_script = ".vm-tools-gui-first-login"

    late_contents = '''#!/bin/sh -e
unset DEBCONF_REDIR
unset DEBCONF_FRONTEND
unset DEBIAN_HAS_FRONTEND
unset DEBIAN_FRONTEND

mkdir /home/%(username)s/.ssh 2>/dev/null || true
mkdir /root/.ssh 2>/dev/null || true
echo "%(sshkey)s" > /root/.ssh/authorized_keys || true
echo "%(sshkey)s" > /home/%(username)s/.ssh/authorized_keys || true
chown -R %(username)s:%(username)s /home/%(username)s/.ssh || true
''' % { 'username': uvt_conf['vm_username'],
        'sshkey': ssh_key }

    # We used to specify the hostname in the preseed files, but that
    # didn't actually work because we cache the iso files once they get
    # preseeded, resulting in some VMs that get the wrong hostnames
    #
    # Actually, changing the hostname in the latecommand file doesn't
    # really work either if we're caching the iso files.
    #
    # Still need to find a solution, but iso caching is disabled by
    # default.
    #
    late_contents += '''
echo %(vmname)s > /etc/hostname || true
sed -i "s/vmtools/%(vmname)s/g" /etc/hosts || true
hostname -b -F /etc/hostname || true
''' % { 'vmname': vm_name }

    # We need to regenerate the snakeoil certs if we changed the hostname
    late_contents += '''
if [ -x "/usr/sbin/make-ssl-cert" ]; then
make-ssl-cert generate-default-snakeoil --force-overwrite || true
fi
'''

    # Not quite sure why this happens, but it prevents the hostname
    # from being sent to the dhcp server
    if (iso_type == "server" or iso_type == "mini") and release == "hardy":
        late_contents += '''
cp -a /etc/dhcp3/dhclient.conf.dpkg-dist /etc/dhcp3/dhclient.conf || true
'''

    # Disable update manager prompt for newer releases. If we're
    # installing an old release in a VM, it's probably not to
    # immediately upgrade it.
    late_contents += '''
sed -i "s/^Prompt=.*$/Prompt=never/" /etc/update-manager/release-upgrades || true
'''

    if iso_type == "desktop" or iso_type == "alternate":

        # Work around LP: #993379
        # On Precise, there is a race condition that makes Network Manager
        # start dnsmasq before the lo interface is up. Work around this for
        # now by modifying the upstart file.
        if release == "precise":
            late_contents += r'''
sed -i "s/started dbus/started dbus\n\t  and static-network-up/" /etc/init/network-manager.conf || true
        '''

        # Set default session to gnome-2d for natty
        if release == "natty":
            late_contents += '''
mkdir -p /var/cache/gdm/%(vm_username)s 2>/dev/null || true
echo "[Desktop]" > /var/cache/gdm/%(vm_username)s/dmrc || true
echo "Session=gnome-2d" >> /var/cache/gdm/%(vm_username)s/dmrc || true
chown %(vm_username)s:%(vm_username)s /var/cache/gdm/%(vm_username)s || true
''' % uvt_conf

        # Kill Amazon and Ubuntu One icons with fire since it makes the
        # launcher scroll in the VM, and that's painfully slow with LLVMpipe
        if release[0] >= "q":
            late_contents += '''
dpkg-statoverride --update --add root root 000 /usr/share/session-migration/scripts/install-default-webapps-in-launcher.py || true
''' % uvt_conf

        # Turn off the Unity keyboard shortcuts panel on first run in Trusty+
        # (See LP: #1283619 for feature details)
        if release[0] >= "t":
            late_contents += r'''
mkdir /home/%(username)s/.cache 2>/dev/null || true
chmod 700 /home/%(username)s/.cache 2>/dev/null || true
chown %(username)s:%(username)s /home/%(username)s/.cache 2>/dev/null || true
mkdir /home/%(username)s/.cache/unity 2>/dev/null || true
chmod 700 /home/%(username)s/.cache/unity 2>/dev/null || true
touch /home/%(username)s/.cache/unity/first_run.stamp 2>/dev/null || true
chown -R %(username)s:%(username)s /home/%(username)s/.cache/unity 2>/dev/null || true
''' % { 'username': uvt_conf['vm_username'] }

        # Since we're using vmvga on quantal+, set the screen resolution to
        # something sane, like the 1024x768 we would have gotten with cirrus
        if release[0] >= "q":
            late_contents += r'''
mkdir /home/%(username)s/.config 2>/dev/null || true
chmod 700 /home/%(username)s/.config 2>/dev/null || true
chown %(username)s:%(username)s /home/%(username)s/.config 2>/dev/null || true
echo "<monitors version=\"1\">" >> /home/%(username)s/.config/monitors.xml || true
echo "  <configuration>" >> /home/%(username)s/.config/monitors.xml || true
echo "      <clone>no</clone>" >> /home/%(username)s/.config/monitors.xml || true
echo "      <output name=\"default\">" >> /home/%(username)s/.config/monitors.xml || true
echo "          <vendor>???</vendor>" >> /home/%(username)s/.config/monitors.xml || true
echo "          <product>0x0000</product>" >> /home/%(username)s/.config/monitors.xml || true
echo "          <serial>0x00000000</serial>" >> /home/%(username)s/.config/monitors.xml || true
echo "          <width>1024</width>" >> /home/%(username)s/.config/monitors.xml || true
echo "          <height>768</height>" >> /home/%(username)s/.config/monitors.xml || true
echo "          <rate>0</rate>" >> /home/%(username)s/.config/monitors.xml || true
echo "          <x>0</x>" >> /home/%(username)s/.config/monitors.xml || true
echo "          <y>0</y>" >> /home/%(username)s/.config/monitors.xml || true
echo "          <rotation>normal</rotation>" >> /home/%(username)s/.config/monitors.xml || true
echo "          <reflect_x>no</reflect_x>" >> /home/%(username)s/.config/monitors.xml || true
echo "          <reflect_y>no</reflect_y>" >> /home/%(username)s/.config/monitors.xml || true
echo "          <primary>yes</primary>" >> /home/%(username)s/.config/monitors.xml || true
echo "      </output>" >> /home/%(username)s/.config/monitors.xml || true
echo "  </configuration>" >> /home/%(username)s/.config/monitors.xml || true
echo "</monitors>" >> /home/%(username)s/.config/monitors.xml || true
''' % { 'username': uvt_conf['vm_username'] }

        # Also set lightdm resolution
        if release[0] == "q" or release[0] == "r":
            late_contents += r'''
echo "display-setup-script=xrandr --output default --mode 1024x768" >> /etc/lightdm/lightdm.conf || true
'''

        # Saucy starts gnome-settings-daemon in the greeter, so the xrandr command gets reverted
        # We need to set up a monitors.xml file for the lightdm user.
        if release[0] >= "s":
            late_contents += r'''
mkdir /var/lib/lightdm/.config 2>/dev/null || true
chmod 700 /var/lib/lightdm/.config 2>/dev/null || true
chown lightdm:lightdm /var/lib/lightdm/.config 2>/dev/null || true
cp /home/%(username)s/.config/monitors.xml /var/lib/lightdm/.config || true
chown lightdm:lightdm /var/lib/lightdm/.config/monitors.xml 2>/dev/null || true
''' % { 'username': uvt_conf['vm_username'] }

        # Set hardy's hostname, Xorg resolution and Xorg keyboard
        if release == "hardy":
            late_contents += r'''
echo %(vmname)s > /etc/hostname || true
sed -i "s/ubuntu-desktop/%(vmname)s.defaultdomain\t%(vmname)s/" /etc/hosts || true
cp -a /etc/X11/xorg.conf /etc/X11/xorg.conf.ori || true
sed -i "s/\"1280x800\" \"1152x768\" //" /etc/X11/xorg.conf || true
''' % { 'vmname': vm_name }

            if uvt_conf['vm_setkeyboard'] == "true":
                late_contents += r'''
sed -i "s/\"XkbModel\"\t\".*\"/\"XkbModel\"\t\"%(vm_xkbmodel)s\"/" /etc/X11/xorg.conf || true
sed -i "s/\"XkbLayout\"\t\".*\"/\"XkbLayout\"\t\"%(vm_xkblayout)s\"\n\tOption\t\t\"XkbVariant\"\t\"%(vm_xkbvariant)s\"\n\tOption\t\t\"XkbOptions\"\t\"%(vm_xkboptions)s\"/" /etc/X11/xorg.conf || true
''' % uvt_conf

        late_contents += r'''
# create %(xsess)s script, which simply executes %(first)s
# %(first)s will do gui setup and remove itself
mkdir -p $(dirname %(xsess)s)
echo "if [ -r \"\$HOME/%(first)s\" ]; then . \"\$HOME/%(first)s\"; fi" > %(xsess)s
echo "# This script is sourced by %(xsess)s" > /home/%(username)s/%(first)s
echo "if [ -x \"/usr/bin/gconftool-2\" ]; then" >> /home/%(username)s/%(first)s
echo "    gconftool-2 --set --type=string /apps/gnome-screensaver/mode blank-only" >> /home/%(username)s/%(first)s
echo "    gconftool-2 --set --type=bool /apps/gnome-screensaver/idle_activation_enabled false" >> /home/%(username)s/%(first)s
echo "fi" >> /home/%(username)s/%(first)s
''' % { 'xsess': xsession_script,
        'first': gui_first_login_script,
        'username': uvt_conf['vm_username'] }

        # Turn off the screensaver and screen blanking with gsettings
        if release[0] >= "o":
            late_contents += r'''
echo "if [ -x \"/usr/bin/gsettings\" ]; then" >> /home/%(username)s/%(first)s
echo "    gsettings set org.gnome.desktop.session idle-delay 0" >> /home/%(username)s/%(first)s
echo "    gsettings set org.gnome.settings-daemon.plugins.power sleep-display-ac 0" >> /home/%(username)s/%(first)s
echo "    gsettings set org.gnome.settings-daemon.plugins.power sleep-display-battery 0" >> /home/%(username)s/%(first)s
echo "fi" >> /home/%(username)s/%(first)s
''' % { 'first': gui_first_login_script,
        'username': uvt_conf['vm_username'] }

        # Add a terminal icon to the launcher
        if release[0] >= "n" and \
           uvt_conf['vm_terminal'] == "true":
            late_contents += r'''
echo "if [ -x \"/usr/bin/gsettings\" ]; then" >> /home/%(username)s/%(first)s
echo "    launchers=\$(gsettings get com.canonical.Unity.Launcher favorites | sed \"s/]/, 'gnome-terminal.desktop']/\")" >> /home/%(username)s/%(first)s
echo "    gsettings set com.canonical.Unity.Launcher favorites \"\$launchers\"" >> /home/%(username)s/%(first)s
echo "fi" >> /home/%(username)s/%(first)s
''' % { 'first': gui_first_login_script,
        'username': uvt_conf['vm_username'] }

        # Kill Ubuntu One and Amazon icons in launcher to make some room
        if release[0] >= "q":
            late_contents += r'''
echo "if [ -x \"/usr/bin/gsettings\" ]; then" >> /home/%(username)s/%(first)s
echo "    launchers=\$(gsettings get com.canonical.Unity.Launcher favorites | sed -e \"s/'application:\/\/UbuntuOneMusiconeubuntucom.desktop', //\" -e \"s/'application:\/\/ubuntu-amazon-default.desktop', //\")" >> /home/%(username)s/%(first)s
echo "    gsettings set com.canonical.Unity.Launcher favorites \"\$launchers\"" >> /home/%(username)s/%(first)s
echo "fi" >> /home/%(username)s/%(first)s
''' % { 'first': gui_first_login_script,
        'username': uvt_conf['vm_username'] }

        # Disable compiz animations and fades in the vm
        if release[0] >= "q":
            late_contents += r'''
echo "if [ -x \"/usr/bin/gsettings\" ]; then" >> /home/%(username)s/%(first)s
echo "    plugins=\$(gsettings get org.compiz.core:/org/compiz/profiles/unity/plugins/core/ active-plugins | sed -e \"s/, 'fade'//\" -e \"s/, 'animation'//\")" >> /home/%(username)s/%(first)s
echo "    gsettings set org.compiz.core:/org/compiz/profiles/unity/plugins/core/ active-plugins \"\$plugins\"" >> /home/%(username)s/%(first)s
echo "    gsettings set org.compiz.unityshell:/org/compiz/profiles/unity/plugins/unityshell/ dash-blur-experimental 0" >> /home/%(username)s/%(first)s
echo "    gsettings set org.compiz.unityshell:/org/compiz/profiles/unity/plugins/unityshell/ launch-animation 2" >> /home/%(username)s/%(first)s
echo "    gsettings set org.compiz.unityshell:/org/compiz/profiles/unity/plugins/unityshell/ launcher-opacity 1.0" >> /home/%(username)s/%(first)s
echo "    gsettings set org.compiz.unityshell:/org/compiz/profiles/unity/plugins/unityshell/ menus-discovery-fadein 0" >> /home/%(username)s/%(first)s
echo "    gsettings set org.compiz.unityshell:/org/compiz/profiles/unity/plugins/unityshell/ menus-discovery-fadeout 0" >> /home/%(username)s/%(first)s
echo "    gsettings set org.compiz.unityshell:/org/compiz/profiles/unity/plugins/unityshell/ menus-fadein 0" >> /home/%(username)s/%(first)s
echo "    gsettings set org.compiz.unityshell:/org/compiz/profiles/unity/plugins/unityshell/ menus-fadeout 0" >> /home/%(username)s/%(first)s
echo "fi" >> /home/%(username)s/%(first)s
sed -i "s/animation;//" /etc/compizconfig/unity.ini || true
sed -i "s/fade;//" /etc/compizconfig/unity.ini || true
''' % { 'first': gui_first_login_script,
        'username': uvt_conf['vm_username'] }

        late_contents += '''
echo "    rm -f /home/%(username)s/%(first)s" >> /home/%(username)s/%(first)s
chmod 644 /home/%(username)s/%(first)s
''' % { 'first': gui_first_login_script,
        'username': uvt_conf['vm_username'] }

    late_contents += r'''
locale-gen en_US en_US.UTF-8
if [ "%(vm_locale)s" != "en_US.UTF-8" ]; then
    locale-gen %(vm_locale)s %(vm_locale_iso)s
fi
dpkg-reconfigure locales

echo "#!/bin/sh -e" > %(post_script)s
echo "" >> %(post_script)s
echo "export PATH=/sbin:/bin:/usr/sbin:/usr/bin" >> %(post_script)s
echo "export DEBIAN_FRONTEND=noninteractive" >> %(post_script)s
echo "export DEBIAN_PRIORITY=critical" >> %(post_script)s
echo "apt-get -f install" >> %(post_script)s
echo "# Translations may mis-download at this point (Debian #657560, LP: #346386) so" >> %(post_script)s
echo "# ignore first 'apt-get update' but still fail on 'apt-get dist-upgrade'" >> %(post_script)s
echo "apt-get update || true" >> %(post_script)s
echo "apt-get -y --force-yes dist-upgrade" >> %(post_script)s
echo "# We should have the updated apt and bzip2 packages to deal with mis-downloaded" >> %(post_script)s
echo "# translations now. So run 'apt-get update' (now failing on legitimate errors) and" >> %(post_script)s
echo "# 'apt-get dist-upgrade' again to ensure we are in a sane state." >> %(post_script)s
echo "apt-get update" >> %(post_script)s
echo "apt-get -y --force-yes dist-upgrade" >> %(post_script)s
echo "apt-get -y --force-yes install openssh-server" >> %(post_script)s
echo "apt-get -y --force-yes install %(vm_extra_packages)s" >> %(post_script)s
''' % { 'vm_locale': uvt_conf['vm_locale'],
        'vm_locale_iso': uvt_conf['vm_locale_iso'],
        'vm_extra_packages': uvt_conf['vm_extra_packages'],
        'post_script': post_script }

    # Since unity in quantal uses llvmpipe, it's pretty slow. Also install
    # gnome-panel.
    if release[0] >= "q" and (iso_type == "desktop" or iso_type == "alternate"):
        late_contents += r'''echo "apt-get -y --force-yes install gnome-panel" >> %(post_script)s
''' % { 'post_script': post_script }

    late_contents += r'''echo "apt-get clean" >> %(post_script)s
echo "dpkg-reconfigure console-setup" >> %(post_script)s
echo "rm -f %(post_script)s.run /etc/cron.daily/find.notslocate /etc/cron.daily/slocate /etc/cron.daily/mlocate /etc/network/if-up.d/postinstall 2>/dev/null" >> %(post_script)s
echo "rm -f /root/latecommand.sh 2>/dev/null" >> %(post_script)s
echo "dpkg --get-selections > /var/log/vm-new.packages" >> %(post_script)s
echo "rm -f /var/crash/* 2>/dev/null" >> %(post_script)s
echo "echo Post Installation Tasks Finished" >> %(post_script)s

chmod 755 %(post_script)s
echo "#!/bin/sh -e" > /etc/network/if-up.d/postinstall
echo "if [ \"\$IFACE\" != "eth0" ] || [ \"\$MODE\" != \"start\" ]; then exit 0; fi" >> /etc/network/if-up.d/postinstall
echo "if [ -x %(post_script)s ] ; then" >> /etc/network/if-up.d/postinstall
echo "    mv %(post_script)s %(post_script)s.run" >> /etc/network/if-up.d/postinstall
echo "    touch /var/log/vm-new.log" >> /etc/network/if-up.d/postinstall
echo "    chmod 600 /var/log/vm-new.log" >> /etc/network/if-up.d/postinstall
echo "    export HOME=/root" >> /etc/network/if-up.d/postinstall
echo "    %(post_script)s.run >> /var/log/vm-new.log 2>&1 &" >> /etc/network/if-up.d/postinstall
echo "fi" >> /etc/network/if-up.d/postinstall
chmod 755 /etc/network/if-up.d/postinstall

echo "LANG=\"%(vm_locale)s\"" > /etc/default/locale
''' % { 'vm_locale': uvt_conf['vm_locale'],
        'vm_locale_iso': uvt_conf['vm_locale_iso'],
        'vm_extra_packages': uvt_conf['vm_extra_packages'],
        'post_script': post_script }

    if uvt_conf['vm_setkeyboard'] == "true":
        late_contents += r'''
sed -i "s/XKBMODEL=\".*\"/XKBMODEL=\"%(model)s\"/" %(keyboard)s
sed -i "s/XKBLAYOUT=\".*\"/XKBLAYOUT=\"%(layout)s\"/" %(keyboard)s
sed -i "s/XKBVARIANT=\".*\"/XKBVARIANT=\"%(variant)s\"/" %(keyboard)s
sed -i "s/XKBOPTIONS=\".*\"/XKBOPTIONS=\"%(options)s\"/" %(keyboard)s
''' % { 'model': uvt_conf['vm_xkbmodel'],
        'layout': uvt_conf['vm_xkblayout'],
        'variant': uvt_conf['vm_xkbvariant'],
        'options': uvt_conf['vm_xkboptions'],
        'keyboard': get_vm_keyboard(release) }

    # add preferred mirror to /etc/apt/sources.list
    tmp_m="http://archive.ubuntu.com/ubuntu"
    if 'vm_mirror' in uvt_conf:
        tmp_m = uvt_conf['vm_mirror']

    tmp_s="http://security.ubuntu.com/ubuntu"
    if 'vm_security_mirror' in uvt_conf:
        tmp_s = uvt_conf['vm_security_mirror']

    # use preferred source mirror if it exists
    tmp_src = tmp_m
    if 'vm_src_mirror' in uvt_conf:
        tmp_src=uvt_conf['vm_src_mirror']

    tmp_ssrc = tmp_s
    if 'vm_src_mirror' in uvt_conf:
        tmp_ssrc=uvt_conf['vm_src_mirror']

    late_contents += '''
mv /etc/apt/sources.list /etc/apt/sources.list.ori
echo deb %(tmp_m)s %(release)s main restricted universe multiverse > /etc/apt/sources.list
echo deb %(tmp_m)s %(release)s-updates main restricted universe multiverse >> /etc/apt/sources.list
echo \#deb %(tmp_m)s %(release)s-proposed main restricted universe multiverse >> /etc/apt/sources.list
echo deb %(tmp_s)s %(release)s-security main restricted universe multiverse >> /etc/apt/sources.list
echo deb-src %(tmp_src)s %(release)s main restricted universe multiverse >> /etc/apt/sources.list
echo deb-src %(tmp_src)s %(release)s-updates main restricted universe multiverse >> /etc/apt/sources.list
echo \#deb-src %(tmp_src)s %(release)s-proposed main restricted universe multiverse >> /etc/apt/sources.list
echo deb-src %(tmp_ssrc)s %(release)s-security main restricted universe multiverse >> /etc/apt/sources.list
''' % { 'tmp_m': tmp_m,
        'tmp_s': tmp_s,
        'tmp_src': tmp_src,
        'tmp_ssrc': tmp_ssrc,
        'release': release }

    open(latecommand_file, 'w').write(late_contents)
    if ovmf == True:
        f = os.path.join("/tmp", os.path.basename(latecommand_file))
        if os.path.exists(f):
            os.unlink(f)
        os.rename(latecommand_file, f)
    else:
        runcmd(['chmod', '755', latecommand_file])

def locate_release_iso(release, release_num, arch, iso_type, force):
    '''Attempts to locate iso'''

    # Try and find the latest point release
    # FIXME: this should list the directory instead of guessing
    for r in [".6", ".5", ".4", ".3", ".2", ".1", ""]:
        release_iso = "ubuntu-%s%s-%s-%s.iso" % (release_num, r, iso_type, arch)
        #print "looking for %s" % release_iso
        iso_path = os.path.join(uvt_conf['vm_dir_iso'],release_iso)
        if os.path.exists(iso_path):
            # iso image is there, but we don't have read access
            # this sometimes happens when libvirt mucks with the
            # permissions
            if os.access(iso_path, os.R_OK) == False:
                print("Found iso at '%s',\nbut you don't have read permission!\n" % iso_path, file=sys.stderr)
                print("Please fix permissions on iso file and try again! Aborting.\n", file=sys.stderr)
                sys.exit(1)
            return release_iso

    # Fall back to daily/alpha/beta style iso naming
    release_iso = "%s-%s-%s.iso" % (release, iso_type, arch)
    #print "looking for %s" % release_iso
    iso_path = os.path.join(uvt_conf['vm_dir_iso'],release_iso)
    if os.path.exists(iso_path):
        return release_iso

    # Looks like we couldn't find one
    print("Could not find %s iso for %s %s" % (iso_type, arch, release))
    if not force:
        if not confirm("Would you like to download it?"):
            print("Aborting!\n", file=sys.stderr)
            sys.exit(1)

    release_iso = download_release_iso(release, release_num, arch, iso_type)
    return release_iso


def download_release_iso(release, release_num, arch, iso_type):
    '''Attempts to download iso'''

    print("Fetching release iso...")
    latest_release = find_latest_release(release_num, iso_type)
    release_iso="ubuntu-%s-%s-%s.iso" % (latest_release, iso_type, arch)

    if iso_type == 'mini':
        url="http://archive.ubuntu.com/ubuntu/dists/%s/main/installer-%s/current/images/netboot/mini.iso" % (release, arch)
    else:
        url="http://releases.ubuntu.com/%s/%s" % (latest_release, release_iso)

    download_file(url, release_iso)

    return release_iso

def download_file(url, filename):
    '''Downloads a file from the web with a progress indicator'''

    from urllib.request import urlopen
    try:
        u = urlopen(url)
        f = open(os.path.join(uvt_conf['vm_dir_iso'],filename), 'wb')
        file_size = int(u.getheader("Content-Length"))
        print("Downloading %s (%s bytes)" % (filename, file_size))

        file_size_dl = 0
        block_sz = 8 * 1024
        while True:
            dl_buffer = u.read(block_sz)
            if not dl_buffer:
                break

            file_size_dl += len(dl_buffer)
            f.write(dl_buffer)
            status = r"%10d  [%3.2f%%]" % (file_size_dl, file_size_dl * 100. / file_size)
            status = status + chr(8)*(len(status)+1)
            print(status, end=' ')

        f.close()
    except:
        print("Error downloading file! Please download manually.")
        sys.exit(1)

def find_latest_release(release_num, iso_type):
    '''Searches web for latest point release'''

    from urllib.request import urlopen
    print("Searching web for latest point release...")
    if iso_type == 'mini':
        # FIXME: we should add logic to search for the latest mini release
        # here. For now, just return the main one.
        return release_num

    # Try and find the latest point release
    try:
        response = urlopen('http://releases.ubuntu.com')
        html = response.read().decode()
    except:
        print("Could not reach web server! Please download manually.")
        sys.exit(1)

    for r in [".6", ".5", ".4", ".3", ".2", ".1", ""]:
        attempt = release_num + r
        search = '<a href="%s/">' % attempt
        if search in html:
            print("Found release %s" % attempt)
            return attempt

    print("Could not find release to download! Please download manually.")
    sys.exit(1)

def check_vm_exists(vm_name, fail=False):
    '''Checks if the vm already exists'''

    if check_dumpxml_exists(vm_name):
        if fail == True:
            print("Virtual machine '%s' already exists. Aborting." % vm_name, file=sys.stderr)
            sys.exit(1)
        return True

    vm_file = os.path.join(uvt_conf["vm_path"],vm_name + '.' + uvt_conf["vm_disk_format"])
    if os.path.exists(vm_file):
        if fail == True:
            print("'%s' already exists. Aborting." % vm_file, file=sys.stderr)
            sys.exit(1)
        return True

    return False

def check_dumpxml_exists(vm_name, tries=1):
    '''uses virsh dumpxml to see if a vm exists'''

    for c in range(tries):
        rc, out = runcmd(["virsh", "--connect", uvt_conf["vm_connect"],
                          "dumpxml", vm_name])
        if rc == 0:
            return True
        time.sleep(1)

    return False

def check_release(release):
    '''Makes sure release is valid'''

    # Only checks validity, not if it's still supported
    if BetterUbuntuDistroInfo().valid(release):
        return
    else:
        print("ERROR: Unsupported release '%s'!\n" % release, file=sys.stderr)
        sys.exit(1)

def get_numerical_release(release):
    '''Converts release name to version'''

    result = BetterUbuntuDistroInfo().get_release(release)

    if result is not None:
        return result
    else:
        print("ERROR: Can't lookup numerical release for '%s'!\n" % release, file=sys.stderr)
        sys.exit(1)

def confirm(prompt=None, resp=False):
    """prompts for yes or no response from the user. Returns True for yes and
    False for no.

    'resp' should be set to the default value assumed by the caller when
    user simply types ENTER.

    >>> confirm(prompt='Create Directory?', resp=True)
    Create Directory? [y]|n:
    True
    >>> confirm(prompt='Create Directory?', resp=False)
    Create Directory? [n]|y:
    False
    >>> confirm(prompt='Create Directory?', resp=False)
    Create Directory? [n]|y: y
    True

    """

    if prompt is None:
        prompt = 'Confirm'

    if resp:
        prompt = '%s [%s]|%s: ' % (prompt, 'Y', 'n')
    else:
        prompt = '%s [%s]|%s: ' % (prompt, 'N', 'y')

    while True:
        if sys.version_info[0] < 3:
            ans = raw_input(prompt)
        else:
            ans = input(prompt)
        if not ans:
            return resp
        if ans not in ['y', 'Y', 'n', 'N']:
            print('please enter y or n.')
            continue
        if ans == 'y' or ans == 'Y':
            return True
        if ans == 'n' or ans == 'N':
            return False

def runcmd(command, input = None, stderr = subprocess.STDOUT,
           stdout = subprocess.PIPE, stdin = subprocess.PIPE,
           shell = False):
    '''Try to execute given command (array) and return its stdout, or return
    a textual error if it failed.'''

    try:
       sp = subprocess.Popen(command, stdin=stdin, stdout=stdout, stderr=stderr, close_fds=True, shell=shell)
    except OSError as e:
        return [127, str(e)]

    out = sp.communicate(input)[0]
    if out:
        out = out.decode()
    return [sp.returncode,out]

def create_uvt_config():
    '''Creates an initial config'''

    conf_file = os.path.join(os.path.expanduser("~"), config_file)
    if os.path.exists(conf_file):
        print("Already have a config file at '%s'!" % config_file)
        if not confirm("Would you like to overwrite it (data will be lost)?"):
            print("Aborting.")
            sys.exit(1)
    else:
        if not confirm("Would you like to create an initial config file?"):
            print("Aborting.")
            sys.exit(1)

    initial_iso_dir = os.path.expanduser("~/iso")
    initial_cache_dir = os.path.expanduser("~/iso/cache")
    initial_machines_dir = os.path.expanduser("~/machines")

    initial_file = \
'''#
# Configuration file for uvt tool
#
#

# vm_dir_iso: sets the location where .iso images are to be found
#             and downloaded to. This defaults to $HOME/iso if unset.
#vm_dir_iso="%s"

#
# vm_dir_iso_cache: sets the location where preseeded iso images will be
#                   cached. This defaults to $HOME/iso/cache if unset.
#vm_dir_iso_cache="%s"

#
# vm_path: sets the location where the libvirt virtual machines will be
#          stored. This defaults to $HOME/machines if unset
#vm_path="%s"

#
# vm_locale: This sets the default locale in the virtual machines. Default
#            setting is inherited from current user.
#vm_locale="en_US.UTF-8"

#
# vm_setkeyboard: If this is set to "true", keyboard configuration will
#                 be setup in the virtual machines using the four following
#                 options. Default is "true", with settings inherited from
#                 host.
#vm_setkeyboard="false"
#vm_xkbmodel="pc105"
#vm_xkblayout="ca"
#vm_xkbvariant=""
#vm_xkboptions="lv3:ralt_switch"

#
# vm_image_size: Default size for virtual machine images (hard disk size).
#                Default is 8GB
#vm_image_size="8"

#
# vm_memory: Default size for virtual machine RAM. A minimum of 384MB is
#            needed for desktops, 256MB for servers. Default is 512MB.
#vm_memory="512"

#
# vm_username: Username of the initial user that is set up in the virtual
#              machines. Default is current user.
#vm_username="awesomedude"

#
# vm_password: Password of the initial user that is set up in the virtual
#              machines. Default is hardcoded to "ubuntu".
#vm_password="ubuntu"

#
# vm_timezone: Timezone that is set up in the virtual machines. Default
#              is inherited from the host.
#vm_timezone="UTC"

#
# vm_ssh_key: SSH public key to copy over to the virtual machines. This
#             sets up SSH public key authentication to the VMs by default.
#             Default is $HOME/.ssh/id_rsa.pub
#vm_ssh_key="$HOME/.ssh/id_rsa.pub"

#
# vm_aptproxy: If set, this sets up an apt proxy in the virtual machine.
#              No default setting. Can be used to set up apt-cacher-ng, for
#              example.
#vm_aptproxy=""

#
# vm_mirror: This is used as the default mirror in the sources.list file.
#            Default is main archive.
#vm_mirror="http://archive.ubuntu.com/ubuntu"

#
# vm_security_mirror: This is used as the default security mirror in the
#                     sources.list file. Default is main security archive.
#vm_security_mirror="http://security.ubuntu.com/ubuntu"

#
# vm_src_mirror: This is used to override the archive used for source
#                packages. By default, source packages are obtained from
#                the archives set by vm_mirror and vm_security_mirror.
#vm_src_mirror="http://myownsources/ubuntu"

#
# vm_mirror_host: FIXME: used with mini iso
#vm_mirror_host="archive.ubuntu.com"

#
# vm_mirror_dir: FIXME: used with mini iso
#vm_mirror_dir="/ubuntu"

#
# vm_repo_url: This is used by the 'uvt repo' command to add or remove
#              a local software repository to a VM. Defaults to a repo
#              on the default libvirt host address:
#              http://192.168.122.1/debs/testing
#
#vm_repo_url="http://192.168.122.1/debs/testing"

#
# vm_terminal: When set to true, this adds a terminal shortcut to the
#              Unity launcher.
#vm_terminal="true"

#
# vm_latecmd: This allows specifying an additional latecommand that is
#             executed by the installer.
#vm_latecmd=""

#
# vm_extra_packages: A list of extra packages can be specified here to be
#                    installed in the VM, for example, "screen". Empty by
#                    default.
#vm_extra_packages=""

#
# vm_disk_format: the disk format to use when creating new VMs. Defaults
#                 to qcow2
#vm_disk_format="qcow2"
''' % (initial_iso_dir, initial_cache_dir, initial_machines_dir)

    try:
        open(conf_file, 'w').write(initial_file)
    except Exception:
        print("Could not create initial config file. Aborting.", file=sys.stderr)
        sys.exit(1)

    print("")
    print("Initial config file creation complete.")
    print("You can adjust the config file in %s" % conf_file)
    print("for vm settings such as disk size, ram size, and language.\n\n")

def check_required_tools():
    '''Checks if the required tools are installed'''

    # binary names, and package names
    tools = { 'virt-install' : 'virtinst',
              'xorriso'      : 'xorriso',
              'mkisofs'      : 'genisoimage',
              'virsh'        : 'libvirt-bin',
              'virt-clone'   : 'virtinst',
              'virt-viewer'  : 'virt-viewer',
              'kvm-ok'       : 'cpu-checker',
              'gzip'         : 'gzip',
              'cpio'         : 'cpio',
              'kvm'          : 'qemu-kvm' }

    missing_tools = []
    for tool in tools:
        ret, out = runcmd(['which', tool])
        if ret != 0:
            missing_tools += [tool]

    if missing_tools != []:
        print("Some tools are required for uvt to function correctly, but\n" +
              "could not be found:\n\n%s" % " ".join(missing_tools), file=sys.stderr)

        print("\nPlease install the missing tools with the following command\n" +
              "and try again:\n", file=sys.stderr)

        package_list = set()
        for tool in missing_tools:
            package_list.add(tools[tool])

        print("sudo apt-get install %s" % " ".join(package_list), file=sys.stderr)
        sys.exit(1)

    # Make sure user is in libvirtd group
    ret, out = runcmd(['groups'])

    if ret != 0 or 'libvirtd' not in out.strip().split(" "):
        print("\nYour user must be in the 'libvirtd' group.\n" +
              "Please add your user to the group, and log back in.\n", file=sys.stderr)
        sys.exit(1)

def check_ssh_key():
    '''Checks if the user has an ssh key'''

    if not os.path.exists(uvt_conf['vm_ssh_key']):
        print("\nYour user must have an ssh key.\n" +
              "Please create one now with 'ssh-keygen -t rsa' and try again.\n", file=sys.stderr)
        sys.exit(1)

def parse_config_file(conf_file):
    '''Parses a config file'''

    # We used to use ConfigObj, but it's not available for Python 3
    # and ConfigParser is meh.

    results = {}

    if not os.path.exists(conf_file):
        return results

    for line in open(conf_file).readlines():
        # Blank line
        if line.strip() == "":
            continue
        # Comment
        if line.strip()[0] == "#":
            continue
        # Comment in middle of line
        if "#" in line:
            line = line.split('#')[0]
        # Looks good:
        if "=" in line:
            key = line.split('=')[0].strip()
            value = line.split('=')[1].strip()
            # Get rid of quotes around value
            for quote in ['"', "'"]:
                if value != "" and value[0] == quote:
                    value = value[1:]
                if value != "" and value[-1] == quote:
                    value = value[:-1]
            results[key] = value

    return results

def load_uvt_config():
    import hashlib
    import pwd
    '''Read in config file'''

    conf_file = os.path.join(os.path.expanduser("~"), config_file)
    config = parse_config_file(conf_file)

    keyboard = parse_config_file('/etc/default/keyboard')

    # Replace $HOME variable to be compatible with old shell vm-new
    for k in config:
        if "$HOME" in config[k]:
            config[k] = re.sub("\$HOME", os.path.expanduser("~"), config[k])

    # Set default directories
    if config.get('vm_dir_iso', "") == "":
        config['vm_dir_iso'] = os.path.expanduser("~/iso")
    if config.get('vm_dir_iso_cache', "") == "":
        config['vm_dir_iso_cache'] = os.path.expanduser("~/iso/cache")
    if config.get('vm_path', "") == "":
        config['vm_path'] = os.path.expanduser("~/machines")

    # Set a default locale and keyboard
    if config.get('vm_locale', "") == "":
        config['vm_locale'] = os.environ.get("LANG", "en_US.UTF-8")

    if "utf-8" in config['vm_locale'].lower():
        config['vm_locale_iso'] = config['vm_locale'].split(".")[0]
    else:
        config['vm_locale_iso'] = ""

    if config.get('vm_setkeyboard', "") == "":
        config['vm_setkeyboard'] = "true"

    if not 'vm_xkbmodel' in config:
        config['vm_xkbmodel'] = keyboard.get('XKBMODEL', "")
    if not 'vm_xkblayout' in config:
        config['vm_xkblayout'] = keyboard.get('XKBLAYOUT', "")
    if not 'vm_xkbvariant' in config:
        config['vm_xkbvariant'] = keyboard.get('XKBVARIANT', "")
    if not 'vm_xkboptions' in config:
        config['vm_xkboptions'] = keyboard.get('XKBOPTIONS', "")

    # Set a default image size to 8GB and memory to 512MB
    if config.get('vm_image_size', "") == "":
        config['vm_image_size'] = "8"
    if config.get('vm_memory', "") == "":
        config['vm_memory'] = "512"

    # Set a default username and password
    if config.get('vm_username', "") == "":
        config['vm_username'] = pwd.getpwuid(os.getuid())[0]

    if not 'vm_password' in config:
        # Yes, this is a hardcoded password! ZOMG, everybody panic!
        config['vm_password'] = "ubuntu"

    # Set a default timezone
    if not 'vm_timezone' in config:
        config['vm_timezone'] = open('/etc/timezone').readline().strip()
    if config['vm_timezone'] == "":
        config['vm_timezone'] = "UTC"

    # Set default ssh key file
    if config.get('vm_ssh_key', "") == "":
        config['vm_ssh_key'] = os.path.expanduser("~/.ssh/id_rsa.pub")

    # Other stuff
    if not 'vm_aptproxy' in config:
        config['vm_aptproxy'] = ""
    if not 'vm_latecmd' in config:
        config['vm_latecmd'] = ""
    if config.get('vm_host_use_avahi', "") == "":
        config['vm_host_use_avahi'] = "yes"
    if config.get('vm_connect', "") == "":
        config['vm_connect'] = "qemu:///system"
    if not 'vm_extra_packages' in config:
        config['vm_extra_packages'] = ""
    if config.get('vm_repo_url', "") == "":
        config['vm_repo_url'] = "http://192.168.122.1/debs/testing"
    if not 'vm_terminal' in config:
        config['vm_terminal'] = "false"

    # Create storage directories
    missing = False
    for d in {'vm_dir_iso', 'vm_dir_iso_cache', 'vm_path'}:
        if not os.path.exists(config[d]):
            missing = True

    # on disk format
    if config.get('vm_disk_format', '') == "":
        config['vm_disk_format'] = "qcow2"

    # If we don't have a config file, and we're missing some
    # directories, warn the user before creating them automatically
    if missing == True and not os.path.exists(conf_file):
        print("It appears you are running this tool for the first time.")
        print("A few directories need to be created in your home")
        print("directory for it to function properly.")
        print("")
        print("~/machines  - For storing virtual machines")
        print("~/iso       - For storing cd image files")
        print("~/iso/cache - For storing preseeded image files")
        print("")
        print("If you wish to change the location where these items are")
        print("stored, you can launch 'uvt config', which will create")
        print("a default config file in ~/%s that you can customize." % config_file)
        print("")

        if not confirm("Would you like to create the initial directories?"):
            print("Aborting.")
            sys.exit(1)

    for d in {'vm_dir_iso', 'vm_dir_iso_cache', 'vm_path'}:
        if not os.path.exists(config[d]):
            print("Creating '%s' directory..." % config[d])
            os.makedirs(config[d])

    conf_hash = hashlib.sha256((repr(sorted(config.items())).encode())).hexdigest()

    return (config, conf_hash)

def print_usage():
    '''Print usage'''
    print('''
Uncomplicated VM Tool (uvt)
uvt COMMAND [OPTIONS]

COMMAND:
new          Install a new virtual machine
start        Start a virtual machine
stop         Stop a virtual machine
snapshot     Take a snapshot of a virtual machine state
revert       Revert virtual machine to pristine snapshot
remove       Remove a virtual machine
repo         Adds or removes a local repo to a VM
update       Runs a dist-upgrade inside a VM
clone        Clones a VM into a new one
cmd          Run a command inside a virtual machine
list         List virtual machines
view         Connect to a virtual machine with VNC
config       Create an optional config file (~/%s)

OPTIONS:
type "uvt COMMAND -h" to get a list of options for each command.
''' % config_file, file=sys.stdout)

#
# BetterUbuntuDistroInfo
#

class BetterUbuntuDistroInfo(distro_info.UbuntuDistroInfo):
    '''Adds required functions to UbuntuDistroInfo'''

    def get_release(self, series, lts_status = False):
        '''Get the release version by looking up the series.'''
        results = [x for x in self._rows if x["series"] == series]
        if results == []:
            return None
        release = results[0]["version"]
        if lts_status == False:
            return release.split()[0]
        else:
            return release

#
# Main program
#

config_file = ".uvt.conf"

cmd = None
if len(sys.argv) > 1:
    cmd = sys.argv.pop(1)

# Don't create initial dirs if we want to create a new config file
if cmd != 'config':
    check_required_tools()
    (uvt_conf, uvt_conf_hash) = load_uvt_config()
    check_ssh_key()

    # Create or modify libvirt storage pool
    vm_check_pool(uvt_conf['vm_path'])

    if cmd == None:
        print("Error: Need to specify a command.\n", file=sys.stderr)
        print_usage()
        sys.exit(1)

# Command dispatch:
commands = {
    'new'      : cmd_new,
    'start'    : cmd_start,
    'stop'     : cmd_stop,
    'snapshot' : cmd_snapshot,
    'revert'   : cmd_revert,
    'remove'   : cmd_remove,
    'repo'     : cmd_repo,
    'update'   : cmd_update,
    'clone'    : cmd_clone,
    'cmd'      : cmd_cmd,
    'list'     : cmd_list,
    'config'   : cmd_config,
    'dump'     : cmd_dump,
    'test'     : cmd_test,
    'view'     : cmd_view,
    'help'     : print_usage
}

if cmd in commands:
    commands[cmd]()
else:
    print("Error: Invalid command.\n", file=sys.stderr)
    print_usage()
    sys.exit(1)