Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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

#!/usr/bin/env python 

# 

# Copyright (C) Citrix Systems Inc. 

# 

# This program is free software; you can redistribute it and/or modify  

# it under the terms of the GNU Lesser General Public License as published  

# by the Free Software Foundation; version 2.1 only. 

# 

# This program is distributed in the hope that it will be useful,  

# but WITHOUT ANY WARRANTY; without even the implied warranty of  

# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the  

# GNU Lesser General Public License for more details. 

# 

# You should have received a copy of the GNU Lesser General Public License 

# along with this program; if not, write to the Free Software Foundation, Inc., 

# 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA 

# 

# blktap2: blktap/tapdisk management layer 

# 

 

 

import os 

import sys 

import re 

import time 

import copy 

from lock import Lock 

import util 

import xmlrpclib 

import httplib 

import errno 

import subprocess 

import syslog as _syslog 

import glob 

import json 

import xs_errors 

import XenAPI 

import scsiutil 

from syslog import openlog, syslog 

from stat import * # S_ISBLK(), ... 

import nfs 

 

import resetvdis 

import vhdutil 

import lvhdutil 

 

 

# For RRDD Plugin Registration 

from xmlrpclib import ServerProxy, Transport 

from socket import socket, AF_UNIX, SOCK_STREAM 

from httplib import HTTP, HTTPConnection 

 

PLUGIN_TAP_PAUSE = "tapdisk-pause" 

 

SOCKPATH = "/var/xapi/xcp-rrdd" 

 

NUM_PAGES_PER_RING = 32 * 11 

MAX_FULL_RINGS = 8 

POOL_NAME_KEY = "mem-pool" 

POOL_SIZE_KEY = "mem-pool-size-rings" 

 

ENABLE_MULTIPLE_ATTACH = "/etc/xensource/allow_multiple_vdi_attach" 

NO_MULTIPLE_ATTACH = not (os.path.exists(ENABLE_MULTIPLE_ATTACH)) 

 

class UnixStreamHTTPConnection(HTTPConnection): 

    def connect(self): 

        self.sock = socket(AF_UNIX, SOCK_STREAM) 

        self.sock.connect(SOCKPATH) 

 

class UnixStreamHTTP(HTTP): 

    _connection_class = UnixStreamHTTPConnection 

 

class UnixStreamTransport(Transport): 

    def make_connection(self, host): 

        return UnixStreamHTTP(SOCKPATH) # overridden, but prevents IndexError 

 

 

def locking(excType, override=True): 

    def locking2(op): 

        def wrapper(self, *args): 

            self.lock.acquire() 

            try: 

                try: 

                    ret = op(self, *args) 

                except (util.CommandException, util.SMException, XenAPI.Failure), e: 

                    util.logException("BLKTAP2:%s" % op) 

                    msg = str(e) 

                    if isinstance(e, util.CommandException): 

                        msg = "Command %s failed (%s): %s" % \ 

                                (e.cmd, e.code, e.reason) 

                    if override: 

                        raise xs_errors.XenError(excType, opterr=msg) 

                    else: 

                        raise 

                except: 

                    util.logException("BLKTAP2:%s" % op) 

                    raise 

            finally: 

                self.lock.release() 

            return ret 

        return wrapper 

    return locking2 

 

class RetryLoop(object): 

 

    def __init__(self, backoff, limit): 

        self.backoff = backoff 

        self.limit   = limit 

 

    def __call__(self, f): 

 

        def loop(*__t, **__d): 

            attempt = 0 

 

            while True: 

                attempt += 1 

 

                try: 

                    return f(*__t, **__d) 

 

                except self.TransientFailure, e: 

                    e = e.exception 

 

                    if attempt >= self.limit: raise e 

 

                    time.sleep(self.backoff) 

 

        return loop 

 

    class TransientFailure(Exception): 

        def __init__(self, exception): 

            self.exception = exception 

 

def retried(**args): return RetryLoop(**args) 

 

class TapCtl(object): 

    """Tapdisk IPC utility calls.""" 

 

    PATH = "/usr/sbin/tap-ctl" 

 

    def __init__(self, cmd, p): 

        self.cmd    = cmd 

        self._p     = p 

        self.stdout = p.stdout 

 

    class CommandFailure(Exception): 

        """TapCtl cmd failure.""" 

 

        def __init__(self, cmd, **info): 

            self.cmd  = cmd 

            self.info = info 

 

        def __str__(self): 

            items = self.info.iteritems() 

            info  = ", ".join("%s=%s" % item 

                              for item in items) 

            return "%s failed: %s" % (self.cmd, info) 

 

        # Trying to get a non-existent attribute throws an AttributeError 

        # exception 

        def __getattr__(self, key): 

            if self.info.has_key(key): return self.info[key] 

            return object.__getattribute__(self, key) 

 

        # Retrieves the error code returned by the command. If the error code 

        # was not supplied at object-construction time, zero is returned. 

        def get_error_code(self): 

            key = 'status' 

            if self.info.has_key(key): 

                return self.info[key] 

            else: 

                return 0 

 

    @classmethod 

    def __mkcmd_real(cls, args): 

        return [ cls.PATH ] + map(str, args) 

 

    __next_mkcmd = __mkcmd_real 

 

    @classmethod 

    def _mkcmd(cls, args): 

 

        __next_mkcmd     = cls.__next_mkcmd 

        cls.__next_mkcmd = cls.__mkcmd_real 

 

        return __next_mkcmd(args) 

 

    @classmethod 

    def failwith(cls, status, prev=False): 

        """ 

        Fail next invocation with @status. If @prev is true, execute 

        the original command 

        """ 

 

        __prev_mkcmd = cls.__next_mkcmd 

 

        @classmethod 

        def __mkcmd(cls, args): 

            if prev: 

                cmd = __prev_mkcmd(args) 

                cmd = "'%s' && exit %d" % ("' '".join(cmd), status) 

            else: 

                cmd = "exit %d" % status 

 

            return [ '/bin/sh', '-c', cmd  ] 

 

        cls.__next_mkcmd = __mkcmd 

 

    __strace_n = 0 

 

    @classmethod 

    def strace(cls): 

        """ 

        Run next invocation through strace. 

        Output goes to /tmp/tap-ctl.<sm-pid>.<n>; <n> counts invocations. 

        """ 

 

        __prev_mkcmd = cls.__next_mkcmd 

 

        @classmethod 

        def __next_mkcmd(cls, args): 

 

            # pylint: disable = E1101 

 

            cmd = __prev_mkcmd(args) 

 

            tracefile = "/tmp/%s.%d.%d" % (os.path.basename(cls.PATH), 

                                           os.getpid(), 

                                           cls.__strace_n) 

            cls.__strace_n += 1 

 

            return \ 

                [ '/usr/bin/strace', '-o', tracefile, '--'] + cmd 

 

        cls.__next_mkcmd = __next_mkcmd 

 

    @classmethod 

    def _call(cls, args, quiet = False, input = None): 

        """ 

        Spawn a tap-ctl process. Return a TapCtl invocation. 

        Raises a TapCtl.CommandFailure if subprocess creation failed. 

        """ 

        cmd = cls._mkcmd(args) 

 

        if not quiet: 

            util.SMlog(cmd) 

        try: 

            p = subprocess.Popen(cmd, 

                                 stdin=subprocess.PIPE, 

                                 stdout=subprocess.PIPE, 

                                 stderr=subprocess.PIPE, 

                                 close_fds=True) 

            if input: 

                p.stdin.write(input) 

                p.stdin.close() 

        except OSError, e: 

            raise cls.CommandFailure(cmd, errno=e.errno) 

 

        return cls(cmd, p) 

 

    def _errmsg(self): 

        output = map(str.rstrip, self._p.stderr) 

        return "; ".join(output) 

 

    def _wait(self, quiet = False): 

        """ 

        Reap the child tap-ctl process of this invocation. 

        Raises a TapCtl.CommandFailure on non-zero exit status. 

        """ 

        status = self._p.wait() 

        if not quiet: 

            util.SMlog(" = %d" % status) 

 

        if status == 0: return 

 

        info = { 'errmsg'   : self._errmsg(), 

                 'pid'      : self._p.pid } 

 

        if status < 0: 

            info['signal'] = -status 

        else: 

            info['status'] = status 

 

        raise self.CommandFailure(self.cmd, **info) 

 

    @classmethod 

    def _pread(cls, args, quiet = False, input = None): 

        """ 

        Spawn a tap-ctl invocation and read a single line. 

        """ 

        tapctl = cls._call(args=args, quiet=quiet, input=input) 

 

        output = tapctl.stdout.readline().rstrip() 

 

        tapctl._wait(quiet) 

        return output 

 

    @staticmethod 

    def _maybe(opt, parm): 

        if parm is not None: return [ opt, parm ] 

        return [] 

 

    @classmethod 

    def __list(cls, minor = None, pid = None, _type = None, path = None): 

        args = [ "list" ] 

        args += cls._maybe("-m", minor) 

        args += cls._maybe("-p", pid) 

        args += cls._maybe("-t", _type) 

        args += cls._maybe("-f", path) 

 

        tapctl = cls._call(args, True) 

 

        for line in tapctl.stdout: 

            # FIXME: tap-ctl writes error messages to stdout and 

            # confuses this parser 

            if line == "blktap kernel module not installed\n": 

                # This isn't pretty but (a) neither is confusing stdout/stderr 

                # and at least causes the error to describe the fix 

                raise Exception, "blktap kernel module not installed: try 'modprobe blktap'" 

            row = {} 

 

            for field in line.rstrip().split(' ', 3): 

                bits = field.split('=') 

                if len(bits) == 2: 

                    key, val = field.split('=') 

 

                    if key in ('pid', 'minor'): 

                        row[key] = int(val, 10) 

 

                    elif key in ('state'): 

                        row[key] = int(val, 0x10) 

 

                    else: 

                        row[key] = val 

                else: 

                    util.SMlog("Ignoring unexpected tap-ctl output: %s" % repr(field)) 

            yield row 

 

        tapctl._wait(True) 

 

    @classmethod 

    @retried(backoff=.5, limit=10) 

    def list(cls, **args): 

 

        # FIXME. We typically get an EPROTO when uevents interleave 

        # with SM ops and a tapdisk shuts down under our feet. Should 

        # be fixed in SM. 

 

        try: 

            return list(cls.__list(**args)) 

 

        except cls.CommandFailure, e: 

            transient = [ errno.EPROTO, errno.ENOENT ] 

            if e.status in transient: 

                raise RetryLoop.TransientFailure(e) 

            raise 

 

    @classmethod 

    def allocate(cls, devpath = None): 

        args = [ "allocate" ] 

        args += cls._maybe("-d", devpath) 

        return cls._pread(args) 

 

    @classmethod 

    def free(cls, minor): 

        args = [ "free", "-m", minor ] 

        cls._pread(args) 

 

    @classmethod 

    @retried(backoff=.5, limit=10) 

    def spawn(cls): 

        args = [ "spawn" ] 

        try: 

            pid = cls._pread(args) 

            return int(pid) 

        except cls.CommandFailure as ce: 

            # intermittent failures to spawn. CA-292268 

            if ce.status == 1: 

                raise RetryLoop.TransientFailure(ce) 

            raise 

 

    @classmethod 

    def attach(cls, pid, minor): 

        args = [ "attach", "-p", pid, "-m", minor ] 

        cls._pread(args) 

 

    @classmethod 

    def detach(cls, pid, minor): 

        args = [ "detach", "-p", pid, "-m", minor ] 

        cls._pread(args) 

 

    @classmethod 

    def open(cls, pid, minor, _type, _file, options): 

        params = Tapdisk.Arg(_type, _file) 

        args = [ "open", "-p", pid, "-m", minor, '-a', str(params) ] 

        input = None 

        if options.get("rdonly"): 

            args.append('-R') 

        if options.get("lcache"): 

            args.append("-r") 

        if options.get("existing_prt") != None: 

            args.append("-e") 

            args.append(str(options["existing_prt"])) 

        if options.get("secondary"): 

            args.append("-2") 

            args.append(options["secondary"]) 

        if options.get("standby"): 

            args.append("-s") 

        if options.get("timeout"): 

            args.append("-t") 

            args.append(str(options["timeout"])) 

        if not options.get("o_direct", True): 

            args.append("-D") 

        if options.get('cbtlog'): 

            args.extend(['-C', options['cbtlog']]) 

        if options.get('key_hash'): 

            import plugins 

 

            key_hash = options['key_hash'] 

            vdi_uuid = options['vdi_uuid'] 

            key = plugins.load_key(key_hash, vdi_uuid) 

            if not key: 

                raise util.SMException("No key found with key hash {}".format(key_hash)) 

            input = key 

            args.append('-E') 

        cls._pread(args=args, input=input) 

 

    @classmethod 

    def close(cls, pid, minor, force = False): 

        args = [ "close", "-p", pid, "-m", minor, "-t", "30" ] 

        if force: args += [ "-f" ] 

        cls._pread(args) 

 

    @classmethod 

    def pause(cls, pid, minor): 

        args = [ "pause", "-p", pid, "-m", minor ] 

        cls._pread(args) 

 

    @classmethod 

    def unpause(cls, pid, minor, _type = None, _file = None, mirror = None, 

                cbtlog = None): 

        args = [ "unpause", "-p", pid, "-m", minor ] 

        if mirror: 

            args.extend(["-2", mirror]) 

        if _type and _file: 

            params = Tapdisk.Arg(_type, _file) 

            args  += [ "-a", str(params) ] 

        if cbtlog: 

            args.extend(["-c", cbtlog]) 

        cls._pread(args) 

 

    @classmethod 

    def stats(cls, pid, minor): 

        args = [ "stats", "-p", pid, "-m", minor ] 

        return cls._pread(args, quiet = True) 

 

    @classmethod 

    def major(cls): 

        args = [ "major" ] 

        major = cls._pread(args) 

        return int(major) 

 

class TapdiskExists(Exception): 

    """Tapdisk already running.""" 

 

    def __init__(self, tapdisk): 

        self.tapdisk = tapdisk 

 

    def __str__(self): 

        return "%s already running" % self.tapdisk 

 

class TapdiskNotRunning(Exception): 

    """No such Tapdisk.""" 

 

    def __init__(self, **attrs): 

        self.attrs = attrs 

 

    def __str__(self): 

        items = self.attrs.iteritems() 

        attrs = ", ".join("%s=%s" % attr 

                          for attr in items) 

        return "No such Tapdisk(%s)" % attrs 

 

class TapdiskNotUnique(Exception): 

    """More than one tapdisk on one path.""" 

 

    def __init__(self, tapdisks): 

        self.tapdisks = tapdisks 

 

    def __str__(self): 

        tapdisks = map(str, self.tapdisks) 

        return "Found multiple tapdisks: %s" % tapdisks 

 

class TapdiskFailed(Exception): 

    """Tapdisk launch failure.""" 

 

    def __init__(self, arg, err): 

        self.arg   = arg 

        self.err   = err 

 

    def __str__(self): 

        return "Tapdisk(%s): %s" % (self.arg, self.err) 

 

    def get_error(self): 

        return self.err 

 

class TapdiskInvalidState(Exception): 

    """Tapdisk pause/unpause failure""" 

 

    def __init__(self, tapdisk): 

        self.tapdisk = tapdisk 

 

    def __str__(self): 

        return str(self.tapdisk) 

 

def mkdirs(path, mode=0777): 

    if not os.path.exists(path): 

        parent, subdir = os.path.split(path) 

        assert parent != path 

        try: 

            if parent: 

                mkdirs(parent, mode) 

            if subdir: 

                os.mkdir(path, mode) 

        except OSError, e: 

            if e.errno != errno.EEXIST: 

                raise 

 

 

class KObject(object): 

 

    SYSFS_CLASSTYPE = None 

 

    def sysfs_devname(self): 

        raise NotImplementedError("sysfs_devname is undefined") 

 

class Attribute(object): 

 

    SYSFS_NODENAME = None 

 

    def __init__(self, path): 

        self.path = path 

 

    @classmethod 

    def from_kobject(cls, kobj): 

        path = "%s/%s" % (kobj.sysfs_path(), cls.SYSFS_NODENAME) 

        return cls(path) 

 

    class NoSuchAttribute(Exception): 

        def __init__(self, name): 

            self.name = name 

 

        def __str__(self): 

            return "No such attribute: %s" % self.name 

 

    def _open(self, mode='r'): 

        try: 

            return file(self.path, mode) 

        except IOError, e: 

            if e.errno == errno.ENOENT: 

                raise self.NoSuchAttribute(self) 

            raise 

 

    def readline(self): 

        f = self._open('r') 

        s = f.readline().rstrip() 

        f.close() 

        return s 

 

    def writeline(self, val): 

        f = self._open('w') 

        f.write(val) 

        f.close() 

 

class ClassDevice(KObject): 

 

    @classmethod 

    def sysfs_class_path(cls): 

        return "/sys/class/%s" % cls.SYSFS_CLASSTYPE 

 

    def sysfs_path(self): 

        return "%s/%s" % (self.sysfs_class_path(), 

                          self.sysfs_devname()) 

 

class Blktap(ClassDevice): 

 

    DEV_BASEDIR = '/dev/xen/blktap-2' 

 

    SYSFS_CLASSTYPE = "blktap2" 

 

    def __init__(self, minor): 

        self.minor = minor 

        self._pool = None 

        self._task = None 

 

    @classmethod 

    def allocate(cls): 

        # FIXME. Should rather go into init. 

        mkdirs(cls.DEV_BASEDIR) 

 

        devname = TapCtl.allocate() 

        minor   = Tapdisk._parse_minor(devname) 

        return cls(minor) 

 

    def free(self): 

        TapCtl.free(self.minor) 

 

    def __str__(self): 

        return "%s(minor=%d)" % (self.__class__.__name__, self.minor) 

 

    def sysfs_devname(self): 

        return "blktap!blktap%d" % self.minor 

 

    class Pool(Attribute): 

        SYSFS_NODENAME = "pool" 

 

    def get_pool_attr(self): 

        if not self._pool: 

            self._pool = self.Pool.from_kobject(self) 

        return self._pool 

 

    def get_pool_name(self): 

        return self.get_pool_attr().readline() 

 

    def set_pool_name(self, name): 

        self.get_pool_attr().writeline(name) 

 

    def set_pool_size(self, pages): 

        self.get_pool().set_size(pages) 

 

    def get_pool(self): 

        return BlktapControl.get_pool(self.get_pool_name()) 

 

    def set_pool(self, pool): 

        self.set_pool_name(pool.name) 

 

    class Task(Attribute): 

        SYSFS_NODENAME = "task" 

 

    def get_task_attr(self): 

        if not self._task: 

            self._task = self.Task.from_kobject(self) 

        return self._task 

 

    def get_task_pid(self): 

        pid = self.get_task_attr().readline() 

        try: 

            return int(pid) 

        except ValueError: 

            return None 

 

    def find_tapdisk(self): 

        pid = self.get_task_pid() 

        if pid is None: return None 

 

        return Tapdisk.find(pid=pid, minor=self.minor) 

 

    def get_tapdisk(self): 

        tapdisk = self.find_tapdisk() 

        if not tapdisk: 

            raise TapdiskNotRunning(minor=self.minor) 

        return tapdisk 

 

class Tapdisk(object): 

 

    TYPES = [ 'aio', 'vhd' ] 

 

    def __init__(self, pid, minor, _type, path, state): 

        self.pid     = pid 

        self.minor   = minor 

        self.type    = _type 

        self.path    = path 

        self.state   = state 

        self._dirty  = False 

        self._blktap = None 

 

    def __str__(self): 

        state = self.pause_state() 

        return "Tapdisk(%s, pid=%d, minor=%s, state=%s)" % \ 

            (self.get_arg(), self.pid, self.minor, state) 

 

    @classmethod 

    def list(cls, **args): 

 

        for row in TapCtl.list(**args): 

 

            args =  { 'pid'     : None, 

                      'minor'   : None, 

                      'state'   : None, 

                      '_type'   : None, 

                      'path'    : None } 

 

            for key, val in row.iteritems(): 

                if key in args: 

                    args[key] = val 

 

            if 'args' in row: 

                image = Tapdisk.Arg.parse(row['args']) 

                args['_type'] = image.type 

                args['path']  = image.path 

 

            if None in args.values(): 

                continue 

 

            yield Tapdisk(**args) 

 

    @classmethod 

    def find(cls, **args): 

 

        found = list(cls.list(**args)) 

 

        if len(found) > 1: 

            raise TapdiskNotUnique(found) 

 

        if found: 

            return found[0] 

 

        return None 

 

    @classmethod 

    def find_by_path(cls, path): 

        return cls.find(path=path) 

 

    @classmethod 

    def find_by_minor(cls, minor): 

        return cls.find(minor=minor) 

 

    @classmethod 

    def get(cls, **attrs): 

 

        tapdisk = cls.find(**attrs) 

 

        if not tapdisk: 

            raise TapdiskNotRunning(**attrs) 

 

        return tapdisk 

 

    @classmethod 

    def from_path(cls, path): 

        return cls.get(path=path) 

 

    @classmethod 

    def from_minor(cls, minor): 

        return cls.get(minor=minor) 

 

    @classmethod 

    def __from_blktap(cls, blktap): 

        tapdisk = cls.from_minor(minor=blktap.minor) 

        tapdisk._blktap = blktap 

        return tapdisk 

 

    def get_blktap(self): 

        if not self._blktap: 

            self._blktap = Blktap(self.minor) 

        return self._blktap 

 

    class Arg: 

 

        def __init__(self, _type, path): 

            self.type = _type 

            self.path =  path 

 

        def __str__(self): 

            return "%s:%s" % (self.type, self.path) 

 

        @classmethod 

        def parse(cls, arg): 

 

            try: 

                _type, path = arg.split(":", 1) 

            except ValueError: 

                raise cls.InvalidArgument(arg) 

 

            if _type not in Tapdisk.TYPES: 

                raise cls.InvalidType(_type) 

 

            return cls(_type, path) 

 

        class InvalidType(Exception): 

            def __init__(self, _type): 

                self.type = _type 

 

            def __str__(self): 

                return "Not a Tapdisk type: %s" % self.type 

 

        class InvalidArgument(Exception): 

            def __init__(self, arg): 

                self.arg = arg 

 

            def __str__(self): 

                return "Not a Tapdisk image: %s" % self.arg 

 

    def get_arg(self): 

        return self.Arg(self.type, self.path) 

 

    def get_devpath(self): 

        return "%s/tapdev%d" % (Blktap.DEV_BASEDIR, self.minor) 

 

    @classmethod 

    def launch_from_arg(cls, arg): 

        arg = cls.Arg.parse(arg) 

        return cls.launch(arg.path, arg.type, False) 

 

    @classmethod 

    def launch_on_tap(cls, blktap, path, _type, options): 

 

        tapdisk = cls.find_by_path(path) 

        if tapdisk: 

            raise TapdiskExists(tapdisk) 

 

        minor = blktap.minor 

 

        try: 

            pid = TapCtl.spawn() 

 

            try: 

                TapCtl.attach(pid, minor) 

 

                try: 

                    TapCtl.open(pid, minor, _type, path, options) 

                    try: 

                        tapdisk = cls.__from_blktap(blktap) 

                        node = '/sys/dev/block/%d:%d' % (tapdisk.major(), tapdisk.minor) 

                        util.set_scheduler_sysfs_node(node, 'noop') 

                        return tapdisk 

                    except: 

                        TapCtl.close(pid, minor) 

                        raise 

 

                except: 

                    TapCtl.detach(pid, minor) 

                    raise 

 

            except: 

                exc_info = sys.exc_info() 

                # FIXME: Should be tap-ctl shutdown. 

                try: 

                    import signal 

                    os.kill(pid, signal.SIGTERM) 

                    os.waitpid(pid, 0) 

                finally: 

                    raise exc_info[0], exc_info[1], exc_info[2] 

 

        except TapCtl.CommandFailure, ctl: 

            util.logException(ctl) 

            if ('/dev/xapi/cd/' in path and 

                    'status' in ctl.info and 

                    ctl.info['status'] == 123): # ENOMEDIUM (No medium found) 

                raise xs_errors.XenError('TapdiskDriveEmpty') 

            else: 

                raise TapdiskFailed(cls.Arg(_type, path), ctl) 

 

    @classmethod 

    def launch(cls, path, _type, rdonly): 

        blktap = Blktap.allocate() 

        try: 

            return cls.launch_on_tap(blktap, path, _type, {"rdonly": rdonly}) 

        except: 

            blktap.free() 

            raise 

 

    def shutdown(self, force = False): 

 

        TapCtl.close(self.pid, self.minor, force) 

 

        TapCtl.detach(self.pid, self.minor) 

 

        self.get_blktap().free() 

 

    def pause(self): 

 

        if not self.is_running(): 

            raise TapdiskInvalidState(self) 

 

        TapCtl.pause(self.pid, self.minor) 

 

        self._set_dirty() 

 

    def unpause(self, _type=None, path=None, mirror=None, cbtlog = None): 

 

        if not self.is_paused(): 

            raise TapdiskInvalidState(self) 

 

        # FIXME: should the arguments be optional? 

        if _type is None: _type = self.type 

        if  path is None:  path = self.path 

 

        TapCtl.unpause(self.pid, self.minor, _type, path, mirror=mirror, 

                       cbtlog=cbtlog) 

 

        self._set_dirty() 

 

    def stats(self): 

        return json.loads(TapCtl.stats(self.pid, self.minor)) 

 

    # 

    # NB. dirty/refresh: reload attributes on next access 

    # 

 

    def _set_dirty(self): 

        self._dirty = True 

 

    def _refresh(self, __get): 

        t = self.from_minor(__get('minor')) 

        self.__init__(t.pid, t.minor, t.type, t.path, t.state) 

 

    def __getattribute__(self, name): 

        def __get(name): 

            # NB. avoid(rec(ursion) 

            return object.__getattribute__(self, name) 

 

914        if __get('_dirty') and \ 

                name in ['minor', 'type', 'path', 'state']: 

            self._refresh(__get) 

            self._dirty = False 

 

        return __get(name) 

 

    class PauseState: 

        RUNNING             = 'R' 

        PAUSING             = 'r' 

        PAUSED              = 'P' 

 

    class Flags: 

        DEAD                 = 0x0001 

        CLOSED               = 0x0002 

        QUIESCE_REQUESTED    = 0x0004 

        QUIESCED             = 0x0008 

        PAUSE_REQUESTED      = 0x0010 

        PAUSED               = 0x0020 

        SHUTDOWN_REQUESTED   = 0x0040 

        LOCKING              = 0x0080 

        RETRY_NEEDED         = 0x0100 

        LOG_DROPPED          = 0x0200 

 

        PAUSE_MASK           = PAUSE_REQUESTED|PAUSED 

 

    def is_paused(self): 

        return not not (self.state & self.Flags.PAUSED) 

 

    def is_running(self): 

        return not (self.state & self.Flags.PAUSE_MASK) 

 

    def pause_state(self): 

        if self.state & self.Flags.PAUSED: 

            return self.PauseState.PAUSED 

 

        if self.state & self.Flags.PAUSE_REQUESTED: 

            return self.PauseState.PAUSING 

 

        return self.PauseState.RUNNING 

 

    @staticmethod 

    def _parse_minor(devpath): 

 

        regex   = '%s/(blktap|tapdev)(\d+)$' % Blktap.DEV_BASEDIR 

        pattern = re.compile(regex) 

        groups  = pattern.search(devpath) 

        if not groups: 

            raise Exception, \ 

                "malformed tap device: '%s' (%s) " % (devpath, regex) 

 

        minor = groups.group(2) 

        return int(minor) 

 

    _major = None 

 

    @classmethod 

    def major(cls): 

        if cls._major: return cls._major 

 

        devices = file("/proc/devices") 

        for line in devices: 

 

            row = line.rstrip().split(' ') 

            if len(row) != 2: continue 

 

            major, name = row 

            if name != 'tapdev': continue 

 

            cls._major = int(major) 

            break 

 

        devices.close() 

        return cls._major 

 

class VDI(object): 

    """SR.vdi driver decorator for blktap2""" 

 

    CONF_KEY_ALLOW_CACHING = "vdi_allow_caching" 

    CONF_KEY_MODE_ON_BOOT = "vdi_on_boot" 

    CONF_KEY_CACHE_SR = "local_cache_sr" 

    CONF_KEY_O_DIRECT = "o_direct" 

    LOCK_CACHE_SETUP = "cachesetup" 

 

    ATTACH_DETACH_RETRY_SECS = 120 

 

    # number of seconds on top of NFS timeo mount option the tapdisk should  

    # wait before reporting errors. This is to allow a retry to succeed in case  

    # packets were lost the first time around, which prevented the NFS client  

    # from returning before the timeo is reached even if the NFS server did  

    # come back earlier 

    TAPDISK_TIMEOUT_MARGIN = 30 

 

    def __init__(self, uuid, target, driver_info): 

        self.target      = self.TargetDriver(target, driver_info) 

        self._vdi_uuid   = uuid 

        self._session    = target.session 

        self.xenstore_data = scsiutil.update_XS_SCSIdata(uuid,scsiutil.gen_synthetic_page_data(uuid)) 

        self.__o_direct  = None 

        self.__o_direct_reason = None 

        self.lock        = Lock("vdi", uuid) 

        self.tap         = None 

 

    def get_o_direct_capability(self, options): 

        """Returns True/False based on licensing and caching_params""" 

        if self.__o_direct is not None: 

            return self.__o_direct, self.__o_direct_reason 

 

        if util.read_caching_is_restricted(self._session): 

            self.__o_direct = True 

            self.__o_direct_reason = "LICENSE_RESTRICTION" 

        elif not ((self.target.vdi.sr.handles("nfs") or self.target.vdi.sr.handles("ext") or self.target.vdi.sr.handles("smb"))): 

            self.__o_direct = True 

            self.__o_direct_reason = "SR_NOT_SUPPORTED" 

        elif not (options.get("rdonly") or self.target.vdi.parent): 

            util.SMlog(self.target.vdi) 

            self.__o_direct = True 

            self.__o_direct_reason = "NO_RO_IMAGE" 

        elif options.get("rdonly") and not self.target.vdi.parent: 

            self.__o_direct = True 

            self.__o_direct_reason = "RO_WITH_NO_PARENT" 

        elif options.get(self.CONF_KEY_O_DIRECT): 

            self.__o_direct = True 

            self.__o_direct_reason = "SR_OVERRIDE" 

 

        if self.__o_direct is None: 

            self.__o_direct = False 

            self.__o_direct_reason = "" 

 

        return self.__o_direct, self.__o_direct_reason 

 

    @classmethod 

    def from_cli(cls, uuid): 

        import VDI as sm 

        import XenAPI 

 

        session = XenAPI.xapi_local() 

        session.xenapi.login_with_password('root', '', '', 'SM') 

 

        target = sm.VDI.from_uuid(session, uuid) 

        driver_info = target.sr.srcmd.driver_info 

 

        session.xenapi.session.logout() 

 

        return cls(uuid, target, driver_info) 

 

    @staticmethod 

    def _tap_type(vdi_type): 

        """Map a VDI type (e.g. 'raw') to a tapdisk driver type (e.g. 'aio')""" 

        return { 

            'raw'  : 'aio', 

            'vhd'  : 'vhd', 

            'iso'  : 'aio', # for ISO SR 

            'aio'  : 'aio', # for LVHD 

            'file' : 'aio', 

            'phy'  : 'aio' 

            } [vdi_type] 

 

    def get_tap_type(self): 

        vdi_type = self.target.get_vdi_type() 

        return VDI._tap_type(vdi_type) 

 

    def get_phy_path(self): 

        return self.target.get_vdi_path() 

 

    class UnexpectedVDIType(Exception): 

 

        def __init__(self, vdi_type, target): 

            self.vdi_type = vdi_type 

            self.target   = target 

 

        def __str__(self): 

            return \ 

                "Target %s has unexpected VDI type '%s'" % \ 

                (type(self.target), self.vdi_type) 

 

    VDI_PLUG_TYPE = { 'phy'  : 'phy',  # for NETAPP 

                      'raw'  : 'phy', 

                      'aio'  : 'tap',  # for LVHD raw nodes 

                      'iso'  : 'tap', # for ISOSR 

                      'file' : 'tap', 

                      'vhd'  : 'tap' } 

 

    def tap_wanted(self): 

 

        # 1. Let the target vdi_type decide 

 

        vdi_type = self.target.get_vdi_type() 

 

        try: 

            plug_type = self.VDI_PLUG_TYPE[vdi_type] 

        except KeyError: 

            raise self.UnexpectedVDIType(vdi_type, 

                                         self.target.vdi) 

 

1108        if plug_type == 'tap': 

            return True 

1116        elif self.target.vdi.sr.handles('udev'): 

            return True 

 

        # 2. Otherwise, there may be more reasons 

        # 

        # .. TBD 

 

        return False 

 

    class TargetDriver: 

        """Safe target driver access.""" 

 

        # NB. *Must* test caps for optional calls. Some targets 

        # actually implement some slots, but do not enable them. Just 

        # try/except would risk breaking compatibility. 

 

        def __init__(self, vdi, driver_info): 

            self.vdi    = vdi 

            self._caps  = driver_info['capabilities'] 

 

        def has_cap(self, cap): 

            """Determine if target has given capability""" 

            return cap in self._caps 

 

        def attach(self, sr_uuid, vdi_uuid): 

            #assert self.has_cap("VDI_ATTACH") 

            return self.vdi.attach(sr_uuid, vdi_uuid) 

 

        def detach(self, sr_uuid, vdi_uuid): 

            #assert self.has_cap("VDI_DETACH") 

            self.vdi.detach(sr_uuid, vdi_uuid) 

 

        def activate(self, sr_uuid, vdi_uuid): 

            if self.has_cap("VDI_ACTIVATE"): 

                return self.vdi.activate(sr_uuid, vdi_uuid) 

 

        def deactivate(self, sr_uuid, vdi_uuid): 

            if self.has_cap("VDI_DEACTIVATE"): 

                self.vdi.deactivate(sr_uuid, vdi_uuid) 

 

        #def resize(self, sr_uuid, vdi_uuid, size): 

        #    return self.vdi.resize(sr_uuid, vdi_uuid, size) 

 

        def get_vdi_type(self): 

            _type = self.vdi.vdi_type 

            if not _type: 

                _type = self.vdi.sr.sr_vditype 

            if not _type: 

                raise VDI.UnexpectedVDIType(_type, self.vdi) 

            return _type 

 

        def get_vdi_path(self): 

            return self.vdi.path 

 

    class Link(object): 

        """Relink a node under a common name""" 

 

        # NB. We have to provide the device node path during 

        # VDI.attach, but currently do not allocate the tapdisk minor 

        # before VDI.activate. Therefore those link steps where we 

        # relink existing devices under deterministic path names. 

 

        BASEDIR = None 

 

        def _mklink(self, target): 

            raise NotImplementedError("_mklink is not defined") 

 

        def _equals(self, target): 

            raise NotImplementedError("_equals is not defined") 

 

        def __init__(self, path): 

            self._path = path 

 

        @classmethod 

        def from_name(cls, name): 

            path = "%s/%s" % (cls.BASEDIR, name) 

            return cls(path) 

 

        @classmethod 

        def from_uuid(cls, sr_uuid, vdi_uuid): 

            name = "%s/%s" % (sr_uuid, vdi_uuid) 

            return cls.from_name(name) 

 

        def path(self): 

            return self._path 

 

        def stat(self): 

            return os.stat(self.path()) 

 

        def mklink(self, target): 

 

            path = self.path() 

            util.SMlog("%s -> %s" % (self, target)) 

 

            mkdirs(os.path.dirname(path)) 

            try: 

                self._mklink(target) 

            except OSError, e: 

                # We do unlink during teardown, but have to stay 

                # idempotent. However, a *wrong* target should never 

                # be seen. 

                if e.errno != errno.EEXIST: raise 

                assert self._equals(target), "'%s' not equal to '%s'" % (path, target) 

 

        def unlink(self): 

            try: 

                os.unlink(self.path()) 

            except OSError, e: 

                if e.errno != errno.ENOENT: raise 

 

        def __str__(self): 

            path = self.path() 

            return "%s(%s)" % (self.__class__.__name__, path) 

 

    class SymLink(Link): 

        """Symlink some file to a common name""" 

 

        def readlink(self): 

            return os.readlink(self.path()) 

 

        def symlink(self): 

            return self.path() 

 

        def _mklink(self, target): 

            os.symlink(target, self.path()) 

 

        def _equals(self, target): 

            return self.readlink() == target 

 

    class DeviceNode(Link): 

        """Relink a block device node to a common name""" 

 

        @classmethod 

        def _real_stat(cls, target): 

            """stat() not on @target, but its realpath()""" 

            _target = os.path.realpath(target) 

            return os.stat(_target) 

 

        @classmethod 

        def is_block(cls, target): 

            """Whether @target refers to a block device.""" 

            return S_ISBLK(cls._real_stat(target).st_mode) 

 

        def _mklink(self, target): 

 

            st = self._real_stat(target) 

            if not S_ISBLK(st.st_mode): 

                raise self.NotABlockDevice(target, st) 

 

            os.mknod(self.path(), st.st_mode, st.st_rdev) 

 

        def _equals(self, target): 

            target_rdev = self._real_stat(target).st_rdev 

            return self.stat().st_rdev == target_rdev 

 

        def rdev(self): 

            st = self.stat() 

            assert S_ISBLK(st.st_mode) 

            return os.major(st.st_rdev), os.minor(st.st_rdev) 

 

        class NotABlockDevice(Exception): 

 

            def __init__(self, path, st): 

                self.path = path 

                self.st   = st 

 

            def __str__(self): 

                return "%s is not a block device: %s" % (self.path, self.st) 

 

    class Hybrid(Link): 

 

        def __init__(self, path): 

            VDI.Link.__init__(self, path) 

            self._devnode = VDI.DeviceNode(path) 

            self._symlink = VDI.SymLink(path) 

 

        def rdev(self): 

            st = self.stat() 

            if S_ISBLK(st.st_mode): return self._devnode.rdev() 

            raise self._devnode.NotABlockDevice(self.path(), st) 

 

        def mklink(self, target): 

            if self._devnode.is_block(target): 

                self._obj = self._devnode 

            else: 

                self._obj = self._symlink 

            self._obj.mklink(target) 

 

        def _equals(self, target): 

            return self._obj._equals(target) 

 

    class PhyLink(SymLink): BASEDIR = "/dev/sm/phy" 

    # NB. Cannot use DeviceNodes, e.g. FileVDIs aren't bdevs. 

 

    class NBDLink(SymLink): 

 

        BASEDIR = "/run/blktap-control/nbd" 

 

    class BackendLink(Hybrid): BASEDIR = "/dev/sm/backend" 

 

    # NB. Could be SymLinks as well, but saving major,minor pairs in 

    # Links enables neat state capturing when managing Tapdisks.  Note 

    # that we essentially have a tap-ctl list replacement here. For 

    # now make it a 'Hybrid'. Likely to collapse into a DeviceNode as 

    # soon as ISOs are tapdisks. 

 

    @staticmethod 

    def _tap_activate(phy_path, vdi_type, sr_uuid, options, pool_size = None): 

 

        tapdisk = Tapdisk.find_by_path(phy_path) 

        if not tapdisk: 

            blktap = Blktap.allocate() 

            blktap.set_pool_name(sr_uuid) 

            if pool_size: 

                blktap.set_pool_size(pool_size) 

 

            try: 

                tapdisk = \ 

                    Tapdisk.launch_on_tap(blktap, 

                                          phy_path, 

                                          VDI._tap_type(vdi_type), 

                                          options) 

            except: 

                blktap.free() 

                raise 

            util.SMlog("tap.activate: Launched %s" % tapdisk) 

 

        else: 

            util.SMlog("tap.activate: Found %s" % tapdisk) 

 

        return tapdisk.get_devpath(), tapdisk 

 

    @staticmethod 

    def _tap_deactivate(minor): 

 

        try: 

            tapdisk = Tapdisk.from_minor(minor) 

        except TapdiskNotRunning, e: 

            util.SMlog("tap.deactivate: Warning, %s" % e) 

            # NB. Should not be here unless the agent refcount 

            # broke. Also, a clean shutdown should not have leaked 

            # the recorded minor. 

        else: 

            tapdisk.shutdown() 

            util.SMlog("tap.deactivate: Shut down %s" % tapdisk) 

 

    @classmethod 

    def tap_pause(cls, session, sr_uuid, vdi_uuid, failfast=False): 

        """ 

        Pauses the tapdisk. 

 

        session: a XAPI session 

        sr_uuid: the UUID of the SR on which VDI lives 

        vdi_uuid: the UUID of the VDI to pause 

        failfast: controls whether the VDI lock should be acquired in a 

            non-blocking manner 

        """ 

        util.SMlog("Pause request for %s" % vdi_uuid) 

        vdi_ref = session.xenapi.VDI.get_by_uuid(vdi_uuid) 

        session.xenapi.VDI.add_to_sm_config(vdi_ref, 'paused', 'true') 

        sm_config = session.xenapi.VDI.get_sm_config(vdi_ref) 

exit   1371        for key in filter(lambda x: x.startswith('host_'), sm_config.keys()): 

            host_ref = key[len('host_'):] 

            util.SMlog("Calling tap-pause on host %s" % host_ref) 

            if not cls.call_pluginhandler(session, host_ref, 

                    sr_uuid, vdi_uuid, "pause", failfast=failfast): 

                # Failed to pause node 

                session.xenapi.VDI.remove_from_sm_config(vdi_ref, 'paused') 

                return False 

        return True 

 

    @classmethod 

    def tap_unpause(cls, session, sr_uuid, vdi_uuid, secondary = None, 

                    activate_parents = False): 

        util.SMlog("Unpause request for %s secondary=%s" % (vdi_uuid, secondary)) 

        vdi_ref = session.xenapi.VDI.get_by_uuid(vdi_uuid) 

        sm_config = session.xenapi.VDI.get_sm_config(vdi_ref) 

exit   1387        for key in filter(lambda x: x.startswith('host_'), sm_config.keys()): 

            host_ref = key[len('host_'):] 

            util.SMlog("Calling tap-unpause on host %s" % host_ref) 

            if not cls.call_pluginhandler(session, host_ref, 

                    sr_uuid, vdi_uuid, "unpause", secondary, activate_parents): 

                # Failed to unpause node 

                return False 

        session.xenapi.VDI.remove_from_sm_config(vdi_ref, 'paused') 

        return True 

 

    @classmethod 

    def tap_refresh(cls, session, sr_uuid, vdi_uuid, activate_parents = False): 

        util.SMlog("Refresh request for %s" % vdi_uuid) 

        vdi_ref = session.xenapi.VDI.get_by_uuid(vdi_uuid) 

        sm_config = session.xenapi.VDI.get_sm_config(vdi_ref) 

        for key in filter(lambda x: x.startswith('host_'), sm_config.keys()): 

            host_ref = key[len('host_'):] 

            util.SMlog("Calling tap-refresh on host %s" % host_ref) 

            if not cls.call_pluginhandler(session, host_ref, 

                       sr_uuid, vdi_uuid, "refresh", None, 

                       activate_parents=activate_parents): 

                # Failed to refresh node 

                return False 

        return True 

 

    @classmethod 

    def tap_status(cls, session, vdi_uuid): 

        """Return True if disk is attached, false if it isn't""" 

        util.SMlog("Disk status request for %s" % vdi_uuid) 

        vdi_ref = session.xenapi.VDI.get_by_uuid(vdi_uuid) 

        sm_config = session.xenapi.VDI.get_sm_config(vdi_ref) 

exit   1418        for key in filter(lambda x: x.startswith('host_'), sm_config.keys()): 

                return True 

        return False 

 

    @classmethod 

    def call_pluginhandler(cls, session, host_ref, sr_uuid, vdi_uuid, action, 

            secondary = None, activate_parents = False, failfast=False): 

        """Optionally, activate the parent LV before unpausing""" 

        try: 

            args = {"sr_uuid":sr_uuid, "vdi_uuid":vdi_uuid, 

                    "failfast": str(failfast)} 

            if secondary: 

                args["secondary"] = secondary 

            if activate_parents: 

                args["activate_parents"] = "true" 

            ret = session.xenapi.host.call_plugin( 

                    host_ref, PLUGIN_TAP_PAUSE, action, 

                    args) 

            return ret == "True" 

        except Exception, e: 

            util.logException("BLKTAP2:call_pluginhandler %s" % e) 

            return False 

 

 

    def _add_tag(self, vdi_uuid, writable): 

        util.SMlog("Adding tag to: %s" % vdi_uuid) 

        attach_mode = "RO" 

        if writable: 

            attach_mode = "RW" 

        vdi_ref = self._session.xenapi.VDI.get_by_uuid(vdi_uuid) 

        host_ref = self._session.xenapi.host.get_by_uuid(util.get_this_host()) 

        sm_config = self._session.xenapi.VDI.get_sm_config(vdi_ref) 

        attached_as = util.attached_as(sm_config) 

        if NO_MULTIPLE_ATTACH and (attached_as == "RW" or \ 

                (attached_as == "RO" and attach_mode == "RW")): 

            util.SMlog("need to reset VDI %s" % vdi_uuid) 

            if not resetvdis.reset_vdi(self._session, vdi_uuid, force=False, 

                    term_output=False, writable=writable): 

                raise util.SMException("VDI %s not detached cleanly" % vdi_uuid) 

            sm_config = self._session.xenapi.VDI.get_sm_config(vdi_ref) 

        if sm_config.has_key('paused'): 

            util.SMlog("Paused or host_ref key found [%s]" % sm_config) 

            return False 

        host_key = "host_%s" % host_ref 

        assert not sm_config.has_key(host_key) 

        self._session.xenapi.VDI.add_to_sm_config(vdi_ref, host_key, 

                                                  attach_mode) 

        sm_config = self._session.xenapi.VDI.get_sm_config(vdi_ref) 

        if sm_config.has_key('paused'): 

            util.SMlog("Found paused key, aborting") 

            self._session.xenapi.VDI.remove_from_sm_config(vdi_ref, host_key) 

            return False 

        util.SMlog("Activate lock succeeded") 

        return True 

 

    def _check_tag(self, vdi_uuid): 

        vdi_ref = self._session.xenapi.VDI.get_by_uuid(vdi_uuid) 

        sm_config = self._session.xenapi.VDI.get_sm_config(vdi_ref) 

        if sm_config.has_key('paused'): 

            util.SMlog("Paused key found [%s]" % sm_config) 

            return False 

        return True 

 

    def _remove_tag(self, vdi_uuid): 

        vdi_ref = self._session.xenapi.VDI.get_by_uuid(vdi_uuid) 

        host_ref = self._session.xenapi.host.get_by_uuid(util.get_this_host()) 

        sm_config = self._session.xenapi.VDI.get_sm_config(vdi_ref) 

        host_key = "host_%s" % host_ref 

        if sm_config.has_key(host_key): 

            self._session.xenapi.VDI.remove_from_sm_config(vdi_ref, host_key) 

            util.SMlog("Removed host key %s for %s" % (host_key, vdi_uuid)) 

        else: 

            util.SMlog("_remove_tag: host key %s not found, ignore" % host_key) 

 

    def _get_pool_config(self, pool_name): 

        pool_info = dict() 

        vdi_ref = self.target.vdi.sr.srcmd.params.get('vdi_ref') 

        if not vdi_ref: 

            # attach_from_config context: HA disks don't need to be in any  

            # special pool 

            return pool_info 

        session = XenAPI.xapi_local() 

        session.xenapi.login_with_password('root', '', '', 'SM') 

        sr_ref = self.target.vdi.sr.srcmd.params.get('sr_ref') 

        sr_config = session.xenapi.SR.get_other_config(sr_ref) 

        vdi_config = session.xenapi.VDI.get_other_config(vdi_ref) 

        pool_size_str = sr_config.get(POOL_SIZE_KEY) 

        pool_name_override = vdi_config.get(POOL_NAME_KEY) 

        if pool_name_override: 

            pool_name = pool_name_override 

            pool_size_override = vdi_config.get(POOL_SIZE_KEY) 

            if pool_size_override: 

                pool_size_str = pool_size_override 

        pool_size = 0 

        if pool_size_str: 

            try: 

                pool_size = int(pool_size_str) 

                if pool_size < 1 or pool_size > MAX_FULL_RINGS: 

                    raise ValueError("outside of range") 

                pool_size = NUM_PAGES_PER_RING * pool_size 

            except ValueError: 

                util.SMlog("Error: invalid mem-pool-size %s" % pool_size_str) 

                pool_size = 0 

 

        pool_info["mem-pool"] = pool_name 

        if pool_size: 

            pool_info["mem-pool-size"] = str(pool_size) 

 

        session.xenapi.session.logout() 

        return pool_info 

 

    def linkNBD(self, sr_uuid, vdi_uuid): 

        if self.tap: 

            nbd_path = '/run/blktap-control/nbd%d.%d' % (int(self.tap.pid), 

                                                         int(self.tap.minor)) 

            VDI.NBDLink.from_uuid(sr_uuid, vdi_uuid).mklink(nbd_path) 

 

    def attach(self, sr_uuid, vdi_uuid, writable, activate = False, caching_params = {}): 

        """Return/dev/sm/backend symlink path""" 

        self.xenstore_data.update(self._get_pool_config(sr_uuid)) 

        if not self.target.has_cap("ATOMIC_PAUSE") or activate: 

            util.SMlog("Attach & activate") 

            self._attach(sr_uuid, vdi_uuid) 

            dev_path = self._activate(sr_uuid, vdi_uuid, 

                    {"rdonly": not writable}) 

            self.BackendLink.from_uuid(sr_uuid, vdi_uuid).mklink(dev_path) 

            self.linkNBD(sr_uuid, vdi_uuid) 

 

        # Return backend/ link 

        back_path = self.BackendLink.from_uuid(sr_uuid, vdi_uuid).path() 

        if self.tap_wanted(): 

            # Only have NBD if we also have a tap 

            nbd_path =\ 

                       "nbd:unix:" + VDI.NBDLink.from_uuid(sr_uuid, vdi_uuid).path() 

        else: 

            nbd_path = "" 

 

        options = {"rdonly": not writable} 

        options.update(caching_params) 

        o_direct, o_direct_reason = self.get_o_direct_capability(options) 

        struct = {'params': back_path, 

                  'params_nbd': nbd_path, 

                  'o_direct': o_direct, 

                  'o_direct_reason': o_direct_reason, 

                  'xenstore_data': self.xenstore_data} 

        util.SMlog('result: %s' % struct) 

 

        try: 

            f=open("%s.attach_info" % back_path, 'a') 

            f.write(xmlrpclib.dumps((struct,), "", True)) 

            f.close() 

        except: 

            pass 

 

        return xmlrpclib.dumps((struct,), "", True) 

 

    def activate(self, sr_uuid, vdi_uuid, writable, caching_params): 

        util.SMlog("blktap2.activate") 

        options = {"rdonly": not writable} 

        options.update(caching_params) 

 

        sr_ref = self.target.vdi.sr.srcmd.params.get('sr_ref') 

        sr_other_config = self._session.xenapi.SR.get_other_config(sr_ref) 

        timeout = nfs.get_nfs_timeout(sr_other_config) 

        if timeout: 

            # Note NFS timeout values are in deciseconds 

            timeout = int((timeout+5) / 10) 

            options["timeout"] = timeout + self.TAPDISK_TIMEOUT_MARGIN 

        for i in range(self.ATTACH_DETACH_RETRY_SECS): 

            try: 

                if self._activate_locked(sr_uuid, vdi_uuid, options): 

                    return 

            except util.SRBusyException: 

                util.SMlog("SR locked, retrying") 

            time.sleep(1) 

        raise util.SMException("VDI %s locked" % vdi_uuid) 

 

    @locking("VDIUnavailable") 

    def _activate_locked(self, sr_uuid, vdi_uuid, options): 

        """Wraps target.activate and adds a tapdisk""" 

        import VDI as sm 

 

        #util.SMlog("VDI.activate %s" % vdi_uuid) 

        if self.tap_wanted(): 

            if not self._add_tag(vdi_uuid, not options["rdonly"]): 

                return False 

            # it is possible that while the VDI was paused some of its  

            # attributes have changed (e.g. its size if it was inflated; or its  

            # path if it was leaf-coalesced onto a raw LV), so refresh the  

            # object completely 

            params = self.target.vdi.sr.srcmd.params 

            target = sm.VDI.from_uuid(self.target.vdi.session, vdi_uuid) 

            target.sr.srcmd.params = params 

            driver_info = target.sr.srcmd.driver_info 

            self.target = self.TargetDriver(target, driver_info) 

 

        try: 

            util.fistpoint.activate_custom_fn( 

                    "blktap_activate_inject_failure", 

                    lambda: util.inject_failure()) 

 

            # Attach the physical node 

            if self.target.has_cap("ATOMIC_PAUSE"): 

                self._attach(sr_uuid, vdi_uuid) 

 

            vdi_type = self.target.get_vdi_type() 

 

            # Take lvchange-p Lock before running 

            # tap-ctl open 

            # Needed to avoid race with lvchange -p which is 

            # now taking the same lock 

            # This is a fix for CA-155766 

            if hasattr(self.target.vdi.sr, 'DRIVER_TYPE') and \ 

               self.target.vdi.sr.DRIVER_TYPE == 'lvhd' and \ 

               vdi_type == vhdutil.VDI_TYPE_VHD: 

                lock = Lock("lvchange-p", lvhdutil.NS_PREFIX_LVM + sr_uuid) 

                lock.acquire() 

 

            # When we attach a static VDI for HA, we cannot communicate with 

            # xapi, because has not started yet. These VDIs are raw. 

            if vdi_type != vhdutil.VDI_TYPE_RAW: 

                session = self.target.vdi.session 

                vdi_ref = session.xenapi.VDI.get_by_uuid(vdi_uuid) 

                sm_config = session.xenapi.VDI.get_sm_config(vdi_ref) 

                if 'key_hash' in sm_config: 

                    key_hash = sm_config['key_hash'] 

                    options['key_hash'] = key_hash 

                    options['vdi_uuid'] = vdi_uuid 

                    util.SMlog('Using key with hash {} for VDI {}'.format(key_hash, vdi_uuid)) 

            # Activate the physical node 

            dev_path = self._activate(sr_uuid, vdi_uuid, options) 

 

            if hasattr(self.target.vdi.sr, 'DRIVER_TYPE') and \ 

               self.target.vdi.sr.DRIVER_TYPE == 'lvhd' and \ 

               self.target.get_vdi_type() == vhdutil.VDI_TYPE_VHD: 

                lock.release() 

        except: 

            util.SMlog("Exception in activate/attach") 

            if self.tap_wanted(): 

                util.fistpoint.activate_custom_fn( 

                        "blktap_activate_error_handling", 

                        lambda: time.sleep(30)) 

                while True: 

                    try: 

                        self._remove_tag(vdi_uuid) 

                        break 

                    except xmlrpclib.ProtocolError, e: 

                        # If there's a connection error, keep trying forever. 

                        if e.errcode == httplib.INTERNAL_SERVER_ERROR: 

                            continue 

                        else: 

                            util.SMlog('failed to remove tag: %s' % e) 

                            break 

                    except Exception, e: 

                        util.SMlog('failed to remove tag: %s' % e) 

                        break 

            raise 

 

        # Link result to backend/ 

        self.BackendLink.from_uuid(sr_uuid, vdi_uuid).mklink(dev_path) 

        self.linkNBD(sr_uuid, vdi_uuid) 

        return True 

 

    def _activate(self, sr_uuid, vdi_uuid, options): 

        vdi_options = self.target.activate(sr_uuid, vdi_uuid) 

 

        dev_path = self.setup_cache(sr_uuid, vdi_uuid, options) 

        if not dev_path: 

            phy_path = self.PhyLink.from_uuid(sr_uuid, vdi_uuid).readlink() 

            # Maybe launch a tapdisk on the physical link 

            if self.tap_wanted(): 

                vdi_type = self.target.get_vdi_type() 

                options["o_direct"] = self.get_o_direct_capability(options)[0] 

                if vdi_options: 

                    options.update(vdi_options) 

                dev_path, self.tap = self._tap_activate(phy_path, vdi_type, 

                        sr_uuid, options, 

                        self._get_pool_config(sr_uuid).get("mem-pool-size")) 

            else: 

                dev_path = phy_path # Just reuse phy 

 

        return dev_path 

 

    def _attach(self, sr_uuid, vdi_uuid): 

        attach_info = xmlrpclib.loads(self.target.attach(sr_uuid, vdi_uuid))[0][0] 

        params = attach_info['params'] 

        xenstore_data = attach_info['xenstore_data'] 

        phy_path = util.to_plain_string(params) 

        self.xenstore_data.update(xenstore_data) 

        # Save it to phy/ 

        self.PhyLink.from_uuid(sr_uuid, vdi_uuid).mklink(phy_path) 

 

    def deactivate(self, sr_uuid, vdi_uuid, caching_params): 

        util.SMlog("blktap2.deactivate") 

        for i in range(self.ATTACH_DETACH_RETRY_SECS): 

            try: 

                if self._deactivate_locked(sr_uuid, vdi_uuid, caching_params): 

                    return 

            except util.SRBusyException, e: 

                util.SMlog("SR locked, retrying") 

            time.sleep(1) 

        raise util.SMException("VDI %s locked" % vdi_uuid) 

 

    @locking("VDIUnavailable") 

    def _deactivate_locked(self, sr_uuid, vdi_uuid, caching_params): 

        """Wraps target.deactivate and removes a tapdisk""" 

 

        #util.SMlog("VDI.deactivate %s" % vdi_uuid) 

        if self.tap_wanted() and not self._check_tag(vdi_uuid): 

            return False 

 

        self._deactivate(sr_uuid, vdi_uuid, caching_params) 

        if self.target.has_cap("ATOMIC_PAUSE"): 

            self._detach(sr_uuid, vdi_uuid) 

        if self.tap_wanted(): 

            self._remove_tag(vdi_uuid) 

 

        return True 

 

    def _resetPhylink(self, sr_uuid, vdi_uuid, path): 

        self.PhyLink.from_uuid(sr_uuid, vdi_uuid).mklink(path) 

 

    def detach(self, sr_uuid, vdi_uuid, deactivate = False, caching_params = {}): 

        if not self.target.has_cap("ATOMIC_PAUSE") or deactivate: 

            util.SMlog("Deactivate & detach") 

            self._deactivate(sr_uuid, vdi_uuid, caching_params) 

            self._detach(sr_uuid, vdi_uuid) 

        else: 

            pass # nothing to do 

 

    def _deactivate(self, sr_uuid, vdi_uuid, caching_params): 

        import VDI as sm 

 

        # Shutdown tapdisk 

        back_link = self.BackendLink.from_uuid(sr_uuid, vdi_uuid) 

 

        if not util.pathexists(back_link.path()): 

            util.SMlog("Backend path %s does not exist" % back_link.path()) 

            return 

 

        try: 

            attach_info_path = "%s.attach_info" % (back_link.path()) 

            os.unlink(attach_info_path) 

        except: 

            util.SMlog("unlink of attach_info failed") 

 

        try: 

            major, minor = back_link.rdev() 

        except self.DeviceNode.NotABlockDevice: 

            pass 

        else: 

            if major == Tapdisk.major(): 

                self._tap_deactivate(minor) 

                self.remove_cache(sr_uuid, vdi_uuid, caching_params) 

 

        # Remove the backend link 

        back_link.unlink() 

        VDI.NBDLink.from_uuid(sr_uuid, vdi_uuid).unlink() 

 

        # Deactivate & detach the physical node 

        if self.tap_wanted() and self.target.vdi.session is not None: 

            # it is possible that while the VDI was paused some of its  

            # attributes have changed (e.g. its size if it was inflated; or its  

            # path if it was leaf-coalesced onto a raw LV), so refresh the  

            # object completely 

            target = sm.VDI.from_uuid(self.target.vdi.session, vdi_uuid) 

            driver_info = target.sr.srcmd.driver_info 

            self.target = self.TargetDriver(target, driver_info) 

 

        self.target.deactivate(sr_uuid, vdi_uuid) 

 

    def _detach(self, sr_uuid, vdi_uuid): 

        self.target.detach(sr_uuid, vdi_uuid) 

 

        # Remove phy/ 

        self.PhyLink.from_uuid(sr_uuid, vdi_uuid).unlink() 

 

    def _updateCacheRecord(self, session, vdi_uuid, on_boot, caching): 

        # Remove existing VDI.sm_config fields 

        vdi_ref = session.xenapi.VDI.get_by_uuid(vdi_uuid) 

        for key in ["on_boot", "caching"]: 

            session.xenapi.VDI.remove_from_sm_config(vdi_ref,key) 

        if not on_boot is None: 

            session.xenapi.VDI.add_to_sm_config(vdi_ref,'on_boot',on_boot) 

        if not caching is None: 

            session.xenapi.VDI.add_to_sm_config(vdi_ref,'caching',caching) 

 

    def setup_cache(self, sr_uuid, vdi_uuid, params): 

        if params.get(self.CONF_KEY_ALLOW_CACHING) != "true": 

            return 

 

        util.SMlog("Requested local caching") 

        if not self.target.has_cap("SR_CACHING"): 

            util.SMlog("Error: local caching not supported by this SR") 

            return 

 

        scratch_mode = False 

        if params.get(self.CONF_KEY_MODE_ON_BOOT) == "reset": 

            scratch_mode = True 

            util.SMlog("Requested scratch mode") 

            if not self.target.has_cap("VDI_RESET_ON_BOOT/2"): 

                util.SMlog("Error: scratch mode not supported by this SR") 

                return 

 

        dev_path = None 

        local_sr_uuid = params.get(self.CONF_KEY_CACHE_SR) 

        if not local_sr_uuid: 

            util.SMlog("ERROR: Local cache SR not specified, not enabling") 

            return 

        dev_path = self._setup_cache(self._session, sr_uuid, vdi_uuid, 

                local_sr_uuid, scratch_mode, params) 

 

        if dev_path: 

            self._updateCacheRecord(self._session, self.target.vdi.uuid, 

                    params.get(self.CONF_KEY_MODE_ON_BOOT), 

                    params.get(self.CONF_KEY_ALLOW_CACHING)) 

 

        return dev_path 

 

    def alert_no_cache(self, session, vdi_uuid, cache_sr_uuid, err): 

        vm_uuid = None 

        vm_label = "" 

        try: 

            cache_sr_ref = session.xenapi.SR.get_by_uuid(cache_sr_uuid) 

            cache_sr_rec = session.xenapi.SR.get_record(cache_sr_ref) 

            cache_sr_label = cache_sr_rec.get("name_label") 

 

            host_ref = session.xenapi.host.get_by_uuid(util.get_this_host()) 

            host_rec = session.xenapi.host.get_record(host_ref) 

            host_label = host_rec.get("name_label") 

 

            vdi_ref = session.xenapi.VDI.get_by_uuid(vdi_uuid) 

            vbds = session.xenapi.VBD.get_all_records_where( \ 

                    "field \"VDI\" = \"%s\"" % vdi_ref) 

            for vbd_rec in vbds.values(): 

                vm_ref = vbd_rec.get("VM") 

                vm_rec = session.xenapi.VM.get_record(vm_ref) 

                vm_uuid = vm_rec.get("uuid") 

                vm_label = vm_rec.get("name_label") 

        except: 

            util.logException("alert_no_cache") 

 

        alert_obj = "SR" 

        alert_uuid = str(cache_sr_uuid) 

        alert_str = "No space left in Local Cache SR %s" % cache_sr_uuid 

        if vm_uuid: 

            alert_obj = "VM" 

            alert_uuid = vm_uuid 

            reason = "" 

            if err == errno.ENOSPC: 

                reason = "because there is no space left" 

            alert_str = "The VM \"%s\" is not using IntelliCache %s on the Local Cache SR (\"%s\") on host \"%s\"" % \ 

                    (vm_label, reason, cache_sr_label, host_label) 

 

        util.SMlog("Creating alert: (%s, %s, \"%s\")" % \ 

                (alert_obj, alert_uuid, alert_str)) 

        session.xenapi.message.create("No space left in local cache", "3", 

                alert_obj, alert_uuid, alert_str) 

 

    def _setup_cache(self, session, sr_uuid, vdi_uuid, local_sr_uuid, 

            scratch_mode, options): 

        import SR 

        import EXTSR 

        import NFSSR 

        import XenAPI 

        from lock import Lock 

        from FileSR import FileVDI 

 

        parent_uuid = vhdutil.getParent(self.target.vdi.path, 

                FileVDI.extractUuid) 

        if not parent_uuid: 

            util.SMlog("ERROR: VDI %s has no parent, not enabling" % \ 

                    self.target.vdi.uuid) 

            return 

 

        util.SMlog("Setting up cache") 

        parent_uuid = parent_uuid.strip() 

        shared_target = NFSSR.NFSFileVDI(self.target.vdi.sr, parent_uuid) 

 

        if shared_target.parent: 

            util.SMlog("ERROR: Parent VDI %s has parent, not enabling" % 

                       shared_target.uuid) 

            return 

 

        SR.registerSR(EXTSR.EXTSR) 

        local_sr = SR.SR.from_uuid(session, local_sr_uuid) 

 

        lock = Lock(self.LOCK_CACHE_SETUP, parent_uuid) 

        lock.acquire() 

 

        # read cache 

        read_cache_path = "%s/%s.vhdcache" % (local_sr.path, shared_target.uuid) 

        if util.pathexists(read_cache_path): 

            util.SMlog("Read cache node (%s) already exists, not creating" % \ 

                    read_cache_path) 

        else: 

            try: 

                vhdutil.snapshot(read_cache_path, shared_target.path, False) 

            except util.CommandException, e: 

                util.SMlog("Error creating parent cache: %s" % e) 

                self.alert_no_cache(session, vdi_uuid, local_sr_uuid, e.code) 

                return None 

 

        # local write node 

        leaf_size = vhdutil.getSizeVirt(self.target.vdi.path) 

        local_leaf_path = "%s/%s.vhdcache" % \ 

                (local_sr.path, self.target.vdi.uuid) 

        if util.pathexists(local_leaf_path): 

            util.SMlog("Local leaf node (%s) already exists, deleting" % \ 

                    local_leaf_path) 

            os.unlink(local_leaf_path) 

        try: 

            vhdutil.snapshot(local_leaf_path, read_cache_path, False, 

                    msize = leaf_size / 1024 / 1024, checkEmpty = False) 

        except util.CommandException, e: 

            util.SMlog("Error creating leaf cache: %s" % e) 

            self.alert_no_cache(session, vdi_uuid, local_sr_uuid, e.code) 

            return None 

 

        local_leaf_size = vhdutil.getSizeVirt(local_leaf_path) 

        if leaf_size > local_leaf_size: 

            util.SMlog("Leaf size %d > local leaf cache size %d, resizing" % 

                    (leaf_size, local_leaf_size)) 

            vhdutil.setSizeVirtFast(local_leaf_path, leaf_size) 

 

        vdi_type = self.target.get_vdi_type() 

 

        prt_tapdisk = Tapdisk.find_by_path(read_cache_path) 

        if not prt_tapdisk: 

            parent_options = copy.deepcopy(options) 

            parent_options["rdonly"] = False 

            parent_options["lcache"] = True 

 

            blktap = Blktap.allocate() 

            try: 

                blktap.set_pool_name("lcache-parent-pool-%s" % blktap.minor) 

                # no need to change pool_size since each parent tapdisk is in  

                # its own pool 

                prt_tapdisk = \ 

                    Tapdisk.launch_on_tap(blktap, read_cache_path, 

                            'vhd', parent_options) 

            except: 

                blktap.free() 

                raise 

 

        secondary = "%s:%s" % (self.target.get_vdi_type(), 

                self.PhyLink.from_uuid(sr_uuid, vdi_uuid).readlink()) 

 

        util.SMlog("Parent tapdisk: %s" % prt_tapdisk) 

        leaf_tapdisk = Tapdisk.find_by_path(local_leaf_path) 

        if not leaf_tapdisk: 

            blktap = Blktap.allocate() 

            child_options = copy.deepcopy(options) 

            child_options["rdonly"] = False 

            child_options["lcache"] = False 

            child_options["existing_prt"] = prt_tapdisk.minor 

            child_options["secondary"] = secondary 

            child_options["standby"] = scratch_mode 

            try: 

                leaf_tapdisk = \ 

                    Tapdisk.launch_on_tap(blktap, local_leaf_path, 

                            'vhd', child_options) 

            except: 

                blktap.free() 

                raise 

 

        lock.release() 

 

        util.SMlog("Local read cache: %s, local leaf: %s" % \ 

                (read_cache_path, local_leaf_path)) 

 

        self.tap = leaf_tapdisk 

        return leaf_tapdisk.get_devpath() 

 

    def remove_cache(self, sr_uuid, vdi_uuid, params): 

        if not self.target.has_cap("SR_CACHING"): 

            return 

 

        caching = params.get(self.CONF_KEY_ALLOW_CACHING) == "true" 

 

        local_sr_uuid = params.get(self.CONF_KEY_CACHE_SR) 

        if caching and not local_sr_uuid: 

            util.SMlog("ERROR: Local cache SR not specified, ignore") 

            return 

 

        if caching: 

            self._remove_cache(self._session, local_sr_uuid) 

 

        if self._session is not None: 

            self._updateCacheRecord(self._session, self.target.vdi.uuid, None, None) 

 

 

    def _is_tapdisk_in_use(self, minor): 

        (retVal, links) = util.findRunningProcessOrOpenFile("tapdisk") 

        if not retVal: 

            # err on the side of caution 

            return True 

 

        for link in links: 

            if link.find("tapdev%d" % minor) != -1: 

                return True 

        return False 

 

    def _remove_cache(self, session, local_sr_uuid): 

        import SR 

        import EXTSR 

        import NFSSR 

        import XenAPI 

        from lock import Lock 

        from FileSR import FileVDI 

 

        parent_uuid = vhdutil.getParent(self.target.vdi.path, 

                FileVDI.extractUuid) 

        if not parent_uuid: 

            util.SMlog("ERROR: No parent for VDI %s, ignore" % \ 

                    self.target.vdi.uuid) 

            return 

 

        util.SMlog("Tearing down the cache") 

 

        parent_uuid = parent_uuid.strip() 

        shared_target = NFSSR.NFSFileVDI(self.target.vdi.sr, parent_uuid) 

 

        SR.registerSR(EXTSR.EXTSR) 

        local_sr = SR.SR.from_uuid(session, local_sr_uuid) 

 

        lock = Lock(self.LOCK_CACHE_SETUP, parent_uuid) 

        lock.acquire() 

 

        # local write node 

        local_leaf_path = "%s/%s.vhdcache" % \ 

                (local_sr.path, self.target.vdi.uuid) 

        if util.pathexists(local_leaf_path): 

            util.SMlog("Deleting local leaf node %s" % local_leaf_path) 

            os.unlink(local_leaf_path) 

 

 

        read_cache_path = "%s/%s.vhdcache" % (local_sr.path, shared_target.uuid) 

        prt_tapdisk = Tapdisk.find_by_path(read_cache_path) 

        if not prt_tapdisk: 

            util.SMlog("Parent tapdisk not found") 

        elif not self._is_tapdisk_in_use(prt_tapdisk.minor): 

            util.SMlog("Parent tapdisk not in use: shutting down %s" % \ 

                    read_cache_path) 

            try: 

                prt_tapdisk.shutdown() 

            except: 

                util.logException("shutting down parent tapdisk") 

        else: 

            util.SMlog("Parent tapdisk still in use: %s" % read_cache_path) 

 

        # the parent cache files are removed during the local SR's background  

        # GC run 

 

        lock.release() 

 

PythonKeyError = KeyError 

 

class UEventHandler(object): 

 

    def __init__(self): 

        self._action = None 

 

    class KeyError(PythonKeyError): 

        def __str__(self): 

            return \ 

                "Key '%s' missing in environment. " % self.args[0] + \ 

                "Not called in udev context?" 

 

    @classmethod 

    def getenv(cls, key): 

        try: 

            return os.environ[key] 

        except KeyError, e: 

            raise cls.KeyError(e.args[0]) 

 

    def get_action(self): 

        if not self._action: 

            self._action = self.getenv('ACTION') 

        return self._action 

 

    class UnhandledEvent(Exception): 

 

        def __init__(self, event, handler): 

            self.event   = event 

            self.handler = handler 

 

        def __str__(self): 

            return "Uevent '%s' not handled by %s" % \ 

                (self.event, self.handler.__class__.__name__) 

 

    ACTIONS = {} 

 

    def run(self): 

 

        action = self.get_action() 

        try: 

            fn = self.ACTIONS[action] 

        except KeyError: 

            raise self.UnhandledEvent(action, self) 

 

        return fn(self) 

 

    def __str__(self): 

        try:    action = self.get_action() 

        except: action = None 

        return "%s[%s]" % (self.__class__.__name__, action) 

 

class __BlktapControl(ClassDevice): 

    SYSFS_CLASSTYPE = "misc" 

 

    def __init__(self): 

        ClassDevice.__init__(self) 

        self._default_pool = None 

 

    def sysfs_devname(self): 

        return "blktap!control" 

 

    class DefaultPool(Attribute): 

        SYSFS_NODENAME = "default_pool" 

 

    def get_default_pool_attr(self): 

        if not self._default_pool: 

            self._default_pool = self.DefaultPool.from_kobject(self) 

        return self._default_pool 

 

    def get_default_pool_name(self): 

        return self.get_default_pool_attr().readline() 

 

    def set_default_pool_name(self, name): 

        self.get_default_pool_attr().writeline(name) 

 

    def get_default_pool(self): 

        return BlktapControl.get_pool(self.get_default_pool_name()) 

 

    def set_default_pool(self, pool): 

        self.set_default_pool_name(pool.name) 

 

    class NoSuchPool(Exception): 

        def __init__(self, name): 

            self.name = name 

 

        def __str__(self): 

            return "No such pool: %s", self.name 

 

    def get_pool(self, name): 

        path = "%s/pools/%s" % (self.sysfs_path(), name) 

 

        if not os.path.isdir(path): 

            raise self.NoSuchPool(name) 

 

        return PagePool(path) 

 

BlktapControl = __BlktapControl() 

 

class PagePool(KObject): 

 

    def __init__(self, path): 

        self.path = path 

        self._size = None 

 

    def sysfs_path(self): 

        return self.path 

 

    class Size(Attribute): 

        SYSFS_NODENAME = "size" 

 

    def get_size_attr(self): 

        if not self._size: 

            self._size = self.Size.from_kobject(self) 

        return self._size 

 

    def set_size(self, pages): 

        pages = str(pages) 

        self.get_size_attr().writeline(pages) 

 

    def get_size(self): 

        pages = self.get_size_attr().readline() 

        return int(pages) 

 

class BusDevice(KObject): 

 

    SYSFS_BUSTYPE = None 

 

    @classmethod 

    def sysfs_bus_path(cls): 

        return "/sys/bus/%s" % cls.SYSFS_BUSTYPE 

 

    def sysfs_path(self): 

        path = "%s/devices/%s" % (self.sysfs_bus_path(), 

                                  self.sysfs_devname()) 

 

        return path 

 

class XenbusDevice(BusDevice): 

    """Xenbus device, in XS and sysfs""" 

 

    XBT_NIL = "" 

 

    XENBUS_DEVTYPE = None 

 

    def __init__(self, domid, devid): 

        self.domid = int(domid) 

        self.devid = int(devid) 

        self._xbt  = XenbusDevice.XBT_NIL 

 

        import xen.lowlevel.xs 

        self.xs = xen.lowlevel.xs.xs() 

 

    def xs_path(self, key=None): 

        path = "backend/%s/%d/%d" % (self.XENBUS_DEVTYPE, 

                                     self.domid, 

                                     self.devid) 

        if key is not None: 

            path = "%s/%s" % (path, key) 

 

        return path 

 

    def _log(self, prio, msg): 

        syslog(prio, msg) 

 

    def info(self, msg): 

        self._log(_syslog.LOG_INFO, msg) 

 

    def warn(self, msg): 

        self._log(_syslog.LOG_WARNING, "WARNING: " + msg) 

 

    def _xs_read_path(self, path): 

        val = self.xs.read(self._xbt, path) 

        #self.info("read %s = '%s'" % (path, val)) 

        return val 

 

    def _xs_write_path(self, path, val): 

        self.xs.write(self._xbt, path, val); 

        self.info("wrote %s = '%s'" % (path, val)) 

 

    def _xs_rm_path(self, path): 

        self.xs.rm(self._xbt, path) 

        self.info("removed %s" % path) 

 

    def read(self, key): 

        return self._xs_read_path(self.xs_path(key)) 

 

    def has_key(self, key): 

        return self.read(key) is not None 

 

    def write(self, key, val): 

        self._xs_write_path(self.xs_path(key), val) 

 

    def rm(self, key): 

        self._xs_rm_path(self.xs_path(key)) 

 

    def exists(self): 

        return self.has_key(None) 

 

    def begin(self): 

        assert(self._xbt == XenbusDevice.XBT_NIL) 

        self._xbt = self.xs.transaction_start() 

 

    def commit(self): 

        ok = self.xs.transaction_end(self._xbt, 0) 

        self._xbt = XenbusDevice.XBT_NIL 

        return ok 

 

    def abort(self): 

        ok = self.xs.transaction_end(self._xbt, 1) 

        assert(ok == True) 

        self._xbt = XenbusDevice.XBT_NIL 

 

    def create_physical_device(self): 

        """The standard protocol is: toolstack writes 'params', linux hotplug 

        script translates this into physical-device=%x:%x""" 

        if self.has_key("physical-device"): 

            return 

        try: 

            params = self.read("params") 

            frontend = self.read("frontend") 

            is_cdrom = self._xs_read_path("%s/device-type") == "cdrom" 

            # We don't have PV drivers for CDROM devices, so we prevent blkback 

            # from opening the physical-device 

            if not(is_cdrom): 

                major_minor = os.stat(params).st_rdev 

                major, minor = divmod(major_minor, 256) 

                self.write("physical-device", "%x:%x" % (major, minor)) 

        except: 

            util.logException("BLKTAP2:create_physical_device") 

 

    def signal_hotplug(self, online=True): 

        xapi_path = "/xapi/%d/hotplug/%s/%d/hotplug" % (self.domid, 

                                                   self.XENBUS_DEVTYPE, 

                                                   self.devid) 

        upstream_path = self.xs_path("hotplug-status") 

        if online: 

            self._xs_write_path(xapi_path, "online") 

            self._xs_write_path(upstream_path, "connected") 

        else: 

            self._xs_rm_path(xapi_path) 

            self._xs_rm_path(upstream_path) 

 

    def sysfs_devname(self): 

        return "%s-%d-%d" % (self.XENBUS_DEVTYPE, 

                             self.domid, self.devid) 

 

    def __str__(self): 

        return self.sysfs_devname() 

 

    @classmethod 

    def find(cls): 

        pattern = "/sys/bus/%s/devices/%s*" % (cls.SYSFS_BUSTYPE, 

                                               cls.XENBUS_DEVTYPE) 

        for path in glob.glob(pattern): 

 

            name = os.path.basename(path) 

            (_type, domid, devid) = name.split('-') 

 

            yield cls(domid, devid) 

 

class XenBackendDevice(XenbusDevice): 

    """Xenbus backend device""" 

    SYSFS_BUSTYPE = "xen-backend" 

 

    @classmethod 

    def from_xs_path(cls, _path): 

        (_backend, _type, domid, devid) = _path.split('/') 

 

        assert _backend == 'backend' 

        assert _type == cls.XENBUS_DEVTYPE 

 

        domid = int(domid) 

        devid = int(devid) 

 

        return cls(domid, devid) 

 

class Blkback(XenBackendDevice): 

    """A blkback VBD""" 

 

    XENBUS_DEVTYPE = "vbd" 

 

    def __init__(self, domid, devid): 

        XenBackendDevice.__init__(self, domid, devid) 

        self._phy      = None 

        self._vdi_uuid = None 

        self._q_state  = None 

        self._q_events = None 

 

    class XenstoreValueError(Exception): 

        KEY = None 

        def __init__(self, vbd, _str): 

            self.vbd = vbd 

            self.str = _str 

 

        def __str__(self): 

            return "Backend %s " % self.vbd + \ 

                "has %s = %s" % (self.KEY, self.str) 

 

    class PhysicalDeviceError(XenstoreValueError): 

        KEY = "physical-device" 

 

    class PhysicalDevice(object): 

 

        def __init__(self, major, minor): 

            self.major = int(major) 

            self.minor = int(minor) 

 

        @classmethod 

        def from_xbdev(cls, xbdev): 

 

            phy = xbdev.read("physical-device") 

 

            try: 

                major, minor = phy.split(':') 

                major = int(major, 0x10) 

                minor = int(minor, 0x10) 

            except Exception, e: 

                raise xbdev.PhysicalDeviceError(xbdev, phy) 

 

            return cls(major, minor) 

 

        def makedev(self): 

            return os.makedev(self.major, self.minor) 

 

        def is_tap(self): 

            return self.major == Tapdisk.major() 

 

        def __str__(self): 

            return "%s:%s" % (self.major, self.minor) 

 

        def __eq__(self, other): 

            return \ 

                self.major == other.major and \ 

                self.minor == other.minor 

 

    def get_physical_device(self): 

        if not self._phy: 

            self._phy = self.PhysicalDevice.from_xbdev(self) 

        return self._phy 

 

    class QueueEvents(Attribute): 

        """Blkback sysfs node to select queue-state event 

        notifications emitted.""" 

 

        SYSFS_NODENAME = "queue_events" 

 

        QUEUE_RUNNING           = (1<<0) 

        QUEUE_PAUSE_DONE        = (1<<1) 

        QUEUE_SHUTDOWN_DONE     = (1<<2) 

        QUEUE_PAUSE_REQUEST     = (1<<3) 

        QUEUE_SHUTDOWN_REQUEST  = (1<<4) 

 

        def get_mask(self): 

            return int(self.readline(), 0x10) 

 

        def set_mask(self, mask): 

            self.writeline("0x%x" % mask) 

 

    def get_queue_events(self): 

        if not self._q_events: 

            self._q_events = self.QueueEvents.from_kobject(self) 

        return self._q_events 

 

    def get_vdi_uuid(self): 

        if not self._vdi_uuid: 

            self._vdi_uuid = self.read("sm-data/vdi-uuid") 

        return self._vdi_uuid 

 

    def pause_requested(self): 

        return self.has_key("pause") 

 

    def shutdown_requested(self): 

        return self.has_key("shutdown-request") 

 

    def shutdown_done(self): 

        return self.has_key("shutdown-done") 

 

    def running(self): 

        return self.has_key('queue-0/kthread-pid') 

 

    @classmethod 

    def find_by_physical_device(cls, phy): 

        for dev in cls.find(): 

            try: 

                _phy = dev.get_physical_device() 

            except cls.PhysicalDeviceError: 

                continue 

 

            if _phy == phy: 

                yield dev 

 

    @classmethod 

    def find_by_tap_minor(cls, minor): 

        phy = cls.PhysicalDevice(Tapdisk.major(), minor) 

        return cls.find_by_physical_device(phy) 

 

    @classmethod 

    def find_by_tap(cls, tapdisk): 

        return cls.find_by_tap_minor(tapdisk.minor) 

 

    def has_tap(self): 

 

        if not self.can_tap(): 

            return False 

 

        phy = self.get_physical_device() 

        if phy: 

            return phy.is_tap() 

 

        return False 

 

    def is_bare_hvm(self): 

        """File VDIs for bare HVM. These are directly accessible by Qemu.""" 

        try: 

            self.get_physical_device() 

 

        except self.PhysicalDeviceError, e: 

            vdi_type = self.read("type") 

 

            self.info("HVM VDI: type=%s" % vdi_type) 

 

            if e.str is not None or vdi_type != 'file': 

                raise 

 

            return True 

 

        return False 

 

    def can_tap(self): 

        return not self.is_bare_hvm() 

 

 

class BlkbackEventHandler(UEventHandler): 

 

    LOG_FACILITY = _syslog.LOG_DAEMON 

 

    def __init__(self, ident=None, action=None): 

        if not ident: ident = self.__class__.__name__ 

 

        self.ident    = ident 

        self._vbd     = None 

        self._tapdisk = None 

 

        UEventHandler.__init__(self) 

 

    def run(self): 

 

        self.xs_path = self.getenv('XENBUS_PATH') 

        openlog(str(self), 0, self.LOG_FACILITY) 

 

        UEventHandler.run(self) 

 

    def __str__(self): 

 

        try:    path = self.xs_path 

        except: path = None 

 

        try:    action = self.get_action() 

        except: action = None 

 

        return "%s[%s](%s)" % (self.ident, action, path) 

 

    def _log(self, prio, msg): 

        syslog(prio, msg) 

        util.SMlog("%s: " % self + msg) 

 

    def info(self, msg): 

        self._log(_syslog.LOG_INFO, msg) 

 

    def warn(self, msg): 

        self._log(_syslog.LOG_WARNING, "WARNING: " + msg) 

 

    def error(self, msg): 

        self._log(_syslog.LOG_ERR, "ERROR: " + msg) 

 

    def get_vbd(self): 

        if not self._vbd: 

            self._vbd = Blkback.from_xs_path(self.xs_path) 

        return self._vbd 

 

    def get_tapdisk(self): 

        if not self._tapdisk: 

            minor = self.get_vbd().get_physical_device().minor 

            self._tapdisk = Tapdisk.from_minor(minor) 

        return self._tapdisk 

 

    # 

    # Events 

    # 

 

    def __add(self): 

        vbd = self.get_vbd() 

 

        # Manage blkback transitions 

        # self._manage_vbd() 

 

        vbd.create_physical_device() 

 

        vbd.signal_hotplug() 

 

    @retried(backoff=.5, limit=10) 

    def add(self): 

        try: 

            self.__add() 

        except Attribute.NoSuchAttribute, e: 

            # 

            # FIXME: KOBJ_ADD is racing backend.probe, which 

            # registers device attributes. So poll a little. 

            # 

            self.warn("%s, still trying." % e) 

            raise RetryLoop.TransientFailure(e) 

 

    def __change(self): 

        vbd = self.get_vbd() 

 

        # 1. Pause or resume tapdisk (if there is one) 

 

        if vbd.has_tap(): 

            pass 

            #self._pause_update_tap() 

 

        # 2. Signal Xapi.VBD.pause/resume completion 

 

        self._signal_xapi() 

 

    def change(self): 

        vbd = self.get_vbd() 

 

        # NB. Beware of spurious change events between shutdown 

        # completion and device removal. Also, Xapi.VM.migrate will 

        # hammer a couple extra shutdown-requests into the source VBD. 

 

        while True: 

            vbd.begin() 

 

            if not vbd.exists() or \ 

                    vbd.shutdown_done(): 

                break 

 

            self.__change() 

 

            if vbd.commit(): 

                return 

 

        vbd.abort() 

        self.info("spurious uevent, ignored.") 

 

    def remove(self): 

        vbd = self.get_vbd() 

 

        vbd.signal_hotplug(False) 

 

    ACTIONS = { 'add':    add, 

                'change': change, 

                'remove': remove } 

 

    # 

    # VDI.pause 

    # 

 

    def _tap_should_pause(self): 

        """Enumerate all VBDs on our tapdisk. Returns true iff any was 

        paused""" 

 

        tapdisk  = self.get_tapdisk() 

        TapState = Tapdisk.PauseState 

 

        PAUSED          = 'P' 

        RUNNING         = 'R' 

        PAUSED_SHUTDOWN = 'P,S' 

        # NB. Shutdown/paused is special. We know it's not going 

        # to restart again, so it's a RUNNING. Still better than 

        # backtracking a removed device during Vbd.unplug completion. 

 

        next = TapState.RUNNING 

        vbds = {} 

 

        for vbd in Blkback.find_by_tap(tapdisk): 

            name = str(vbd) 

 

            pausing = vbd.pause_requested() 

            closing = vbd.shutdown_requested() 

            running = vbd.running() 

 

            if pausing: 

                if closing and not running: 

                    vbds[name] = PAUSED_SHUTDOWN 

                else: 

                    vbds[name] = PAUSED 

                    next = TapState.PAUSED 

 

            else: 

                vbds[name] = RUNNING 

 

        self.info("tapdev%d (%s): %s -> %s" 

                  % (tapdisk.minor, tapdisk.pause_state(), 

                     vbds, next)) 

 

        return next == TapState.PAUSED 

 

    def _pause_update_tap(self): 

        vbd = self.get_vbd() 

 

        if self._tap_should_pause(): 

            self._pause_tap() 

        else: 

            self._resume_tap() 

 

    def _pause_tap(self): 

        tapdisk = self.get_tapdisk() 

 

        if not tapdisk.is_paused(): 

            self.info("pausing %s" % tapdisk) 

            tapdisk.pause() 

 

    def _resume_tap(self): 

        tapdisk = self.get_tapdisk() 

 

        # NB. Raw VDI snapshots. Refresh the physical path and 

        # type while resuming. 

        vbd      = self.get_vbd() 

        vdi_uuid = vbd.get_vdi_uuid() 

 

        if tapdisk.is_paused(): 

            self.info("loading vdi uuid=%s" % vdi_uuid) 

            vdi      = VDI.from_cli(vdi_uuid) 

            _type    = vdi.get_tap_type() 

            path     = vdi.get_phy_path() 

            self.info("resuming %s on %s:%s" % (tapdisk, _type, path)) 

            tapdisk.unpause(_type, path) 

 

    # 

    # VBD.pause/shutdown 

    # 

 

    def _manage_vbd(self): 

        vbd = self.get_vbd() 

 

        # NB. Hook into VBD state transitions. 

 

        events = vbd.get_queue_events() 

 

        mask  = 0 

        mask |= events.QUEUE_PAUSE_DONE    # pause/unpause 

        mask |= events.QUEUE_SHUTDOWN_DONE # shutdown 

 

        # TODO: mask |= events.QUEUE_SHUTDOWN_REQUEST, for shutdown=force 

        # TODO: mask |= events.QUEUE_RUNNING, for ionice updates etc 

 

        events.set_mask(mask) 

        self.info("wrote %s = %#02x" % (events.path, mask)) 

 

    def _signal_xapi(self): 

        vbd = self.get_vbd() 

 

        pausing = vbd.pause_requested() 

        closing = vbd.shutdown_requested() 

        running = vbd.running() 

 

        handled = 0 

 

        if pausing and not running: 

            if not vbd.has_key('pause-done'): 

                vbd.write('pause-done', '') 

                handled += 1 

 

        if not pausing: 

            if vbd.has_key('pause-done'): 

                vbd.rm('pause-done') 

                handled += 1 

 

        if closing and not running: 

            if not vbd.has_key('shutdown-done'): 

                vbd.write('shutdown-done', '') 

                handled += 1 

 

        if handled > 1: 

            self.warn("handled %d events, " % handled + 

                      "pausing=%s closing=%s running=%s" % \ 

                          (pausing, closing, running)) 

 

2757if __name__ == '__main__': 

 

    import sys 

    prog  = os.path.basename(sys.argv[0]) 

 

    # 

    # Simple CLI interface for manual operation 

    # 

    #  tap.*  level calls go down to local Tapdisk()s (by physical path) 

    #  vdi.*  level calls run the plugin calls across host boundaries. 

    # 

 

    def usage(stream): 

        print >>stream, \ 

            "usage: %s tap.{list|major}" % prog 

        print >>stream, \ 

            "       %s tap.{launch|find|get|pause|" % prog + \ 

            "unpause|shutdown|stats} {[<tt>:]<path>} | [minor=]<int> | .. }" 

        print >>stream, \ 

            "       %s vbd.uevent" % prog 

 

    try: 

        cmd = sys.argv[1] 

    except IndexError: 

        usage(sys.stderr) 

        sys.exit(1) 

 

    try: 

        _class, method = cmd.split('.') 

    except: 

        usage(sys.stderr) 

        sys.exit(1) 

 

    # 

    # Local Tapdisks 

    # 

 

    if cmd == 'tap.major': 

 

        print "%d" % Tapdisk.major() 

 

    elif cmd == 'tap.launch': 

 

        tapdisk = Tapdisk.launch_from_arg(sys.argv[2]) 

        print >> sys.stderr, "Launched %s" % tapdisk 

 

    elif _class == 'tap': 

 

        attrs = {} 

        for item in sys.argv[2:]: 

            try: 

                key, val = item.split('=') 

                attrs[key] = val 

                continue 

            except ValueError: 

                pass 

 

            try: 

                attrs['minor'] = int(item) 

                continue 

            except ValueError: 

                pass 

 

            try: 

                arg = Tapdisk.Arg.parse(item) 

                attrs['_type'] = arg.type 

                attrs['path']  = arg.path 

                continue 

            except Tapdisk.Arg.InvalidArgument: 

                pass 

 

            attrs['path'] = item 

 

        if cmd == 'tap.list': 

 

            for tapdisk in Tapdisk.list(**attrs): 

                blktap = tapdisk.get_blktap() 

                print tapdisk, 

                print "%s: task=%s pool=%s" % \ 

                    (blktap, 

                     blktap.get_task_pid(), 

                     blktap.get_pool_name()) 

 

        elif cmd == 'tap.vbds': 

            # Find all Blkback instances for a given tapdisk 

 

            for tapdisk in Tapdisk.list(**attrs): 

                print "%s:" % tapdisk, 

                for vbd in Blkback.find_by_tap(tapdisk): 

                    print vbd, 

                print 

 

        else: 

 

            if not attrs: 

                usage(sys.stderr) 

                sys.exit(1) 

 

            try: 

                tapdisk = Tapdisk.get(**attrs) 

            except TypeError: 

                usage(sys.stderr) 

                sys.exit(1) 

 

            if cmd == 'tap.shutdown': 

                # Shutdown a running tapdisk, or raise 

                tapdisk.shutdown() 

                print >> sys.stderr, "Shut down %s" % tapdisk 

 

            elif cmd == 'tap.pause': 

                # Pause an unpaused tapdisk, or raise 

                tapdisk.pause() 

                print >> sys.stderr, "Paused %s" % tapdisk 

 

            elif cmd == 'tap.unpause': 

                # Unpause a paused tapdisk, or raise 

                tapdisk.unpause() 

                print >> sys.stderr, "Unpaused %s" % tapdisk 

 

            elif cmd == 'tap.stats': 

                # Gather tapdisk status 

                stats = tapdisk.stats() 

                print "%s:" % tapdisk 

                print json.dumps(stats, indent=True) 

 

            else: 

                usage(sys.stderr) 

                sys.exit(1) 

 

    elif cmd == 'vbd.uevent': 

 

        hnd = BlkbackEventHandler(cmd) 

 

        if not sys.stdin.isatty(): 

            try: 

                hnd.run() 

            except Exception, e: 

                hnd.error("Unhandled Exception: %s" % e) 

 

                import traceback 

                _type, value, tb = sys.exc_info() 

                trace = traceback.format_exception(_type, value, tb) 

                for entry in trace: 

                    for line in entry.rstrip().split('\n'): 

                        util.SMlog(line) 

        else: 

            hnd.run() 

 

    elif cmd == 'vbd.list': 

 

        for vbd in Blkback.find(): 

            print vbd, \ 

                "physical-device=%s" % vbd.get_physical_device(), \ 

                "pause=%s" % vbd.pause_requested() 

 

    else: 

        usage(sys.stderr) 

        sys.exit(1)