summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Sterba <dsterba@suse.com>2016-02-10 00:23:29 +0100
committerDavid Sterba <dsterba@suse.com>2016-10-25 14:28:36 +0200
commit508000138c53707c961ce7364aff798e594604b7 (patch)
treeeb3991cb9dd04e0df3bc2802f34aae8cda070147
parent4d958295d2c7608c8c695d8fc3e9e9e94bae089b (diff)
btrfs-progs: mkfs: detect version of running kernel
Use the uname syscall and parse the string. Signed-off-by: David Sterba <dsterba@suse.com>
-rw-r--r--utils.c48
-rw-r--r--utils.h2
2 files changed, 50 insertions, 0 deletions
diff --git a/utils.c b/utils.c
index 72e6ae90..011e163d 100644
--- a/utils.c
+++ b/utils.c
@@ -38,6 +38,7 @@
#include <sys/statfs.h>
#include <linux/magic.h>
#include <getopt.h>
+#include <sys/utsname.h>
#include "kerncompat.h"
#include "radix-tree.h"
@@ -1555,6 +1556,53 @@ char* btrfs_parse_fs_features(char *namelist, u64 *flags)
return NULL;
}
+void print_kernel_version(FILE *stream, u32 version)
+{
+ u32 v[3];
+
+ v[0] = version & 0xFF;
+ v[1] = (version >> 8) & 0xFF;
+ v[2] = version >> 16;
+ fprintf(stream, "%u.%u", v[2], v[1]);
+ if (v[0])
+ fprintf(stream, ".%u", v[0]);
+}
+
+u32 get_running_kernel_version(void)
+{
+ struct utsname utsbuf;
+ char *tmp;
+ char *saveptr = NULL;
+ u32 version;
+
+ uname(&utsbuf);
+ if (strcmp(utsbuf.sysname, "Linux") != 0) {
+ error("unsupported system: %s", utsbuf.sysname);
+ exit(1);
+ }
+ /* 1.2.3-4-name */
+ tmp = strchr(utsbuf.release, '-');
+ if (tmp)
+ *tmp = 0;
+
+ tmp = strtok_r(utsbuf.release, ".", &saveptr);
+ if (!string_is_numerical(tmp))
+ return (u32)-1;
+ version = atoi(tmp) << 16;
+ tmp = strtok_r(NULL, ".", &saveptr);
+ if (!string_is_numerical(tmp))
+ return (u32)-1;
+ version |= atoi(tmp) << 8;
+ tmp = strtok_r(NULL, ".", &saveptr);
+ if (tmp) {
+ if (!string_is_numerical(tmp))
+ return (u32)-1;
+ version |= atoi(tmp);
+ }
+
+ return version;
+}
+
u64 btrfs_device_size(int fd, struct stat *st)
{
u64 size;
diff --git a/utils.h b/utils.h
index 117fed8e..d382534f 100644
--- a/utils.h
+++ b/utils.h
@@ -109,6 +109,8 @@ void btrfs_list_all_fs_features(u64 mask_disallowed);
char* btrfs_parse_fs_features(char *namelist, u64 *flags);
void btrfs_process_fs_features(u64 flags);
void btrfs_parse_features_to_string(char *buf, u64 flags);
+void print_kernel_version(FILE *stream, u32 version);
+u32 get_running_kernel_version(void);
struct btrfs_mkfs_config {
char *label;