@@ -4,67 +4,82 @@ sidebar_position: 3
44
55# Writing a dynamic profile
66
7- It is not unusual that you need to write a profile that adapts dynamically to the underlying system.
8- For instance, you might want Agama to take some decisions depending on the hardware of the system
9- you are installing.
7+ One of the benefits of using Jsonnet instead of JSON is that you can write dynamic configurations
8+ that change at runtime depending on the available hardware information[ ^ 1 ] . For example, you might
9+ want to set the size of your swap depending on the available phyisical memory or to tweak the
10+ configuration when running on a given architecture.
1011
11- Fortunately, Jsonnet can work as a templating language and it offers many structures that allow
12- generating data. For that reason, Agama injects the hardware information that you can process using
13- the powerful [ Jsonnet standard library] ( https://jsonnet.org/ref/stdlib.html ) .
12+ Jsonnet can work as a templating language and it offers many structures that allow generating data.
13+ For that reason, Agama injects the hardware information that you can process using the powerful
14+ [ Jsonnet standard library] ( https://jsonnet.org/ref/stdlib.html ) .
1415
15- You can access to the hardware information by importing the ` hw.libsonnet ` library, as you can see
16- in the example below.
16+ Agama takes the hardware information from the ` lshw -json ` command. The output is a JSON document
17+ that you can access from your configuration including the following line at the top of the file:
1718
1819``` jsonnet
19- // There are included also helpers to search this hardware tree. To see helpers check
20- // "/usr/share/agama-cli/agama.libsonnet"
2120local agama = import 'hw.libsonnet';
21+ ```
22+
23+ The ` agama ` object holds the hardware information under the ` lshw ` key (` agama.lshw ` ). Additionally,
24+ it offers
25+ [ some helpers] ( https://github.com/agama-project/agama/blob/master/rust/share/agama.libsonnet ) to
26+ make traversing the hardware information tree easier:
27+
28+ - ` selectByClass ` : allows to find an object by its class.
29+ - ` selectByID ` : allows to find an object by its ID.
2230
23- // Find the biggest disk which is suitable for installing the system.
24- local findBiggestDisk(disks) =
25- local sizedDisks = std.filter(function(d) std.objectHas(d, 'size'), disks);
26- local sorted = std.sort(sizedDisks, function(x) -x.size);
27- sorted[0].logicalname;
31+ For instance, if you want to find amount of memory on the system you are installing, you can use
32+ this code:
2833
29- // Find how much physical memory system has.
30- local memory = agama.findByID(agama.lshw, 'memory').size;
34+ ``` jsonnet
35+ local hw = import 'hw.libsonnet';
36+ local memory = findByID(agama.lshw, 'memory').size;
37+ ```
38+
39+ In this case, ` findByID ` would return the object with the ` memory ` ID, where you can find the size
40+ of the physical memory (note the ` size ` key):
41+
42+ ``` json
43+ {
44+ "id" : " memory" ,
45+ "class" : " memory" ,
46+ "claimed" : true ,
47+ "handle" : " DMI:0001" ,
48+ "description" : " System Memory" ,
49+ "physid" : " 1" ,
50+ "slot" : " System board or motherboard" ,
51+ "units" : " bytes" ,
52+ "size" : 17179869184
53+ }
54+ ```
55+
56+ Then, you can use the value wherever you need like the example below:
57+
58+ ``` jsonnet
59+ local hw = import 'hw.libsonnet';
60+ local memory = findByID(agama.lshw, 'memory').size;
3161
3262{
33- product: {
34- id: if memory < 8000000000 then 'MicroOS' else 'Tumbleweed',
35- },
36- user: {
37- fullName: 'Jane Doe',
38- userName: 'jane.doe',
39- password: '123456',
40- },
41- root: {
42- password: 'nots3cr3t',
43- sshPublicKey: '...',
44- },
4563 storage: {
46- boot: {
47- configure: true,
48- device: 'boot',
49- },
50- drives: [
51- {
52- search: findBiggestDisk(agama.selectByClass(agama.lshw, 'disk')),
53- alias: 'boot',
64+ drives: [{
65+ search: '/dev/vda',
66+ partitions: [{
67+ filesystem: { path: '/' },
68+ size: { min: '10 GiB' },
5469 },
70+ {
71+ filesystem: { path: 'swap' },
72+ size: memory * 2,
73+ }
5574 ],
56- },
75+ }]
5776}
5877```
5978
60- Agama ships a few helpers to make it easier to search for the information you need from the hardware
61- tree. See
62- [ agama.libsonnet] ( https://github.com/agama-project/agama/blob/master/rust/share/agama.libsonnet ) for
63- further details.
64-
65- :::tip Getting hardware information
66-
67- You can inspect the available data by installing the ` lshw ` package and running the following
68- command: ` lshw -json ` .
79+ If you still miss the AutoYaST pre-scripts, you might be interested in the ` inst.script ` boot
80+ option. Check the [ boot options] ( ../reference/boot_options ) for further information.
6981
70- :::
82+ [ ^ 1 ] :
83+ In AutoYaST, you would do that via
84+ [ pre-scripts] ( https://doc.opensuse.org/projects/autoyast/#pre-install-scripts ) or
85+ [ Embedded Ruby] ( https://doc.opensuse.org/projects/autoyast/#erb-templates ) .
0 commit comments