# MachineType

MachineType是qemu中所有虚拟机类型的父类。虽然这是一个抽象类，但是对这个类的了解也能帮助我们对后续子类的理解有帮助。

## 继承关系

先来看一下MachineType类本身的继承关系

```
   TYPE_OBJECT
   +-------------------------------+
   |class_init                     | = object_class_init
   |                               |
   |instance_size                  | = sizeof(Object)
   +-------------------------------+


   TYPE_MACHINE
   +-------------------------------+
   |class_size                     | = sizeof(MachineClass)
   |class_init                     | = machine_class_init
   |class_base_init                | = machine_class_base_init
   |                               |
   |instance_size                  | = sizeof(MachineState)
   |instance_init                  | = machine_initfn
   |instance_finalize              | = machine_finalize
   +-------------------------------+
```

基本的东西我想大家都一目了然了，值得注意的是MachineType继承于Object，而不是Device。所以之前的realize一套在这里就不适用了。

## 类型选择

在Qemu中包含着多种MachineType类型，也就是这个类型有多个子类。那在进程启动时就需要选择指定类型，让我们来看一看。

这个过程在main函数中。

```
  main
    select_machine()
      find_default_machine()
      machine_parse()
```

这几个函数很简单，我想大家看一眼也就明白了。

## 创建Machine

刚才说了，MachineType的父类是Object，而不是Device。所以创建时和普通设备有很大的不同。

具体的细节散落在main函数中，而且涉及到了不同MachineType类型的不同处理，这里就不一一展开了。（其实我不懂）

在这里我们指出几个重要的点：

```
main
  current_machine =
    MACHINE(object_new(object_class_get_name(OBJECT_CLASS(machine_class))));
  machine_run_board_init(current_machine);
    machine_class->init(machine);
```

第一句是创建出了一个虚拟机 current\_machine。 第二句是调用init函数。


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://richardweiyang-2.gitbook.io/understanding_qemu/00-mt/01-machine_type.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
