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

#!/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 

# 

# FileSR: local-file storage repository 

 

import SR, VDI, SRCommand, util, scsiutil, vhdutil 

import os 

import errno 

import xs_errors 

import cleanup 

import blktap2 

import time 

import glob 

from uuid import uuid4 

from lock import Lock 

import xmlrpclib 

import XenAPI 

from constants import CBTLOG_TAG 

 

geneology = {} 

CAPABILITIES = ["SR_PROBE","SR_UPDATE", \ 

                "VDI_CREATE","VDI_DELETE","VDI_ATTACH","VDI_DETACH", \ 

                "VDI_CLONE","VDI_SNAPSHOT","VDI_RESIZE", "VDI_MIRROR", 

                "VDI_GENERATE_CONFIG", "ATOMIC_PAUSE", "VDI_CONFIG_CBT", 

                "VDI_ACTIVATE", "VDI_DEACTIVATE", "THIN_PROVISIONING"] 

 

CONFIGURATION = [ [ 'location', 'local directory path (required)' ] ] 

 

DRIVER_INFO = { 

    'name': 'Local Path VHD', 

    'description': 'SR plugin which represents disks as VHD files stored on a local path', 

    'vendor': 'Citrix Systems Inc', 

    'copyright': '(C) 2008 Citrix Systems Inc', 

    'driver_version': '1.0', 

    'required_api_version': '1.0', 

    'capabilities': CAPABILITIES, 

    'configuration': CONFIGURATION 

    } 

 

ENFORCE_VIRT_ALLOC = False 

 

JOURNAL_FILE_PREFIX = ".journal-" 

 

OPS_EXCLUSIVE = [ 

        "sr_create", "sr_delete", "sr_probe", "sr_attach", "sr_detach", 

        "sr_scan", "vdi_init", "vdi_create", "vdi_delete", "vdi_attach", 

        "vdi_detach", "vdi_resize_online", "vdi_snapshot", "vdi_clone" ] 

 

DRIVER_CONFIG = {"ATTACH_FROM_CONFIG_WITH_TAPDISK": True} 

 

class FileSR(SR.SR): 

    """Local file storage repository""" 

 

    SR_TYPE = "file" 

 

    def handles(srtype): 

        return srtype == 'file' 

    handles = staticmethod(handles) 

 

    def _check_o_direct(self): 

        if self.sr_ref and self.session is not None: 

            other_config = self.session.xenapi.SR.get_other_config(self.sr_ref) 

            o_direct = other_config.get("o_direct") 

            self.o_direct = o_direct is not None and o_direct == "true" 

        else: 

            self.o_direct = True 

 

 

    def __init__(self, srcmd, sr_uuid): 

        # We call SR.SR.__init__ explicitly because 

        # "super" sometimes failed due to circular imports 

        SR.SR.__init__(self, srcmd, sr_uuid) 

        self._check_o_direct() 

 

 

    def load(self, sr_uuid): 

        self.ops_exclusive = OPS_EXCLUSIVE 

        self.lock = Lock(vhdutil.LOCK_TYPE_SR, self.uuid) 

        self.sr_vditype = vhdutil.VDI_TYPE_VHD 

95        if not self.dconf.has_key('location') or  not self.dconf['location']: 

            raise xs_errors.XenError('ConfigLocationMissing') 

        self.remotepath = self.dconf['location'] 

        self.path = os.path.join(SR.MOUNT_BASE, sr_uuid) 

        self.linkpath = self.path 

        self.mountpoint = self.path 

        self.attached = False 

        self.driver_config = DRIVER_CONFIG 

 

    def create(self, sr_uuid, size): 

        """ Create the SR.  The path must not already exist, or if it does,  

        it must be empty.  (This accounts for the case where the user has 

        mounted a device onto a directory manually and want to use this as the 

        root of a file-based SR.) """ 

        try: 

110            if util.ioretry(lambda: util.pathexists(self.remotepath)): 

                if len(util.ioretry(lambda: util.listdir(self.remotepath))) != 0: 

                    raise xs_errors.XenError('SRExists') 

            else: 

                try: 

122                    util.ioretry(lambda: os.mkdir(self.remotepath)) 

                except util.CommandException, inst: 

                    if inst.code == errno.EEXIST: 

                        raise xs_errors.XenError('SRExists') 

                    else: 

                        raise xs_errors.XenError('FileSRCreate', \ 

                              opterr='directory creation failure %d' \ 

                              % inst.code) 

        except: 

            raise xs_errors.XenError('FileSRCreate') 

 

    def delete(self, sr_uuid): 

        self.attach(sr_uuid) 

        cleanup.gc_force(self.session, self.uuid) 

 

        # check to make sure no VDIs are present; then remove old  

        # files that are non VDI's 

        try: 

            if util.ioretry(lambda: util.pathexists(self.path)): 

                #Load the VDI list 

                self._loadvdis() 

                for uuid in self.vdis: 

                    if not self.vdis[uuid].deleted: 

                        raise xs_errors.XenError('SRNotEmpty', \ 

                              opterr='VDIs still exist in SR') 

 

                # remove everything else, there are no vdi's 

                for name in util.ioretry(lambda: util.listdir(self.path)): 

                    fullpath =  os.path.join(self.path,name) 

                    try: 

                        util.ioretry(lambda: os.unlink(fullpath)) 

                    except util.CommandException, inst: 

                        if inst.code != errno.ENOENT and \ 

                           inst.code != errno.EISDIR: 

                            raise xs_errors.XenError('FileSRDelete', \ 

                                  opterr='failed to remove %s error %d' \ 

                                  % (fullpath, inst.code)) 

            self.detach(sr_uuid) 

        except util.CommandException, inst: 

            self.detach(sr_uuid) 

            raise xs_errors.XenError('FileSRDelete', \ 

                  opterr='error %d' % inst.code) 

 

    def attach(self, sr_uuid): 

        if not self._checkmount(): 

            try: 

                util.ioretry(lambda: util.makedirs(self.path)) 

            except util.CommandException, inst: 

                if inst.code != errno.EEXIST: 

                    raise xs_errors.XenError("FileSRCreate", \ 

                                             opterr='fail to create mount point. Errno is %s' % inst.code) 

            try: 

                util.pread(["mount", "--bind", self.remotepath, self.path]) 

            except util.CommandException, inst: 

                raise xs_errors.XenError('FileSRCreate', \ 

                                         opterr='fail to mount FileSR. Errno is %s' % inst.code) 

        self.attached = True 

 

    def detach(self, sr_uuid): 

        if self._checkmount(): 

            try: 

                util.SMlog("Aborting GC/coalesce") 

                cleanup.abort(self.uuid) 

                os.chdir(SR.MOUNT_BASE) 

                util.pread(["umount", self.path]) 

                os.rmdir(self.path) 

            except Exception, e: 

                raise xs_errors.XenError('SRInUse', opterr=str(e)) 

        self.attached = False 

 

    def scan(self, sr_uuid): 

        if not self._checkmount(): 

            raise xs_errors.XenError('SRUnavailable', \ 

                  opterr='no such directory %s' % self.path) 

 

192        if not self.vdis: 

            self._loadvdis() 

 

193        if not self.passthrough: 

            self.physical_size = self._getsize() 

            self.physical_utilisation  = self._getutilisation() 

 

197        for uuid in self.vdis.keys(): 

            if self.vdis[uuid].deleted: 

                del self.vdis[uuid] 

 

        # CA-15607: make sure we are robust to the directory being unmounted beneath 

        # us (eg by a confused user). Without this we might forget all our VDI references 

        # which would be a shame. 

        # For SMB SRs, this path is mountpoint 

        mount_path = self.path 

206        if self.handles("smb"): 

            mount_path = self.mountpoint 

 

209        if not self.handles("file") and not os.path.ismount(mount_path): 

            util.SMlog("Error: FileSR.scan called but directory %s isn't a mountpoint" % mount_path) 

            raise xs_errors.XenError('SRUnavailable', \ 

                                     opterr='not mounted %s' % mount_path) 

 

        self._kickGC() 

 

        # default behaviour from here on 

        return super(FileSR, self).scan(sr_uuid) 

 

    def update(self, sr_uuid): 

        if not self._checkmount(): 

            raise xs_errors.XenError('SRUnavailable', \ 

                  opterr='no such directory %s' % self.path) 

        self._update(sr_uuid, 0) 

 

    def _update(self, sr_uuid, virt_alloc_delta): 

        valloc = int(self.session.xenapi.SR.get_virtual_allocation(self.sr_ref)) 

        self.virtual_allocation = valloc + virt_alloc_delta 

        self.physical_size = self._getsize() 

        self.physical_utilisation  = self._getutilisation() 

        self._db_update() 

 

    def content_type(self, sr_uuid): 

        return super(FileSR, self).content_type(sr_uuid) 

 

    def vdi(self, uuid, loadLocked = False): 

        return FileVDI(self, uuid) 

 

    def added_vdi(self, vdi): 

        self.vdis[vdi.uuid] = vdi 

 

    def deleted_vdi(self, uuid): 

        if uuid in self.vdis: 

            del self.vdis[uuid] 

 

    def replay(self, uuid): 

        try: 

            file = open(self.path + "/filelog.txt", "r") 

            data = file.readlines() 

            file.close() 

            self._process_replay(data) 

        except: 

            raise xs_errors.XenError('SRLog') 

 

    def _loadvdis(self): 

        if self.vdis: 

            return 

 

        pattern = os.path.join(self.path, "*%s" % vhdutil.FILE_EXTN_VHD) 

        try: 

            self.vhds = vhdutil.getAllVHDs(pattern, FileVDI.extractUuid) 

        except util.CommandException, inst: 

            raise xs_errors.XenError('SRScan', opterr="error VHD-scanning " \ 

                    "path %s (%s)" % (self.path, inst)) 

        try: 

            list_vhds = [FileVDI.extractUuid(v) for v in util.ioretry(lambda: glob.glob(pattern))] 

            if len(self.vhds) != len(list_vhds): 

                util.SMlog("VHD scan returns %d VHDs: %s" % (len(self.vhds), sorted(list(self.vhds)))) 

                util.SMlog("VHD list returns %d VHDs: %s" % (len(list_vhds), sorted(list_vhds))) 

        except: 

            pass 

        for uuid in self.vhds.iterkeys(): 

            if self.vhds[uuid].error: 

                raise xs_errors.XenError('SRScan', opterr='uuid=%s' % uuid) 

            self.vdis[uuid] = self.vdi(uuid, True) 

            # Get the key hash of any encrypted VDIs: 

            vhd_path = os.path.join(self.path, self.vhds[uuid].path) 

            key_hash = vhdutil.getKeyHash(vhd_path) 

            self.vdis[uuid].sm_config_override['key_hash'] = key_hash 

 

        # raw VDIs and CBT log files 

        files = util.ioretry(lambda: util.listdir(self.path)) 

        for fn in files: 

            if fn.endswith(vhdutil.FILE_EXTN_RAW): 

                uuid = fn[:-(len(vhdutil.FILE_EXTN_RAW))] 

                self.vdis[uuid] = self.vdi(uuid, True) 

            elif fn.endswith(CBTLOG_TAG): 

                cbt_uuid = fn.split(".")[0] 

                # If an associated disk exists, update CBT status 

                # else create new VDI of type cbt_metadata 

                if self.vdis.has_key(cbt_uuid): 

                    self.vdis[cbt_uuid].cbt_enabled = True 

                else: 

                    new_vdi = self.vdi(cbt_uuid) 

                    new_vdi.ty = "cbt_metadata" 

                    new_vdi.cbt_enabled = True 

                    self.vdis[cbt_uuid] = new_vdi 

 

        # Mark parent VDIs as Read-only and generate virtual allocation 

        self.virtual_allocation = 0 

        for uuid, vdi in self.vdis.iteritems(): 

            if vdi.parent: 

                if self.vdis.has_key(vdi.parent): 

                    self.vdis[vdi.parent].read_only = True 

                if geneology.has_key(vdi.parent): 

                    geneology[vdi.parent].append(uuid) 

                else: 

                    geneology[vdi.parent] = [uuid] 

            if not vdi.hidden: 

                self.virtual_allocation += (vdi.size) 

 

        # now remove all hidden leaf nodes from self.vdis so that they are not  

        # introduced into the Agent DB when SR is synchronized. With the  

        # asynchronous GC, a deleted VDI might stay around until the next  

        # SR.scan, so if we don't ignore hidden leaves we would pick up  

        # freshly-deleted VDIs as newly-added VDIs 

        for uuid in self.vdis.keys(): 

            if not geneology.has_key(uuid) and self.vdis[uuid].hidden: 

                util.SMlog("Scan found hidden leaf (%s), ignoring" % uuid) 

                del self.vdis[uuid] 

 

    def _getsize(self): 

        path = self.path 

        if self.handles("smb"): 

            path = self.linkpath 

        return util.get_fs_size(path) 

 

    def _getutilisation(self): 

        return util.get_fs_utilisation(self.path) 

 

    def _replay(self, logentry): 

        # all replay commands have the same 5,6,7th arguments 

        # vdi_command, sr-uuid, vdi-uuid 

        back_cmd = logentry[5].replace("vdi_","") 

        target = self.vdi(logentry[7]) 

        cmd = getattr(target, back_cmd) 

        args = [] 

        for item in logentry[6:]: 

            item = item.replace("\n","") 

            args.append(item) 

        ret = cmd(*args) 

        if ret: 

            print ret 

 

    def _compare_args(self, a, b): 

        try: 

            if a[2] != "log:": 

                return 1 

            if b[2] != "end:" and b[2] != "error:": 

                return 1 

            if a[3] != b[3]: 

                return 1 

            if a[4] != b[4]: 

                return 1 

            return 0 

        except: 

            return 1 

 

    def _process_replay(self, data): 

        logentries=[] 

        for logentry in data: 

            logentry = logentry.split(" ") 

            logentries.append(logentry) 

        # we are looking for a log entry that has a log but no end or error 

        # wkcfix -- recreate (adjusted) logfile  

        index = 0 

        while index < len(logentries)-1: 

            if self._compare_args(logentries[index],logentries[index+1]): 

                self._replay(logentries[index]) 

            else: 

                # skip the paired one 

                index += 1 

            # next 

            index += 1 

 

    def _kickGC(self): 

        # don't bother if an instance already running (this is just an  

        # optimization to reduce the overhead of forking a new process if we  

        # don't have to, but the process will check the lock anyways) 

        lockRunning = Lock(cleanup.LOCK_TYPE_RUNNING, self.uuid) 

380        if not lockRunning.acquireNoblock(): 

            if cleanup.should_preempt(self.session, self.uuid): 

                util.SMlog("Aborting currently-running coalesce of garbage VDI") 

                try: 

                    if not cleanup.abort(self.uuid, soft=True): 

                        util.SMlog("The GC has already been scheduled to " 

                                "re-start") 

                except util.CommandException, e: 

                    if e.code != errno.ETIMEDOUT: 

                        raise 

                    util.SMlog('failed to abort the GC') 

                finally: 

                    return 

            else: 

                util.SMlog("A GC instance already running, not kicking") 

                return 

        else: 

            lockRunning.release() 

 

        util.SMlog("Kicking GC") 

        cleanup.gc(self.session, self.uuid, True) 

 

    def _isbind(self): 

        # os.path.ismount can't deal with bind mount 

        st1 = os.stat(self.path) 

        st2 = os.stat(self.remotepath) 

        return st1.st_dev == st2.st_dev and st1.st_ino == st2.st_ino 

 

    def _checkmount(self): 

        mount_path = self.path 

410        if self.handles("smb"): 

            mount_path = self.mountpoint 

 

        return util.ioretry(lambda: util.pathexists(mount_path) and \ 

                                (util.ismount(mount_path) or \ 

                                 util.pathexists(self.remotepath) and self._isbind())) 

 

 

class FileVDI(VDI.VDI): 

    PARAM_VHD = "vhd" 

    PARAM_RAW = "raw" 

    VDI_TYPE = { 

            PARAM_VHD : vhdutil.VDI_TYPE_VHD, 

            PARAM_RAW : vhdutil.VDI_TYPE_RAW 

    } 

 

    def _find_path_with_retries(self, vdi_uuid, maxretry=5, period=2.0): 

        vhd_path = os.path.join(self.sr.path, "%s.%s" % \ 

                                (vdi_uuid, self.PARAM_VHD)) 

        raw_path = os.path.join(self.sr.path, "%s.%s" % \ 

                                (vdi_uuid, self.PARAM_RAW)) 

        cbt_path = os.path.join(self.sr.path, "%s.%s" % 

                                (vdi_uuid, CBTLOG_TAG)) 

        found = False 

        tries = 0 

        while tries < maxretry and not found: 

            tries += 1 

            if util.ioretry(lambda: util.pathexists(vhd_path)): 

                self.vdi_type = vhdutil.VDI_TYPE_VHD 

                self.path = vhd_path 

                found = True 

            elif util.ioretry(lambda: util.pathexists(raw_path)): 

                self.vdi_type = vhdutil.VDI_TYPE_RAW 

                self.path = raw_path 

                self.hidden = False 

                found = True 

446            elif util.ioretry(lambda: util.pathexists(cbt_path)): 

                self.vdi_type = CBTLOG_TAG 

                self.path = cbt_path 

                self.hidden = False 

                found = True 

 

            if not found: 

                util.SMlog("VHD %s not found, retry %s of %s" % (vhd_path, tries, maxretry)) 

                time.sleep(period) 

 

        return found 

 

    def load(self, vdi_uuid): 

        self.lock = self.sr.lock 

 

        self.sr.srcmd.params['o_direct'] = self.sr.o_direct 

 

        if self.sr.srcmd.cmd == "vdi_create": 

            self.vdi_type = vhdutil.VDI_TYPE_VHD 

            self.key_hash = None 

            if self.sr.srcmd.params.has_key("vdi_sm_config"): 

                if self.sr.srcmd.params["vdi_sm_config"].has_key("key_hash"): 

                    self.key_hash = self.sr.srcmd.params["vdi_sm_config"]["key_hash"] 

 

                if self.sr.srcmd.params["vdi_sm_config"].has_key("type"): 

                    vdi_type = self.sr.srcmd.params["vdi_sm_config"]["type"] 

                    if not self.VDI_TYPE.get(vdi_type): 

                        raise xs_errors.XenError('VDIType', 

                                opterr='Invalid VDI type %s' % vdi_type) 

                    self.vdi_type = self.VDI_TYPE[vdi_type] 

            self.path = os.path.join(self.sr.path, "%s%s" % \ 

                    (vdi_uuid, vhdutil.FILE_EXTN[self.vdi_type])) 

        else: 

            found = self._find_path_with_retries(vdi_uuid) 

            if not found: 

                if self.sr.srcmd.cmd == "vdi_delete": 

                    # Could be delete for CBT log file 

                    self.path = os.path.join(self.sr.path, "%s.%s" % 

                                             (vdi_uuid, self.PARAM_VHD)) 

                    return 

                if self.sr.srcmd.cmd == "vdi_attach_from_config": 

                    return 

                raise xs_errors.XenError('VDIUnavailable', 

                                         opterr="VDI %s not found" % vdi_uuid) 

 

 

        if self.vdi_type == vhdutil.VDI_TYPE_VHD and \ 

                self.sr.__dict__.get("vhds") and self.sr.vhds.get(vdi_uuid): 

            # VHD info already preloaded: use it instead of querying directly 

            vhdInfo = self.sr.vhds[vdi_uuid] 

            self.utilisation = vhdInfo.sizePhys 

            self.size = vhdInfo.sizeVirt 

            self.hidden = vhdInfo.hidden 

            if self.hidden: 

                self.managed = False 

            self.parent = vhdInfo.parentUuid 

            if self.parent: 

                self.sm_config_override = {'vhd-parent':self.parent} 

            else: 

                self.sm_config_override = {'vhd-parent':None} 

            return 

 

        try: 

            # Change to the SR directory in case parent 

            # locator field path has changed 

            os.chdir(self.sr.path) 

        except Exception as chdir_exception: 

            util.SMlog("Unable to change to SR directory, SR unavailable, %s" % 

                       str(chdir_exception)) 

            raise xs_errors.XenError('SRUnavailable', opterr=str(chdir_exception)) 

 

        if util.ioretry( 

                lambda: util.pathexists(self.path), 

                errlist=[errno.EIO, errno.ENOENT]): 

            try: 

                st = util.ioretry(lambda: os.stat(self.path), 

                                  errlist=[errno.EIO, errno.ENOENT]) 

                self.utilisation = long(st.st_size) 

            except util.CommandException, inst: 

                if inst.code == errno.EIO: 

                    raise xs_errors.XenError('VDILoad', \ 

                          opterr='Failed load VDI information %s' % self.path) 

                else: 

                    util.SMlog("Stat failed for %s, %s" % ( 

                        self.path, str(inst))) 

                    raise xs_errors.XenError('VDIType', \ 

                          opterr='Invalid VDI type %s' % self.vdi_type) 

 

            if self.vdi_type == vhdutil.VDI_TYPE_RAW: 

                self.exists = True 

                self.size = self.utilisation 

                self.sm_config_override = {'type':self.PARAM_RAW} 

                return 

 

            if self.vdi_type == CBTLOG_TAG: 

                self.exists = True 

                self.size = self.utilisation 

                return 

 

            try: 

                # The VDI might be activated in R/W mode so the VHD footer 

                # won't be valid, use the back-up one instead. 

                diskinfo = util.ioretry( 

                    lambda: self._query_info(self.path, True), 

                    errlist=[errno.EIO, errno.ENOENT]) 

 

                if diskinfo.has_key('parent'): 

                    self.parent = diskinfo['parent'] 

                    self.sm_config_override = {'vhd-parent':self.parent} 

                else: 

                    self.sm_config_override = {'vhd-parent':None} 

                    self.parent = '' 

                self.size = long(diskinfo['size']) * 1024 * 1024 

                self.hidden = long(diskinfo['hidden']) 

                if self.hidden: 

                    self.managed = False 

                self.exists = True 

            except util.CommandException, inst: 

                raise xs_errors.XenError('VDILoad', \ 

                      opterr='Failed load VDI information %s' % self.path) 

 

    def update(self, sr_uuid, vdi_location): 

        self.load(vdi_location) 

        vdi_ref = self.sr.srcmd.params['vdi_ref'] 

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

        self._db_update() 

 

    def create(self, sr_uuid, vdi_uuid, size): 

        if util.ioretry(lambda: util.pathexists(self.path)): 

            raise xs_errors.XenError('VDIExists') 

 

        overhead = 0 

        if self.vdi_type == vhdutil.VDI_TYPE_VHD: 

            overhead = vhdutil.calcOverheadFull(long(size)) 

 

        # Test the amount of actual disk space 

        if ENFORCE_VIRT_ALLOC: 

            self.sr._loadvdis() 

            reserved = self.sr.virtual_allocation 

            sr_size = self.sr._getsize() 

            if (sr_size - reserved) < (long(size) + overhead): 

                raise xs_errors.XenError('SRNoSpace') 

 

        if self.vdi_type == vhdutil.VDI_TYPE_VHD: 

            try: 

                size = vhdutil.validate_and_round_vhd_size(long(size)) 

                mb = 1024L * 1024L 

                size_mb = long(size) / mb 

                util.ioretry(lambda: self._create(str(size_mb), self.path)) 

                self.size = util.ioretry(lambda: self._query_v(self.path)) 

            except util.CommandException, inst: 

                raise xs_errors.XenError('VDICreate', 

                        opterr='error %d' % inst.code) 

        else: 

            f = open(self.path, 'w') 

            f.truncate(long(size)) 

            f.close() 

            self.size = size 

 

        self.sr.added_vdi(self) 

 

        st = util.ioretry(lambda: os.stat(self.path)) 

        self.utilisation = long(st.st_size) 

        if self.vdi_type == vhdutil.VDI_TYPE_RAW: 

            self.sm_config = {"type": self.PARAM_RAW} 

 

        self._db_introduce() 

        self.sr._update(self.sr.uuid, self.size) 

        return super(FileVDI, self).get_params() 

 

    def delete(self, sr_uuid, vdi_uuid, data_only = False): 

        if not util.ioretry(lambda: util.pathexists(self.path)): 

            return super(FileVDI, self).delete(sr_uuid, vdi_uuid, data_only) 

 

        if self.attached: 

            raise xs_errors.XenError('VDIInUse') 

 

        try: 

            util.force_unlink(self.path) 

        except Exception, e: 

            raise xs_errors.XenError( 

                'VDIDelete', 

                opterr='Failed to unlink file during deleting VDI: %s' % str(e)) 

 

        self.sr.deleted_vdi(vdi_uuid) 

        # If this is a data_destroy call, don't remove from XAPI db 

        if not data_only: 

            self._db_forget() 

        self.sr._update(self.sr.uuid, -self.size) 

        self.sr.lock.cleanupAll(vdi_uuid) 

        self.sr._kickGC() 

        return super(FileVDI, self).delete(sr_uuid, vdi_uuid, data_only) 

 

    def attach(self, sr_uuid, vdi_uuid): 

        if self.path is None: 

            self._find_path_with_retries(vdi_uuid) 

        if not self._checkpath(self.path): 

            raise xs_errors.XenError('VDIUnavailable', \ 

                  opterr='VDI %s unavailable %s' % (vdi_uuid, self.path)) 

        try: 

            self.attached = True 

 

            if not hasattr(self,'xenstore_data'): 

                self.xenstore_data = {} 

 

            self.xenstore_data.update(scsiutil.update_XS_SCSIdata(vdi_uuid, \ 

                                                                      scsiutil.gen_synthetic_page_data(vdi_uuid))) 

 

            if self.sr.handles("file"): 

                    # XXX: PR-1255: if these are constants then they should 

                    # be returned by the attach API call, not persisted in the 

                    # pool database. 

                self.xenstore_data['storage-type']='ext' 

            return super(FileVDI, self).attach(sr_uuid, vdi_uuid) 

        except util.CommandException, inst: 

            raise xs_errors.XenError('VDILoad', opterr='error %d' % inst.code) 

 

    def detach(self, sr_uuid, vdi_uuid): 

        self.attached = False 

 

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

        return self.resize_online(sr_uuid, vdi_uuid, size) 

 

    def resize_online(self, sr_uuid, vdi_uuid, size): 

        if not self.exists: 

            raise xs_errors.XenError('VDIUnavailable', \ 

                  opterr='VDI %s unavailable %s' % (vdi_uuid, self.path)) 

 

        if self.vdi_type != vhdutil.VDI_TYPE_VHD: 

            raise xs_errors.XenError('Unimplemented') 

 

        if self.hidden: 

            raise xs_errors.XenError('VDIUnavailable', opterr='hidden VDI') 

 

        if size < self.size: 

            util.SMlog('vdi_resize: shrinking not supported: ' + \ 

                    '(current size: %d, new size: %d)' % (self.size, size)) 

            raise xs_errors.XenError('VDISize', opterr='shrinking not allowed') 

 

        if size == self.size: 

            return VDI.VDI.get_params(self) 

 

        # We already checked it is a VDI_TYPE_VHD 

        size = vhdutil.validate_and_round_vhd_size(long(size)) 

        overhead = vhdutil.calcOverheadFull(long(size)) 

 

        # Test the amount of actual disk space 

        if ENFORCE_VIRT_ALLOC: 

            self.sr._loadvdis() 

            reserved = self.sr.virtual_allocation 

            sr_size = self.sr._getsize() 

            delta = long(size - self.size) 

            if (sr_size - reserved) < delta: 

                raise xs_errors.XenError('SRNoSpace') 

        jFile = JOURNAL_FILE_PREFIX + self.uuid 

        try: 

            vhdutil.setSizeVirt(self.path, size, jFile) 

        except: 

            # Revert the operation 

            vhdutil.revert(self.path, jFile) 

            raise xs_errors.XenError('VDISize', opterr='resize operation failed') 

 

        old_size = self.size 

        self.size = vhdutil.getSizeVirt(self.path) 

        st = util.ioretry(lambda: os.stat(self.path)) 

        self.utilisation = long(st.st_size) 

 

        self._db_update() 

        self.sr._update(self.sr.uuid, self.size - old_size) 

        super(FileVDI, self).resize_cbt(self.sr.uuid, self.uuid, self.size) 

        return VDI.VDI.get_params(self) 

 

    def clone(self, sr_uuid, vdi_uuid): 

            return self._do_snapshot(sr_uuid, vdi_uuid, VDI.SNAPSHOT_DOUBLE) 

 

    def compose(self, sr_uuid, vdi1, vdi2): 

        if self.vdi_type != vhdutil.VDI_TYPE_VHD: 

            raise xs_errors.XenError('Unimplemented') 

        parent_fn = vdi1 + vhdutil.FILE_EXTN[vhdutil.VDI_TYPE_VHD] 

        parent_path = os.path.join(self.sr.path, parent_fn) 

        assert(util.pathexists(parent_path)) 

        vhdutil.setParent(self.path, parent_path, False) 

        vhdutil.setHidden(parent_path) 

        self.sr.session.xenapi.VDI.set_managed(self.sr.srcmd.params['args'][0], False) 

        util.pread2([vhdutil.VHD_UTIL, "modify", "-p", parent_path, 

            "-n", self.path]) 

        # Tell tapdisk the chain has changed 

        if not blktap2.VDI.tap_refresh(self.session, sr_uuid, vdi2): 

            raise util.SMException("failed to refresh VDI %s" % self.uuid) 

        util.SMlog("VDI.compose: relinked %s->%s" % (vdi2, vdi1)) 

 

    def reset_leaf(self, sr_uuid, vdi_uuid): 

        if self.vdi_type != vhdutil.VDI_TYPE_VHD: 

            raise xs_errors.XenError('Unimplemented') 

 

        # safety check 

        if not vhdutil.hasParent(self.path): 

            raise util.SMException("ERROR: VDI %s has no parent, " + \ 

                    "will not reset contents" % self.uuid) 

 

        vhdutil.killData(self.path) 

 

    def _do_snapshot(self, sr_uuid, vdi_uuid, snap_type, 

                     secondary=None, cbtlog=None): 

        # If cbt enabled, save file consistency state 

        if cbtlog is not None: 

            if blktap2.VDI.tap_status(self.session, vdi_uuid): 

                consistency_state = False 

            else: 

                consistency_state = True 

            util.SMlog("Saving log consistency state of %s for vdi: %s" % 

                       (consistency_state, vdi_uuid)) 

        else: 

            consistency_state = None 

 

        if self.vdi_type != vhdutil.VDI_TYPE_VHD: 

            raise xs_errors.XenError('Unimplemented') 

 

        if not blktap2.VDI.tap_pause(self.session, sr_uuid, vdi_uuid): 

            raise util.SMException("failed to pause VDI %s" % vdi_uuid) 

        try: 

            return self._snapshot(snap_type, cbtlog, consistency_state) 

        finally: 

            blktap2.VDI.tap_unpause(self.session, sr_uuid, vdi_uuid, secondary) 

 

    def _rename(self, src, dst): 

        util.SMlog("FileVDI._rename %s to %s" % (src, dst)) 

        util.ioretry(lambda: os.rename(src, dst)) 

 

    def _link(self, src, dst): 

        util.SMlog("FileVDI._link %s to %s" % (src, dst)) 

        os.link(src, dst) 

 

    def _unlink(self, path): 

        util.SMlog("FileVDI._unlink %s" % (path)) 

        os.unlink(path) 

 

    def _create_new_parent(self, src, newsrc): 

        sr_sm_config = self.session.xenapi.SR.get_sm_config(self.sr.sr_ref) 

        if SharedFileSR.NO_HARDLINK_SUPPORT in sr_sm_config: 

            self._rename(src, newsrc) 

        else: 

            self._link(src, newsrc) 

 

    def __fist_enospace(self): 

        raise util.CommandException(28, "vhd-util snapshot", reason="No space") 

 

    def _snapshot(self, snap_type, cbtlog=None, cbt_consistency=None): 

        util.SMlog("FileVDI._snapshot for %s (type %s)" % (self.uuid, snap_type)) 

 

        args = [] 

        args.append("vdi_clone") 

        args.append(self.sr.uuid) 

        args.append(self.uuid) 

 

        dest = None 

        dst = None 

        if snap_type == VDI.SNAPSHOT_DOUBLE: 

            dest = util.gen_uuid() 

            dst = os.path.join(self.sr.path, "%s.%s" % (dest,self.vdi_type)) 

            args.append(dest) 

 

        if self.hidden: 

            raise xs_errors.XenError('VDIClone', opterr='hidden VDI') 

 

        depth = vhdutil.getDepth(self.path) 

        if depth == -1: 

            raise xs_errors.XenError('VDIUnavailable', \ 

                  opterr='failed to get VHD depth') 

        elif depth >= vhdutil.MAX_CHAIN_SIZE: 

            raise xs_errors.XenError('SnapshotChainTooLong') 

 

        # Test the amount of actual disk space 

        if ENFORCE_VIRT_ALLOC: 

            self.sr._loadvdis() 

            reserved = self.sr.virtual_allocation 

            sr_size = self.sr._getsize() 

            num_vdis = 2 

            if (snap_type == VDI.SNAPSHOT_SINGLE or snap_type == VDI.SNAPSHOT_INTERNAL): 

                num_vdis = 1 

            if (sr_size - reserved) < ((self.size + VDI.VDIMetadataSize( \ 

                    vhdutil.VDI_TYPE_VHD, self.size)) * num_vdis): 

                raise xs_errors.XenError('SRNoSpace') 

 

        newuuid = util.gen_uuid() 

        src = self.path 

        newsrc = os.path.join(self.sr.path, "%s.%s" % (newuuid,self.vdi_type)) 

        newsrcname = "%s.%s" % (newuuid,self.vdi_type) 

 

        if not self._checkpath(src): 

            raise xs_errors.XenError('VDIUnavailable', \ 

                  opterr='VDI %s unavailable %s' % (self.uuid, src)) 

 

        # wkcfix: multiphase 

        util.start_log_entry(self.sr.path, self.path, args) 

 

        # We assume the filehandle has been released 

        try: 

            self._create_new_parent(src, newsrc) 

 

            # Create the snapshot under a temporary name, then rename 

            # it afterwards. This avoids a small window where it exists 

            # but is invalid. We do not need to do this for 

            # snap_type == VDI.SNAPSHOT_DOUBLE because dst never existed 

            # before so nobody will try to query it. 

            tmpsrc = "%s.%s" % (src, "new") 

            # Fault injection site to fail the snapshot with ENOSPACE 

            util.fistpoint.activate_custom_fn( 

                "FileSR_fail_snap1", 

                self.__fist_enospace) 

            util.ioretry(lambda: self._snap(tmpsrc, newsrcname)) 

            self._rename(tmpsrc, src) 

            if snap_type == VDI.SNAPSHOT_DOUBLE: 

                # Fault injection site to fail the snapshot with ENOSPACE 

                util.fistpoint.activate_custom_fn( 

                    "FileSR_fail_snap2", 

                    self.__fist_enospace) 

                util.ioretry(lambda: self._snap(dst, newsrcname)) 

            # mark the original file (in this case, its newsrc) 

            # as hidden so that it does not show up in subsequent scans 

            util.ioretry(lambda: self._mark_hidden(newsrc)) 

 

            #Verify parent locator field of both children and delete newsrc if unused 

            introduce_parent = True 

            try: 

                srcparent = util.ioretry(lambda: self._query_p_uuid(src)) 

                dstparent = None 

                if snap_type == VDI.SNAPSHOT_DOUBLE: 

                    dstparent = util.ioretry(lambda: self._query_p_uuid(dst)) 

                if srcparent != newuuid and \ 

                        (snap_type == VDI.SNAPSHOT_SINGLE or \ 

                        snap_type == VDI.SNAPSHOT_INTERNAL or \ 

                        dstparent != newuuid): 

                    util.ioretry(lambda: self._unlink(newsrc)) 

                    introduce_parent = False 

            except: 

                pass 

 

            # Introduce the new VDI records 

            leaf_vdi = None 

            if snap_type == VDI.SNAPSHOT_DOUBLE: 

                leaf_vdi = VDI.VDI(self.sr, dest) # user-visible leaf VDI 

                leaf_vdi.read_only = False 

                leaf_vdi.location = dest 

                leaf_vdi.size = self.size 

                leaf_vdi.utilisation = self.utilisation 

                leaf_vdi.sm_config = {} 

                leaf_vdi.sm_config['vhd-parent'] = dstparent 

                # If the parent is encrypted set the key_hash 

                # for the new snapshot disk 

                vdi_ref = self.sr.srcmd.params['vdi_ref'] 

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

                if "key_hash" in sm_config: 

                    leaf_vdi.sm_config['key_hash'] = sm_config['key_hash'] 

                # If we have CBT enabled on the VDI, 

                # set CBT status for the new snapshot disk 

                if cbtlog: 

                    leaf_vdi.cbt_enabled = True 

 

            base_vdi = None 

            if introduce_parent: 

                base_vdi = VDI.VDI(self.sr, newuuid) # readonly parent 

                base_vdi.label = "base copy" 

                base_vdi.read_only = True 

                base_vdi.location = newuuid 

                base_vdi.size = self.size 

                base_vdi.utilisation = self.utilisation 

                base_vdi.sm_config = {} 

                grandparent = util.ioretry(lambda: self._query_p_uuid(newsrc)) 

                if grandparent.find("no parent") == -1: 

                    base_vdi.sm_config['vhd-parent'] = grandparent 

 

            try: 

                if snap_type == VDI.SNAPSHOT_DOUBLE: 

                    leaf_vdi_ref = leaf_vdi._db_introduce() 

                    util.SMlog("vdi_clone: introduced VDI: %s (%s)" % \ 

                            (leaf_vdi_ref,dest)) 

 

                if introduce_parent: 

                    base_vdi_ref = base_vdi._db_introduce() 

                    self.session.xenapi.VDI.set_managed(base_vdi_ref, False) 

                    util.SMlog("vdi_clone: introduced VDI: %s (%s)" % (base_vdi_ref,newuuid)) 

                vdi_ref = self.sr.srcmd.params['vdi_ref'] 

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

                sm_config['vhd-parent'] = srcparent 

                self.session.xenapi.VDI.set_sm_config(vdi_ref, sm_config) 

            except Exception, e: 

                util.SMlog("vdi_clone: caught error during VDI.db_introduce: %s" % (str(e))) 

                # Note it's too late to actually clean stuff up here: the base disk has 

                # been marked as deleted already. 

                util.end_log_entry(self.sr.path, self.path, ["error"]) 

                raise 

        except util.CommandException, inst: 

            # XXX: it might be too late if the base disk has been marked as deleted! 

            self._clonecleanup(src,dst,newsrc) 

            util.end_log_entry(self.sr.path, self.path, ["error"]) 

            raise xs_errors.XenError('VDIClone', 

                  opterr='VDI clone failed error %d' % inst.code) 

 

        # Update cbt files if user created snapshot (SNAPSHOT_DOUBLE) 

        if snap_type == VDI.SNAPSHOT_DOUBLE and cbtlog: 

            try: 

                self._cbt_snapshot(dest, cbt_consistency) 

            except: 

                # CBT operation failed. 

                util.end_log_entry(self.sr.path, self.path, ["error"]) 

                raise 

 

        util.end_log_entry(self.sr.path, self.path, ["done"]) 

        if snap_type != VDI.SNAPSHOT_INTERNAL: 

            self.sr._update(self.sr.uuid, self.size) 

        # Return info on the new user-visible leaf VDI 

        ret_vdi = leaf_vdi 

        if not ret_vdi: 

            ret_vdi = base_vdi 

        if not ret_vdi: 

            ret_vdi = self 

        return ret_vdi.get_params() 

 

    def get_params(self): 

        if not self._checkpath(self.path): 

            raise xs_errors.XenError('VDIUnavailable', \ 

                  opterr='VDI %s unavailable %s' % (self.uuid, self.path)) 

        return super(FileVDI, self).get_params() 

 

    def _snap(self, child, parent): 

        cmd = [SR.TAPDISK_UTIL, "snapshot", vhdutil.VDI_TYPE_VHD, child, parent] 

        text = util.pread(cmd) 

 

    def _clonecleanup(self,src,dst,newsrc): 

        try: 

            if dst: 

                util.ioretry(lambda: self._unlink(dst)) 

        except util.CommandException as inst: 

            pass 

        try: 

            if util.ioretry(lambda: util.pathexists(newsrc)): 

                stats = os.stat(newsrc) 

                # Check if we have more than one link to newsrc 

                if (stats.st_nlink > 1): 

                    util.ioretry(lambda: self._unlink(newsrc)) 

                elif not self._is_hidden(newsrc): 

                    self._rename(newsrc, src) 

        except util.CommandException as inst: 

            pass 

 

    def _checkpath(self, path): 

        try: 

            if not util.ioretry(lambda: util.pathexists(path)): 

                return False 

            return True 

        except util.CommandException, inst: 

            raise xs_errors.XenError('EIO', \ 

                  opterr='IO error checking path %s' % path) 

 

    def _query_v(self, path): 

        cmd = [SR.TAPDISK_UTIL, "query", vhdutil.VDI_TYPE_VHD, "-v", path] 

        return long(util.pread(cmd)) * 1024 * 1024 

 

    def _query_p_uuid(self, path): 

        cmd = [SR.TAPDISK_UTIL, "query", vhdutil.VDI_TYPE_VHD, "-p", path] 

        parent = util.pread(cmd) 

        parent = parent[:-1] 

        ls = parent.split('/') 

        return ls[len(ls) - 1].replace(vhdutil.FILE_EXTN_VHD, '') 

 

    def _query_info(self, path, use_bkp_footer=False): 

        diskinfo = {} 

        qopts = '-vpf' 

        if use_bkp_footer: 

            qopts += 'b' 

        cmd = [SR.TAPDISK_UTIL, "query", vhdutil.VDI_TYPE_VHD, qopts, path] 

        txt = util.pread(cmd).split('\n') 

        diskinfo['size'] = txt[0] 

        lst = [txt[1].split('/')[-1].replace(vhdutil.FILE_EXTN_VHD, "")] 

        for val in filter(util.exactmatch_uuid, lst): 

            diskinfo['parent'] = val 

        diskinfo['hidden'] = txt[2].split()[1] 

        return diskinfo 

 

    def _create(self, size, path): 

        cmd = [SR.TAPDISK_UTIL, "create", vhdutil.VDI_TYPE_VHD, size, path] 

        text = util.pread(cmd) 

        if self.key_hash: 

            vhdutil.setKey(path, self.key_hash) 

 

    def _mark_hidden(self, path): 

        vhdutil.setHidden(path, True) 

        self.hidden = 1 

 

    def _is_hidden(self, path): 

        return vhdutil.getHidden(path) == 1 

 

    def extractUuid(path): 

        fileName = os.path.basename(path) 

        uuid = fileName.replace(vhdutil.FILE_EXTN_VHD, "") 

        return uuid 

    extractUuid = staticmethod(extractUuid) 

 

    def generate_config(self, sr_uuid, vdi_uuid): 

        """ 

        Generate the XML config required to attach and activate 

        a VDI for use when XAPI is not running. Attach and 

        activation is handled by vdi_attach_from_config below. 

        """ 

        util.SMlog("FileVDI.generate_config") 

        if not util.pathexists(self.path): 

            raise xs_errors.XenError('VDIUnavailable') 

        resp = {} 

        resp['device_config'] = self.sr.dconf 

        resp['sr_uuid'] = sr_uuid 

        resp['vdi_uuid'] = vdi_uuid 

        resp['command'] = 'vdi_attach_from_config' 

        # Return the 'config' encoded within a normal XMLRPC response so that 

        # we can use the regular response/error parsing code. 

        config = xmlrpclib.dumps(tuple([resp]), "vdi_attach_from_config") 

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

 

    def attach_from_config(self, sr_uuid, vdi_uuid): 

        """ 

        Attach and activate a VDI using config generated by 

        vdi_generate_config above. This is used for cases such as 

        the HA state-file and the redo-log. 

        """ 

        util.SMlog("FileVDI.attach_from_config") 

        try: 

            if not util.pathexists(self.sr.path): 

                self.sr.attach(sr_uuid) 

        except: 

            util.logException("FileVDI.attach_from_config") 

            raise xs_errors.XenError( 

                'SRUnavailable', 

                opterr='Unable to attach from config' 

            ) 

 

    def _create_cbt_log(self): 

        # Create CBT log file 

        # Name: <vdi_uuid>.cbtlog 

        #Handle if file already exists 

        log_path = self._get_cbt_logpath(self.uuid) 

        open_file = open(log_path, "w+") 

        open_file.close() 

        return super(FileVDI, self)._create_cbt_log() 

 

    def _delete_cbt_log(self): 

        logPath = self._get_cbt_logpath(self.uuid) 

        try: 

            os.remove(logPath) 

        except OSError as e: 

            if e.errno != errno.ENOENT: 

                raise 

 

    def _cbt_log_exists(self, logpath): 

        return util.pathexists(logpath) 

 

 

class SharedFileSR(FileSR): 

    """ 

    FileSR subclass for SRs that use shared network storage 

    """ 

    NO_HARDLINK_SUPPORT = "no_hardlinks" 

 

    def _raise_hardlink_error(self): 

        raise OSError(542, "Unknown error 524") 

 

    def _check_hardlinks(self): 

        test_name = os.path.join(self.path, str(uuid4())) 

        open(test_name, 'ab').close() 

 

        link_name = '%s.new' % test_name 

        try: 

            # XSI-1100: Fail the link operation 

            util.fistpoint.activate_custom_fn( 

                "FileSR_fail_hardlink", 

                self._raise_hardlink_error) 

 

            os.link(test_name, link_name) 

            self.session.xenapi.SR.remove_from_sm_config( 

                self.sr_ref, SharedFileSR.NO_HARDLINK_SUPPORT) 

        except OSError: 

            msg = "File system for SR %s does not support hardlinks, crash " \ 

                "consistency of snapshots cannot be assured" % self.uuid 

            util.SMlog(msg, priority=util.LOG_WARNING) 

            try: 

                self.session.xenapi.SR.add_to_sm_config( 

                    self.sr_ref, SharedFileSR.NO_HARDLINK_SUPPORT, 'True') 

                self.session.xenapi.message.create( 

                    "sr_does_not_support_hardlinks", 2, "SR", self.uuid, 

                    msg) 

            except XenAPI.Failure: 

                # Might already be set and checking has TOCTOU issues 

                pass 

        finally: 

            util.force_unlink(link_name) 

            util.force_unlink(test_name) 

 

 

1143if __name__ == '__main__': 

    SRCommand.run(FileSR, DRIVER_INFO) 

else: 

    SR.registerSR(FileSR)