def test_deleted_subvolumes(self):
     subvol = os.path.join(self.mountpoint, 'subvol')
     btrfsutil.create_subvolume(subvol + '1')
     btrfsutil.delete_subvolume(subvol + '1')
     for arg in self.path_or_fd(self.mountpoint):
         with self.subTest(type=type(arg)):
             self.assertEqual(btrfsutil.deleted_subvolumes(arg), [256])
 def test_deleted_subvolumes(self):
     subvol = os.path.join(self.mountpoint, 'subvol')
     btrfsutil.create_subvolume(subvol + '1')
     btrfsutil.delete_subvolume(subvol + '1')
     for arg in self.path_or_fd(self.mountpoint):
         with self.subTest(type=type(arg)):
             self.assertEqual(btrfsutil.deleted_subvolumes(arg), [256])
Exemple #3
0
    def delete_snapshot(self, snapshot: Subvolume) -> None:
        logger = self._logger
        filesystem_path = snapshot.filesystem_path
        logical_path = snapshot.logical_path

        try:
            filesystem_path_str = str(filesystem_path)
            is_subvolume = filesystem_path.exists() and btrfsutil.is_subvolume(
                filesystem_path_str)

            if is_subvolume:
                root_dir_str = str(constants.ROOT_DIR)
                num_id = snapshot.num_id
                deleted_subvolumes = checked_cast(
                    list[int], btrfsutil.deleted_subvolumes(root_dir_str))

                if num_id not in deleted_subvolumes:
                    logger.info(f"Deleting the '{logical_path}' snapshot.")

                    btrfsutil.delete_subvolume(filesystem_path_str)
                else:
                    logger.warning(
                        f"The '{logical_path}' snapshot has already "
                        "been deleted but not yet cleaned up.")
            else:
                logger.warning(
                    f"The '{filesystem_path}' directory is not a subvolume.")
        except btrfsutil.BtrfsUtilError as e:
            logger.exception("btrfsutil call failed!")
            raise SubvolumeError(
                f"Could not delete the '{logical_path}' snapshot!") from e
 def _create_and_delete_subvolume(i):
     dir_name = f'dir{i}'
     subvol_name = dir_name + '/subvol'
     while True:
         os.mkdir(dir_name)
         btrfsutil.create_subvolume(subvol_name)
         btrfsutil.delete_subvolume(subvol_name)
         os.rmdir(dir_name)
Exemple #5
0
def purge_old_snapshots(directory: Path, keep: int):
    if keep < 1:
        raise globalstuff.Bug('Argument "keep" must be an integer >= 1')
    snapshots = snapshotkit2.snapshot_dict(
        snapshotkit2.scan_dir(directory, bnames.filter), bnames.parse_path)
    keys = list(snapshots)
    if len(keys) > keep:
        keys.sort(reverse=True)
        for r in range(keep):
            logger.debug(f'Keeping subvolume "{str(snapshots[keys[r]])}"')
            del snapshots[keys[r]]
        for v in snapshots.values():
            logger.debug(f'Deleting subvolume "{str(v)}"')
            btrfsutil.delete_subvolume(v)
Exemple #6
0
def mocker_rmi(uuid1):
    '''+
    rmi <image_id> - удаляет
    ранее созданный образ из локального хранилища.
    '''
    if uuid1[0:4] == "img_":
        if mocker_check(uuid1) == 1:
            print('No image named ' + str(uuid1))
            return
        btrfsutil.delete_subvolume(btrfs_path + '/' + str(uuid1))
        cg = Cgroup(uuid1)
        cg.delete()
        print('Removed ' + str(uuid1))
    else:
        print('This is not image')
Exemple #7
0
def mocker_rm(uuid1):
    '''+
    rm <container_id> - удаляет ранее
    созданный контейнер
    '''
    if uuid1[0:3] == "ps_":
        if mocker_check(uuid1) == 1:
            print('No container named ' + str(uuid1))
            return
        btrfsutil.delete_subvolume(btrfs_path + '/' + str(uuid1))
        cg = Cgroup(uuid1)
        cg.delete()
        netns_name = 'netns_' + str(uuid1)
        netns.remove(netns_name)
        print('Removed ' + str(uuid1))
    else:
        print('This is not container')
Exemple #8
0
def mocker_rmi(uuid1):
    '''+
    rmi <image_id> - удаляет
    ранее созданный образ из локального хранилища.
    '''
    '''
    	[[ "$(bocker_check "$1")" == 1 ]] && echo "No container named '$1' exists" && exit 1
	btrfs subvolume delete "$btrfs_path/$1" > /dev/null
	cgdelete -g "$cgroups:/$1" &> /dev/null || true
	echo "Removed: $1"
    '''

    if mocker_check(uuid1) == 1:
        print('No container named ' + str(uuid1))
        return
    btrfsutil.delete_subvolume(btrfs_path + '/' + str(uuid1))
    cg = Cgroup(uuid1)
    cg.remove(uuid1)
    print('Removed ' + str(uuid1))
    # Cgroup.remove(uuid)

    pass
    def test_delete_subvolume(self):
        subvol = os.path.join(self.mountpoint, 'subvol')
        btrfsutil.create_subvolume(subvol + '1')
        self.assertTrue(os.path.exists(subvol + '1'))
        btrfsutil.create_subvolume(subvol + '2')
        self.assertTrue(os.path.exists(subvol + '2'))
        btrfsutil.create_subvolume(subvol + '3')
        self.assertTrue(os.path.exists(subvol + '3'))

        btrfsutil.delete_subvolume(subvol + '1')
        self.assertFalse(os.path.exists(subvol + '1'))
        btrfsutil.delete_subvolume((subvol + '2').encode())
        self.assertFalse(os.path.exists(subvol + '2'))
        if HAVE_PATH_LIKE:
            btrfsutil.delete_subvolume(PurePath(subvol + '3'))
            self.assertFalse(os.path.exists(subvol + '3'))

        # Test deleting subvolumes under '/' in a chroot.
        pid = os.fork()
        if pid == 0:
            try:
                os.chroot(self.mountpoint)
                os.chdir('/')
                btrfsutil.create_subvolume('/subvol4')
                self.assertTrue(os.path.exists('/subvol4'))
                btrfsutil.delete_subvolume('/subvol4')
                self.assertFalse(os.path.exists('/subvol4'))
                with self.assertRaises(btrfsutil.BtrfsUtilError):
                    btrfsutil.delete_subvolume('/')
                os._exit(0)
            except Exception:
                traceback.print_exc()
                os._exit(1)
        wstatus = os.waitpid(pid, 0)[1]
        self.assertTrue(os.WIFEXITED(wstatus))
        self.assertEqual(os.WEXITSTATUS(wstatus), 0)

        btrfsutil.create_subvolume(subvol + '5')
        btrfsutil.create_subvolume(subvol + '5/foo')
        btrfsutil.create_subvolume(subvol + '5/bar')
        btrfsutil.create_subvolume(subvol + '5/bar/baz')
        btrfsutil.create_subvolume(subvol + '5/bar/qux')
        btrfsutil.create_subvolume(subvol + '5/quux')
        with self.assertRaises(btrfsutil.BtrfsUtilError):
            btrfsutil.delete_subvolume(subvol + '5')
        btrfsutil.delete_subvolume(subvol + '5', recursive=True)
        self.assertFalse(os.path.exists(subvol + '5'))
    def test_delete_subvolume(self):
        subvol = os.path.join(self.mountpoint, 'subvol')
        btrfsutil.create_subvolume(subvol + '1')
        self.assertTrue(os.path.exists(subvol + '1'))
        btrfsutil.create_subvolume(subvol + '2')
        self.assertTrue(os.path.exists(subvol + '2'))
        btrfsutil.create_subvolume(subvol + '3')
        self.assertTrue(os.path.exists(subvol + '3'))

        btrfsutil.delete_subvolume(subvol + '1')
        self.assertFalse(os.path.exists(subvol + '1'))
        btrfsutil.delete_subvolume((subvol + '2').encode())
        self.assertFalse(os.path.exists(subvol + '2'))
        if HAVE_PATH_LIKE:
            btrfsutil.delete_subvolume(PurePath(subvol + '3'))
            self.assertFalse(os.path.exists(subvol + '3'))

        # Test deleting subvolumes under '/' in a chroot.
        pid = os.fork()
        if pid == 0:
            try:
                os.chroot(self.mountpoint)
                os.chdir('/')
                btrfsutil.create_subvolume('/subvol4')
                self.assertTrue(os.path.exists('/subvol4'))
                btrfsutil.delete_subvolume('/subvol4')
                self.assertFalse(os.path.exists('/subvol4'))
                with self.assertRaises(btrfsutil.BtrfsUtilError):
                    btrfsutil.delete_subvolume('/')
                os._exit(0)
            except Exception:
                traceback.print_exc()
                os._exit(1)
        wstatus = os.waitpid(pid, 0)[1]
        self.assertTrue(os.WIFEXITED(wstatus))
        self.assertEqual(os.WEXITSTATUS(wstatus), 0)

        btrfsutil.create_subvolume(subvol + '5')
        btrfsutil.create_subvolume(subvol + '5/foo')
        btrfsutil.create_subvolume(subvol + '5/bar')
        btrfsutil.create_subvolume(subvol + '5/bar/baz')
        btrfsutil.create_subvolume(subvol + '5/bar/qux')
        btrfsutil.create_subvolume(subvol + '5/quux')
        with self.assertRaises(btrfsutil.BtrfsUtilError):
            btrfsutil.delete_subvolume(subvol + '5')
        btrfsutil.delete_subvolume(subvol + '5', recursive=True)
        self.assertFalse(os.path.exists(subvol + '5'))
Exemple #11
0
 def rollback(self):
     for v in self._snapshots.values():
         btrfsutil.delete_subvolume(v)
         logger.debug('Rollback: Removed snapshot "%s"', v)
     self._state = State.UNDONE
Exemple #12
0
def delete(volume):
    if not volume.exists():
        raise VolumeNotFoundError(volume.name())
    btfrs.delete_subvolume(str(volume.path()))
    print('Deleted: ' + volume.name())