> ## Documentation Index
> Fetch the complete documentation index at: https://mintlify.com/stratosphere-ve/ozone/llms.txt
> Use this file to discover all available pages before exploring further.

# How to Write VM Configuration Files in Ozone vmparser

> Learn how to use vmparser writer functions to update VM config sections on disk without overwriting unrelated fields using the read-merge-write pattern.

Ozone's `vmparser` writer functions follow a read-merge-write pattern: each function reads the existing `vms/<vmname>.json` file, replaces only the target section with the new value, then serialises the full `VM` struct back to disk using `json.MarshalIndent`. This means calling `VMParserWriterCPU` to update CPU settings will never touch the disk, memory, or network sections of the same file.

## Import

```go theme={null}
import "github.com/stratosphere-ve/ozone/vmparser"
```

<Tip>
  Writer functions are safe to call in sequence. Because each one reads the current file before writing, successive calls accumulate changes — no section is silently reset by a later write.
</Tip>

<Warning>
  The `vms/` directory must exist before calling any writer function. The functions do not create the directory automatically. If it is absent, `os.WriteFile` will return an error and the write will fail.
</Warning>

***

## Writing by resource

<Tabs>
  <Tab title="VMInfo">
    Update the `vm` identity section. The VM name is derived from `info.Name`, so the file path is `vms/<info.Name>.json`.

    ```go theme={null}
    info := vmparser.VMInfo{
        Name:      "web-01",
        UUID:      "550e8400-e29b-41d4-a716-446655440000",
        CreatedAt: "2024-06-01T12:00:00Z",
    }

    if err := vmparser.VMParserWriterInfo(info); err != nil {
        log.Fatalf("failed to write VMInfo: %v", err)
    }
    ```

    **Signature:** `VMParserWriterInfo(info VMInfo) error`

    Note that this writer takes a `VMInfo` value directly (not a `vmname` string) because the name is embedded in the struct.
  </Tab>

  <Tab title="CPU">
    Update the `cpu` section for a named VM while preserving all other sections.

    ```go theme={null}
    cpu := vmparser.CPU{
        Cores: 8,
        Type:  "host",
    }

    if err := vmparser.VMParserWriterCPU("web-01", cpu); err != nil {
        log.Fatalf("failed to write CPU config: %v", err)
    }
    ```

    **Signature:** `VMParserWriterCPU(vmname string, cpu CPU) error`
  </Tab>

  <Tab title="Memory">
    Update the `memory` section for a named VM.

    ```go theme={null}
    memory := vmparser.Memory{
        SizeMB: 4096,
    }

    if err := vmparser.VMParserWriterMemory("web-01", memory); err != nil {
        log.Fatalf("failed to write memory config: %v", err)
    }
    ```

    **Signature:** `VMParserWriterMemory(vmname string, memory Memory) error`
  </Tab>

  <Tab title="Network">
    Update the `network` section for a named VM.

    ```go theme={null}
    network := vmparser.Network{
        Interface: "virbr0",
        Type:      "virtio",
        MAC:       "52:54:00:ab:cd:ef",
    }

    if err := vmparser.VMParserWriterNetwork("web-01", network); err != nil {
        log.Fatalf("failed to write network config: %v", err)
    }
    ```

    **Signature:** `VMParserWriterNetwork(vmname string, network Network) error`
  </Tab>

  <Tab title="Disk">
    Update the `disk` section for a named VM.

    ```go theme={null}
    disk := vmparser.Disk{
        Name:   "web-01-disk",
        Path:   "/var/lib/ozone/images/web-01.qcow2",
        SizeGB: 40.0,
        Format: "qcow2",
    }

    if err := vmparser.VMParserWriterDisk("web-01", disk); err != nil {
        log.Fatalf("failed to write disk config: %v", err)
    }
    ```

    **Signature:** `VMParserWriterDisk(vmname string, disk Disk) error`
  </Tab>
</Tabs>

***

## Full working example

The following snippet provisions a new VM configuration from scratch by writing each resource section in sequence. Because each writer preserves unrelated fields, the order does not matter.

```go theme={null}
package main

import (
    "log"

    "github.com/stratosphere-ve/ozone/vmparser"
)

func main() {
    const vmname = "web-01"

    // 1. Write identity information
    if err := vmparser.VMParserWriterInfo(vmparser.VMInfo{
        Name:      vmname,
        UUID:      "550e8400-e29b-41d4-a716-446655440000",
        CreatedAt: "2024-06-01T12:00:00Z",
    }); err != nil {
        log.Fatalf("write VMInfo: %v", err)
    }

    // 2. Configure CPU
    if err := vmparser.VMParserWriterCPU(vmname, vmparser.CPU{
        Cores: 4,
        Type:  "host",
    }); err != nil {
        log.Fatalf("write CPU: %v", err)
    }

    // 3. Configure memory
    if err := vmparser.VMParserWriterMemory(vmname, vmparser.Memory{
        SizeMB: 2048,
    }); err != nil {
        log.Fatalf("write memory: %v", err)
    }

    // 4. Configure network
    if err := vmparser.VMParserWriterNetwork(vmname, vmparser.Network{
        Interface: "virbr0",
        Type:      "virtio",
        MAC:       "52:54:00:ab:cd:ef",
    }); err != nil {
        log.Fatalf("write network: %v", err)
    }

    // 5. Configure disk
    if err := vmparser.VMParserWriterDisk(vmname, vmparser.Disk{
        Name:   "web-01-disk",
        Path:   "/var/lib/ozone/images/web-01.qcow2",
        SizeGB: 20.0,
        Format: "qcow2",
    }); err != nil {
        log.Fatalf("write disk: %v", err)
    }

    log.Println("VM configuration written successfully.")
}
```
