Skip to content

Instantly share code, notes, and snippets.

@Mattias-
Created April 3, 2019 11:18
Show Gist options
  • Save Mattias-/9847b14490144e05a3a1ba5e396dbe03 to your computer and use it in GitHub Desktop.
Save Mattias-/9847b14490144e05a3a1ba5e396dbe03 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
# Copyright (c) 2018 The CoreOS Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
usage() {
echo "Usage: $0 [-d|-n|-h] <device>" 1>&2
}
# Get partition ID from device name, e.g. '/dev/nvme0n1p2' -> '1'
namespace_id() {
check_udev_aws
NSID=$(echo -n "$1" | cut -f 3 -d 'n' | cut -f 1 -d 'p')
echo "_NS_ID=${NSID}"
}
# Get device name from raw metadata,
# see https://github.com/coreos/bugs/issues/2399.
devname() {
check_udev_aws
RAWVOL=$(nvme id-ctrl --raw-binary "$1" | cut -c3073-3104 | tr -s ' ' | sed 's/ $//g')
VOL="${RAWVOL#/dev/}"
if [[ -n "$VOL" ]]; then
echo "${VOL}"
else
exit 1
fi
}
# Ensure this is run with proper environment populated by udev,
# and acting on an AWS EBS device.
check_udev_aws() {
if [ "${ID_MODEL}" != "Amazon Elastic Block Store" ]; then
if [ "$(cut -d"_" -f1 <<<"${ID_SERIAL}")" != "Amazon Elastic Block Store" ]; then
echo 'Stopping due to non-matching ID_MODEL or ID_SERIAL env variable'
exit 1
fi
fi
}
while getopts "hd:n:" o; do
case "${o}" in
d)
devname "${OPTARG}"
exit 0
;;
n)
namespace_id "${OPTARG}"
exit 0
;;
h)
usage
exit 0
;;
*)
usage
exit 1
;;
esac
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment