docs: Update the doc to reflect the latest changes
[snf-image] / docs / architecture.rst
1 Architecture
2 ============
3
4 Overview
5 ^^^^^^^^
6
7 snf-image is a Ganeti OS definition. This means that Ganeti provisions a new
8 disk (block device) and passes it to snf-image. Then, snf-image is responsible
9 to deploy an Image on that disk. If snf-image returns successfully, Ganeti will
10 then spawn a VM with that disk as its primary disk.
11
12 Thus, snf-image is responsible for two (2) things, which are executed in two
13 separate steps:
14
15 | 1. Fill the newly provisioned disk with Image data
16 | 2. Customize the Image accordingly
17
18 For (1), snf-image can fetch the Image from a number of back-ends, as we
19 describe later. For (2) snf-image spawns a helper VM and runs a number of
20 configuration tasks inside the isolated environment. Once the last task returns
21 successfully, the helper VM ceases and snf-image returns the newly configured
22 disk to Ganeti.
23
24 The whole procedure is configurable via OS interface parameters, that can be
25 passed to snf-image from the Ganeti command line or RAPI.
26
27 snf-image is split in two components: The main program running on the Ganeti
28 host with full root privilege (*snf-image*, previously *snf-image-host*) and a
29 part running inside an unprivileged helper VM (*snf-image-helper*).
30
31 We describe each part in the following sections:
32
33 snf-image
34 ^^^^^^^^^
35
36 This part implements the Ganeti OS interface. It extracts the Image onto the
37 Ganeti-provided block device, using streaming block I/O (dd with oflag=direct),
38 then spawns a helper VM, and passes control to snf-image-helper running inside
39 that helper VM. The helper VM is created using either KVM or XEN depending on
40 the supported hypervisor as dictated by Ganeti. It runs as an unprivileged
41 user.
42
43 There is no restriction on the distribution running inside the helper VM, as
44 long as it executes the snf-image-helper component automatically upon bootup.
45 The snf-image-update-helper script is provided with snf-image to automate the
46 creation of a helper VM image based on Debian Stable, using multistrap.
47
48 The snf-image-helper component runs inside a specific environment, which is
49 created and ensured by snf-image:
50
51  * The VM features a virtual floppy, containing an ext2 file system with all
52    parameters needed for image customization.
53  * The hard disk provided by Ganeti that we want to deploy and customize is
54    accessible as the first VirtIO hard disk.
55  * All kernel/console output is redirected to the first virtual serial console,
56    and eventually finds its way into the OS definition log files that Ganeti
57    maintains.
58  * The helper VM is expected to output "SUCCESS" to its second serial port if
59    image customization was successful inside the VM.
60  * If "SUCCESS" is not returned, snf-image assumes that, execution of the helper
61    VM or snf-image-helper has failed.
62  * The helper VM is expected to shutdown automatically once it is done. Its
63    execution is time-limited; if it has not terminated after a number of
64    seconds, configurable via ``/etc/default/snf-image``, snf-image sends a
65    SIGTERM and/or a SIGKILL to it.
66
67 snf-image-helper
68 ^^^^^^^^^^^^^^^^
69
70 This part runs inside the helper VM during boot-up and undertakes customization
71 of the target disk. It does so, by running a number of :ref:`configuration
72 tasks <image-configuration-tasks>`. The exact tasks that should run, are
73 specified by rules found in the virtual floppy, placed there by *snf-image*,
74 before spawning the helper VM. *snf-image-helper* uses *runparts* to run the
75 tasks which are found under ``/usr/lib/snf-image-helper/tasks``.
76
77 Graphical Representation
78 ^^^^^^^^^^^^^^^^^^^^^^^^
79
80 The architecture is presented below:
81
82 .. image:: /images/arch.png
83
84
85 .. _storage-backends:
86
87 Storage back-ends
88 ^^^^^^^^^^^^^^^^^
89
90 As stated above, for step (1), *snf-image* is capable of fetching images that
91 are stored in a variety of different back-ends and then extracting them onto
92 the newly created block device. The following back-ends are supported:
93
94  * **Local back-end**:
95    The local back-end is used to retrieve images that are stored on the Ganeti
96    node that the image deployment takes place. All local images are expected to
97    be found under a predefined image directory. By default */var/lib/snf-image*
98    is used, but the user may change this by overwriting the value of the
99    *IMAGE_DIR* variable under ``/etc/default/snf-image``.
100
101  * **Network back-end**:
102    The network back-end is used to retrieve images that are accessible from the
103    network. snf-image can fetch images via *http:*, *https:*, *ftp:* or
104    *ftps:*, using `cURL <http://curl.haxx.se/>`_.
105
106  * **Pithos back-end**:
107    *snf-image* contains a special command-line tool (*pithcat*) for retrieving
108    images residing on a Pithos installation. To set up snf-image's Pithos
109    back-end the user needs to setup the ``PITHOS_DATA`` and ``PITHOS_DB``
110    variables inside ``/etc/default/snf-image`` accordingly.
111
112  * **Null back-end**:
113    If the null back-end is selected, no image copying is performed. This
114    actually is meant for bypassing step (1) altogether. This is useful, if the
115    disk provisioned by Ganeti already contains an OS installation before
116    *snf-image* is executed (for example if the disk was created as a clone of
117    an existing VM's hard disk).
118
119 .. _image-configuration-tasks:
120
121 Image Configuration Tasks
122 ^^^^^^^^^^^^^^^^^^^^^^^^^
123
124 Configuration tasks are scripts called by snf-image-helper inside the helper VM
125 to accomplish various configuration steps on the newly created instance. See
126 below for a description of each one of them:
127
128 **FixPartitionTable**: Enlarges the last partition in the partition table of
129 the instance, to consume all the available space and optionally adds a swap
130 partition in the end. The task will fail if the environmental variable
131 *SNF_IMAGE_DEV*, which specifies the device file of the instance's hard disk,
132 is missing.
133
134 **FilesystemResizeUnmounted**: Extends the file system of the last partition to
135 cover up the whole partition. This only works for ext{2,3,4} and ufs2 file
136 systems. Any other file system type is ignored and a warning is triggered. The
137 task will fail if *SNF_IMAGE_DEV* environmental variable is missing.
138
139 **MountImage**: Mounts the root partition of the instance, specified by the
140 *SNF_IMAGE_PROPERTY_ROOT_PARTITION* variable. On Linux systems after the root
141 fs is mounted, the instance's ``/etc/fstab`` file is examined and the rest of
142 the disk file systems are mounted too, in a correct order. The script will fail
143 if any of the environmental variables *SNF_IMAGE_DEV*,
144 *SNF_IMAGE_PROPERTY_ROOT_PARTITION* or *SNF_IMAGE_TARGET* is unset or has a
145 non-sane value.
146
147 **AddSwap**: Formats the swap partition added by *FixPartitionTable* task and
148 adds an appropriate swap entry in the system's ``/etc/fstab``. The script will
149 only run if *SNF_IMAGE_PROPERTY_SWAP* is present and will fail if
150 *SNF_IMAGE_TARGET* in not defined.
151
152 **DeleteSSHKeys**: On Linux and \*BSD instances, this script will clear out any
153 ssh keys found in the instance's disk. For Debian and Ubuntu systems, the keys
154 are also recreated. Besides removing files that comply to the
155 ``/etc/ssh/ssh_*_key`` pattern, the script will also parses
156 ``/etc/ssh/sshd_config`` file for custom keys. The only variable this script
157 depends on is *SNF_IMAGE_TARGET*.
158
159 **DisableRemoteDesktopConnections**: This script temporary disables RDP
160 connections on windows instances by changing the value of *fDenyTSConnection*
161 registry key. RDP connections will be enabled back during the specialize pass
162 of the Windows setup. The task will fail if *SNF_IMAGE_TARGET* is not defined.
163
164 **InstallUnattend**: Installs the Unattend.xml files on windows instances. This
165 is needed by windows in order to perform an unattended setup. The
166 *SNF_IMAGE_TARGET* variables needs to be present for this task to run.
167
168 **SELinuxAutorelabel**: Creates *.autorelabel* file in Red Hat images. This is
169 needed if SELinux is enabled to enforce an automatic file system relabeling
170 during the first boot. The only environmental variable required by this task is
171 *SNF_IMAGE_TARGET*.
172
173 **AssignHostname**: Assigns or changes the hostname of the instance. The task
174 will fail if the Linux distribution is not supported and ``/etc/hostname`` is
175 not present on the file system. For now, we support Debian, Red Hat, Slackware,
176 SUSE and Gentoo derived distributions. The hostname is read from
177 *SNF_IMAGE_HOSTNAME* variable. In addition to the latter, *SNF_IMAGE_TARGET* is
178 also required.
179
180 **ChangePassword**: Changes the password for a list of existing users. On Linux 
181 systems this is accomplished by directly altering the instance's
182 ``/etc/shadow`` file. On Windows systems a script is injected into the VM's
183 hard disk. This script will be executed during the specialize pass of the
184 Windows setup. On \*BSD systems ``/etc/master.passwd`` is altered,
185 ``/etc/spwd.db`` is removed and a script is injected into the VM's hard disk
186 that will recreate the aforementioned file during the first boot. The list of
187 users whose passwords will changed is determined by the
188 *SNF_IMAGE_PROPERTY_USERS* variable (see :ref:`image-properties`). For this
189 task to run *SNF_IMAGE_TARGET* and *SNF_IMAGE_PASSWORD* variables need to be
190 present.
191
192 **FilesystemResizeMounted**: Injects a script into a Windows image file system
193 that will enlarge the last file system to cover up the whole partition. The
194 script will run during the specialize pass of the Windows setup. If the
195 *SNF_IMAGE_TARGET* variable is missing, the task will fail.
196
197 **EnforcePersonality**: Injects the files specified by the
198 *SNF_IMAGE_PROPERTY_OSFAMILY* variable into the file system. If the variable is
199 missing a warning is produced. Only *SNF_IMAGE_TARGET* is required for this
200 task to run.
201
202 **UmountImage**: Umounts the file systems previously mounted by MountImage. The
203 only environmental variable required is *SNF_IMAGE_TARGET*.
204
205
206 +-------------------------------+---+--------------------------------------------+--------------------------------------------------+
207 |                               |   |               Dependencies                 |               Enviromental Variables [#]_        |
208 +          Name                 |   +------------------+-------------------------+-------------------------+------------------------+
209 |                               |Pr.|        Run-After |        Run-Before       |        Required         |      Optional          |
210 +===============================+===+==================+=========================+=========================+========================+
211 |FixPartitionTable              |10 |                  |FilesystemResizeUnmounted|DEV                      |                        |
212 +-------------------------------+---+------------------+-------------------------+-------------------------+------------------------+
213 |FilesystemResizeUnmounted      |20 |FixPartitionTable |MountImage               |DEV                      |                        |
214 +-------------------------------+---+------------------+-------------------------+-------------------------+------------------------+
215 |MountImage                     |30 |                  |UmountImage              |DEV                      |                        |
216 |                               |   |                  |                         |TARGET                   |                        |
217 |                               |   |                  |                         |PROPERTY_ROOT_PARTITION  |                        |
218 +-------------------------------+---+------------------+-------------------------+-------------------------+------------------------+
219 |AddSwap                        |40 |MountImage        |EnforcePersonality       |TARGET                   |PROPERTY_OSFAMILY       |
220 |                               |   |                  |                         |                         |PROPERTY_SWAP           |
221 +-------------------------------+---+------------------+-------------------------+-------------------------+------------------------+
222 |DeleteSSHKeys                  |40 |MountImage        |EnforcePersonality       |TARGET                   |PROPERTY_OSFAMILY       |
223 +-------------------------------+---+------------------+-------------------------+-------------------------+------------------------+
224 |DisableRemoteDesktopConnections|40 |EnforcePersonality|UmountImage              |TARGET                   |PROPERTY_OSFAMILY       |
225 +-------------------------------+---+------------------+-------------------------+-------------------------+------------------------+
226 |InstallUnattend                |40 |MountImage        |EnforcePersonality       |TARGET                   |PROPERTY_OSFAMILY       |
227 +-------------------------------+---+------------------+-------------------------+-------------------------+------------------------+
228 |SELinuxAutorelabel             |40 |MountImage        |EnforcePersonality       |TARGET                   |PROPERTY_OSFAMILY       |
229 +-------------------------------+---+------------------+-------------------------+-------------------------+------------------------+
230 |AssignHostname                 |50 |InstallUnattend   |EnforcePersonality       |TARGET                   |                        |
231 |                               |   |                  |                         |HOSTNAME                 |PROPERTY_OSFAMILY       |
232 +-------------------------------+---+------------------+-------------------------+-------------------------+------------------------+
233 |ChangePassword                 |50 |InstallUnattend   |EnforcePersonality       |TARGET                   |PROPERTY_USERS          |
234 |                               |   |                  |                         |PASSWORD                 |PROPERTY_OSFAMILY       |
235 +-------------------------------+---+------------------+-------------------------+-------------------------+------------------------+
236 |FilesystemResizeMounted        |50 |InstallUnattend   |EnforcePersonality       |TARGET                   |PROPERTY_OSFAMILY       |
237 +-------------------------------+---+------------------+-------------------------+-------------------------+------------------------+
238 |EnforcePersonality             |60 |MountImage        |UmountImage              |TARGET                   |PERSONALITY             |
239 |                               |   |                  |                         |                         |PROPERTY_OSFAMILY       |
240 +-------------------------------+---+------------------+-------------------------+-------------------------+------------------------+
241 |UmountImage                    |80 |MountImage        |                         |TARGET                   |                        |
242 +-------------------------------+---+------------------+-------------------------+-------------------------+------------------------+
243
244 .. [#] all environmental variables are prefixed with *SNF_IMAGE_*