knorrie / python-btrfs

Python Btrfs module
GNU Lesser General Public License v3.0
112 stars 22 forks source link

Unmounted (or unmountable) filesystems #16

Closed ghost closed 5 years ago

ghost commented 5 years ago

Hi, I'm trying to somehow fix my currently broken raid10 array. Unfortunately, filesystem is unmountable, but with btrfs tools I was able to locate blocks I think are broken (invalid checksums, wrong parent key), but I don't have enough knowledge to at least try to fix them. Your library looks very promising, because I'm much more familiar with python than C, and it already helped me to better understand some things.

Is it possible to use this library to dig into unmounted/unmountable/broken filesystem? When trying to use with unmounted device (/dev/mapper/data0 is first of four) I get following error (IPython output):

Python 3.7.2 (default, Jan 10 2019, 23:51:51) 
Type 'copyright', 'credits' or 'license' for more information
IPython 7.2.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: import btrfs                                                                                                  

In [2]: with btrfs.FileSystem('/dev/mapper/data0') as fs: 
   ...:     pass 
   ...:                                                                                                               
---------------------------------------------------------------------------
OSError                                   Traceback (most recent call last)
<ipython-input-2-5ee87a216dac> in <module>
----> 1 with btrfs.FileSystem('/dev/mapper/data0') as fs:
      2     pass
      3 

~/src/array/.venv/lib/python3.7/site-packages/btrfs/ctree.py in __init__(self, path)
    632         self.path = path
    633         self.fd = os.open(path, os.O_RDONLY)
--> 634         _fs_info = self.fs_info()
    635         self.fsid = _fs_info.fsid
    636         self.nodesize = _fs_info.nodesize

~/src/array/.venv/lib/python3.7/site-packages/btrfs/ctree.py in fs_info(self)
    645         :rtype: :class:`btrfs.ioctl.FsInfo`
    646         """
--> 647         return btrfs.ioctl.fs_info(self.fd)
    648 
    649     def dev_info(self, devid):

~/src/array/.venv/lib/python3.7/site-packages/btrfs/ioctl.py in fs_info(fd)
    149     """
    150     buf = bytearray(ioctl_fs_info_args.size)
--> 151     fcntl.ioctl(fd, IOC_FS_INFO, buf)
    152     return FsInfo(buf)
    153 

OSError: [Errno 25] Inappropriate ioctl for device

Kernel: 4.20.7 (Arch Linux) Btrfs-progs: v4.20.1

knorrie commented 5 years ago

Hi. Nope, this library only interacts with already mounted filesystems, using the "live" kernel API. It never reads from or writes to the disk directly. Actually, two weeks ago I realized the top level README was not super clear about that and I added commit 9568709e4, but that's not in master branch yet.

Of course, it would be super fun to have a library that can handle offline filesystems. I have been playing around with ideas for that, but there's nothing yet, not even a small proof of concept.

A big reason why the current code should never be used to write things back is that python does not have the concept of an 8,16,64bit integer etc. Everything is just a number. When reading values and looking at them or printing them on screen, that's not a problem. But when writing something back, it certainly is.

knorrie commented 5 years ago

The README text has changed a bit in v11, to explicitly mention the library can only work with online, mounted filesystems.