When you run KVM-based virtual machine, you can specify ‘-startdate’ option. What format we can use and what actions are happening inside the qemu-kvm.
Options are checked in main() function in vl.c.
case QEMU_OPTION_startdate: { struct tm tm; time_t rtc_start_date; if (!strcmp(optarg, "now")) { rtc_date_offset = -1; } else { if (sscanf(optarg, "%d-%d-%dT%d:%d:%d", &tm.tm_year, &tm.tm_mon, &tm.tm_mday, &tm.tm_hour, &tm.tm_min, &tm.tm_sec) == 6) { /* OK */ } else if (sscanf(optarg, "%d-%d-%d", &tm.tm_year, &tm.tm_mon, &tm.tm_mday) == 3) { tm.tm_hour = 0; tm.tm_min = 0; tm.tm_sec = 0; } else { goto date_fail; } tm.tm_year -= 1900; tm.tm_mon--; rtc_start_date = mktimegm(&tm); if (rtc_start_date == -1) { date_fail: fprintf(stderr, "Invalid date format. Valid format are:n" "'now' or '2006-06-17T16:01:21' or '2006-06-17'n"); exit(1); } rtc_date_offset = time(NULL) - rtc_start_date; } } break;
rtc_date_offset is declared as a global variable in vl.c.
static int rtc_date_offset = -1; /* -1 means no change */
This variable is used to use time information in virtual machine as shown below.
/***********************************************************/ /* host time/date access */ void qemu_get_timedate(struct tm *tm, int offset) { time_t ti; struct tm *ret; time(&ti); ti += offset; if (rtc_date_offset == -1) { if (rtc_utc) ret = gmtime(&ti); else ret = localtime(&ti); } else { ti -= rtc_date_offset; ret = gmtime(&ti); } memcpy(tm, ret, sizeof(struct tm)); } int qemu_timedate_diff(struct tm *tm) { time_t seconds; if (rtc_date_offset == -1) if (rtc_utc) seconds = mktimegm(tm); else seconds = mktime(tm); else seconds = mktimegm(tm) + rtc_date_offset; return seconds - time(NULL); }
In here, we can see that the time information is basically come from system OS’s time facilities such as gmtime and localtime. This information can be modified if customer wants to.
Leave a Reply