📄 probe.c
字号:
/* vi: set sw=4 ts=4: *//* * probe.c - identify a block device by its contents, and return a dev * struct with the details * * Copyright (C) 1999 by Andries Brouwer * Copyright (C) 1999, 2000, 2003 by Theodore Ts'o * Copyright (C) 2001 by Andreas Dilger * * %Begin-Header% * This file may be redistributed under the terms of the * GNU Lesser General Public License. * %End-Header% */#include <stdio.h>#include <string.h>#include <stdlib.h>#include <unistd.h>#include <fcntl.h>#include <sys/types.h>#ifdef HAVE_SYS_STAT_H#include <sys/stat.h>#endif#ifdef HAVE_SYS_MKDEV_H#include <sys/mkdev.h>#endif#ifdef HAVE_ERRNO_H#include <errno.h>#endif#include "blkidP.h"#include "../uuid/uuid.h"#include "probe.h"/* * This is a special case code to check for an MDRAID device. We do * this special since it requires checking for a superblock at the end * of the device. */static int check_mdraid(int fd, unsigned char *ret_uuid){ struct mdp_superblock_s *md; blkid_loff_t offset; char buf[4096]; if (fd < 0) return -BLKID_ERR_PARAM; offset = (blkid_get_dev_size(fd) & ~((blkid_loff_t)65535)) - 65536; if (blkid_llseek(fd, offset, 0) < 0 || read(fd, buf, 4096) != 4096) return -BLKID_ERR_IO; /* Check for magic number */ if (memcmp("\251+N\374", buf, 4)) return -BLKID_ERR_PARAM; if (!ret_uuid) return 0; *ret_uuid = 0; /* The MD UUID is not contiguous in the superblock, make it so */ md = (struct mdp_superblock_s *)buf; if (md->set_uuid0 || md->set_uuid1 || md->set_uuid2 || md->set_uuid3) { memcpy(ret_uuid, &md->set_uuid0, 4); memcpy(ret_uuid, &md->set_uuid1, 12); } return 0;}static void set_uuid(blkid_dev dev, uuid_t uuid){ char str[37]; if (!uuid_is_null(uuid)) { uuid_unparse(uuid, str); blkid_set_tag(dev, "UUID", str, sizeof(str)); }}static void get_ext2_info(blkid_dev dev, unsigned char *buf){ struct ext2_super_block *es = (struct ext2_super_block *) buf; const char *label = 0; DBG(DEBUG_PROBE, printf("ext2_sb.compat = %08X:%08X:%08X\n", blkid_le32(es->s_feature_compat), blkid_le32(es->s_feature_incompat), blkid_le32(es->s_feature_ro_compat))); if (strlen(es->s_volume_name)) label = es->s_volume_name; blkid_set_tag(dev, "LABEL", label, sizeof(es->s_volume_name)); set_uuid(dev, es->s_uuid);}static int probe_ext3(int fd __BLKID_ATTR((unused)), blkid_cache cache __BLKID_ATTR((unused)), blkid_dev dev, const struct blkid_magic *id __BLKID_ATTR((unused)), unsigned char *buf){ struct ext2_super_block *es; es = (struct ext2_super_block *)buf; /* Distinguish between jbd and ext2/3 fs */ if (blkid_le32(es->s_feature_incompat) & EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) return -BLKID_ERR_PARAM; /* Distinguish between ext3 and ext2 */ if (!(blkid_le32(es->s_feature_compat) & EXT3_FEATURE_COMPAT_HAS_JOURNAL)) return -BLKID_ERR_PARAM; get_ext2_info(dev, buf); blkid_set_tag(dev, "SEC_TYPE", "ext2", sizeof("ext2")); return 0;}static int probe_ext2(int fd __BLKID_ATTR((unused)), blkid_cache cache __BLKID_ATTR((unused)), blkid_dev dev, const struct blkid_magic *id __BLKID_ATTR((unused)), unsigned char *buf){ struct ext2_super_block *es; es = (struct ext2_super_block *)buf; /* Distinguish between jbd and ext2/3 fs */ if (blkid_le32(es->s_feature_incompat) & EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) return -BLKID_ERR_PARAM; get_ext2_info(dev, buf); return 0;}static int probe_jbd(int fd __BLKID_ATTR((unused)), blkid_cache cache __BLKID_ATTR((unused)), blkid_dev dev, const struct blkid_magic *id __BLKID_ATTR((unused)), unsigned char *buf){ struct ext2_super_block *es = (struct ext2_super_block *) buf; if (!(blkid_le32(es->s_feature_incompat) & EXT3_FEATURE_INCOMPAT_JOURNAL_DEV)) return -BLKID_ERR_PARAM; get_ext2_info(dev, buf); return 0;}static int probe_vfat(int fd __BLKID_ATTR((unused)), blkid_cache cache __BLKID_ATTR((unused)), blkid_dev dev, const struct blkid_magic *id __BLKID_ATTR((unused)), unsigned char *buf){ struct vfat_super_block *vs; char serno[10]; const char *label = 0; int label_len = 0; vs = (struct vfat_super_block *)buf; if (strncmp(vs->vs_label, "NO NAME", 7)) { char *end = vs->vs_label + sizeof(vs->vs_label) - 1; while (*end == ' ' && end >= vs->vs_label) --end; if (end >= vs->vs_label) { label = vs->vs_label; label_len = end - vs->vs_label + 1; } } /* We can't just print them as %04X, because they are unaligned */ sprintf(serno, "%02X%02X-%02X%02X", vs->vs_serno[3], vs->vs_serno[2], vs->vs_serno[1], vs->vs_serno[0]); blkid_set_tag(dev, "LABEL", label, label_len); blkid_set_tag(dev, "UUID", serno, sizeof(serno)); return 0;}static int probe_msdos(int fd __BLKID_ATTR((unused)), blkid_cache cache __BLKID_ATTR((unused)), blkid_dev dev, const struct blkid_magic *id __BLKID_ATTR((unused)), unsigned char *buf){ struct msdos_super_block *ms = (struct msdos_super_block *) buf; char serno[10]; const char *label = 0; int label_len = 0; if (strncmp(ms->ms_label, "NO NAME", 7)) { char *end = ms->ms_label + sizeof(ms->ms_label) - 1; while (*end == ' ' && end >= ms->ms_label) --end; if (end >= ms->ms_label) { label = ms->ms_label; label_len = end - ms->ms_label + 1; } } /* We can't just print them as %04X, because they are unaligned */ sprintf(serno, "%02X%02X-%02X%02X", ms->ms_serno[3], ms->ms_serno[2], ms->ms_serno[1], ms->ms_serno[0]); blkid_set_tag(dev, "UUID", serno, 0); blkid_set_tag(dev, "LABEL", label, label_len); blkid_set_tag(dev, "SEC_TYPE", "msdos", sizeof("msdos")); return 0;}static int probe_xfs(int fd __BLKID_ATTR((unused)), blkid_cache cache __BLKID_ATTR((unused)), blkid_dev dev, const struct blkid_magic *id __BLKID_ATTR((unused)), unsigned char *buf){ struct xfs_super_block *xs; const char *label = 0; xs = (struct xfs_super_block *)buf; if (strlen(xs->xs_fname)) label = xs->xs_fname; blkid_set_tag(dev, "LABEL", label, sizeof(xs->xs_fname)); set_uuid(dev, xs->xs_uuid); return 0;}static int probe_reiserfs(int fd __BLKID_ATTR((unused)), blkid_cache cache __BLKID_ATTR((unused)), blkid_dev dev, const struct blkid_magic *id, unsigned char *buf){ struct reiserfs_super_block *rs = (struct reiserfs_super_block *) buf; unsigned int blocksize; const char *label = 0; blocksize = blkid_le16(rs->rs_blocksize); /* If the superblock is inside the journal, we have the wrong one */ if (id->bim_kboff/(blocksize>>10) > blkid_le32(rs->rs_journal_block)) return -BLKID_ERR_BIG; /* LABEL/UUID are only valid for later versions of Reiserfs v3.6. */ if (!strcmp(id->bim_magic, "ReIsEr2Fs") || !strcmp(id->bim_magic, "ReIsEr3Fs")) { if (strlen(rs->rs_label)) label = rs->rs_label; set_uuid(dev, rs->rs_uuid); } blkid_set_tag(dev, "LABEL", label, sizeof(rs->rs_label)); return 0;}static int probe_jfs(int fd __BLKID_ATTR((unused)), blkid_cache cache __BLKID_ATTR((unused)), blkid_dev dev, const struct blkid_magic *id __BLKID_ATTR((unused)), unsigned char *buf){ struct jfs_super_block *js; const char *label = 0; js = (struct jfs_super_block *)buf; if (strlen((char *) js->js_label)) label = (char *) js->js_label; blkid_set_tag(dev, "LABEL", label, sizeof(js->js_label)); set_uuid(dev, js->js_uuid); return 0;}static int probe_romfs(int fd __BLKID_ATTR((unused)), blkid_cache cache __BLKID_ATTR((unused)), blkid_dev dev, const struct blkid_magic *id __BLKID_ATTR((unused)), unsigned char *buf){ struct romfs_super_block *ros; const char *label = 0; ros = (struct romfs_super_block *)buf; if (strlen((char *) ros->ros_volume)) label = (char *) ros->ros_volume; blkid_set_tag(dev, "LABEL", label, 0); return 0;}static int probe_cramfs(int fd __BLKID_ATTR((unused)), blkid_cache cache __BLKID_ATTR((unused)), blkid_dev dev, const struct blkid_magic *id __BLKID_ATTR((unused)), unsigned char *buf){ struct cramfs_super_block *csb; const char *label = 0; csb = (struct cramfs_super_block *)buf; if (strlen((char *) csb->name)) label = (char *) csb->name; blkid_set_tag(dev, "LABEL", label, 0); return 0;}static int probe_swap0(int fd __BLKID_ATTR((unused)), blkid_cache cache __BLKID_ATTR((unused)), blkid_dev dev, const struct blkid_magic *id __BLKID_ATTR((unused)), unsigned char *buf __BLKID_ATTR((unused))){ blkid_set_tag(dev, "UUID", 0, 0); blkid_set_tag(dev, "LABEL", 0, 0); return 0;}static int probe_swap1(int fd, blkid_cache cache __BLKID_ATTR((unused)), blkid_dev dev, const struct blkid_magic *id __BLKID_ATTR((unused)), unsigned char *buf __BLKID_ATTR((unused))){ struct swap_id_block *sws; probe_swap0(fd, cache, dev, id, buf); /* * Version 1 swap headers are always located at offset of 1024 * bytes, although the swap signature itself is located at the * end of the page (which may vary depending on hardware * pagesize). */ if (lseek(fd, 1024, SEEK_SET) < 0) return 1; sws = xmalloc(1024); if (read(fd, sws, 1024) != 1024) { free(sws); return 1; } /* arbitrary sanity check.. is there any garbage down there? */ if (sws->sws_pad[32] == 0 && sws->sws_pad[33] == 0) { if (sws->sws_volume[0]) blkid_set_tag(dev, "LABEL", (const char*)sws->sws_volume,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -