MatthewKing / DeviceId

A simple library providing functionality to generate a 'device ID' that can be used to uniquely identify a computer.
MIT License
785 stars 118 forks source link

Added support for System drive serial number on Linux #13

Closed progdogusmc closed 4 years ago

progdogusmc commented 4 years ago

Thanks for the great library! I added a DeviceId class so System drive serial number is supported on Linux. It could probably be rolled into the existing class instead if you'd prefer.

MatthewKing commented 4 years ago

This looks good, thanks. I should have some time to properly review, merge, and release this in the next few days. Cheers!

MatthewKing commented 4 years ago

Just having a more in-depth look at this now. I'm not a Linux expert by any means so I might need some help here. I'm running this on a linux VM and on WSL and I'm not getting any ID_SERIAL fields from the udevadm info query. I assume this is because I'm on a VM. Any workaround for that, or is it just something that simply won't work on a VM? Cheers

progdogusmc commented 4 years ago

I tested it in a Virtualbox Ubuntu 18.04 VM with both LVM and normal partitioning, on a Google cloud VM and on a baremetal server, but I did notice it doesn't work in WSL.

On Sat, Apr 25, 2020, 6:05 PM Matthew King notifications@github.com wrote:

Just having a look at this now. I'm not a Linux expert by any means so I might need some help here. I'm running this on a linux VM and on WSL and I'm not getting any ID_SERIAL fields from the udevadm info query. I assume this is because I'm on a VM. Any workaround for that, or is it just something that simply won't work on a VM? Cheers

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/MatthewKing/DeviceId/pull/13#issuecomment-619462527, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAW5BLIYMQUZ6TP3SC35B7LROOCGPANCNFSM4MOZK5EA .

MatthewKing commented 4 years ago

That seems reasonable. I'm OK with it not working in WSL. I may have to spin up another VM somewhere to try and get some more results; my VM isn't giving me any results.

Would you mind sharing the results of a sblk -f -J command and a udevadm info --query=all --name=/dev/{device.Name} | grep ID_SERIAL= command on a machine that it "works" on? I'm just writing some units tests etc. and I want to make sure I have a good idea of the various cases.

Thanks again.

progdogusmc commented 4 years ago

Of course, here is the result of 'lsblk -f -J' from a Google Cloud Ubuntu 18.04 VM:

{
   "blockdevices": [
      {"name": "loop0", "fstype": "squashfs", "label": null, "uuid": null, "mountpoint": "/snap/core18/1705"},
      {"name": "loop1", "fstype": "squashfs", "label": null, "uuid": null, "mountpoint": "/snap/google-cloud-sdk/127"},
      {"name": "loop2", "fstype": "squashfs", "label": null, "uuid": null, "mountpoint": "/snap/core/8935"},
      {"name": "loop3", "fstype": "squashfs", "label": null, "uuid": null, "mountpoint": "/snap/core/9066"},
      {"name": "loop4", "fstype": "squashfs", "label": null, "uuid": null, "mountpoint": "/snap/google-cloud-sdk/128"},
      {"name": "sda", "fstype": null, "label": null, "uuid": null, "mountpoint": null,
         "children": [
            {"name": "sda1", "fstype": "ext4", "label": "cloudimg-rootfs", "uuid": "e0c095ca-21b2-4a02-bbea-0dc95c07cfc8", "mountpoint": "/"},
            {"name": "sda14", "fstype": null, "label": null, "uuid": null, "mountpoint": null},
            {"name": "sda15", "fstype": "vfat", "label": "UEFI", "uuid": "84B5-FED0", "mountpoint": "/boot/efi"}
         ]
      }
   ]
}

And the result of 'udevadm info --query=all --name=/dev/sda | grep ID_SERIAL=':

E: ID_SERIAL=0Google_PersistentDisk_webserver

Here is the result of 'lsblk -f -J' on a VirtualBox VM using LVM:

{
   "blockdevices": [
      {"name": "loop0", "fstype": "squashfs", "label": null, "uuid": null, "mountpoint": "/snap/gnome-logs/45"},
      {"name": "loop1", "fstype": "squashfs", "label": null, "uuid": null, "mountpoint": "/snap/gnome-characters/139"},
      {"name": "loop2", "fstype": "squashfs", "label": null, "uuid": null, "mountpoint": "/snap/gnome-calculator/260"},
      {"name": "loop3", "fstype": "squashfs", "label": null, "uuid": null, "mountpoint": "/snap/gnome-3-26-1604/74"},
      {"name": "loop4", "fstype": "squashfs", "label": null, "uuid": null, "mountpoint": "/snap/core/6350"},
      {"name": "loop5", "fstype": "squashfs", "label": null, "uuid": null, "mountpoint": "/snap/gtk-common-themes/818"},
      {"name": "loop6", "fstype": "squashfs", "label": null, "uuid": null, "mountpoint": "/snap/gnome-system-monitor/57"},
      {"name": "sda", "fstype": null, "label": null, "uuid": null, "mountpoint": null,
         "children": [
            {"name": "sda1", "fstype": "LVM2_member", "label": null, "uuid": "Eqsoey-BADi-y8Sf-JFVM-UPyk-gXQE-uu9XCv", "mountpoint": null,
               "children": [
                  {"name": "ubuntu--vg-root", "fstype": "ext4", "label": null, "uuid": "c72aecd8-a254-4b1c-9a17-4257e89796d2", "mountpoint": "/"},
                  {"name": "ubuntu--vg-swap_1", "fstype": "swap", "label": null, "uuid": "93ff3926-f0df-470d-996b-af57b0991b06", "mountpoint": "[SWAP]"}
               ]
            }
         ]
      },
      {"name": "sr0", "fstype": "iso9660", "label": "VBox_GAs_6.0.14", "uuid": "2019-10-10-18-52-14-12", "mountpoint": "/media/kevin/VBox_GAs_6.0.14"}
   ]
}

And 'udevadm info --query=all --name=/dev/sda | grep ID_SERIAL=':

E: ID_SERIAL=VBOX_HARDDISK_VB5e245c00-220de489

Finally, here is the bare-metal server's 'lsblk -f- J':

{
   "blockdevices": [
      {"name": "sda", "fstype": null, "label": null, "uuid": null, "mountpoint": null,
         "children": [
            {"name": "sda1", "fstype": null, "label": null, "uuid": null, "mountpoint": null},
            {"name": "sda2", "fstype": "linux_raid_member", "label": "s590:0", "uuid": "22aca146-b40c-6b41-265b-383c15d49106", "mountpoint": null,
               "children": [
                  {"name": "md0", "fstype": "xfs", "label": null, "uuid": "3d22ab9f-9ddb-43d5-82c7-a02725c1f4cb", "mountpoint": "/"}
               ]
            },
            {"name": "sda3", "fstype": null, "label": null, "uuid": null, "mountpoint": null},
            {"name": "sda4", "fstype": null, "label": null, "uuid": null, "mountpoint": null}
         ]
      },
      {"name": "sdb", "fstype": null, "label": null, "uuid": null, "mountpoint": null,
         "children": [
            {"name": "sdb1", "fstype": null, "label": null, "uuid": null, "mountpoint": null},
            {"name": "sdb2", "fstype": "linux_raid_member", "label": "s590:0", "uuid": "22aca146-b40c-6b41-265b-383c15d49106", "mountpoint": null,
               "children": [
                  {"name": "md0", "fstype": "xfs", "label": null, "uuid": "3d22ab9f-9ddb-43d5-82c7-a02725c1f4cb", "mountpoint": "/"}
               ]
            },
            {"name": "sdb3", "fstype": null, "label": null, "uuid": null, "mountpoint": null},
            {"name": "sdb4", "fstype": null, "label": null, "uuid": null, "mountpoint": null}
         ]
      }
   ]
}

and 'udevadm info --query=all --name=/dev/sda | grep ID_SERIAL=':

E: ID_SERIAL=WDC_WD4000FYYZ-01UL1B1_WD-WCC131942520

Thanks again for considering the pull request!

MatthewKing commented 4 years ago

Great, thanks for providing that info. I've reviewed the changes and put some tests together. I've got some other commitments for the rest of the day but I'm hoping to get this finalized and released some time next week. Thanks again for the pull request!

progdogusmc commented 4 years ago

For what it's worth, it might be a good idea to modify the code to use both the UUID and the serial number, that should make it more durable on VMs without unique serial numbers for the HDDs like the Google Cloud VM.

If you'd like I can add that to the code and submit a new pull request, just let me know,

On Sat, Apr 25, 2020, 10:21 PM Matthew King notifications@github.com wrote:

Great, thanks for providing that info. I've reviewed the changes and put some tests together. I've got some other commitments for the rest of the day but I'm hoping to get this finalized and released some time next week. Thanks again for the pull request!

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/MatthewKing/DeviceId/pull/13#issuecomment-619484186, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAW5BLOP4XT6J2765AVA56DROPAEZANCNFSM4MOZK5EA .

MatthewKing commented 4 years ago

I think I'm happy with just the serial ID, as you currently have it. I ended up finding a bit of time to get this to a point I'm happy with. I rebased your commit to include a missing dependency so it's out of sync with this PR. Would you mind having a look at the current master branch to confirm that it's working as you expect it to? Much appreciated! Cheers

progdogusmc commented 4 years ago

Hello! I was able to build the current master branch and check it on my bare-metal server, everything looks good to me. Thanks again for the library. :D

MatthewKing commented 4 years ago

Great. Thanks for the excellent pull request. Will get v5.1.0 released in the next few days - with your changes in it. Cheers!