Skip to content

Commit

Permalink
Auto-merge for PR #278 via VersionBot
Browse files Browse the repository at this point in the history
feat(linux): Add unique device path
  • Loading branch information
resin-io-modules-versionbot[bot] authored May 9, 2018
2 parents dc19f66 + 7f5dc14 commit 8965d89
Show file tree
Hide file tree
Showing 8 changed files with 20 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file
automatically by Versionist. DO NOT EDIT THIS FILE MANUALLY!
This project adheres to [Semantic Versioning](http://semver.org/).

## v6.2.0 - 2018-05-09

* Feat(linux): Add unique device path #278 [User Name]

## v6.1.8 - 2018-04-26

* Chore(ci): Build, test & publish releases for Node.js v10 #273 [Tim Perry]
Expand Down
1 change: 1 addition & 0 deletions lib/diskutil.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ const transform = (disk) => {
busType: disk.BusProtocol || 'UNKNOWN',
busVersion: null,
device: disk.DeviceNode,
devicePath: null,
raw: disk.DeviceNode && disk.DeviceNode.replace('/disk', '/rdisk'),
description: disk.IORegistryEntryName || disk.MediaName,
error: null,
Expand Down
2 changes: 1 addition & 1 deletion lib/scripts.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"linux": {
"content": "#!/bin/bash\n\nset -u\nset -e\n\nignore_first_line() {\n tail -n +2\n}\n\nget_uuids() {\n /sbin/blkid -s UUID -o value \"$1\"*\n}\n\nget_mountpoints() {\n grep \"^$1\" /proc/mounts | cut -d ' ' -f 2 | sed 's,\\\\040, ,g' | sed 's,\\\\011,\\t,g' | sed 's,\\\\012,\\\\n,g' | sed 's,\\\\134,\\\\\\\\,g'\n}\n\nDISKS=\"$(lsblk -d --output NAME | ignore_first_line)\"\n\nfor disk in $DISKS; do\n\n # Omit loop devices and CD/DVD drives\n if [[ $disk == loop* ]] || [[ $disk == sr* ]]; then\n continue\n fi\n\n device=\"/dev/$disk\"\n diskinfo=($(lsblk -b -d \"$device\" --output SIZE,RO,RM,MODEL | ignore_first_line))\n\n # Omit drives for which `lsblk` failed, which means they\n # were unplugged right after we got the list of all drives\n if [ -z \"${diskinfo-}\" ]; then\n continue\n fi\n\n size=${diskinfo[0]}\n protected=${diskinfo[1]}\n removable=${diskinfo[2]}\n description=${diskinfo[*]:3}\n mountpoints=\"$(get_mountpoints \"$device\")\"\n\n # If we couldn't get the mount points as `/dev/$disk`,\n # get the disk UUIDs, and check as `/dev/disk/by-uuid/$uuid`\n if [ -z \"$mountpoints\" ]; then\n for uuid in $(get_uuids \"$device\"); do\n mountpoints=\"$mountpoints$(get_mountpoints \"/dev/disk/by-uuid/$uuid\")\"\n done\n fi\n\n # If we couldn't get the description from `lsblk`, see if we can get it\n # from sysfs (e.g. PCI-connected SD cards that appear as `/dev/mmcblk0`)\n if [ -z \"$description\" ]; then\n subdevice=\"$(echo \"$device\" | cut -d '/' -f 3)\"\n if [ -f \"/sys/class/block/$subdevice/device/name\" ]; then\n description=\"$(cat \"/sys/class/block/$subdevice/device/name\")\"\n fi\n fi\n\n echo \"enumerator: lsblk\"\n echo \"busType: UNKNOWN\"\n echo \"busVersion: \\\"0.0\\\"\"\n echo \"device: $device\"\n echo \"raw: $device\"\n echo \"description: \\\"$description\\\"\"\n echo \"error: null\"\n echo \"size: $size\"\n echo \"blockSize: null\"\n echo \"logicalBlockSize: null\"\n\n if [ -z \"$mountpoints\" ]; then\n echo \"mountpoints: []\"\n else\n echo \"mountpoints:\"\n echo \"$mountpoints\" | while read -r mountpoint ; do\n echo \" - path: \\\"$mountpoint\\\"\"\n done\n fi\n\n if [[ \"$protected\" == \"1\" ]]; then\n echo \"isReadOnly: True\"\n else\n echo \"isReadOnly: False\"\n fi\n\n eval \"$(udevadm info \\\n --query=property \\\n --export \\\n --export-prefix=UDEV_ \\\n --name=\"$disk\" \\\n | awk -F= '{gsub(\"\\\\.\",\"_\",$1); print $1 \"=\" $2}')\"\n\n set +u\n\n if [[ \"$removable\" == \"1\" ]] && \\\n [[ \"$UDEV_ID_DRIVE_FLASH_SD\" == \"1\" ]] || \\\n [[ \"$UDEV_ID_DRIVE_MEDIA_FLASH_SD\" == \"1\" ]] || \\\n [[ \"$UDEV_ID_BUS\" == \"usb\" ]]\n then\n echo \"isSystem: False\"\n else\n echo \"isSystem: True\"\n fi\n\n echo \"isVirtual: null\"\n echo \"isRemovable: null\"\n echo \"isCard: null\"\n echo \"isSCSI: null\"\n echo \"isUSB: null\"\n echo \"isUAS: null\"\n\n set -u\n\n # Unset UDEV variables used above to prevent them from\n # being interpreted as properties of another drive\n unset UDEV_ID_DRIVE_FLASH_SD\n unset UDEV_ID_DRIVE_MEDIA_FLASH_SD\n unset UDEV_ID_BUS\n\n echo \"\"\ndone\n",
"content": "#!/bin/bash\n\nset -u\nset -e\n\nignore_first_line() {\n tail -n +2\n}\n\nget_uuids() {\n /sbin/blkid -s UUID -o value \"$1\"*\n}\n\nget_mountpoints() {\n grep \"^$1\" /proc/mounts | cut -d ' ' -f 2 | sed 's,\\\\040, ,g' | sed 's,\\\\011,\\t,g' | sed 's,\\\\012,\\\\n,g' | sed 's,\\\\134,\\\\\\\\,g'\n}\n\nget_path_id() {\n # udevadm test-builtin path_id /sys/block/sda | grep ID_PATH=\n find -L /dev/disk/by-path/ -samefile \"$1\" | cut -c 19-\n}\n\nDISKS=\"$(lsblk -d --output NAME | ignore_first_line)\"\n\nfor disk in $DISKS; do\n\n # Omit loop devices and CD/DVD drives\n if [[ $disk == loop* ]] || [[ $disk == sr* ]]; then\n continue\n fi\n\n device=\"/dev/$disk\"\n diskinfo=($(lsblk -b -d \"$device\" --output SIZE,RO,RM,MODEL | ignore_first_line))\n\n # Omit drives for which `lsblk` failed, which means they\n # were unplugged right after we got the list of all drives\n if [ -z \"${diskinfo-}\" ]; then\n continue\n fi\n\n size=${diskinfo[0]}\n protected=${diskinfo[1]}\n removable=${diskinfo[2]}\n description=${diskinfo[*]:3}\n mountpoints=\"$(get_mountpoints \"$device\")\"\n devicePath=\"$(get_path_id \"$device\")\"\n\n # If we couldn't get the mount points as `/dev/$disk`,\n # get the disk UUIDs, and check as `/dev/disk/by-uuid/$uuid`\n if [ -z \"$mountpoints\" ]; then\n for uuid in $(get_uuids \"$device\"); do\n mountpoints=\"$mountpoints$(get_mountpoints \"/dev/disk/by-uuid/$uuid\")\"\n done\n fi\n\n # If we couldn't get the description from `lsblk`, see if we can get it\n # from sysfs (e.g. PCI-connected SD cards that appear as `/dev/mmcblk0`)\n if [ -z \"$description\" ]; then\n subdevice=\"$(echo \"$device\" | cut -d '/' -f 3)\"\n if [ -f \"/sys/class/block/$subdevice/device/name\" ]; then\n description=\"$(cat \"/sys/class/block/$subdevice/device/name\")\"\n fi\n fi\n\n echo \"enumerator: lsblk\"\n echo \"busType: UNKNOWN\"\n echo \"busVersion: \\\"0.0\\\"\"\n echo \"device: $device\"\n echo \"devicePath: $devicePath\"\n echo \"raw: $device\"\n echo \"description: \\\"$description\\\"\"\n echo \"error: null\"\n echo \"size: $size\"\n echo \"blockSize: null\"\n echo \"logicalBlockSize: null\"\n\n if [ -z \"$mountpoints\" ]; then\n echo \"mountpoints: []\"\n else\n echo \"mountpoints:\"\n echo \"$mountpoints\" | while read -r mountpoint ; do\n echo \" - path: \\\"$mountpoint\\\"\"\n done\n fi\n\n if [[ \"$protected\" == \"1\" ]]; then\n echo \"isReadOnly: True\"\n else\n echo \"isReadOnly: False\"\n fi\n\n eval \"$(udevadm info \\\n --query=property \\\n --export \\\n --export-prefix=UDEV_ \\\n --name=\"$disk\" \\\n | awk -F= '{gsub(\"\\\\.\",\"_\",$1); print $1 \"=\" $2}')\"\n\n set +u\n\n if [[ \"$removable\" == \"1\" ]] && \\\n [[ \"$UDEV_ID_DRIVE_FLASH_SD\" == \"1\" ]] || \\\n [[ \"$UDEV_ID_DRIVE_MEDIA_FLASH_SD\" == \"1\" ]] || \\\n [[ \"$UDEV_ID_BUS\" == \"usb\" ]]\n then\n echo \"isSystem: False\"\n else\n echo \"isSystem: True\"\n fi\n\n echo \"isVirtual: null\"\n echo \"isRemovable: null\"\n echo \"isCard: null\"\n echo \"isSCSI: null\"\n echo \"isUSB: null\"\n echo \"isUAS: null\"\n\n set -u\n\n # Unset UDEV variables used above to prevent them from\n # being interpreted as properties of another drive\n unset UDEV_ID_DRIVE_FLASH_SD\n unset UDEV_ID_DRIVE_MEDIA_FLASH_SD\n unset UDEV_ID_BUS\n\n echo \"\"\ndone\n",
"originalFilename": "linux.sh",
"type": "text"
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "drivelist",
"version": "6.1.8",
"version": "6.2.0",
"description": "List all connected drives in your computer, in all major operating systems",
"main": "lib/drivelist.js",
"homepage": "https://github.com/resin-io-modules/drivelist",
Expand Down
7 changes: 7 additions & 0 deletions scripts/linux.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ get_mountpoints() {
grep "^$1" /proc/mounts | cut -d ' ' -f 2 | sed 's,\\040, ,g' | sed 's,\\011,\t,g' | sed 's,\\012,\\n,g' | sed 's,\\134,\\\\,g'
}

get_path_id() {
# udevadm test-builtin path_id /sys/block/sda | grep ID_PATH=
find -L /dev/disk/by-path/ -samefile "$1" | cut -c 19-
}

DISKS="$(lsblk -d --output NAME | ignore_first_line)"

for disk in $DISKS; do
Expand All @@ -38,6 +43,7 @@ for disk in $DISKS; do
removable=${diskinfo[2]}
description=${diskinfo[*]:3}
mountpoints="$(get_mountpoints "$device")"
devicePath="$(get_path_id "$device")"

# If we couldn't get the mount points as `/dev/$disk`,
# get the disk UUIDs, and check as `/dev/disk/by-uuid/$uuid`
Expand All @@ -60,6 +66,7 @@ for disk in $DISKS; do
echo "busType: UNKNOWN"
echo "busVersion: \"0.0\""
echo "device: $device"
echo "devicePath: $devicePath"
echo "raw: $device"
echo "description: \"$description\""
echo "error: null"
Expand Down
4 changes: 4 additions & 0 deletions src/device-descriptor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ v8::Local<v8::Object> PackDriveDescriptor(const DeviceDescriptor *instance) {
New<String>("device").ToLocalChecked(),
New<String>(instance->device).ToLocalChecked());

Nan::Set(object,
New<String>("devicePath").ToLocalChecked(),
Nan::Null());

Nan::Set(object,
New<String>("raw").ToLocalChecked(),
New<String>(instance->raw).ToLocalChecked());
Expand Down
1 change: 1 addition & 0 deletions src/drivelist.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ struct DeviceDescriptor {
std::string busType;
std::string busVersion;
std::string device;
std::string devicePath;
std::string raw;
std::string description;
std::string error;
Expand Down
1 change: 1 addition & 0 deletions tests/diskutil.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ describe('Drivelist', function() {
busType: 'PCI-Express',
busVersion: null,
device: '/dev/disk1',
devicePath: null,
raw: '/dev/rdisk1',
description: 'Macintosh HD',
error: null,
Expand Down

0 comments on commit 8965d89

Please sign in to comment.