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

#!/usr/bin/env python 

# 

# Copyright (C) 2020  Vates SAS - ronan.abhamon@vates.fr 

# 

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

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

# the Free Software Foundation, either version 3 of the License, or 

# (at your option) any later version. 

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

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

# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 

# GNU General Public License for more details. 

# 

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

# along with this program.  If not, see <https://www.gnu.org/licenses/>. 

# 

 

 

import json 

import linstor 

import os.path 

import re 

import socket 

import time 

import util 

 

 

def round_up(value, divisor): 

    assert divisor 

    divisor = int(divisor) 

    return int((int(value) + divisor - 1) / divisor) * divisor 

 

 

def round_down(value, divisor): 

    assert divisor 

    value = int(value) 

    return value - (value % int(divisor)) 

 

 

class LinstorVolumeManagerError(Exception): 

    ERR_GENERIC = 0, 

    ERR_VOLUME_EXISTS = 1, 

    ERR_VOLUME_NOT_EXISTS = 2 

 

    def __init__(self, message, code=ERR_GENERIC): 

        super(LinstorVolumeManagerError, self).__init__(message) 

        self._code = code 

 

    @property 

    def code(self): 

        return self._code 

 

# ============================================================================== 

 

# Note: 

# If a storage pool is not accessible after a network change: 

# linstor node interface modify <NODE> default --ip <IP> 

 

 

class LinstorVolumeManager(object): 

    """ 

    API to manager LINSTOR volumes in XCP-ng. 

    A volume in this context is a physical part of the storage layer. 

    """ 

 

    DEV_ROOT_PATH = '/dev/drbd/by-res/' 

 

    # Default LVM extent size. 

    BLOCK_SIZE = 4 * 1024 * 1024 

 

    # List of volume properties. 

    PROP_METADATA = 'metadata' 

    PROP_NOT_EXISTS = 'not-exists' 

    PROP_VOLUME_NAME = 'volume-name' 

    PROP_IS_READONLY_TIMESTAMP = 'readonly-timestamp' 

 

    # A volume can only be locked for a limited duration. 

    # The goal is to give enough time to slaves to execute some actions on 

    # a device before an UUID update or a coalesce for example. 

    # Expiration is expressed in seconds. 

    LOCKED_EXPIRATION_DELAY = 1 * 60 

 

    # Used when volume uuid is being updated. 

    PROP_UPDATING_UUID_SRC = 'updating-uuid-src' 

 

    # States of property PROP_NOT_EXISTS. 

    STATE_EXISTS = '0' 

    STATE_NOT_EXISTS = '1' 

    STATE_CREATING = '2' 

 

    # Property namespaces. 

    NAMESPACE_SR = 'xcp/sr' 

    NAMESPACE_VOLUME = 'volume' 

 

    # Regex to match properties. 

    REG_PROP = '^([^/]+)/{}$' 

 

    REG_METADATA = re.compile(REG_PROP.format(PROP_METADATA)) 

    REG_NOT_EXISTS = re.compile(REG_PROP.format(PROP_NOT_EXISTS)) 

    REG_VOLUME_NAME = re.compile(REG_PROP.format(PROP_VOLUME_NAME)) 

    REG_UPDATING_UUID_SRC = re.compile(REG_PROP.format(PROP_UPDATING_UUID_SRC)) 

 

    # Prefixes of SR/VOLUME in the LINSTOR DB. 

    # A LINSTOR (resource, group, ...) name cannot start with a number. 

    # So we add a prefix behind our SR/VOLUME uuids. 

    PREFIX_SR = 'xcp-sr-' 

    PREFIX_VOLUME = 'xcp-volume-' 

 

    @staticmethod 

    def default_logger(*args): 

        print(args) 

 

    # -------------------------------------------------------------------------- 

    # API. 

    # -------------------------------------------------------------------------- 

 

    class VolumeInfo(object): 

        __slots__ = ( 

            'name', 

            'physical_size',  # Total physical size used by this volume on 

                              # all disks. 

            'virtual_size'    # Total virtual available size of this volume 

                              # (i.e. the user size at creation). 

        ) 

 

        def __init__(self, name): 

            self.name = name 

            self.physical_size = 0 

            self.virtual_size = 0 

 

        def __repr__(self): 

            return 'VolumeInfo("{}", {}, {})'.format( 

                self.name, self.physical_size, self.virtual_size 

            ) 

 

    # -------------------------------------------------------------------------- 

 

    def __init__( 

        self, uri, group_name, repair=False, logger=default_logger.__func__ 

    ): 

        """ 

        Create a new LinstorApi object. 

        :param str uri: URI to communicate with the LINSTOR controller. 

        :param str group_name: The SR goup name to use. 

        :param bool repair: If true we try to remove bad volumes due to a crash 

        or unexpected behavior. 

        :param function logger: Function to log messages. 

        """ 

 

        self._uri = uri 

        self._linstor = self._create_linstor_instance(uri) 

        self._base_group_name = group_name 

 

        # Ensure group exists. 

        group_name = self._build_group_name(group_name) 

        groups = self._linstor.resource_group_list_raise([group_name]) 

        groups = groups.resource_groups 

        if not groups: 

            raise LinstorVolumeManagerError( 

                'Unable to find `{}` Linstor SR'.format(group_name) 

            ) 

 

        # Ok. ;) 

        self._logger = logger 

        self._redundancy = groups[0].select_filter.place_count 

        self._group_name = group_name 

        self._build_volumes(repair=repair) 

 

    @property 

    def group_name(self): 

        """ 

        Give the used group name. 

        :return: The group name. 

        :rtype: str 

        """ 

        return self._base_group_name 

 

    @property 

    def volumes(self): 

        """ 

        Give the volumes uuid set. 

        :return: The volumes uuid set. 

        :rtype: set(str) 

        """ 

        return self._volumes 

 

    @property 

    def volumes_with_name(self): 

        """ 

        Give a volume dictionnary that contains names actually owned. 

        :return: A volume/name dict. 

        :rtype: dict(str, str) 

        """ 

        return self._get_volumes_by_property(self.REG_VOLUME_NAME) 

 

    @property 

    def volumes_with_info(self): 

        """ 

        Give a volume dictionnary that contains VolumeInfos. 

        :return: A volume/VolumeInfo dict. 

        :rtype: dict(str, VolumeInfo) 

        """ 

 

        volumes = {} 

 

        all_volume_info = self._get_volumes_info() 

        volume_names = self.volumes_with_name 

        for volume_uuid, volume_name in volume_names.items(): 

            if volume_name: 

                volume_info = all_volume_info.get(volume_name) 

                if volume_info: 

                    volumes[volume_uuid] = volume_info 

                    continue 

 

            # Well I suppose if this volume is not available, 

            # LINSTOR has been used directly without using this API. 

            volumes[volume_uuid] = self.VolumeInfo('') 

 

        return volumes 

 

    @property 

    def volumes_with_metadata(self): 

        """ 

        Give a volume dictionnary that contains metadata. 

        :return: A volume/metadata dict. 

        :rtype: dict(str, dict) 

        """ 

 

        volumes = {} 

 

        metadata = self._get_volumes_by_property(self.REG_METADATA) 

        for volume_uuid, volume_metadata in metadata.items(): 

            if volume_metadata: 

                volume_metadata = json.loads(volume_metadata) 

                if isinstance(volume_metadata, dict): 

                    volumes[volume_uuid] = volume_metadata 

                    continue 

                raise LinstorVolumeManagerError( 

                    'Expected dictionary in volume metadata: {}' 

                    .format(volume_uuid) 

                ) 

 

            volumes[volume_uuid] = {} 

 

        return volumes 

 

    @property 

    def max_volume_size_allowed(self): 

        """ 

        Give the max volume size currently available in B. 

        :return: The current size. 

        :rtype: int 

        """ 

 

        candidates = self._find_best_size_candidates() 

        if not candidates: 

            raise LinstorVolumeManagerError( 

                'Failed to get max volume size allowed' 

            ) 

 

        size = candidates[0].max_volume_size 

        if size < 0: 

            raise LinstorVolumeManagerError( 

                'Invalid max volume size allowed given: {}'.format(size) 

            ) 

        return self.round_down_volume_size(size * 1024) 

 

    @property 

    def physical_size(self): 

        """ 

        Give the total physical size of the SR. 

        :return: The physical size. 

        :rtype: int 

        """ 

        return self._compute_size('total_capacity') 

 

    @property 

    def physical_free_size(self): 

        """ 

        Give the total free physical size of the SR. 

        :return: The physical free size. 

        :rtype: int 

        """ 

        return self._compute_size('free_capacity') 

 

    @property 

    def total_allocated_volume_size(self): 

        """ 

        Give the sum of all created volumes. 

        :return: The physical required size to use the volumes. 

        :rtype: int 

        """ 

 

        size = 0 

        for resource in self._linstor.resource_list_raise().resources: 

            for volume in resource.volumes: 

                # We ignore diskless pools of the form "DfltDisklessStorPool". 

                if volume.storage_pool_name == self._group_name: 

                    current_size = volume.usable_size 

                    if current_size < 0: 

                        raise LinstorVolumeManagerError( 

                           'Failed to get usable size of `{}` on `{}`' 

                           .format(resource.name, volume.storage_pool_name) 

                        ) 

                    size += current_size 

        return size * 1024 

 

    @property 

    def metadata(self): 

        """ 

        Get the metadata of the SR. 

        :return: Dictionary that contains metadata. 

        :rtype: dict(str, dict) 

        """ 

 

        sr_properties = self._get_sr_properties() 

        metadata = sr_properties.get(self.PROP_METADATA) 

        if metadata is not None: 

            metadata = json.loads(metadata) 

            if isinstance(metadata, dict): 

                return metadata 

            raise LinstorVolumeManagerError( 

                'Expected dictionary in SR metadata: {}'.format( 

                    self._group_name 

                ) 

            ) 

 

        return {} 

 

    @metadata.setter 

    def metadata(self, metadata): 

        """ 

        Set the metadata of the SR. 

        :param dict metadata: Dictionary that contains metadata. 

        """ 

 

        assert isinstance(metadata, dict) 

        sr_properties = self._get_sr_properties() 

        sr_properties[self.PROP_METADATA] = json.dumps(metadata) 

 

    @property 

    def disconnected_hosts(self): 

        """ 

        Get the list of disconnected hosts. 

        :return: Set that contains disconnected hosts. 

        :rtype: set(str) 

        """ 

 

        pools = self._linstor.storage_pool_list_raise( 

            filter_by_stor_pools=[self._group_name] 

        ).storage_pools 

 

        disconnected_hosts = set() 

        for pool in pools: 

            for report in pool.reports: 

                if report.ret_code & linstor.consts.WARN_NOT_CONNECTED == \ 

                        linstor.consts.WARN_NOT_CONNECTED: 

                    disconnected_hosts.add(pool.node_name) 

                    break 

        return disconnected_hosts 

 

    def check_volume_exists(self, volume_uuid): 

        """ 

        Check if a volume exists in the SR. 

        :return: True if volume exists. 

        :rtype: bool 

        """ 

        return volume_uuid in self._volumes 

 

    def create_volume(self, volume_uuid, size, persistent=True): 

        """ 

        Create a new volume on the SR. 

        :param str volume_uuid: The volume uuid to use. 

        :param int size: volume size in B. 

        :param bool persistent: If false the volume will be unavailable 

        on the next constructor call LinstorSR(...). 

        :return: The current device path of the volume. 

        :rtype: str 

        """ 

 

        self._logger('Creating LINSTOR volume {}...'.format(volume_uuid)) 

        volume_name = self.build_volume_name(util.gen_uuid()) 

        volume_properties = self._create_volume_with_properties( 

            volume_uuid, volume_name, size, place_resources=True 

        ) 

 

        try: 

            self._logger( 

                'Find device path of LINSTOR volume {}...'.format(volume_uuid) 

            ) 

            device_path = self._find_device_path(volume_uuid, volume_name) 

            if persistent: 

                volume_properties[self.PROP_NOT_EXISTS] = self.STATE_EXISTS 

            self._volumes.add(volume_uuid) 

            self._logger( 

                'LINSTOR volume {} created!'.format(volume_uuid) 

            ) 

            return device_path 

        except Exception: 

            self._force_destroy_volume(volume_uuid, volume_properties) 

            raise 

 

    def mark_volume_as_persistent(self, volume_uuid): 

        """ 

        Mark volume as persistent if created with persistent=False. 

        :param str volume_uuid: The volume uuid to mark. 

        """ 

 

        self._ensure_volume_exists(volume_uuid) 

 

        # Mark volume as persistent. 

        volume_properties = self._get_volume_properties(volume_uuid) 

        volume_properties[self.PROP_NOT_EXISTS] = self.STATE_EXISTS 

 

    def destroy_volume(self, volume_uuid): 

        """ 

        Destroy a volume. 

        :param str volume_uuid: The volume uuid to destroy. 

        """ 

 

        self._ensure_volume_exists(volume_uuid) 

        self.ensure_volume_is_not_locked(volume_uuid) 

 

        # Mark volume as destroyed. 

        volume_properties = self._get_volume_properties(volume_uuid) 

        volume_properties[self.PROP_NOT_EXISTS] = self.STATE_NOT_EXISTS 

 

        self._volumes.remove(volume_uuid) 

        self._destroy_volume(volume_uuid, volume_properties) 

 

    def lock_volume(self, volume_uuid, locked=True): 

        """ 

        Prevent modifications of the volume properties during 

        "self.LOCKED_EXPIRATION_DELAY" seconds. The SR must be locked 

        when used. This method is useful to attach/detach correctly a volume on 

        a slave. Without it the GC can rename a volume, in this case the old 

        volume path can be used by a slave... 

        :param str volume_uuid: The volume uuid to protect/unprotect. 

        :param bool locked: Lock/unlock the volume. 

        """ 

 

        self._ensure_volume_exists(volume_uuid) 

 

        self._logger( 

            '{} volume {} as locked'.format( 

                'Mark' if locked else 'Unmark', 

                volume_uuid 

            ) 

        ) 

 

        volume_properties = self._get_volume_properties(volume_uuid) 

        if locked: 

            volume_properties[ 

                self.PROP_IS_READONLY_TIMESTAMP 

            ] = str(time.time()) 

        elif self.PROP_IS_READONLY_TIMESTAMP in volume_properties: 

            volume_properties.pop(self.PROP_IS_READONLY_TIMESTAMP) 

 

    def ensure_volume_is_not_locked(self, volume_uuid, timeout=None): 

        """ 

        Ensure a volume is not locked. Wait if necessary. 

        :param str volume_uuid: The volume uuid to check. 

        :param int timeout: If the volume is always locked after the expiration 

        of the timeout, an exception is thrown. 

        """ 

        return self.ensure_volume_list_is_not_locked([volume_uuid], timeout) 

 

    def ensure_volume_list_is_not_locked(self, volume_uuids, timeout=None): 

        checked = set() 

        for volume_uuid in volume_uuids: 

            if volume_uuid in self._volumes: 

                checked.add(volume_uuid) 

 

        if not checked: 

            return 

 

        waiting = False 

 

        start = time.time() 

        while True: 

            # Can't delete in for loop, use a copy of the list. 

            remaining = checked.copy() 

            for volume_uuid in checked: 

                volume_properties = self._get_volume_properties(volume_uuid) 

                timestamp = volume_properties.get( 

                    self.PROP_IS_READONLY_TIMESTAMP 

                ) 

                if timestamp is None: 

                    remaining.remove(volume_uuid) 

                    continue 

 

                now = time.time() 

                if now - float(timestamp) > self.LOCKED_EXPIRATION_DELAY: 

                    self._logger( 

                        'Remove readonly timestamp on {}'.format(volume_uuid) 

                    ) 

                    volume_properties.pop(self.PROP_IS_READONLY_TIMESTAMP) 

                    remaining.remove(volume_uuid) 

                    continue 

 

                if not waiting: 

                    self._logger( 

                        'Volume {} is locked, waiting...'.format(volume_uuid) 

                    ) 

                    waiting = True 

                break 

 

            if not remaining: 

                break 

            checked = remaining 

 

            if timeout is not None and now - start > timeout: 

                raise LinstorVolumeManagerError( 

                    'volume `{}` is locked and timeout has been reached' 

                    .format(volume_uuid), 

                    LinstorVolumeManagerError.ERR_VOLUME_NOT_EXISTS 

                ) 

 

            # We must wait to use the volume. After that we can modify it 

            # ONLY if the SR is locked to avoid bad reads on the slaves. 

            time.sleep(1) 

 

        if waiting: 

            self._logger('No volume locked now!') 

 

    def introduce_volume(self, volume_uuid): 

        pass  # TODO: Implement me. 

 

    def resize_volume(self, volume_uuid, new_size): 

        """ 

        Resize a volume. 

        :param str volume_uuid: The volume uuid to resize. 

        :param int new_size: New size in B. 

        """ 

 

        volume_name = self.get_volume_name(volume_uuid) 

        self.ensure_volume_is_not_locked(volume_uuid) 

        new_size = self.round_up_volume_size(new_size) 

 

        result = self._linstor.volume_dfn_modify( 

            rsc_name=volume_name, 

            volume_nr=0, 

            size=new_size / 1024 

        ) 

        error_str = self._get_error_str(result) 

        if error_str: 

            raise LinstorVolumeManagerError( 

                'Could not resize volume `{}` from SR `{}`: {}' 

                .format(volume_uuid, self._group_name, error_str) 

            ) 

 

    def get_volume_name(self, volume_uuid): 

        """ 

        Get the name of a particular volume. 

        :param str volume_uuid: The volume uuid of the name to get. 

        :return: The volume name. 

        :rtype: str 

        """ 

 

        self._ensure_volume_exists(volume_uuid) 

        volume_properties = self._get_volume_properties(volume_uuid) 

        volume_name = volume_properties.get(self.PROP_VOLUME_NAME) 

        if volume_name: 

            return volume_name 

        raise LinstorVolumeManagerError( 

            'Failed to get volume name of {}'.format(volume_uuid) 

        ) 

 

    def get_volume_size(self, volume_uuid): 

        """ 

        Get the size of a particular volume. 

        :param str volume_uuid: The volume uuid of the size to get. 

        :return: The volume size. 

        :rtype: int 

        """ 

 

        volume_name = self.get_volume_name(volume_uuid) 

        dfns = self._linstor.resource_dfn_list_raise( 

            query_volume_definitions=True, 

            filter_by_resource_definitions=[volume_name] 

        ).resource_definitions 

 

        size = dfns[0].volume_definitions[0].size 

        if size < 0: 

            raise LinstorVolumeManagerError( 

                'Failed to get volume size of: {}'.format(volume_uuid) 

            ) 

        return size * 1024 

 

    def get_volume_info(self, volume_uuid): 

        """ 

        Get the volume info of a particular volume. 

        :param str volume_uuid: The volume uuid of the volume info to get. 

        :return: The volume info. 

        :rtype: VolumeInfo 

        """ 

 

        volume_name = self.get_volume_name(volume_uuid) 

        return self._get_volumes_info(filter=[volume_name])[volume_name] 

 

    def get_device_path(self, volume_uuid): 

        """ 

        Get the dev path of a volume. 

        :param str volume_uuid: The volume uuid to get the dev path. 

        :return: The current device path of the volume. 

        :rtype: str 

        """ 

 

        volume_name = self.get_volume_name(volume_uuid) 

        return self._find_device_path(volume_uuid, volume_name) 

 

    def get_volume_uuid_from_device_path(self, device_path): 

        """ 

        Get the volume uuid of a device_path. 

        :param str device_path: The dev path to find the volume uuid. 

        :return: The volume uuid of the local device path. 

        :rtype: str 

        """ 

 

        expected_volume_name = \ 

            self.get_volume_name_from_device_path(device_path) 

 

        volume_names = self.volumes_with_name 

        for volume_uuid, volume_name in volume_names.items(): 

            if volume_name == expected_volume_name: 

                return volume_uuid 

 

        raise LinstorVolumeManagerError( 

            'Unable to find volume uuid from dev path `{}`'.format(device_path) 

        ) 

 

    def get_volume_name_from_device_path(self, device_path): 

        """ 

        Get the volume name of a device_path on the current host. 

        :param str device_path: The dev path to find the volume name. 

        :return: The volume name of the local device path. 

        :rtype: str 

        """ 

 

        node_name = socket.gethostname() 

        resources = self._linstor.resource_list_raise( 

            filter_by_nodes=[node_name] 

        ).resources 

 

        real_device_path = os.path.realpath(device_path) 

        for resource in resources: 

            if resource.volumes[0].device_path == real_device_path: 

                return resource.name 

 

        raise LinstorVolumeManagerError( 

            'Unable to find volume name from dev path `{}`' 

            .format(device_path) 

        ) 

 

    def update_volume_uuid(self, volume_uuid, new_volume_uuid, force=False): 

        """ 

        Change the uuid of a volume. 

        :param str volume_uuid: The volume to modify. 

        :param str new_volume_uuid: The new volume uuid to use. 

        :param bool force: If true we doesn't check if volume_uuid is in the 

        volume list. I.e. the volume can be marked as deleted but the volume 

        can still be in the LINSTOR KV store if the deletion has failed. 

        In specific cases like "undo" after a failed clone we must rename a bad 

        deleted VDI. 

        """ 

 

        self._logger( 

            'Trying to update volume UUID {} to {}...' 

            .format(volume_uuid, new_volume_uuid) 

        ) 

        if not force: 

            self._ensure_volume_exists(volume_uuid) 

        self.ensure_volume_is_not_locked(volume_uuid) 

 

        if new_volume_uuid in self._volumes: 

            raise LinstorVolumeManagerError( 

                'Volume `{}` already exists'.format(new_volume_uuid), 

                LinstorVolumeManagerError.ERR_VOLUME_EXISTS 

            ) 

 

        volume_properties = self._get_volume_properties(volume_uuid) 

        if volume_properties.get(self.PROP_UPDATING_UUID_SRC): 

            raise LinstorVolumeManagerError( 

                'Cannot update volume uuid {}: invalid state' 

                .format(volume_uuid) 

            ) 

 

        new_volume_properties = self._get_volume_properties( 

            new_volume_uuid 

        ) 

        if list(new_volume_properties.items()): 

            raise LinstorVolumeManagerError( 

                'Cannot update volume uuid {} to {}: ' 

                .format(volume_uuid, new_volume_uuid) + 

                'this last one is not empty' 

            ) 

 

        assert volume_properties.namespace != \ 

            new_volume_properties.namespace 

 

        try: 

            # 1. Mark new volume properties with PROP_UPDATING_UUID_SRC. 

            # If we crash after that, the new properties can be removed 

            # properly. 

            new_volume_properties[self.PROP_NOT_EXISTS] = self.STATE_NOT_EXISTS 

            new_volume_properties[self.PROP_UPDATING_UUID_SRC] = volume_uuid 

 

            # 2. Copy the properties. 

            for property in [self.PROP_METADATA, self.PROP_VOLUME_NAME]: 

                new_volume_properties[property] = \ 

                    volume_properties.get(property) 

 

            # 3. Ok! 

            new_volume_properties[self.PROP_NOT_EXISTS] = self.STATE_EXISTS 

        except Exception as e: 

            try: 

                new_volume_properties.clear() 

            except Exception as e: 

                self._logger( 

                    'Failed to clear new volume properties: {} (ignoring...)' 

                    .format(e) 

                ) 

            raise LinstorVolumeManagerError( 

                'Failed to copy volume properties: {}'.format(e) 

            ) 

 

        try: 

            # 4. After this point, it's ok we can remove the 

            # PROP_UPDATING_UUID_SRC property and clear the src properties 

            # without problems. 

            volume_properties.clear() 

            new_volume_properties.pop(self.PROP_UPDATING_UUID_SRC) 

        except Exception as e: 

            raise LinstorVolumeManagerError( 

                'Failed to clear volume properties ' 

                'after volume uuid update: {}'.format(e) 

            ) 

 

        self._volumes.remove(volume_uuid) 

        self._volumes.add(new_volume_uuid) 

 

        self._logger( 

            'UUID update succeeded of {} to {}! (properties={})' 

            .format( 

                volume_uuid, new_volume_uuid, 

                self._get_filtered_properties(new_volume_properties) 

            ) 

        ) 

 

    def update_volume_name(self, volume_uuid, volume_name): 

        """ 

        Change the volume name of a volume. 

        :param str volume_uuid: The volume to modify. 

        :param str volume_name: The volume_name to use. 

        """ 

 

        self._ensure_volume_exists(volume_uuid) 

        self.ensure_volume_is_not_locked(volume_uuid) 

        if not volume_name.startswith(self.PREFIX_VOLUME): 

            raise LinstorVolumeManagerError( 

                'Volume name `{}` must be start with `{}`' 

                .format(volume_name, self.PREFIX_VOLUME) 

            ) 

 

        if volume_name not in self._fetch_resource_names(): 

            raise LinstorVolumeManagerError( 

                'Volume `{}` doesn\'t exist'.format(volume_name) 

            ) 

 

        volume_properties = self._get_volume_properties(volume_uuid) 

        volume_properties[self.PROP_VOLUME_NAME] = volume_name 

 

    def get_usage_states(self, volume_uuid): 

        """ 

        Check if a volume is currently used. 

        :param str volume_uuid: The volume uuid to check. 

        :return: A dictionnary that contains states. 

        :rtype: dict(str, bool or None) 

        """ 

 

        states = {} 

 

        volume_name = self.get_volume_name(volume_uuid) 

        for resource_state in self._linstor.resource_list_raise( 

            filter_by_resources=[volume_name] 

        ).resource_states: 

            states[resource_state.node_name] = resource_state.in_use 

 

        return states 

 

    def get_volume_metadata(self, volume_uuid): 

        """ 

        Get the metadata of a volume. 

        :return: Dictionary that contains metadata. 

        :rtype: dict 

        """ 

 

        self._ensure_volume_exists(volume_uuid) 

        volume_properties = self._get_volume_properties(volume_uuid) 

        metadata = volume_properties.get(self.PROP_METADATA) 

        if metadata: 

            metadata = json.loads(metadata) 

            if isinstance(metadata, dict): 

                return metadata 

            raise LinstorVolumeManagerError( 

                'Expected dictionary in volume metadata: {}' 

                .format(volume_uuid) 

            ) 

        return {} 

 

    def set_volume_metadata(self, volume_uuid, metadata): 

        """ 

        Set the metadata of a volume. 

        :param dict metadata: Dictionary that contains metadata. 

        """ 

 

        self._ensure_volume_exists(volume_uuid) 

        self.ensure_volume_is_not_locked(volume_uuid) 

 

        assert isinstance(metadata, dict) 

        volume_properties = self._get_volume_properties(volume_uuid) 

        volume_properties[self.PROP_METADATA] = json.dumps(metadata) 

 

    def update_volume_metadata(self, volume_uuid, metadata): 

        """ 

        Update the metadata of a volume. It modify only the given keys. 

        It doesn't remove unreferenced key instead of set_volume_metadata. 

        :param dict metadata: Dictionary that contains metadata. 

        """ 

 

        self._ensure_volume_exists(volume_uuid) 

        self.ensure_volume_is_not_locked(volume_uuid) 

 

        assert isinstance(metadata, dict) 

        volume_properties = self._get_volume_properties(volume_uuid) 

 

        current_metadata = json.loads( 

            volume_properties.get(self.PROP_METADATA, '{}') 

        ) 

        if not isinstance(metadata, dict): 

            raise LinstorVolumeManagerError( 

                'Expected dictionary in volume metadata: {}' 

                .format(volume_uuid) 

            ) 

 

        for key, value in metadata.items(): 

            current_metadata[key] = value 

        volume_properties[self.PROP_METADATA] = json.dumps(current_metadata) 

 

    def shallow_clone_volume(self, volume_uuid, clone_uuid, persistent=True): 

        """ 

        Clone a volume. Do not copy the data, this method creates a new volume 

        with the same size. It tries to create the volume on the same host 

        than volume source. 

        :param str volume_uuid: The volume to clone. 

        :param str clone_uuid: The cloned volume. 

        :param bool persistent: If false the volume will be unavailable 

        on the next constructor call LinstorSR(...). 

        :return: The current device path of the cloned volume. 

        :rtype: str 

        """ 

 

        volume_name = self.get_volume_name(volume_uuid) 

        self.ensure_volume_is_not_locked(volume_uuid) 

 

        # 1. Find ideal nodes + size to use. 

        ideal_node_names, size = self._get_volume_node_names_and_size( 

            volume_name 

        ) 

        if size <= 0: 

            raise LinstorVolumeManagerError( 

                'Invalid size of {} for volume `{}`'.format(size, volume_name) 

            ) 

 

        # 2. Find the node(s) with the maximum space. 

        candidates = self._find_best_size_candidates() 

        if not candidates: 

            raise LinstorVolumeManagerError( 

                'Unable to shallow clone volume `{}`, no free space found.' 

            ) 

 

        # 3. Compute node names and search if we can try to clone 

        # on the same nodes than volume. 

        def find_best_nodes(): 

            for candidate in candidates: 

                for node_name in candidate.node_names: 

                    if node_name in ideal_node_names: 

                        return candidate.node_names 

 

        node_names = find_best_nodes() 

        if not node_names: 

            node_names = candidates[0].node_names 

 

        if len(node_names) < self._redundancy: 

            raise LinstorVolumeManagerError( 

                'Unable to shallow clone volume `{}`, '.format(volume_uuid) + 

                '{} are required to clone, found: {}'.format( 

                    self._redundancy, len(node_names) 

                ) 

            ) 

 

        # 4. Compute resources to create. 

        clone_volume_name = self.build_volume_name(util.gen_uuid()) 

        diskless_node_names = self._get_node_names() 

        resources = [] 

        for node_name in node_names: 

            diskless_node_names.remove(node_name) 

            resources.append(linstor.ResourceData( 

                node_name=node_name, 

                rsc_name=clone_volume_name, 

                storage_pool=self._group_name 

            )) 

        for node_name in diskless_node_names: 

            resources.append(linstor.ResourceData( 

                node_name=node_name, 

                rsc_name=clone_volume_name, 

                diskless=True 

            )) 

 

        # 5. Create resources! 

        def clean(properties): 

            try: 

                self._destroy_volume(clone_uuid, properties) 

            except Exception as e: 

                self._logger( 

                    'Unable to destroy volume {} after shallow clone fail: {}' 

                    .format(clone_uuid, e) 

                ) 

 

        def create(): 

            try: 

                volume_properties = self._create_volume_with_properties( 

                    clone_uuid, clone_volume_name, size, 

                    place_resources=False 

                ) 

 

                result = self._linstor.resource_create(resources) 

                error_str = self._get_error_str(result) 

                if error_str: 

                    raise LinstorVolumeManagerError( 

                        'Could not create cloned volume `{}` of `{}` from ' 

                        'SR `{}`: {}'.format( 

                            clone_uuid, volume_uuid, self._group_name, 

                            error_str 

                        ) 

                    ) 

                return volume_properties 

            except Exception: 

                clean(volume_properties) 

                raise 

 

        # Retry because we can get errors like this: 

        # "Resource disappeared while waiting for it to be ready" or 

        # "Resource did not became ready on node 'XXX' within reasonable time, check Satellite for errors." 

        # in the LINSTOR server. 

        volume_properties = util.retry(create, maxretry=5) 

 

        try: 

            device_path = self._find_device_path(clone_uuid, clone_volume_name) 

            if persistent: 

                volume_properties[self.PROP_NOT_EXISTS] = self.STATE_EXISTS 

            self._volumes.add(clone_uuid) 

            return device_path 

        except Exception as e: 

            clean(volume_properties) 

            raise 

 

    def remove_resourceless_volumes(self): 

        """ 

        Remove all volumes without valid or non-empty name 

        (i.e. without LINSTOR resource). It's different than 

        LinstorVolumeManager constructor that takes a `repair` param that 

        removes volumes with `PROP_NOT_EXISTS` to 1. 

        """ 

 

        resource_names = self._fetch_resource_names() 

        for volume_uuid, volume_name in self.volumes_with_name.items(): 

            if not volume_name or volume_name not in resource_names: 

                self.destroy_volume(volume_uuid) 

 

    def destroy(self, force=False): 

        """ 

        Destroy this SR. Object should not be used after that. 

        :param bool force: Try to destroy volumes before if true. 

        """ 

 

        if (force): 

            for volume_uuid in self._volumes: 

                self.destroy_volume(volume_uuid) 

 

        # TODO: Throw exceptions in the helpers below if necessary. 

        # TODO: What's the required action if it exists remaining volumes? 

 

        self._destroy_resource_group(self._linstor, self._group_name) 

 

        pools = self._linstor.storage_pool_list_raise( 

            filter_by_stor_pools=[self._group_name] 

        ).storage_pools 

        for pool in pools: 

            self._destroy_storage_pool( 

                self._linstor, pool.name, pool.node_name 

            ) 

 

    def find_up_to_date_diskfull_nodes(self, volume_uuid): 

        """ 

        Find all nodes that contain a specific volume using diskfull disks. 

        The disk must be up to data to be used. 

        :param str volume_uuid: The volume to use. 

        :return: The available nodes. 

        :rtype: tuple(set(str), bool) 

        """ 

 

        volume_name = self.get_volume_name(volume_uuid) 

 

        in_use = False 

        node_names = set() 

        resource_list = self._linstor.resource_list_raise( 

            filter_by_resources=[volume_name] 

        ) 

        for resource_state in resource_list.resource_states: 

            volume_state = resource_state.volume_states[0] 

            if volume_state.disk_state == 'UpToDate': 

                node_names.add(resource_state.node_name) 

            if resource_state.in_use: 

                in_use = True 

 

        return (node_names, in_use) 

 

    @classmethod 

    def create_sr( 

        cls, uri, group_name, node_names, redundancy, 

        thin_provisioning=False, 

        logger=default_logger.__func__ 

    ): 

        """ 

        Create a new SR on the given nodes. 

        :param str uri: URI to communicate with the LINSTOR controller. 

        :param str group_name: The SR group_name to use. 

        :param list[str] node_names: String list of nodes. 

        :param int redundancy: How many copy of volumes should we store? 

        :param function logger: Function to log messages. 

        :return: A new LinstorSr instance. 

        :rtype: LinstorSr 

        """ 

 

        # 1. Check if SR already exists. 

        lin = cls._create_linstor_instance(uri) 

        driver_pool_name = group_name 

        group_name = cls._build_group_name(group_name) 

        pools = lin.storage_pool_list_raise(filter_by_stor_pools=[group_name]) 

 

        # TODO: Maybe if the SR already exists and if the nodes are the same, 

        # we can try to use it directly. 

        pools = pools.storage_pools 

        if pools: 

            existing_node_names = map(lambda pool: pool.node_name, pools) 

            raise LinstorVolumeManagerError( 

                'Unable to create SR `{}`. It already exists on node(s): {}' 

                .format(group_name, existing_node_names) 

            ) 

 

        if lin.resource_group_list_raise( 

            [group_name] 

        ).resource_groups: 

            raise LinstorVolumeManagerError( 

                'Unable to create SR `{}`: The group name already exists' 

                .format(group_name) 

            ) 

 

        if thin_provisioning: 

            driver_pool_parts = driver_pool_name.split('/') 

            if not len(driver_pool_parts) == 2: 

                raise LinstorVolumeManagerError( 

                    'Invalid group name using thin provisioning. ' 

                    'Expected format: \'VG/LV`\'' 

                ) 

 

        # 2. Create storage pool on each node + resource group. 

        i = 0 

        try: 

            # 2.a. Create storage pools. 

            while i < len(node_names): 

                node_name = node_names[i] 

 

                result = lin.storage_pool_create( 

                    node_name=node_name, 

                    storage_pool_name=group_name, 

                    storage_driver='LVM_THIN' if thin_provisioning else 'LVM', 

                    driver_pool_name=driver_pool_name 

                ) 

 

                error_str = cls._get_error_str(result) 

                if error_str: 

                    raise LinstorVolumeManagerError( 

                        'Could not create SP `{}` on node `{}`: {}'.format( 

                            group_name, 

                            node_name, 

                            error_str 

                        ) 

                    ) 

                i += 1 

 

            # 2.b. Create resource group. 

            result = lin.resource_group_create( 

                name=group_name, 

                place_count=redundancy, 

                storage_pool=group_name, 

                diskless_on_remaining=True 

            ) 

            error_str = cls._get_error_str(result) 

            if error_str: 

                raise LinstorVolumeManagerError( 

                    'Could not create RG `{}`: {}'.format( 

                        group_name, error_str 

                    ) 

                ) 

 

            # 2.c. Create volume group. 

            result = lin.volume_group_create(group_name) 

            error_str = cls._get_error_str(result) 

            if error_str: 

                raise LinstorVolumeManagerError( 

                    'Could not create VG `{}`: {}'.format( 

                        group_name, error_str 

                    ) 

                ) 

 

        # 3. Remove storage pools/resource/volume group in the case of errors. 

        except Exception as e: 

            try: 

                cls._destroy_resource_group(lin, group_name) 

            except Exception: 

                pass 

            j = 0 

            i = min(i, len(node_names) - 1) 

            while j <= i: 

                try: 

                    cls._destroy_storage_pool(lin, group_name, node_names[j]) 

                except Exception: 

                    pass 

                j += 1 

            raise e 

 

        # 4. Return new instance. 

        instance = cls.__new__(cls) 

        instance._uri = uri 

        instance._linstor = lin 

        instance._logger = logger 

        instance._redundancy = redundancy 

        instance._group_name = group_name 

        instance._volumes = set() 

        return instance 

 

    @classmethod 

    def build_device_path(cls, volume_name): 

        """ 

        Build a device path given a volume name. 

        :param str volume_name: The volume name to use. 

        :return: A valid or not device path. 

        :rtype: str 

        """ 

 

        return '{}{}/0'.format(cls.DEV_ROOT_PATH, volume_name) 

 

    @classmethod 

    def build_volume_name(cls, base_name): 

        """ 

        Build a volume name given a base name (i.e. a UUID). 

        :param str volume_name: The volume name to use. 

        :return: A valid or not device path. 

        :rtype: str 

        """ 

        return '{}{}'.format(cls.PREFIX_VOLUME, base_name) 

 

    @classmethod 

    def round_up_volume_size(cls, volume_size): 

        """ 

        Align volume size on higher multiple of BLOCK_SIZE. 

        :param int volume_size: The volume size to align. 

        :return: An aligned volume size. 

        :rtype: int 

        """ 

        return round_up(volume_size, cls.BLOCK_SIZE) 

 

    @classmethod 

    def round_down_volume_size(cls, volume_size): 

        """ 

        Align volume size on lower multiple of BLOCK_SIZE. 

        :param int volume_size: The volume size to align. 

        :return: An aligned volume size. 

        :rtype: int 

        """ 

        return round_down(volume_size, cls.BLOCK_SIZE) 

 

    # -------------------------------------------------------------------------- 

    # Private helpers. 

    # -------------------------------------------------------------------------- 

 

    def _ensure_volume_exists(self, volume_uuid): 

        if volume_uuid not in self._volumes: 

            raise LinstorVolumeManagerError( 

                'volume `{}` doesn\'t exist'.format(volume_uuid), 

                LinstorVolumeManagerError.ERR_VOLUME_NOT_EXISTS 

            ) 

 

    def _find_best_size_candidates(self): 

        result = self._linstor.resource_group_qmvs(self._group_name) 

        error_str = self._get_error_str(result) 

        if error_str: 

            raise LinstorVolumeManagerError( 

                'Failed to get max volume size allowed of SR `{}`: {}'.format( 

                    self._group_name, 

                    error_str 

                ) 

            ) 

        return result[0].candidates 

 

    def _fetch_resource_names(self): 

        resource_names = set() 

        dfns = self._linstor.resource_dfn_list_raise().resource_definitions 

        for dfn in dfns: 

            if dfn.resource_group_name == self._group_name and \ 

                    linstor.consts.FLAG_DELETE not in dfn.flags: 

                resource_names.add(dfn.name) 

        return resource_names 

 

    def _get_volumes_info(self, filter=None): 

        all_volume_info = {} 

        resources = self._linstor.resource_list_raise( 

            filter_by_resources=filter 

        ) 

        for resource in resources.resources: 

            if resource.name not in all_volume_info: 

                current = all_volume_info[resource.name] = self.VolumeInfo( 

                    resource.name 

                ) 

            else: 

                current = all_volume_info[resource.name] 

 

            for volume in resource.volumes: 

                # We ignore diskless pools of the form "DfltDisklessStorPool". 

                if volume.storage_pool_name == self._group_name: 

                    if volume.allocated_size < 0: 

                        raise LinstorVolumeManagerError( 

                           'Failed to get allocated size of `{}` on `{}`' 

                           .format(resource.name, volume.storage_pool_name) 

                        ) 

                    current.physical_size += volume.allocated_size 

 

                    if volume.usable_size < 0: 

                        raise LinstorVolumeManagerError( 

                           'Failed to get usable size of `{}` on `{}`' 

                           .format(resource.name, volume.storage_pool_name) 

                        ) 

                    virtual_size = volume.usable_size 

 

                    current.virtual_size = current.virtual_size and \ 

                        min(current.virtual_size, virtual_size) or virtual_size 

 

        for current in all_volume_info.values(): 

            current.physical_size *= 1024 

            current.virtual_size *= 1024 

 

        return all_volume_info 

 

    def _get_volume_node_names_and_size(self, volume_name): 

        node_names = set() 

        size = -1 

        for resource in self._linstor.resource_list_raise( 

            filter_by_resources=[volume_name] 

        ).resources: 

            for volume in resource.volumes: 

                # We ignore diskless pools of the form "DfltDisklessStorPool". 

                if volume.storage_pool_name == self._group_name: 

                    node_names.add(resource.node_name) 

 

                    current_size = volume.usable_size 

                    if current_size < 0: 

                        raise LinstorVolumeManagerError( 

                           'Failed to get usable size of `{}` on `{}`' 

                           .format(resource.name, volume.storage_pool_name) 

                        ) 

 

                    if size < 0: 

                        size = current_size 

                    else: 

                        size = min(size, current_size) 

 

        return (node_names, size * 1024) 

 

    def _compute_size(self, attr): 

        pools = self._linstor.storage_pool_list_raise( 

            filter_by_stor_pools=[self._group_name] 

        ).storage_pools 

 

        capacity = 0 

        for pool in pools: 

            space = pool.free_space 

            if space: 

                size = getattr(space, attr) 

                if size < 0: 

                    raise LinstorVolumeManagerError( 

                        'Failed to get pool {} attr of `{}`' 

                        .format(attr, pool.node_name) 

                    ) 

                capacity += size 

        return capacity * 1024 

 

    def _get_node_names(self): 

        node_names = set() 

        pools = self._linstor.storage_pool_list_raise( 

            filter_by_stor_pools=[self._group_name] 

        ).storage_pools 

        for pool in pools: 

            node_names.add(pool.node_name) 

        return node_names 

 

    def _check_volume_creation_errors(self, result, volume_uuid): 

        errors = self._filter_errors(result) 

        if self._check_errors(errors, [ 

            linstor.consts.FAIL_EXISTS_RSC, linstor.consts.FAIL_EXISTS_RSC_DFN 

        ]): 

            raise LinstorVolumeManagerError( 

                'Failed to create volume `{}` from SR `{}`, it already exists' 

                .format(volume_uuid, self._group_name), 

                LinstorVolumeManagerError.ERR_VOLUME_EXISTS 

            ) 

 

        if errors: 

            raise LinstorVolumeManagerError( 

                'Failed to create volume `{}` from SR `{}`: {}'.format( 

                    volume_uuid, 

                    self._group_name, 

                    self._get_error_str(errors) 

                ) 

            ) 

 

    def _create_volume(self, volume_uuid, volume_name, size, place_resources): 

        size = self.round_up_volume_size(size) 

 

        self._check_volume_creation_errors(self._linstor.resource_group_spawn( 

            rsc_grp_name=self._group_name, 

            rsc_dfn_name=volume_name, 

            vlm_sizes=['{}B'.format(size)], 

            definitions_only=not place_resources 

        ), volume_uuid) 

 

    def _create_volume_with_properties( 

        self, volume_uuid, volume_name, size, place_resources 

    ): 

        if self.check_volume_exists(volume_uuid): 

            raise LinstorVolumeManagerError( 

                'Could not create volume `{}` from SR `{}`, it already exists' 

                .format(volume_uuid, self._group_name) + ' in properties', 

                LinstorVolumeManagerError.ERR_VOLUME_EXISTS 

            ) 

 

        if volume_name in self._fetch_resource_names(): 

            raise LinstorVolumeManagerError( 

                'Could not create volume `{}` from SR `{}`, '.format( 

                    volume_uuid, self._group_name 

                ) + 'resource of the same name already exists in LINSTOR' 

            ) 

 

        # I am paranoid. 

        volume_properties = self._get_volume_properties(volume_uuid) 

        if (volume_properties.get(self.PROP_NOT_EXISTS) is not None): 

            raise LinstorVolumeManagerError( 

                'Could not create volume `{}`, '.format(volume_uuid) + 

                'properties already exist' 

            ) 

 

        try: 

            volume_properties[self.PROP_NOT_EXISTS] = self.STATE_CREATING 

            volume_properties[self.PROP_VOLUME_NAME] = volume_name 

 

            self._create_volume( 

                volume_uuid, volume_name, size, place_resources 

            ) 

 

            return volume_properties 

        except LinstorVolumeManagerError as e: 

            # Do not destroy existing resource! 

            # In theory we can't get this error because we check this event 

            # before the `self._create_volume` case. 

            # It can only happen if the same volume uuid is used in the same 

            # call in another host. 

            if e.code == LinstorVolumeManagerError.ERR_VOLUME_EXISTS: 

                raise 

            self._force_destroy_volume(volume_uuid, volume_properties) 

            raise 

        except Exception: 

            self._force_destroy_volume(volume_uuid, volume_properties) 

            raise 

 

    def _find_device_path(self, volume_uuid, volume_name): 

        current_device_path = self._request_device_path( 

            volume_uuid, volume_name, activate=True 

        ) 

 

        # We use realpath here to get the /dev/drbd<id> path instead of 

        # /dev/drbd/by-res/<resource_name>. 

        expected_device_path = self.build_device_path(volume_name) 

        util.wait_for_path(expected_device_path, 5) 

 

        device_realpath = os.path.realpath(expected_device_path) 

        if current_device_path != device_realpath: 

            raise LinstorVolumeManagerError( 

                'Invalid path, current={}, expected={} (realpath={})' 

                .format( 

                    current_device_path, 

                    expected_device_path, 

                    device_realpath 

                ) 

            ) 

        return expected_device_path 

 

    def _request_device_path(self, volume_uuid, volume_name, activate=False): 

        node_name = socket.gethostname() 

        resources = self._linstor.resource_list( 

            filter_by_nodes=[node_name], 

            filter_by_resources=[volume_name] 

        ) 

 

        if not resources or not resources[0]: 

            raise LinstorVolumeManagerError( 

                'No response list for dev path of `{}`'.format(volume_uuid) 

            ) 

        if isinstance(resources[0], linstor.responses.ResourceResponse): 

            if not resources[0].resources: 

                if activate: 

                    self._activate_device_path(node_name, volume_name) 

                    return self._request_device_path(volume_uuid, volume_name) 

                raise LinstorVolumeManagerError( 

                    'Empty dev path for `{}`, but definition "seems" to exist' 

                    .format(volume_uuid) 

                ) 

            # Contains a path of the /dev/drbd<id> form. 

            return resources[0].resources[0].volumes[0].device_path 

 

        raise LinstorVolumeManagerError( 

            'Unable to get volume dev path `{}`: {}'.format( 

                volume_uuid, str(resources[0]) 

            ) 

        ) 

 

    def _activate_device_path(self, node_name, volume_name): 

        result = self._linstor.resource_create([ 

            linstor.ResourceData(node_name, volume_name, diskless=True) 

        ]) 

        if linstor.Linstor.all_api_responses_no_error(result): 

            return 

        errors = linstor.Linstor.filter_api_call_response_errors(result) 

        if len(errors) == 1 and errors[0].is_error( 

            linstor.consts.FAIL_EXISTS_RSC 

        ): 

            return 

 

        raise LinstorVolumeManagerError( 

            'Unable to activate device path of `{}` on node `{}`: {}' 

            .format(volume_name, node_name, ', '.join( 

                [str(x) for x in result])) 

        ) 

 

    def _destroy_resource(self, resource_name): 

        result = self._linstor.resource_dfn_delete(resource_name) 

        error_str = self._get_error_str(result) 

        if error_str: 

            raise LinstorVolumeManagerError( 

                'Could not destroy resource `{}` from SR `{}`: {}' 

                .format(resource_name, self._group_name, error_str) 

            ) 

 

    def _destroy_volume(self, volume_uuid, volume_properties): 

        assert volume_properties.namespace == \ 

            self._build_volume_namespace(volume_uuid) 

 

        try: 

            volume_name = volume_properties.get(self.PROP_VOLUME_NAME) 

            if volume_name in self._fetch_resource_names(): 

                self._destroy_resource(volume_name) 

 

            # Assume this call is atomic. 

            volume_properties.clear() 

        except Exception as e: 

            raise LinstorVolumeManagerError( 

                'Cannot destroy volume `{}`: {}'.format(volume_uuid, e) 

            ) 

 

    def _force_destroy_volume(self, volume_uuid, volume_properties): 

        try: 

            self._destroy_volume(volume_uuid, volume_properties) 

        except Exception as e: 

            self._logger('Ignore fail: {}'.format(e)) 

 

    def _build_volumes(self, repair): 

        properties = linstor.KV( 

            self._get_store_name(), 

            uri=self._uri, 

            namespace=self._build_volume_namespace() 

        ) 

 

        resource_names = self._fetch_resource_names() 

 

        self._volumes = set() 

 

        updating_uuid_volumes = self._get_volumes_by_property( 

            self.REG_UPDATING_UUID_SRC, ignore_inexisting_volumes=False 

        ) 

        if updating_uuid_volumes and not repair: 

            raise LinstorVolumeManagerError( 

                'Cannot build LINSTOR volume list: ' 

                'It exists invalid "updating uuid volumes", repair is required' 

            ) 

 

        existing_volumes = self._get_volumes_by_property( 

            self.REG_NOT_EXISTS, ignore_inexisting_volumes=False 

        ) 

        for volume_uuid, not_exists in existing_volumes.items(): 

            properties.namespace = self._build_volume_namespace( 

                volume_uuid 

            ) 

 

            src_uuid = properties.get(self.PROP_UPDATING_UUID_SRC) 

            if src_uuid: 

                self._logger( 

                    'Ignoring volume during manager initialization with prop ' 

                    ' PROP_UPDATING_UUID_SRC: {} (properties={})' 

                    .format( 

                        volume_uuid, 

                        self._get_filtered_properties(properties) 

                    ) 

                ) 

                continue 

 

            # Insert volume in list if the volume exists. Or if the volume 

            # is being created and a slave wants to use it (repair = False). 

            # 

            # If we are on the master and if repair is True and state is 

            # Creating, it's probably a bug or crash: the creation process has 

            # been stopped. 

            if not_exists == self.STATE_EXISTS or ( 

                not repair and not_exists == self.STATE_CREATING 

            ): 

                self._volumes.add(volume_uuid) 

                continue 

 

            if not repair: 

                self._logger( 

                    'Ignoring bad volume during manager initialization: {} ' 

                    '(properties={})'.format( 

                        volume_uuid, 

                        self._get_filtered_properties(properties) 

                    ) 

                ) 

                continue 

 

            # Remove bad volume. 

            try: 

                self._logger( 

                    'Removing bad volume during manager initialization: {} ' 

                    '(properties={})'.format( 

                        volume_uuid, 

                        self._get_filtered_properties(properties) 

                    ) 

                ) 

                volume_name = properties.get(self.PROP_VOLUME_NAME) 

 

                # Little optimization, don't call `self._destroy_volume`, 

                # we already have resource name list. 

                if volume_name in resource_names: 

                    self._destroy_resource(volume_name) 

 

                # Assume this call is atomic. 

                properties.clear() 

            except Exception as e: 

                # Do not raise, we don't want to block user action. 

                self._logger( 

                    'Cannot clean volume {}: {}'.format(volume_uuid, e) 

                ) 

 

        for dest_uuid, src_uuid in updating_uuid_volumes.items(): 

            dest_properties = self._get_volume_properties(dest_uuid) 

            if int(dest_properties.get(self.PROP_NOT_EXISTS) or 

                    self.STATE_EXISTS): 

                dest_properties.clear() 

                continue 

 

            src_properties = self._get_volume_properties(src_uuid) 

            src_properties.clear() 

 

            dest_properties.pop(self.PROP_UPDATING_UUID_SRC) 

 

            if src_uuid in self._volumes: 

                self._volumes.remove(src_uuid) 

            self._volumes.add(dest_uuid) 

 

    def _get_sr_properties(self): 

        return linstor.KV( 

            self._get_store_name(), 

            uri=self._uri, 

            namespace=self._build_sr_namespace() 

        ) 

 

    def _get_volumes_by_property( 

        self, reg_prop, ignore_inexisting_volumes=True 

    ): 

        base_properties = linstor.KV( 

            self._get_store_name(), 

            uri=self._uri, 

            namespace=self._build_volume_namespace() 

        ) 

 

        volume_properties = {} 

        for volume_uuid in self._volumes: 

            volume_properties[volume_uuid] = '' 

 

        for key, value in base_properties.items(): 

            res = reg_prop.match(key) 

            if res: 

                volume_uuid = res.groups()[0] 

                if not ignore_inexisting_volumes or \ 

                        volume_uuid in self._volumes: 

                    volume_properties[volume_uuid] = value 

 

        return volume_properties 

 

    def _get_volume_properties(self, volume_uuid): 

        return linstor.KV( 

            self._get_store_name(), 

            uri=self._uri, 

            namespace=self._build_volume_namespace(volume_uuid) 

        ) 

 

    def _get_store_name(self): 

        return 'xcp-sr-{}'.format(self._group_name) 

 

    @classmethod 

    def _build_sr_namespace(cls): 

        return '/{}/'.format(cls.NAMESPACE_SR) 

 

    @classmethod 

    def _build_volume_namespace(cls, volume_uuid=None): 

        # Return a path to all volumes if `volume_uuid` is not given. 

        if volume_uuid is None: 

            return '/{}/'.format(cls.NAMESPACE_VOLUME) 

        return '/{}/{}/'.format(cls.NAMESPACE_VOLUME, volume_uuid) 

 

    @classmethod 

    def _get_error_str(cls, result): 

        return ', '.join([ 

            err.message for err in cls._filter_errors(result) 

        ]) 

 

    @classmethod 

    def _create_linstor_instance(cls, uri): 

        def connect(): 

            instance = linstor.Linstor(uri, keep_alive=True) 

            instance.connect() 

            return instance 

 

        return util.retry( 

            connect, 

            maxretry=60, 

            exceptions=[linstor.errors.LinstorNetworkError] 

        ) 

 

    @classmethod 

    def _destroy_storage_pool(cls, lin, group_name, node_name): 

        result = lin.storage_pool_delete(node_name, group_name) 

        error_str = cls._get_error_str(result) 

        if error_str: 

            raise LinstorVolumeManagerError( 

                'Failed to destroy SP `{}` on node `{}`: {}'.format( 

                    group_name, 

                    node_name, 

                    error_str 

                ) 

            ) 

 

    @classmethod 

    def _destroy_resource_group(cls, lin, group_name): 

        result = lin.resource_group_delete(group_name) 

        error_str = cls._get_error_str(result) 

        if error_str: 

            raise LinstorVolumeManagerError( 

                'Failed to destroy RG `{}`: {}'.format(group_name, error_str) 

            ) 

 

    @classmethod 

    def _build_group_name(cls, base_name): 

        # If thin provisioning is used we have a path like this: 

        # `VG/LV`. "/" is not accepted by LINSTOR. 

        return '{}{}'.format(cls.PREFIX_SR, base_name.replace('/', '_')) 

 

    @staticmethod 

    def _get_filtered_properties(properties): 

        return dict(properties.items()) 

 

    @staticmethod 

    def _filter_errors(result): 

        return [ 

            err for err in result 

            if hasattr(err, 'is_error') and err.is_error() 

        ] 

 

    @staticmethod 

    def _check_errors(result, codes): 

        for err in result: 

            for code in codes: 

                if err.is_error(code): 

                    return True 

        return False