# CloneEngine Module

## Introduction
CloneEngine is the component responsible for executing cloning, partitioning, creation, and restoration operations of images on clients registered in OpenGnsys. It acts as an execution engine that receives instructions from the web interface and translates them into commands that are executed on client computers.

It does not have its own visual interface, but operates transparently as a backend for management modules. Among its main functionalities are:

- Disk and partition preparation.

- Image creation and restoration.

- Boot configuration (BIOS/UEFI).

- Cache and inventory management.

- Execution of custom scripts.

These operations are executed in real-time or in a scheduled manner, and their results are reported through the web interface.



## Disk and Partition Management

### Introduction to partitioning in OpenGnsys
Disk partitioning is one of the most delicate operations that the administrator can execute from the OpenGnsys web console. Its correct definition is essential to ensure compatibility with the images to be restored and guarantee that operating systems can boot properly.

The web interface allows you to remotely define the partition table of one or several disks of registered clients, using the visual partitioning wizard. Internally, the CloneEngine motor is responsible for applying this structure on the client, removing previous content and creating new partitions with the defined sizes and types.

!!! warning "Warning"
    Incorrect partitioning can render the client system inoperative, delete existing systems, or prevent restorations due to type or size incompatibilities.

### Previous considerations
Before defining a partition table, it is recommended to consider:

- **Disk size**: verify that the total detected space is usable. It is recommended to test first on a model computer.

- **Environment (classroom or computer group)**: maintaining homogeneous partitioning structures facilitates maintenance.

- **Operating systems to restore**: ensure that the size and partition type is compatible with the corresponding image.

- **CACHE partition size**: dimension correctly if you want to use it as an intermediate container or for ogLive.

- **Partition table type**: the wizard automatically detects if it is MSDOS (BIOS) or GPT (UEFI) type, but its recreation can be forced.

- **Previous partitioning**: old structures or those created with external software may interfere. It is always recommended to test on a model.
- Partition types and file systems:
    The interface allows you to configure for each partition:
    - Partition type (e.g., LINUX, WINDOWS, CACHE, EMPTY, EXTENDED, etc.).
    - File system type (e.g., EXT4, NTFS, CACHE, etc.).
    - Size in MB or percentage.
    - Format option.


 Compatibility summary table

| Partition type         | Allowed file systems                    | Description                                              |
|------------------------|-----------------------------------------|----------------------------------------------------------|
| EMPTY                  | EMPTY                                   | Reserved space with no defined use                       |
| CACHE                  | CACHE                                   | Used as intermediate container by OpenGnsys              |
| LINUX                  | EXT4, BTRFS, XFS, ...                   | For GNU/Linux systems                                    |
| WINDOWS                | NTFS, FAT32, HFAT32, ...                | For Windows systems                                      |
| LINUX-SWAP             | LINUX-SWAP                              | Linux swap area                                          |
| EXTENDED               | –                                       | BIOS only, allows adding more logical partitions (not implemented)|
| FREEBSD, SOLARIS, …    | Corresponding native systems            | For alternative systems (not common)                     |

!!! note "Note"
    The system identifies CACHE partition as such, but internally uses an EXT4 file system with specific configuration.


### The CACHE partition
The CACHE partition is a special type of partition managed by OpenGnsys, which offers several essential functionalities for system efficiency:

- Intermediate image container:
Allows saving operating system images locally, reducing time in subsequent restorations. It also acts as a buffer in Torrent-type transfers.

- Auxiliary container for ogLive:
During PXE boot with ogLiveAdmin, the system can copy the kernel and initrd to this partition to reduce server dependency and improve boot times.

!!! warning "Warning"
    If the CACHE partition is formatted, images and ogLive temporary data are lost. It will be necessary to restart from ogLiveAdmin to regenerate them.

### Rules and recommendations

- Only one CACHE partition should exist per computer.

- In MSDOS partitions it must be in position 4 and be primary.

- In GPT partitions it can occupy any order, but its identifier will be the fourth.

- The minimum recommended size is 1.5 GB if it will only be used for ogLive.
If you want to store images, dimension according to the estimated number and size of these.



### Accessing the wizard
To access the partitioning wizard:

- Go to the Groups section from the side menu.

- Select an Organizational Unit.

- Click the ⋮ button on any client.

- In the dropdown menu, select the Partition and Format option.


This will open the wizard in a new view.

![oggui-cloneengine-particionar-acceso.png.png](../assets/images/screenshots/oggui-cloneengine-particionar-acceso.png)

### Form structure
The wizard is divided into three main blocks:

- Selected clients
At the top, the client (or group of clients) where the partition table will be applied is shown.

- Design of the new partition table
Here the disk structure is defined:

- Available disk and its size.

    - Firmware type (BIOS/UEFI).

    - Partition table type: automatically detected (MSDOS or GPT).

    - Editable partition list: type, file system, size (MB or %), and format option.

    - Graphical disk distribution:
    On the right, a circular graph is shown with the total, free, and used space according to the current configuration.


![oggui-cloneengine-particionar.png](../assets/images/screenshots/oggui-cloneengine-particionar.png)

### Partition table design
The following details the steps to design a partition table:

1. Select computers

    In case you have selected several computers in the previous window when selecting Partition, these computers will appear in the upper dropdown. Clicking on a computer you can select or deselect in case you don't want to apply the partitioning command on a specific computer. The **Model** checkbox indicates that the selected computer will be used as a partitioning model and will load its partition table to the view dynamically. You can select a computer as Model and deselect it in case you don't want to partition it again but want to apply its partitioning configuration to the rest of the machines.

![oggui-cloneengine-particionar.png](../assets/images/screenshots/oggui-cloneengine-particionar.png)

1. Select disk

    In the upper left dropdown, choose the disk to partition (example: Disk 1 (80.00 GB)).

2. Firmware type and partition table

    - Firmware: indicates if the computer is in BIOS or UEFI mode.

    - Partition table: can be MSDOS or GPT, and is automatically assigned according to the firmware.

3. Add partitions

- Click the Add partition button if you are using GPT table.

4. For each partition:

    - Define the partition type (e.g., LINUX, CACHE, WINDOWS, etc.).

    - Select the file system (e.g., EXT4, NTFS, etc.).

    - Enter the size, in MB or percentage.

    - Format: if you want to format the partition after its creation.

5. Disk usage graph

    - Make sure the free space is not negative.

    - Leave free margin (recommended ≥ 1 GB) on large disks to avoid precision failures during the process.

![oggui-cloneengine-particionar-estructura.png](../assets/images/screenshots/oggui-cloneengine-particionar-estructura.png)

!!! note "Note"
    In systems with MSDOS partition table, the maximum number of primary partitions is four. In GPT there is no such limit, but the identification of the CACHE partition as the fourth must be respected.



Execution options
Once the disk structure is defined, you can:

- Execute the command directly on the computer.

- Generate instructions to apply them later (in a scheduled manner).

- Access advanced scheduling options if you want to include it in an action queue.

In case instructions are generated, these are shown in an editable block that can be reviewed before execution.


![oggui-cloneengine-particionar-generar-instruccion.png](../assets/images/screenshots/oggui-cloneengine-particionar-generar-instruccion.png)

# Cloning Engine

The Cloning Engine is a fundamental component that manages the complete image cloning process in our system. This component integrates several critical procedures that are described below.

It manages the complete lifecycle of images, from their creation to their deployment in production environments. This system automates complex tasks and guarantees consistency across all environments.

## Main Procedures

### Image Creation

For the detailed image creation procedure, consult the specific documentation:

[Create Image](DocumentacionCrearImage.md)

### Image Deployment

Image deployment follows a standardized protocol that ensures system integrity:

[Deploy Image](DocumentacionDeployImage.md)

## Integrated Workflow

The complete cloning process follows these steps:

1. Engine initialization
    1. Dependency verification
    2. Configuration loading
    3. Permission validation
2. Image creation (see [detailed documentation](DocumentacionCrearImage.md))
    1. Base template selection
    2. Parameter configuration
    3. Image generation
3. Integrity validation
    1. Checksum verification
    2. Automated testing
    3. Manual approval (if necessary)
4. Image deployment (see [detailed documentation](DocumentacionDeployImage.md))
    1. Target environment selection
    2. Deployment sequence
    3. Post-deployment verification


## Troubleshooting

To resolve common problems during the cloning process, consult:

- [Create Image - Troubleshooting](DocumentacionCrearImage.md#troubleshooting)
- [Deploy Image - Troubleshooting](DocumentacionDeployImage.md#troubleshooting)

## Additional References

For additional information about the Cloning Engine, consult:

- System administration guide
- Cloning API documentation
- Change logs and updates
