def umount_root(root_pool_mnt): if is_mounted(root_pool_mnt): run_command([UMOUNT, "-l", root_pool_mnt]) for i in range(10): if not is_mounted(root_pool_mnt): return run_command([RMDIR, root_pool_mnt]) time.sleep(1) run_command([UMOUNT, "-f", root_pool_mnt]) if os.path.exists(root_pool_mnt): return run_command([RMDIR, root_pool_mnt]) return True
def umount_root(root_pool_mnt): if (is_mounted(root_pool_mnt)): run_command([UMOUNT, '-l', root_pool_mnt]) for i in range(10): if (not is_mounted(root_pool_mnt)): return run_command([RMDIR, root_pool_mnt]) time.sleep(1) run_command([UMOUNT, '-f', root_pool_mnt]) if (os.path.exists(root_pool_mnt)): return run_command([RMDIR, root_pool_mnt]) return True
def mount_source(source_ip, source_share): mnt_pt = ('/mnt/backup/%s_%s' % (source_ip, source_share)) if (is_mounted(mnt_pt)): return True if (not os.path.isdir(mnt_pt)): os.makedirs(mnt_pt) cmd = [MOUNT, '%s:%s' % (source_ip, source_share), mnt_pt] return run_command(cmd)
def mount_share(share, pool_device, mnt_pt): if is_mounted(mnt_pt): return pool_device = "/dev/%s" % pool_device mount_root(share.pool, pool_device) subvol_str = "subvol=%s" % share.subvol_name create_tmp_dir(mnt_pt) mnt_cmd = [MOUNT, "-t", "btrfs", "-o", subvol_str, pool_device, mnt_pt] return run_command(mnt_cmd)
def mount_share(share, mnt_pt): if (is_mounted(mnt_pt)): return mount_root(share["pool"]) pool_device = ('/dev/%s' % share["pool"]["disks"][0]["name"]) subvol_str = 'subvol=%s' % share["subvol_name"] create_tmp_dir(mnt_pt) mnt_cmd = [MOUNT, '-t', 'btrfs', '-o', subvol_str, pool_device, mnt_pt] return run_command(mnt_cmd)
def mount_share(share, mnt_pt): if (is_mounted(mnt_pt)): return mount_root(share.pool) pool_device = ('/dev/%s' % share.pool.disk_set.first().name) subvol_str = 'subvol=%s' % share.subvol_name create_tmp_dir(mnt_pt) mnt_cmd = [MOUNT, '-t', 'btrfs', '-o', subvol_str, pool_device, mnt_pt] return run_command(mnt_cmd)
def remove_snap(pool, pool_device, share_name, snap_name): root_mnt = mount_root(pool, pool_device) snap_path = "%s/.snapshots/%s/%s" % (root_mnt, share_name, snap_name) if not os.path.exists(snap_path): return remove_snap_old(pool, pool_device, share_name, snap_name) if is_mounted(snap_path): umount_root(snap_path) if is_subvol(snap_path): return run_command([BTRFS, "subvolume", "delete", snap_path]) return True
def remove_snap(pool, pool_device, share_name, snap_name): root_mnt = mount_root(pool, pool_device) snap_path = ('%s/.snapshots/%s/%s' % (root_mnt, share_name, snap_name)) if (not os.path.exists(snap_path)): return remove_snap_old(pool, pool_device, share_name, snap_name) if (is_mounted(snap_path)): umount_root(snap_path) if (is_subvol(snap_path)): return run_command([BTRFS, 'subvolume', 'delete', snap_path]) return True
def mount_share(share, mnt_pt): if (is_mounted(mnt_pt)): return mount_root(share.pool) pool_device = ('/dev/disk/by-id/%s' % share.pool.disk_set.attached().first().target_name) subvol_str = 'subvol=%s' % share.subvol_name create_tmp_dir(mnt_pt) toggle_path_rw(mnt_pt, rw=False) mnt_cmd = [MOUNT, '-t', 'btrfs', '-o', subvol_str, pool_device, mnt_pt] return run_command(mnt_cmd)
def mount_snap(share, snap_name, snap_mnt=None): pool_device = "/dev/%s" % share.pool.disk_set.first().name share_path = "%s%s" % (DEFAULT_MNT_DIR, share.name) rel_snap_path = ".snapshots/%s/%s" % (share.name, snap_name) snap_path = "%s%s/%s" % (DEFAULT_MNT_DIR, share.pool.name, rel_snap_path) if snap_mnt is None: snap_mnt = "%s/.%s" % (share_path, snap_name) if is_mounted(snap_mnt): return mount_share(share, share_path) if is_subvol(snap_path): create_tmp_dir(snap_mnt) return run_command([MOUNT, "-o", "subvol=%s" % rel_snap_path, pool_device, snap_mnt])
def mount_snap(share, snap_name, snap_mnt=None): pool_device = ('/dev/%s' % share.pool.disk_set.first().name) share_path = ('%s%s' % (DEFAULT_MNT_DIR, share.name)) rel_snap_path = ('.snapshots/%s/%s' % (share.name, snap_name)) snap_path = ('%s%s/%s' % (DEFAULT_MNT_DIR, share.pool.name, rel_snap_path)) if (snap_mnt is None): snap_mnt = ('%s/.%s' % (share_path, snap_name)) if (is_mounted(snap_mnt)): return mount_share(share, share_path) if (is_subvol(snap_path)): create_tmp_dir(snap_mnt) return run_command( [MOUNT, '-o', 'subvol=%s' % rel_snap_path, pool_device, snap_mnt])
def mount_snap(share, snap_name, snap_mnt=None): pool_device = ('/dev/%s' % share.pool.disk_set.first().name) share_path = ('%s%s' % (DEFAULT_MNT_DIR, share.name)) rel_snap_path = ('.snapshots/%s/%s' % (share.name, snap_name)) snap_path = ('%s%s/%s' % (DEFAULT_MNT_DIR, share.pool.name, rel_snap_path)) if (snap_mnt is None): snap_mnt = ('%s/.%s' % (share_path, snap_name)) if (is_mounted(snap_mnt)): return mount_share(share, share_path) if (is_subvol(snap_path)): create_tmp_dir(snap_mnt) return run_command([MOUNT, '-o', 'subvol=%s' % rel_snap_path, pool_device, snap_mnt])
def remove_snap(pool, share_name, snap_name): root_mnt = mount_root(pool) snap_path = "%s/.snapshots/%s/%s" % (root_mnt, share_name, snap_name) if is_mounted(snap_path): umount_root(snap_path) if is_subvol(snap_path): qgroup = "0/%s" % share_id(pool, snap_name) run_command([BTRFS, "subvolume", "delete", snap_path], log=True) return qgroup_destroy(qgroup, root_mnt) else: o, e, rc = run_command([BTRFS, "subvolume", "list", "-s", root_mnt]) for l in o: # just give the first match. if re.match("ID.*%s$" % snap_name, l) is not None: snap = "%s/%s" % (root_mnt, l.split()[-1]) return run_command([BTRFS, "subvolume", "delete", snap], log=True)
def remove_snap(pool, share_name, snap_name): root_mnt = mount_root(pool) snap_path = ('%s/.snapshots/%s/%s' % (root_mnt, share_name, snap_name)) if (is_mounted(snap_path)): umount_root(snap_path) if (is_subvol(snap_path)): qgroup = ('0/%s' % share_id(pool, snap_name)) run_command([BTRFS, 'subvolume', 'delete', snap_path], log=True) return qgroup_destroy(qgroup, root_mnt) else: o, e, rc = run_command([BTRFS, 'subvolume', 'list', '-s', root_mnt]) for l in o: #just give the first match. if (re.match('ID.*%s$' % snap_name, l) is not None): snap = '%s/%s' % (root_mnt, l.split()[-1]) return run_command([BTRFS, 'subvolume', 'delete', snap], log=True)
def remove_snap(pool, pool_device, share_name, snap_name): root_mnt = mount_root(pool, pool_device) snap_path = ('%s/.snapshots/%s/%s' % (root_mnt, share_name, snap_name)) if (is_mounted(snap_path)): umount_root(snap_path) if (is_subvol(snap_path)): return run_command([BTRFS, 'subvolume', 'delete', snap_path]) else: o, e, rc = run_command([BTRFS, 'subvolume', 'list', '-s', root_mnt]) snap = None for l in o: #just give the first match. if (re.match('ID.*%s$' % snap_name, l) is not None): snap = '%s/%s' % (root_mnt, l.split()[-1]) break e_msg = ('This snapshot(%s) was created outside of Rockstor. If you ' 'really want to delete it, you can do so manually with this ' 'command: btrfs subvol delete %s' % (snap_name, snap)) raise Exception(e_msg)
def umount_root(root_pool_mnt): if (not os.path.exists(root_pool_mnt)): return try: o, e, rc = run_command([UMOUNT, '-l', root_pool_mnt]) except CommandException as ce: if (ce.rc == 32): for l in ce.err: l = l.strip() if (re.search('not mounted$', l) is not None): return raise ce for i in range(20): if (not is_mounted(root_pool_mnt)): toggle_path_rw(root_pool_mnt, rw=True) run_command([RMDIR, root_pool_mnt]) return time.sleep(2) run_command([UMOUNT, '-f', root_pool_mnt]) toggle_path_rw(root_pool_mnt, rw=True) run_command([RMDIR, root_pool_mnt]) return
def remove_snap(pool, pool_device, share_name, snap_name): root_mnt = mount_root(pool, pool_device) snap_path = ('%s/.snapshots/%s/%s' % (root_mnt, share_name, snap_name)) if (is_mounted(snap_path)): umount_root(snap_path) if (is_subvol(snap_path)): qgroup = ('0/%s' % share_id(pool, pool_device, snap_name)) run_command([BTRFS, 'subvolume', 'delete', snap_path], log=True) return qgroup_destroy(qgroup, root_mnt) else: o, e, rc = run_command([BTRFS, 'subvolume', 'list', '-s', root_mnt]) snap = None for l in o: #just give the first match. if (re.match('ID.*%s$' % snap_name, l) is not None): snap = '%s/%s' % (root_mnt, l.split()[-1]) break e_msg = ('This snapshot(%s) was created outside of Rockstor. If you ' 'really want to delete it, you can do so manually with this ' 'command: btrfs subvol delete %s' % (snap_name, snap)) raise Exception(e_msg)
def set_property(mnt_pt, name, val, mount=True): if (mount is not True or is_mounted(mnt_pt)): cmd = [BTRFS, 'property', 'set', mnt_pt, name, val] return run_command(cmd)
def remount(mnt_pt, mnt_options): if (is_mounted(mnt_pt)): run_command([MOUNT, '-o', 'remount,%s' % mnt_options, mnt_pt]) return True
def set_property(mnt_pt, name, val, mount=True): if mount is not True or is_mounted(mnt_pt): cmd = [BTRFS, "property", "set", mnt_pt, name, val] return run_command(cmd)
def umount_root(root_pool_mnt): if (not os.path.exists(root_pool_mnt)): return try: o, e, rc = run_command([UMOUNT, '-l', root_pool_mnt]) except CommandException, ce: if (ce.rc == 32): for l in ce.err: l = l.strip() if (re.search('not mounted$', l) is not None): return raise ce for i in range(20): if (not is_mounted(root_pool_mnt)): run_command([RMDIR, root_pool_mnt]) return time.sleep(2) run_command([UMOUNT, '-f', root_pool_mnt]) run_command([RMDIR, root_pool_mnt]) return def remount(mnt_pt, mnt_options): if (is_mounted(mnt_pt)): run_command([MOUNT, '-o', 'remount,%s' % mnt_options, mnt_pt]) return True def is_subvol(mnt_pt):
def remount(mnt_pt, mnt_options): if is_mounted(mnt_pt): run_command([MOUNT, "-o", "remount,%s" % mnt_options, mnt_pt]) return True
def main(): mnt_pt = sys.argv[1] if (not is_mounted(mnt_pt)): sys.exit('Docker root(%s) not mounted.' % mnt_pt) run_command([DOCKER, '-d', '-s', 'btrfs', '-g', mnt_pt])
def umount_root(root_pool_mnt): if (not os.path.exists(root_pool_mnt)): return try: o, e, rc = run_command([UMOUNT, '-l', root_pool_mnt]) except CommandException, ce: if (ce.rc == 32): for l in ce.err: l = l.strip() if (re.search('not mounted$', l) is not None): return raise ce for i in range(20): if (not is_mounted(root_pool_mnt)): run_command([RMDIR, root_pool_mnt]) return time.sleep(2) run_command([UMOUNT, '-f', root_pool_mnt]) run_command([RMDIR, root_pool_mnt]) return def is_subvol(mnt_pt): """ Simple wrapper around "btrfs subvolume show mnt_pt" :param mnt_pt: mount point of subvolume to query :return: True if subvolume mnt_pt exists, else False """ show_cmd = [BTRFS, 'subvolume', 'show', mnt_pt]