5

I see some good explanation for fetching information about network cards and their statistics in ubuntu on this page. This gave a nice output as mentioned on the page. I tried reading other documentations too but could not find a flag or something similar where I can differentiate between the real and virtual network cards on my system.

Is there are way to differentiate ? Thanks.

Gagan93
  • 153
  • 1
  • 9
  • 1
    Note that for HVM devices there may be no way of telling as hypervisors emulate a real device (usually Intel or Realtek). – André Borie May 30 '17 at 12:03

1 Answers1

6

Check the /sys/class/net/<device_name> symlink. If it points into /sys/devices/virtual/, then it is a virtual interface. If it points to a "real" device (e.g. into /sys/devices/pci0000:00/), then it is not.

Edit:

From code, you can use readlink to check if the device is virtual. Here is a very dummy sample code to do so:

#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>

int main(int argc, char **argv) {
    char theLink[128];
    char thePath[128];

    strcpy(thePath,"/sys/class/net/");
    memset(theLink,0,128);

    if (argc>1) {
        strcat(thePath,argv[1]);
    } else {
        printf("Gimme device\n");
        return 1;
    }

    if (readlink(thePath, theLink, 127)==-1) {
        perror(argv[1]);
    } else {
        if (strstr(theLink,"/virtual")) {
            printf("%s is a virtual device\n",argv[1]);
        } else {
            printf("%s is a physical device\n",argv[1]);
        }
    }
}
Lacek
  • 7,233
  • 24
  • 28
  • sounds promising, anything similar for virtual disks also ? And it seems like I need to do `ls -l` for confirming this. Any way from code ? – Gagan93 May 30 '17 at 07:40
  • For disks, check the symlinks in `/sys/class/block`. Also, I added a primitive sample to check if the device is virtual. – Lacek May 30 '17 at 08:22
  • The sample works excellent for EC2 instances also, for the `/sys/class/block` link, probably I used the wrong word (disk). I need for mount points (/root, /mnt, etc). Is there a local in system for these also ? @Lacek – Gagan93 May 30 '17 at 09:58
  • I think mount points can only be extracted from `/proc/mounts` (on Linux, this is a symlink to `/proc/self/mounts`). Parsing that file is a bit more difficult though. – Lacek May 30 '17 at 10:57
  • @Gagan93: What do you mean by virtual mount points? Every since the big VFS re-write ~20 years ago, I don't think that makes much sense. The current Linux VFS is very Plan9-like where everything is a (potential) file system, everything is a (potential) mount point, and every mount point is (in some sense) virtual. – Jörg W Mittag May 30 '17 at 13:23
  • Why have you written a C program? Why not `readlink /path/to/device && ... || ...` or whatever in (ba)sh? – cat May 30 '17 at 15:24
  • @cat : There is an existing C binary doing some operations so I am planning to add this logic to exclude virtual network cards. – Gagan93 May 31 '17 at 06:20