Add the --unlink option to mirror

This deletes the local file before tearing down the mirror connection,
allowing us to avoid an ambiguous recovery situation.
This commit is contained in:
Alex Young
2012-07-23 13:39:27 +01:00
parent fd935ce4c9
commit 33f95e1986
9 changed files with 140 additions and 44 deletions

View File

@@ -476,7 +476,7 @@ int client_serve_request(struct client* client)
}
server_unlock_io( client->serve );
return failure;
return stop;
}

View File

@@ -324,22 +324,15 @@ int control_mirror(struct control_client* client, int linesc, char** lines)
}
connect_to->v4.sin_port = htobe16(raw_port);
if (linesc > 2) {
connect_from = xmalloc( sizeof( union mysockaddr ) );
if (parse_ip_to_sockaddr(&connect_from->generic, lines[2]) == 0) {
write_socket("1: bad bind address");
return -1;
}
}
if (linesc > 3) { max_Bps = atoi(lines[2]); }
action_at_finish = ACTION_EXIT;
if (linesc > 4) {
if (strcmp("exit", lines[3]) == 0) {
if (linesc > 2) {
if (strcmp("exit", lines[2]) == 0) {
action_at_finish = ACTION_EXIT;
}
else if (strcmp("nothing", lines[3]) == 0) {
else if (strcmp( "unlink", lines[2]) == 0 ) {
action_at_finish = ACTION_UNLINK;
}
else if (strcmp("nothing", lines[2]) == 0) {
action_at_finish = ACTION_NOTHING;
}
else {
@@ -347,6 +340,19 @@ int control_mirror(struct control_client* client, int linesc, char** lines)
return -1;
}
}
if (linesc > 3) {
connect_from = xmalloc( sizeof( union mysockaddr ) );
if (parse_ip_to_sockaddr(&connect_from->generic, lines[2]) == 0) {
write_socket("1: bad bind address");
return -1;
}
}
if (linesc > 4) {
max_Bps = atoi(lines[2]);
}
if (linesc > 5) {
write_socket("1: unrecognised parameters to mirror");
@@ -384,7 +390,7 @@ int control_mirror(struct control_client* client, int linesc, char** lines)
control_write_mirror_response( state, client->socket );
}
debug( "Control thread going away." );
return 0;
}

View File

@@ -236,7 +236,7 @@ void mirror_on_exit( struct server * serve )
debug("Unlinking %s", serve->filename );
if ( ACTION_UNLINK == serve->mirror->action_at_finish ) {
server_unlink( serve->mirror );
server_unlink( serve );
}
debug("Sending disconnect");

View File

@@ -32,15 +32,25 @@ static char serve_help_text[] =
QUIET_LINE;
static struct option * listen_options = serve_options;
static char * listen_short_options = serve_short_options;
static struct option listen_options[] = {
GETOPT_HELP,
GETOPT_ADDR,
GETOPT_PORT,
GETOPT_FILE,
GETOPT_SOCK,
GETOPT_DENY,
GETOPT_QUIET,
GETOPT_VERBOSE,
{0}
};
static char listen_short_options[] = "hl:p:f:s:d" SOPT_QUIET SOPT_VERBOSE;
static char listen_help_text[] =
"Usage: flexnbd " CMD_LISTEN " <options> [<acl_address>*]\n\n"
"Listen for an incoming migration on ADDR:PORT.\n\n"
"Listen for an incoming migration on ADDR:PORT."
HELP_LINE
"\t--" OPT_ADDR ",-l <ADDR>\tThe address to listen on.\n"
"\t--" OPT_PORT ",-p <PORT>\tThe port to listen on.\n"
"\t--" OPT_FILE ",-f <FILE>\tThe file to write to.\n"
"\t--" OPT_FILE ",-f <FILE>\tThe file to serve.\n"
"\t--" OPT_DENY ",-d\tDeny connections by default unless in ACL.\n"
SOCK_LINE
VERBOSE_LINE
@@ -107,12 +117,13 @@ static struct option mirror_options[] = {
GETOPT_SOCK,
GETOPT_ADDR,
GETOPT_PORT,
GETOPT_UNLINK,
GETOPT_BIND,
GETOPT_QUIET,
GETOPT_VERBOSE,
{0}
};
static char mirror_short_options[] = "hs:l:p:b:" SOPT_QUIET SOPT_VERBOSE;
static char mirror_short_options[] = "hs:l:p:ub:" SOPT_QUIET SOPT_VERBOSE;
static char mirror_help_text[] =
"Usage: flexnbd " CMD_MIRROR " <options>\n\n"
"Start mirroring from the server with control socket SOCK to one at ADDR:PORT.\n\n"
@@ -120,6 +131,7 @@ static char mirror_help_text[] =
"\t--" OPT_ADDR ",-l <ADDR>\tThe address to mirror to.\n"
"\t--" OPT_PORT ",-p <PORT>\tThe port to mirror to.\n"
SOCK_LINE
"\t--" OPT_UNLINK ",-u\tUnlink the local file when done.\n"
BIND_LINE
VERBOSE_LINE
QUIET_LINE;
@@ -316,7 +328,13 @@ void read_acl_param( int c, char **sock )
read_sock_param( c, sock, acl_help_text );
}
void read_mirror_param( int c, char **sock, char **ip_addr, char **ip_port, char **bind_addr )
void read_mirror_param(
int c,
char **sock,
char **ip_addr,
char **ip_port,
int *unlink,
char **bind_addr )
{
switch( c ){
case 'h':
@@ -332,6 +350,9 @@ void read_mirror_param( int c, char **sock, char **ip_addr, char **ip_port, char
case 'p':
*ip_port = optarg;
break;
case 'u':
*unlink = 1;
break;
case 'b':
*bind_addr = optarg;
break;
@@ -404,15 +425,7 @@ int mode_serve( int argc, char *argv[] )
}
if ( err ) { exit_err( serve_help_text ); }
flexnbd = flexnbd_create_serving(
ip_addr,
ip_port,
file,
sock,
default_deny,
argc - optind,
argv + optind,
MAX_NBD_CLIENTS );
flexnbd = flexnbd_create_serving( ip_addr, ip_port, file, sock, default_deny, argc - optind, argv + optind, MAX_NBD_CLIENTS );
flexnbd_serve( flexnbd );
flexnbd_destroy( flexnbd );
@@ -459,7 +472,7 @@ int mode_listen( int argc, char *argv[] )
sock,
default_deny,
argc - optind,
argv + optind );
argv + optind);
success = flexnbd_serve( flexnbd );
flexnbd_destroy( flexnbd );
@@ -639,11 +652,19 @@ int mode_mirror( int argc, char *argv[] )
char *sock = NULL;
char *remote_argv[4] = {0};
int err = 0;
int unlink = 0;
remote_argv[2] = "exit";
while (1) {
c = getopt_long( argc, argv, mirror_short_options, mirror_options, NULL);
if ( -1 == c ) { break; }
read_mirror_param( c, &sock, &remote_argv[0], &remote_argv[1], &remote_argv[2] );
read_mirror_param( c,
&sock,
&remote_argv[0],
&remote_argv[1],
&unlink,
&remote_argv[3] );
}
if ( NULL == sock ){
@@ -655,12 +676,13 @@ int mode_mirror( int argc, char *argv[] )
err = 1;
}
if ( err ) { exit_err( mirror_help_text ); }
if ( unlink ) { remote_argv[2] = "unlink"; }
if (remote_argv[2] == NULL) {
do_remote_command( "mirror", sock, 2, remote_argv );
if (remote_argv[3] == NULL) {
do_remote_command( "mirror", sock, 3, remote_argv );
}
else {
do_remote_command( "mirror", sock, 3, remote_argv );
do_remote_command( "mirror", sock, 4, remote_argv );
}
return 0;

View File

@@ -21,6 +21,7 @@ void mode(char* mode, int argc, char **argv);
#define OPT_FROM "from"
#define OPT_SIZE "size"
#define OPT_DENY "default-deny"
#define OPT_UNLINK "unlink"
#define CMD_SERVE "serve"
#define CMD_LISTEN "listen"
@@ -51,6 +52,7 @@ void mode(char* mode, int argc, char **argv);
#define GETOPT_FROM GETOPT_ARG( OPT_FROM, 'F' )
#define GETOPT_SIZE GETOPT_ARG( OPT_SIZE, 'S' )
#define GETOPT_BIND GETOPT_ARG( OPT_BIND, 'b' )
#define GETOPT_UNLINK GETOPT_ARG( OPT_UNLINK, 'u' )
#define OPT_VERBOSE "verbose"
#define SOPT_VERBOSE "v"