DVM: fix savefile to contain ip address

needed for routed networking
This commit is contained in:
Rafal Wojtczuk 2010-09-07 17:36:28 +02:00
parent 2dd9bab23a
commit 6472e8c926
3 changed files with 59 additions and 35 deletions

View File

@ -1098,7 +1098,7 @@ class QubesAppVm(QubesVm):
label=self.label.name) label=self.label.name)
return element return element
def start(self, debug_console = False, verbose = False): def start(self, debug_console = False, verbose = False, preparing_dvm = False):
if dry_run: if dry_run:
return return
@ -1113,7 +1113,7 @@ class QubesAppVm(QubesVm):
self.reset_cow_storage() self.reset_cow_storage()
self.reset_swap_cow_storage() self.reset_swap_cow_storage()
return super(QubesAppVm, self).start(debug_console=debug_console, verbose=verbose) return super(QubesAppVm, self).start(debug_console=debug_console, verbose=verbose, preparing_dvm=preparing_dvm)
def reset_cow_storage (self): def reset_cow_storage (self):

View File

@ -1,4 +1,5 @@
CC=gcc CC=gcc
CFLAGS=-Wall
all: qubes_restore xenstore-watch xenfreepages all: qubes_restore xenstore-watch xenfreepages
qubes_restore: qubes_restore.o qubes_restore: qubes_restore.o
$(CC) -o qubes_restore qubes_restore.o -lxenstore $(CC) -o qubes_restore qubes_restore.o -lxenstore

View File

@ -221,32 +221,33 @@ void fix_savefile(int fd, char *buf, char *pattern, char *val)
} }
char * dispname_by_dispid(int dispid) char *dispname_by_dispid(int dispid)
{ {
static char retbuf[16]; static char retbuf[16];
snprintf(retbuf, sizeof(retbuf), "disp%d", dispid); snprintf(retbuf, sizeof(retbuf), "disp%d", dispid);
return retbuf; return retbuf;
} }
char *build_dvm_ip(int netvm, int id)
{
static char buf[256];
snprintf(buf, sizeof(buf), "10.%d.%d.%d", netvm, id / 254 + 200,
(id % 254) + 1);
return buf;
}
#define NAME_PATTERN "/root-cow.img" #define NAME_PATTERN "/root-cow.img"
char *fix_savefile_and_get_vmname(int fd, int dispid) char *get_vmname_from_savefile(int fd)
{ {
static char buf[4096]; static char buf[4096];
char *name; char *name;
char *slash; char *slash;
char val[256]; lseek(fd, 0, SEEK_SET);
if (read(fd, buf, sizeof(buf)) != sizeof(buf)) { if (read(fd, buf, sizeof(buf)) != sizeof(buf)) {
perror("read savefile"); perror("read savefile");
exit(1); exit(1);
} }
buf[sizeof(buf) - 1] = 0; buf[sizeof(buf) - 1] = 0;
snprintf(val, sizeof(val),
"064cd14c-95ad-4fc2-a4c9-cf9f522e5b%02x", dispid);
fix_savefile(fd, buf, "(uuid ", val);
fix_savefile(fd, buf, "(name ", dispname_by_dispid(dispid));
snprintf(val, sizeof(val), "00:16:3e:7c:8b:%02x", dispid);
fix_savefile(fd, buf, "(mac ", val);
lseek(fd, 0, SEEK_SET);
name = strstr(buf + 20, NAME_PATTERN); name = strstr(buf + 20, NAME_PATTERN);
if (!name) { if (!name) {
fprintf(stderr, fprintf(stderr,
@ -264,6 +265,25 @@ char *fix_savefile_and_get_vmname(int fd, int dispid)
return slash + 1; return slash + 1;
} }
void fix_savefile_all(int fd, int dispid, int netvm_id)
{
char val[256];
char buf[4096];
lseek(fd, 0, SEEK_SET);
if (read(fd, buf, sizeof(buf)) != sizeof(buf)) {
perror("read savefile");
exit(1);
}
buf[sizeof(buf) - 1] = 0;
snprintf(val, sizeof(val),
"064cd14c-95ad-4fc2-a4c9-cf9f522e5b%02x", dispid);
fix_savefile(fd, buf, "(uuid ", val);
fix_savefile(fd, buf, "(name ", dispname_by_dispid(dispid));
snprintf(val, sizeof(val), "00:16:3e:7c:8b:%02x", dispid);
fix_savefile(fd, buf, "(mac ", val);
fix_savefile(fd, buf, "(ip ", build_dvm_ip(netvm_id, dispid));
}
void unpack_cows(char *name) void unpack_cows(char *name)
{ {
char vmdir[4096]; char vmdir[4096];
@ -304,18 +324,11 @@ void write_xs_single(struct xs_handle *xs, int domid, char *name,
} }
} }
int get_netvm_id_from_name(char *name)
void setup_xenstore(int domid, char *name)
{ {
char val[256];
char netvm_id_path[256];
int fd, n; int fd, n;
char netvm_id[256]; char netvm_id[256];
struct xs_handle *xs = xs_daemon_open(); char netvm_id_path[256];
if (!xs) {
perror("xs_daemon_open");
exit(1);
}
snprintf(netvm_id_path, sizeof(netvm_id_path), snprintf(netvm_id_path, sizeof(netvm_id_path),
"/var/lib/qubes/appvms/%s/netvm_id.txt", name); "/var/lib/qubes/appvms/%s/netvm_id.txt", name);
fd = open(netvm_id_path, O_RDONLY); fd = open(netvm_id_path, O_RDONLY);
@ -326,14 +339,24 @@ void setup_xenstore(int domid, char *name)
n = read(fd, netvm_id, sizeof(netvm_id) - 1); n = read(fd, netvm_id, sizeof(netvm_id) - 1);
close(fd); close(fd);
netvm_id[n] = 0; netvm_id[n] = 0;
return atoi(netvm_id);
}
snprintf(val, sizeof(val), "10.%s.%d.%d", netvm_id, void setup_xenstore(int netvm_id, int domid, int dvmid, char *name)
domid / 254 + 200, (domid % 254) + 1); {
write_xs_single(xs, domid, "qubes_ip", val); char val[256];
struct xs_handle *xs = xs_daemon_open();
if (!xs) {
perror("xs_daemon_open");
exit(1);
}
write_xs_single(xs, domid, "qubes_ip",
build_dvm_ip(netvm_id, dvmid));
write_xs_single(xs, domid, "qubes_netmask", "255.255.0.0"); write_xs_single(xs, domid, "qubes_netmask", "255.255.0.0");
snprintf(val, sizeof(val), "10.%s.0.1", netvm_id); snprintf(val, sizeof(val), "10.%d.0.1", netvm_id);
write_xs_single(xs, domid, "qubes_gateway", val); write_xs_single(xs, domid, "qubes_gateway", val);
snprintf(val, sizeof(val), "10.%s.255.254", netvm_id); snprintf(val, sizeof(val), "10.%d.255.254", netvm_id);
write_xs_single(xs, domid, "qubes_secondary_dns", val); write_xs_single(xs, domid, "qubes_secondary_dns", val);
write_xs_single(xs, domid, "qubes_vm_type", "AppVM"); write_xs_single(xs, domid, "qubes_vm_type", "AppVM");
xs_daemon_close(xs); xs_daemon_close(xs);
@ -356,7 +379,7 @@ int get_next_disposable_id()
return seq; return seq;
} }
void write_varrun_domid(int domid, char * dispname, char *orig) void write_varrun_domid(int domid, char *dispname, char *orig)
{ {
FILE *f = fopen("/var/run/qubes/dispVM_xid", "w"); FILE *f = fopen("/var/run/qubes/dispVM_xid", "w");
if (!f) { if (!f) {
@ -370,8 +393,7 @@ void write_varrun_domid(int domid, char * dispname, char *orig)
void redirect_stderr() void redirect_stderr()
{ {
int fd = int fd = open("/var/log/qubes/qubes_restore.log",
open("/var/log/qubes/qubes_restore.log",
O_CREAT | O_TRUNC | O_WRONLY, 0600); O_CREAT | O_TRUNC | O_WRONLY, 0600);
if (fd < 0) { if (fd < 0) {
syslog(LOG_DAEMON | LOG_ERR, "open qubes_restore.log"); syslog(LOG_DAEMON | LOG_ERR, "open qubes_restore.log");
@ -383,12 +405,12 @@ void redirect_stderr()
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
int fd, domid, dispid; int fd, domid, dispid, netvm_id;
char *resp; char *resp;
char *name; char *name;
if (argc < 2) { if (argc < 2) {
fprintf(stderr, "usage: %s savefile [guid args] \n", fprintf(stderr,
argv[0]); "usage: %s savefile [guid args] \n", argv[0]);
exit(1); exit(1);
} }
redirect_stderr(); redirect_stderr();
@ -401,7 +423,8 @@ int main(int argc, char **argv)
exit(1); exit(1);
} }
dispid = get_next_disposable_id(); dispid = get_next_disposable_id();
name = fix_savefile_and_get_vmname(fd, dispid); name = get_vmname_from_savefile(fd);
netvm_id = get_netvm_id_from_name(name);
// printf("name=%s\n", name); // printf("name=%s\n", name);
unpack_cows(name); unpack_cows(name);
// no preloading for now, assume savefile in shm // no preloading for now, assume savefile in shm
@ -419,7 +442,7 @@ int main(int argc, char **argv)
resp = recv_resp(fd); resp = recv_resp(fd);
// printf("%s\n", resp); // printf("%s\n", resp);
fprintf(stderr, "time=%s, creating xenstore entries\n", gettime()); fprintf(stderr, "time=%s, creating xenstore entries\n", gettime());
setup_xenstore(domid, name); setup_xenstore(netvm_id, domid, dispid, name);
fprintf(stderr, "time=%s, starting qubes_guid\n", gettime()); fprintf(stderr, "time=%s, starting qubes_guid\n", gettime());
rm_fast_flag(); rm_fast_flag();
start_guid(domid, argc, argv); start_guid(domid, argc, argv);