# sanity check:  ensure device is now mounted
        if not self.is_mounted:
            self.error("Device is not mounted")
        self.infomsg("Device successfully mounted\n")

    def safe_teardown(self):
        self.infomsg("Unmounting directory %s" % self.parent_device)

        # sanity check:  if mount point not mounted upon, nothing to do
        if not self.is_mounted_by_something:
            self.infomsg("Mount point not mounted upon; nothing to do\n")
            return
        self.debugmsg("  Sanity check passed:  mount point is mounted upon")

        # do the umount
        self.do_umount()
        self.infomsg("  Ran 'umount' command")

        # remove the mount directory
        self.remove_mount_point()

        # sanity check:  ensure device is now unmounted
        if self.is_mounted_by_something:
            self.error("Device is still mounted; umount failed")
        self.infomsg("Device successfully unmounted\n")


# Register this layer
Stack.register_layer(MountPartition)
        #session.xenapi.login_with_password(self.username, self.password)

    @property
    def vdi_name_label(self):
        return self.arg_str

    @property
    def vdi_record(self):
        for (key, val) in self.session.xenapi.VDI.get_all_records().items():
            if val['name_label'] == self.vdi_name_label:
                return val
        return None

    @property
    def sr_record(self):
        try:
            sr = self.session.xenapi.SR.get_record(self.vdi_record['SR'])
        except Exception, e:
            self.error("Unable to get SR:  %s" % e)
        return sr

    @property
    def orig_device(self):
        return '/dev/VG_XenStorage-%s/VHD-%s' % ( self.sr_record['uuid'],
                                                  self.vdi_record['uuid'] )
    

# Register this layer
if have_xenserver:
    Stack.register_layer(XenVDISnapLayer)
        (res,stdout,stderr) = self.run_cmd(cmd)
        return res

    @property
    def is_snapshot(self):
        cmd = ['lvs', '--noheadings', '-o', 'lv_attr', self.device]
        (res,stdout,stderr) = self.run_cmd(cmd)
        return stdout.lstrip().rstrip().lower()[0] == 's'

    @property
    def matches_target(self):
        cmd = ['lvs', '--noheadings', '-o', 'origin', self.device]
        (res,stdout,stderr) = self.run_cmd(cmd)
        return stdout.lstrip().rstrip() == self.lv_name

    def create_snapshot(self):
        cmd = [self.lvcreate, '-s', '-n', self.device,
               '-L', self.size, self.orig_device]
        (res,stdout,stderr) = self.run_cmd(cmd)

        self.infomsg("  Ran 'lvcreate' command")
        
    def remove_snapshot(self):
        # delete the snapshot
        cmd = [self.lvremove, '-f', self.device]
        (res,stdout,stderr) = self.run_cmd(cmd)

        self.infomsg("  Ran 'lvremove' command")

Stack.register_layer(LV)
            self.debugmsg('      waiting for %s; currently %s @ %s' %
                          (('detach','attach')[detach],
                           ('detached','attached')[self.device_exists],
                           self.timestr))
            time.sleep(1)

        self.error("Failed to attach/detach disk device '%s' "
                   "within %d seconds" %
                   (self.device, self.params.libvirt_attach_timeout))

    def create_snapshot(self):
        self.libvirt_storage_volume_attach()
        # Wait a bit for volume to be attached
        self.wait_attach()

    def remove_snapshot(self):
        self.libvirt_storage_volume_detach()
        # Wait a bit for volume to be detached
        self.wait_attach(detach=True)

    def freshen(self):
        '''
        Be sure the storage pool is updated whenever setting
        up/tearing down
        '''
        self.libvirt_storage_pool_refresh()

# Register this layer
if have_libvirt:
    Stack.register_layer(LibvirtVolLayer)