static void read_evms(void) { char line[100]; int ma, mi, sz; FILE *procpt; char uuid[16], *label, *devname; char device[110]; dev_t dev; struct stat statbuf; procpt = fopen(PROC_EVMS_VOLUMES, "r"); if (!procpt) return; while (fgets(line, sizeof(line), procpt)) { if (sscanf (line, " %d %d %d %*s %*s %[^\n ]", &ma, &mi, &sz, device) != 4) continue; /* * We first look for the device in the named location, * but if we don't find it, or if the stat information * doesn't check out, we use ext2fs_find_block_device * to find it. */ dev = makedev(ma, mi); if ((stat(device, &statbuf) < 0) || (statbuf.st_rdev != dev)) { devname = ext2fs_find_block_device(dev); } else devname = string_copy(device); if (!devname) continue; #ifdef DEBUG printf("Checking partition %s (%d, %d)\n", devname, ma, mi); #endif if (!get_label_uuid(devname, &label, uuid)) uuidcache_addentry(devname, label, uuid); else free(devname); } fclose(procpt); }
int main(int argc, char** argv) { char *devname, *tmp; int major, minor; dev_t device; const char *errmsg = "Cannot parse %s: %s\n"; if ((argc != 2) && (argc != 3)) { fprintf(stderr, "Usage: %s device_number\n", argv[0]); fprintf(stderr, "\t: %s major minor\n", argv[0]); exit(1); } if (argc == 2) { device = strtoul(argv[1], &tmp, 0); if (*tmp) { fprintf(stderr, errmsg, "device number", argv[1]); exit(1); } } else { major = strtoul(argv[1], &tmp, 0); if (*tmp) { fprintf(stderr, errmsg, "major number", argv[1]); exit(1); } minor = strtoul(argv[2], &tmp, 0); if (*tmp) { fprintf(stderr, errmsg, "minor number", argv[2]); exit(1); } device = makedev(major, minor); printf("Looking for device 0x%04x (%d:%d)\n", device, major, minor); } devname = ext2fs_find_block_device(device); if (devname) { printf("Found device! %s\n", devname); free(devname); } else { printf("Cannot find device.\n"); } return 0; }
/* * Remove an external journal from the filesystem */ static void remove_journal_device(ext2_filsys fs) { char *journal_path; ext2_filsys jfs; char buf[1024]; journal_superblock_t *jsb; int i, nr_users; errcode_t retval; int commit_remove_journal = 0; if (f_flag) commit_remove_journal = 1; /* force removal even if error */ uuid_unparse(fs->super->s_journal_uuid, buf); journal_path = get_spec_by_uuid(buf); if (!journal_path) { journal_path = ext2fs_find_block_device(fs->super->s_journal_dev); if (!journal_path) return; } retval = ext2fs_open(journal_path, EXT2_FLAG_RW| EXT2_FLAG_JOURNAL_DEV_OK, 0, fs->blocksize, unix_io_manager, &jfs); if (retval) { com_err(program_name, retval, _("while trying to open external journal")); goto no_valid_journal; } if (!(jfs->super->s_feature_incompat & EXT3_FEATURE_INCOMPAT_JOURNAL_DEV)) { fprintf(stderr, _("%s is not a journal device.\n"), journal_path); goto no_valid_journal; } /* Get the journal superblock */ if ((retval = io_channel_read_blk(jfs->io, 1, -1024, buf))) { com_err(program_name, retval, _("while reading journal superblock")); goto no_valid_journal; } jsb = (journal_superblock_t *) buf; if ((jsb->s_header.h_magic != (unsigned) ntohl(JFS_MAGIC_NUMBER)) || (jsb->s_header.h_blocktype != (unsigned) ntohl(JFS_SUPERBLOCK_V2))) { fprintf(stderr, _("Journal superblock not found!\n")); goto no_valid_journal; } /* Find the filesystem UUID */ nr_users = ntohl(jsb->s_nr_users); for (i=0; i < nr_users; i++) { if (memcmp(fs->super->s_uuid, &jsb->s_users[i*16], 16) == 0) break; } if (i >= nr_users) { fprintf(stderr, _("Filesystem's UUID not found on journal device.\n")); commit_remove_journal = 1; goto no_valid_journal; } nr_users--; for (i=0; i < nr_users; i++) memcpy(&jsb->s_users[i*16], &jsb->s_users[(i+1)*16], 16); jsb->s_nr_users = htonl(nr_users); /* Write back the journal superblock */ if ((retval = io_channel_write_blk(jfs->io, 1, -1024, buf))) { com_err(program_name, retval, "while writing journal superblock."); goto no_valid_journal; } commit_remove_journal = 1; no_valid_journal: if (commit_remove_journal == 0) { fprintf(stderr, _("Journal NOT removed\n")); exit(1); } fs->super->s_journal_dev = 0; memset(fs->super->s_journal_uuid, 0, sizeof(fs->super->s_journal_uuid)); ext2fs_mark_super_dirty(fs); printf(_("Journal removed\n")); free(journal_path); }
/* * Remove an external journal from the filesystem */ static void remove_journal_device(ext2_filsys fs) { char *journal_path; ext2_filsys jfs; char buf[1024]; journal_superblock_t *jsb; int i, nr_users; errcode_t retval; int commit_remove_journal = 0; io_manager io_ptr; if (f_flag) commit_remove_journal = 1; /* force removal even if error */ uuid_unparse(fs->super->s_journal_uuid, buf); journal_path = blkid_get_devname(NULL, "UUID", buf); if (!journal_path) { journal_path = ext2fs_find_block_device(fs->super->s_journal_dev); if (!journal_path) return; } io_ptr = unix_io_manager; retval = ext2fs_open(journal_path, EXT2_FLAG_RW| EXT2_FLAG_JOURNAL_DEV_OK, 0, fs->blocksize, io_ptr, &jfs); if (retval) { bb_error_msg("Failed to open external journal"); goto no_valid_journal; } if (!(jfs->super->s_feature_incompat & EXT3_FEATURE_INCOMPAT_JOURNAL_DEV)) { bb_error_msg("%s is not a journal device", journal_path); goto no_valid_journal; } /* Get the journal superblock */ if ((retval = io_channel_read_blk(jfs->io, 1, -1024, buf))) { bb_error_msg("Failed to read journal superblock"); goto no_valid_journal; } jsb = (journal_superblock_t *) buf; if ((jsb->s_header.h_magic != (unsigned) ntohl(JFS_MAGIC_NUMBER)) || (jsb->s_header.h_blocktype != (unsigned) ntohl(JFS_SUPERBLOCK_V2))) { bb_error_msg("Journal superblock not found!"); goto no_valid_journal; } /* Find the filesystem UUID */ nr_users = ntohl(jsb->s_nr_users); for (i=0; i < nr_users; i++) { if (memcmp(fs->super->s_uuid, &jsb->s_users[i*16], 16) == 0) break; } if (i >= nr_users) { bb_error_msg("Filesystem's UUID not found on journal device"); commit_remove_journal = 1; goto no_valid_journal; } nr_users--; for (i=0; i < nr_users; i++) memcpy(&jsb->s_users[i*16], &jsb->s_users[(i+1)*16], 16); jsb->s_nr_users = htonl(nr_users); /* Write back the journal superblock */ if ((retval = io_channel_write_blk(jfs->io, 1, -1024, buf))) { bb_error_msg("Failed to write journal superblock"); goto no_valid_journal; } commit_remove_journal = 1; no_valid_journal: if (commit_remove_journal == 0) bb_error_msg_and_die("Journal NOT removed"); fs->super->s_journal_dev = 0; uuid_clear(fs->super->s_journal_uuid); ext2fs_mark_super_dirty(fs); puts("Journal removed"); free(journal_path); }
static void read_partitions(void) { char line[100]; char *s; int ma, mi, sz; static char ptname[100]; FILE *procpt; char uuid[16], *label, *devname; char device[110]; dev_t dev; struct stat statbuf; int firstPass; int handleOnFirst; char *iobuf; procpt = fopen(PROC_PARTITIONS, "r"); if (!procpt) return; iobuf = (char *)malloc(CBBUF); if (iobuf) setvbuf(procpt, iobuf, _IOFBF, CBBUF); for (firstPass = 1; firstPass >= 0; firstPass--) { fseek(procpt, 0, SEEK_SET); while (fgets(line, sizeof(line), procpt)) { if (sscanf (line, " %d %d %d %[^\n ]", &ma, &mi, &sz, ptname) != 4) continue; /* skip extended partitions (heuristic: size 1) */ if (sz == 1) continue; /* look only at md devices on first pass */ handleOnFirst = !strncmp(ptname, "md", 2); if (firstPass != handleOnFirst) continue; /* skip entire disk (minor 0, 64, ... on ide; 0, 16, ... on sd) */ /* heuristic: partition name ends in a digit */ /* OR partition name starts with 'lvm' */ for(s = ptname; *s; s++); if (isdigit(s[-1]) || !strncmp(ptname, "lvm", 3)) { /* * We first look in /dev for the device, but * if we don't find it, or if the stat * information doesn't check out, we use * ext2fs_find_block_device to find it. */ sprintf(device, "%s/%s", DEVLABELDIR, ptname); dev = makedev(ma, mi); if ((stat(device, &statbuf) < 0) || (statbuf.st_rdev != dev)) { devname = ext2fs_find_block_device(dev); } else devname = string_copy(device); if (!devname) continue; #ifdef DEBUG printf("Checking partition %s (%d, %d)\n", devname, ma, mi); #endif if (!get_label_uuid(devname, &label, uuid)) uuidcache_addentry(devname, label, uuid); else free(devname); } } } fclose(procpt); if (iobuf) free(iobuf); }