summaryrefslogtreecommitdiff
path: root/tools/ds-identify
diff options
context:
space:
mode:
authorMina Galić <me+git@igalic.co>2020-11-04 15:49:34 +0100
committerGitHub <noreply@github.com>2020-11-04 09:49:34 -0500
commitcf6c36a18de233190996f7c2dde1e04908bad50a (patch)
tree332a0f625ba1699b9fb9ee4d9dd8148f7190f015 /tools/ds-identify
parentd1c01c1df94e07afd3843e31b1e1ce670cf07387 (diff)
downloadvyos-cloud-init-cf6c36a18de233190996f7c2dde1e04908bad50a.tar.gz
vyos-cloud-init-cf6c36a18de233190996f7c2dde1e04908bad50a.zip
split read_fs_info into linux & freebsd parts (#625)
FreeBSD doesn't have blkid, so we want to use geom to list devices and their fstypes and labels. This PR also adds `jail` to the list of is_container() And we now also properly cache geom and blkid output! A test is added to verify the new behaviour by correctly identifying NoCloud on FreeBSD. Co-authored-by: Scott Moser <smoser@brickies.net>
Diffstat (limited to 'tools/ds-identify')
-rwxr-xr-xtools/ds-identify106
1 files changed, 97 insertions, 9 deletions
diff --git a/tools/ds-identify b/tools/ds-identify
index 4e5700fc..ec9e775e 100755
--- a/tools/ds-identify
+++ b/tools/ds-identify
@@ -92,7 +92,8 @@ _DI_LOGGED=""
# set DI_MAIN='noop' in environment to source this file with no main called.
DI_MAIN=${DI_MAIN:-main}
-DI_BLKID_OUTPUT=""
+DI_BLKID_EXPORT_OUT=""
+DI_GEOM_LABEL_STATUS_OUT=""
DI_DEFAULT_POLICY="search,found=all,maybe=all,notfound=${DI_DISABLED}"
DI_DEFAULT_POLICY_NO_DMI="search,found=all,maybe=all,notfound=${DI_ENABLED}"
DI_DMI_CHASSIS_ASSET_TAG=""
@@ -231,8 +232,19 @@ ensure_sane_path() {
done
}
-read_fs_info() {
- cached "${DI_BLKID_OUTPUT}" && return 0
+blkid_export() {
+ # call 'blkid -c /dev/null export', set DI_BLKID_EXPORT_OUT
+ cached "$DI_BLKID_EXPORT_OUT" && return 0
+ local out="" ret=0
+ out=$(blkid -c /dev/null -o export) && DI_BLKID_EXPORT_OUT="$out" || {
+ ret=$?
+ error "failed running [$ret]: blkid -c /dev/null -o export"
+ DI_BLKID_EXPORT_OUT="$UNAVAILABLE"
+ }
+ return $ret
+}
+
+read_fs_info_linux() {
# do not rely on links in /dev/disk which might not be present yet.
# Note that blkid < 2.22 (centos6, trusty) do not output DEVNAME.
# that means that DI_ISO9660_DEVS will not be set.
@@ -244,20 +256,23 @@ read_fs_info() {
return
fi
local oifs="$IFS" line="" delim=","
- local ret=0 out="" labels="" dev="" label="" ftype="" isodevs="" uuids=""
- out=$(blkid -c /dev/null -o export) || {
- ret=$?
- error "failed running [$ret]: blkid -c /dev/null -o export"
+ local ret=0 labels="" dev="" label="" ftype="" isodevs="" uuids=""
+
+ blkid_export
+ ret=$?
+ [ "$DI_BLKID_EXPORT_OUT" = "$UNAVAILABLE" ] && {
DI_FS_LABELS="$UNAVAILABLE:error"
DI_ISO9660_DEVS="$UNAVAILABLE:error"
+ DI_FS_UUIDS="$UNAVAILABLE:error"
return $ret
}
+
# 'set --' will collapse multiple consecutive entries in IFS for
# whitespace characters (\n, tab, " ") so we cannot rely on getting
# empty lines in "$@" below.
# shellcheck disable=2086
- { IFS="$CR"; set -- $out; IFS="$oifs"; }
+ { IFS="$CR"; set -- $DI_BLKID_EXPORT_OUT; IFS="$oifs"; }
for line in "$@"; do
case "${line}" in
@@ -281,6 +296,74 @@ read_fs_info() {
DI_ISO9660_DEVS="${isodevs#,}"
}
+geom_label_status_as() {
+ # call 'geom label status -as', set DI_GEOM_LABEL_STATUS_OUT
+ cached "$DI_GEOM_LABEL_STATUS_OUT" && return 0
+ local out="" ret=0
+ out=$(geom label status -as) && DI_GEOM_LABEL_STATUS_OUT="$out" || {
+ ret=$?
+ error "failed running [$ret]: geom label status -as"
+ DI_GEOM_LABEL_STATUS_OUT="$UNAVAILABLE"
+ }
+ return $ret
+}
+
+
+read_fs_info_freebsd() {
+ local oifs="$IFS" line="" delim=","
+ local ret=0 labels="" dev="" label="" ftype="" isodevs=""
+
+ geom_label_status_as
+ ret=$?
+ [ "$DI_GEOM_LABEL_STATUS_OUT" = "$UNAVAILABLE" ] && {
+ DI_FS_LABELS="$UNAVAILABLE:error"
+ DI_ISO9660_DEVS="$UNAVAILABLE:error"
+ return $ret
+ }
+
+ # The expected output looks like this:
+ # gpt/gptboot0 N/A vtbd1p1
+ # gpt/swap0 N/A vtbd1p2
+ # iso9660/cidata N/A vtbd2
+
+ # shellcheck disable=2086
+ { IFS="$CR"; set -- $DI_GEOM_LABEL_STATUS_OUT; IFS="$oifs"; }
+
+ for line in "$@"; do
+ # shellcheck disable=2086
+ set -- $line
+ provider=$1
+ ftype="${provider%/*}"
+ label="${provider#*/}"
+ dev=$3
+
+ [ -n "$dev" -a "$ftype" = "iso9660" ] &&
+ isodevs="${isodevs},${dev}=$label"
+
+ labels="${labels}${label}${delim}"
+ done
+
+ DI_FS_LABELS="${labels%${delim}}"
+ DI_ISO9660_DEVS="${isodevs#,}"
+}
+
+read_fs_info() {
+ # After calling its subfunctions, read_fs_info() will set the following
+ # variables:
+ #
+ # - DI_FS_LABELS
+ # - DI_ISO9660_DEVS
+ # - DI_FS_UUIDS
+
+ if [ "$DI_UNAME_KERNEL_NAME" = "FreeBSD" ]; then
+ read_fs_info_freebsd
+ return $?
+ else
+ read_fs_info_linux
+ return $?
+ fi
+}
+
cached() {
[ -n "$1" ] && _RET="$1" && return || return 1
}
@@ -319,6 +402,11 @@ detect_virt() {
*) virt="$out"
esac
}
+ out=$(sysctl -qn security.jail.jailed 2>/dev/null) && {
+ if [ "$out" = "1" ]; then
+ virt="jail"
+ fi
+ }
fi
_RET="$virt"
}
@@ -331,7 +419,7 @@ read_virt() {
is_container() {
case "${DI_VIRT}" in
- container-other|lxc|lxc-libvirt|systemd-nspawn|docker|rkt) return 0;;
+ container-other|lxc|lxc-libvirt|systemd-nspawn|docker|rkt|jail) return 0;;
*) return 1;;
esac
}