blkid on docker container

430 Views Asked by At

I am working on a legacy C++ project which uses functions from blkid/blkid.h to access files on the local hard disk.

Since the project is quite big and hard to compile/run, I thought it would be a good idea to run it in a container. Creating the dockerfile to create the images went fine, the problems started when actually running the legacy project in a container.

I get errors when trying to access files on the disk using "blkid_devno_to_devname" to get the device id on which to write.

Here is a sample program that reproduces my problem

#include <iostream>
#include <sys/types.h>
#include <sys/stat.h>
#include <blkid/blkid.h>

int main() {
  struct stat st;
  std::string path = "my_file_path";
  if (stat(path.data(), &st))
  {
    std::cout << "cannot get file statistics for file " << path << std::endl;
  }
  char *device_path = blkid_devno_to_devname(st.st_dev);
  if(device_path == NULL)
  {
    std::cout << "cannot get device path for device ID " + std::to_string(st.st_dev) << std::endl;
  }
}

On a regular file on my machine the device_path is not null, but running it on a docker always returns null.

What is not clear to me is how a docker container sees the disks it writes to.

To dig in, I looked into the different macros coming with stat (https://en.wikibooks.org/wiki/C_Programming/POSIX_Reference/sys/stat.h), I printed them on files on a container with different type of files (symlink, file, directory).

I modified the above little program to add this:

  std::cout << "mode:" << st.st_mode << std::endl;
  std::cout << "S_ISREG:" << S_ISREG(st.st_mode) << std::endl;
  std::cout << "S_ISDIR:" << S_ISDIR(st.st_mode) << std::endl;
  std::cout << "S_ISCHR:" << S_ISCHR(st.st_mode) << std::endl;
  std::cout << "S_ISBLK:" << S_ISBLK(st.st_mode) << std::endl;
  std::cout << "S_ISFIFO:" << S_ISFIFO(st.st_mode) << std::endl;
  std::cout << "S_ISLNK:" << S_ISLNK(st.st_mode) << std::endl;
  std::cout << "S_ISSOCK:" << S_ISSOCK(st.st_mode) << std::endl;

I thought there would be a difference depending on the type of layer the docker writes to, but I don't see any.

Be it on a volume, mount, or even on container layer (I mean on files neither on a volume nor mount).

On a mount, I would have expected the device id to be correct since as I understand it the mount is giving the container access to a physical drive (at least in my case - may not be true in general).

So why is blkid_devno_to_devname not returning anything on a docker container? What am I not understanding properly?

P.S. for the record, blkid or /etc/fstab don't return anything on a container either.

Notes to the readers about my system:

  • working on a VM on windows (Oracle VM) with ubuntu 18 installed.
  • docker version: 19.03.6
  • Driver: overlay2
0

There are 0 best solutions below