1import logging, time
2from autotest_lib.client.common_lib import error
3
4
5def run_linux_s3(test, params, env):
6    """
7    Suspend a guest Linux OS to memory.
8
9    @param test: kvm test object.
10    @param params: Dictionary with test parameters.
11    @param env: Dictionary with the test environment.
12    """
13    vm = env.get_vm(params["main_vm"])
14    vm.verify_alive()
15    timeout = int(params.get("login_timeout", 360))
16    session = vm.wait_for_login(timeout=timeout)
17
18    logging.info("Checking that VM supports S3")
19    session.cmd("grep -q mem /sys/power/state")
20
21    logging.info("Waiting for a while for X to start")
22    time.sleep(10)
23
24    src_tty = session.cmd_output("fgconsole").strip()
25    logging.info("Current virtual terminal is %s", src_tty)
26    if src_tty not in map(str, range(1, 10)):
27        raise error.TestFail("Got a strange current vt (%s)" % src_tty)
28
29    dst_tty = "1"
30    if src_tty == "1":
31        dst_tty = "2"
32
33    logging.info("Putting VM into S3")
34    command = "chvt %s && echo mem > /sys/power/state && chvt %s" % (dst_tty,
35                                                                     src_tty)
36    suspend_timeout = 120 + int(params.get("smp")) * 60
37    session.cmd(command, timeout=suspend_timeout)
38
39    logging.info("VM resumed after S3")
40
41    session.close()
42