From c5d0aa01c1cb94e3847f8649ff8a13ae5d7026fe Mon Sep 17 00:00:00 2001 From: Andrea Mazzoleni Date: Sat, 21 Sep 2013 15:53:46 +0200 Subject: [PATCH] Adds a new -C, --gen-conf option to generate the configuration file from the content file. --- HISTORY | 4 ++++ snapraid.1 | 11 ++++++++- snapraid.c | 19 +++++++++++---- snapraid.d | 11 ++++++++- snapraid.txt | 11 ++++++++- state.c | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++++ state.h | 9 +++++++ 7 files changed, 124 insertions(+), 7 deletions(-) diff --git a/HISTORY b/HISTORY index 961ecfec..c8cc946a 100644 --- a/HISTORY +++ b/HISTORY @@ -5,6 +5,10 @@ SnapRAID HISTORY ========== * If the underline filesystem doesn't support the FIEMAP command, fallback to use the inode number for sorting files. + This happens with reiserfs in Linux. + * Added a new -C, --gen-conf option to generate a dummy configuration + file from the info in the content file. + Just in case that you lose everything, except the content file. 4.0 2013/9 ========== diff --git a/snapraid.1 b/snapraid.1 index 881f193e..3b0df3d6 100644 --- a/snapraid.1 +++ b/snapraid.1 @@ -43,7 +43,7 @@ snapraid [\(hyc, \(hy\(hyconf CONFIG] .PP .PD .PP -snapraid [\(hyV, \(hy\(hyversion] [\(hyh, \(hy\(hyhelp] +snapraid [\(hyV, \(hy\(hyversion] [\(hyh, \(hy\(hyhelp] [\(hyC, \(hy\(hygen\(hyconf CONTENT] .PD 0 .PP .PD @@ -689,6 +689,15 @@ It\(cqs present mainly for advanced manual recovering. Processes only the specified number of blocks. It\(cqs present mainly for advanced manual recovering. .TP +.B \(hyC, \(hy\(hygen\(hyconf CONTENT_FILE +Generates a dummy configuration file from an existing +content file. +The configuration file is written in the standard output, +and it doesn\(cqt overwrite an existing one. +This configuration file also contains the information +needed to reconstruct the disk mount points, in case you +lose the entire system. +.TP .B \(hyv, \(hy\(hyverbose Prints more information in the processing. .TP diff --git a/snapraid.c b/snapraid.c index fccea839..5d94dfdc 100644 --- a/snapraid.c +++ b/snapraid.c @@ -119,6 +119,7 @@ struct option long_options[] = { { "find-by-name", 0, 0, 'N' }, /* deprecated in SnapRAID 4.0 */ { "audit-only", 0, 0, 'a' }, { "speed-test", 0, 0, 'T' }, + { "gen-conf", 1, 0, 'C' }, { "verbose", 0, 0, 'v' }, { "gui", 0, 0, 'G' }, /* undocumented GUI interface command */ { "help", 0, 0, 'h' }, @@ -181,7 +182,7 @@ struct option long_options[] = { }; #endif -#define OPTIONS "c:f:d:mep:o:s:t:i:l:ZEUDNaTvhVG" +#define OPTIONS "c:f:d:mep:o:s:t:i:l:ZEUDNaTC:vhVG" volatile int global_interrupt = 0; @@ -228,8 +229,11 @@ int main(int argc, char* argv[]) const char* import; const char* log; int lock; + const char* gen_conf; os_init(); + raid_init(); + crc32c_init(); /* defaults */ conf = CONF; @@ -246,6 +250,7 @@ int main(int argc, char* argv[]) import = 0; log = 0; lock = 0; + gen_conf = 0; opterr = 0; while ((c = @@ -356,6 +361,9 @@ int main(int argc, char* argv[]) case 'T' : speed(); exit(EXIT_SUCCESS); + case 'C' : + gen_conf = optarg; + break; case OPT_TEST_KILL_AFTER_SYNC : opt.kill_after_sync = 1; break; @@ -419,6 +427,12 @@ int main(int argc, char* argv[]) } } + if (gen_conf != 0) { + generate_configuration(gen_conf); + os_done(); + exit(EXIT_SUCCESS); + } + if (optind + 1 != argc) { usage(); exit(EXIT_FAILURE); @@ -505,9 +519,6 @@ int main(int argc, char* argv[]) } } - raid_init(); - crc32c_init(); - /* open the log file */ if (log == 0) log = "2"; diff --git a/snapraid.d b/snapraid.d index e7bce5e6..558dcefa 100644 --- a/snapraid.d +++ b/snapraid.d @@ -13,7 +13,7 @@ Synopsis : [-s, --start BLKSTART] [-t, --count BLKCOUNT] : sync|status|scrub|diff|dup|pool|check|fix|rehash - :snapraid [-V, --version] [-h, --help] + :snapraid [-V, --version] [-h, --help] [-C, --gen-conf CONTENT] Description SnapRAID is a backup program for disk arrays. It stores redundancy @@ -513,6 +513,15 @@ Options Processes only the specified number of blocks. It's present mainly for advanced manual recovering. + -C, --gen-conf CONTENT_FILE + Generates a dummy configuration file from an existing + content file. + The configuration file is written in the standard output, + and it doesn't overwrite an existing one. + This configuration file also contains the information + needed to reconstruct the disk mount points, in case you + lose the entire system. + -v, --verbose Prints more information in the processing. diff --git a/snapraid.txt b/snapraid.txt index b92c902d..12ba6719 100644 --- a/snapraid.txt +++ b/snapraid.txt @@ -17,7 +17,7 @@ snapraid [-c, --conf CONFIG] [-s, --start BLKSTART] [-t, --count BLKCOUNT] sync|status|scrub|diff|dup|pool|check|fix|rehash -snapraid [-V, --version] [-h, --help] +snapraid [-V, --version] [-h, --help] [-C, --gen-conf CONTENT] 2 DESCRIPTION @@ -556,6 +556,15 @@ SnapRAID provides the following options: Processes only the specified number of blocks. It's present mainly for advanced manual recovering. + -C, --gen-conf CONTENT_FILE + Generates a dummy configuration file from an existing + content file. + The configuration file is written in the standard output, + and it doesn't overwrite an existing one. + This configuration file also contains the information + needed to reconstruct the disk mount points, in case you + lose the entire system. + -v, --verbose Prints more information in the processing. diff --git a/state.c b/state.c index dd3274ce..1646871c 100644 --- a/state.c +++ b/state.c @@ -39,6 +39,7 @@ void state_init(struct snapraid_state* state) state->lockfile[0] = 0; state->level = 1; /* default is the lowest protection */ state->loaded_blockmax = 0; + state->no_conf = 0; tommy_list_init(&state->disklist); tommy_list_init(&state->maplist); @@ -873,6 +874,17 @@ static struct snapraid_disk* find_disk(struct snapraid_state* state, const char* return disk; } + if (state->no_conf) { + /* without a configuration file, add disks automatically */ + struct snapraid_disk* disk; + + disk = disk_alloc(name, "DUMMY/", -1); + + tommy_list_insert_tail(&state->disklist, &disk->node, disk); + + return disk; + } + return 0; } @@ -3477,3 +3489,57 @@ int state_progress(struct snapraid_state* state, block_off_t blockpos, block_off return 0; } +void generate_configuration(const char* path) +{ + struct snapraid_state state; + struct snapraid_content* content; + tommy_node* j; + + state_init(&state); + + /* mark that we are without a configuration file */ + state.no_conf = 1; + + /* create the dummy content entry */ + content = content_alloc(path, -1); + + /* adds the content entry */ + tommy_list_insert_tail(&state.contentlist, &content->node, content); + + /* read the content file */ + state_read(&state); + + /* output a dummy configuration file */ + printf("# Configuration file generated from %s\n", path); + printf("\n"); + printf("# Use this blocksize\n"); + printf("block_size %u\n", state.block_size / 1024); + printf("\n"); + printf("# Set the correct path for the Parity file\n"); + printf("parity ENTER_HERE_THE_PARITY_FILE\n"); + printf("\n"); + printf("# Set the correct path for the Q-Parity file (if used)\n"); + printf("q-parity ENTER_HERE_THE_PARITY_FILE\n"); + printf("\n"); + printf("# Add any other content file\n"); + printf("content %s\n", path); + printf("\n"); + for(j=state.maplist;j;j=j->next) { + struct snapraid_map* map = j->data; + struct snapraid_disk* disk; + printf("# Set the correct dir for disk '%s'\n", map->name); + printf("# Disk '%s' is the one with system id '%s'\n", map->name, map->uuid); + disk = find_disk(&state, map->name); + if (disk && disk->filelist) { + struct snapraid_file* file = disk->filelist->data; + if (file) { + printf("# and containing: %s\n", file->sub); + } + } + printf("disk %s ENTER_HERE_THE_DIR\n", map->name); + printf("\n"); + } + + state_done(&state); +} + diff --git a/state.h b/state.h index 802ee6cc..6437770e 100644 --- a/state.h +++ b/state.h @@ -93,6 +93,7 @@ struct snapraid_state { time_t progress_last; /**< Last update of progress visualization. */ time_t progress_interruption; /**< Start of the measure interruption. */ time_t progress_subtract; /**< Time to subtract for the interruptions. */ + int no_conf; /**< Automatically add missing info. Used to load content without a configuration file. */ }; /** @@ -198,5 +199,13 @@ void state_progress_stop(struct snapraid_state* state); */ void state_progress_restart(struct snapraid_state* state); +/****************************************************************************/ +/* misc */ + +/** + * Generate a dummy configuration file from a content file. + */ +void generate_configuration(const char* content); + #endif