What’s the internal details for ‘-startdate’ option in ‘qemu-kvm’

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_sec) == 6) {
                            /* OK */
                        } else if (sscanf(optarg, "%d-%d-%d",
                                          &tm.tm_mday) == 3) {
                            tm.tm_hour = 0;
                            tm.tm_min = 0;
                            tm.tm_sec = 0;
                        } else {
                            goto date_fail;
                        tm.tm_year -= 1900;
                        rtc_start_date = mktimegm(&tm);
                        if (rtc_start_date == -1) {
                            fprintf(stderr, "Invalid date format. Valid format are:n"
                                    "'now' or '2006-06-17T16:01:21' or '2006-06-17'n");
                        rtc_date_offset = time(NULL) - rtc_start_date;

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;

    ti += offset;
    if (rtc_date_offset == -1) {
        if (rtc_utc)
            ret = gmtime(&ti);
            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);
            seconds = mktime(tm);
        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

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: