discover: Add disable param for dm-snapshots
[petitboot] / discover / platform.c
1
2 #define _GNU_SOURCE
3
4 #include <fcntl.h>
5 #include <string.h>
6
7 #include <log/log.h>
8 #include <file/file.h>
9 #include <types/types.h>
10 #include <talloc/talloc.h>
11
12 #include "platform.h"
13
14 void                    *platform_ctx;
15 static struct platform  *platform;
16 static struct config    *config;
17
18 static const char *kernel_cmdline_debug = "petitboot.debug";
19
20 static void dump_config(struct config *config)
21 {
22         unsigned int i;
23
24         pb_log("configuration:\n");
25
26         if (config->autoboot_enabled)
27                 pb_log(" autoboot: enabled, %d sec\n",
28                                 config->autoboot_timeout_sec);
29         else
30                 pb_log(" autoboot: disabled\n");
31
32         if (config->network.n_interfaces || config->network.n_dns_servers)
33                 pb_log(" network configuration:\n");
34
35         if (config->safe_mode)
36                 pb_log(" safe mode: active\n");
37
38         if (config->disable_snapshots)
39                 pb_log(" dm-snapshots disabled\n");
40
41         for (i = 0; i < config->network.n_interfaces; i++) {
42                 struct interface_config *ifconf =
43                         config->network.interfaces[i];
44
45                 pb_log("  interface %02x:%02x:%02x:%02x:%02x:%02x\n",
46                                 ifconf->hwaddr[0], ifconf->hwaddr[1],
47                                 ifconf->hwaddr[2], ifconf->hwaddr[3],
48                                 ifconf->hwaddr[4], ifconf->hwaddr[5]);
49
50                 if (ifconf->ignore) {
51                         pb_log("   ignore\n");
52                         continue;
53                 }
54
55                 if (ifconf->method == CONFIG_METHOD_DHCP) {
56                         pb_log("   dhcp\n");
57
58                 } else if (ifconf->method == CONFIG_METHOD_STATIC) {
59                         pb_log("   static:\n");
60                         pb_log("    ip:  %s\n", ifconf->static_config.address);
61                         pb_log("    gw:  %s\n", ifconf->static_config.gateway);
62
63                 }
64         }
65         for (i = 0; i < config->network.n_dns_servers; i++)
66                 pb_log("  dns server %s\n", config->network.dns_servers[i]);
67
68         for (i = 0; i < config->n_autoboot_opts; i++) {
69                 if (config->autoboot_opts[i].boot_type == BOOT_DEVICE_TYPE)
70                         pb_log("  boot device %d: %s\n", i,
71                                device_type_name(config->autoboot_opts[i].type));
72                 else
73                         pb_log("  boot device %d: uuid: %s\n",
74                                i, config->autoboot_opts[i].uuid);
75         }
76
77         pb_log("  IPMI boot device 0x%02x%s\n", config->ipmi_bootdev,
78                         config->ipmi_bootdev_persistent ? " (persistent)" : "");
79
80
81         pb_log(" language: %s\n", config->lang ?: "");
82 }
83
84 static bool config_debug_on_cmdline(void)
85 {
86         char buf[600];
87         int rc, fd;
88
89         fd = open("/proc/cmdline", O_RDONLY);
90         if (fd < 0)
91                 return false;
92
93         rc = read(fd, buf, sizeof(buf));
94         close(fd);
95
96         if (rc <= 0)
97                 return false;
98
99         return memmem(buf, rc, kernel_cmdline_debug,
100                         strlen(kernel_cmdline_debug)) != NULL;
101 }
102
103 void config_set_defaults(struct config *config)
104 {
105         config->autoboot_enabled = true;
106         config->autoboot_timeout_sec = 10;
107         config->autoboot_enabled = true;
108         config->network.interfaces = NULL;
109         config->network.n_interfaces = 0;
110         config->network.dns_servers = NULL;
111         config->network.n_dns_servers = 0;
112         config->safe_mode = false;
113         config->lang = NULL;
114         config->allow_writes = true;
115         config->disable_snapshots = false;
116
117         config->n_autoboot_opts = 2;
118         config->autoboot_opts = talloc_array(config, struct autoboot_option,
119                                                 config->n_autoboot_opts);
120         config->autoboot_opts[0].boot_type = BOOT_DEVICE_TYPE;
121         config->autoboot_opts[0].type = DEVICE_TYPE_NETWORK;
122         config->autoboot_opts[1].boot_type = BOOT_DEVICE_TYPE;
123         config->autoboot_opts[1].type = DEVICE_TYPE_ANY;
124
125         config->ipmi_bootdev = 0;
126         config->ipmi_bootdev_persistent = false;
127
128         config->debug = config_debug_on_cmdline();
129 }
130
131 int platform_init(void *ctx)
132 {
133         extern struct platform *__start_platforms,  *__stop_platforms;
134         struct platform **p;
135
136         platform_ctx = talloc_new(ctx);
137
138         for (p = &__start_platforms; p < &__stop_platforms; p++) {
139                 if (!(*p)->probe(*p, platform_ctx))
140                         continue;
141                 platform = *p;
142                 break;
143         }
144
145         config = talloc(platform_ctx, struct config);
146         config_set_defaults(config);
147
148         if (platform) {
149                 pb_log("Detected platform type: %s\n", platform->name);
150                 if (platform->load_config)
151                         platform->load_config(platform, config);
152         } else {
153                 pb_log("No platform type detected, some platform-specific "
154                                 "functionality will be disabled\n");
155         }
156
157         dump_config(config);
158
159         return 0;
160 }
161
162 const struct platform *platform_get(void)
163 {
164         return platform;
165 }
166
167 void platform_pre_boot(void)
168 {
169         const struct config *config = config_get();
170
171         if (platform && config && platform->pre_boot)
172                 platform->pre_boot(platform, config);
173 }
174
175 int platform_get_sysinfo(struct system_info *info)
176 {
177         if (platform && platform->get_sysinfo)
178                 return platform->get_sysinfo(platform, info);
179         return -1;
180 }
181
182 int config_set(struct config *newconfig)
183 {
184         int rc;
185
186         if (!platform || !platform->save_config)
187                 return -1;
188
189         if (newconfig == config)
190                 return 0;
191
192         pb_log("new configuration data received\n");
193         dump_config(newconfig);
194
195         rc = platform->save_config(platform, newconfig);
196
197         if (!rc)
198                 config = talloc_steal(platform_ctx, newconfig);
199         else
200                 pb_log("error saving new configuration; changes lost\n");
201
202         return rc;
203 }
204
205 /* A non-exported function to allow the test infrastructure to initialise
206  * (and change) the configuration variables */
207 struct parser_test;
208 struct config __attribute__((unused)) *test_config_init(
209                 struct parser_test *test);
210 struct config *test_config_init(struct parser_test *test)
211 {
212         config = talloc(test, struct config);
213         config_set_defaults(config);
214         return config;
215 }
216
217 const struct config *config_get(void)
218 {
219         return config;
220 }
221
222 void config_set_autoboot(bool autoboot_enabled)
223 {
224         config->autoboot_enabled = autoboot_enabled;
225
226         pb_log("set autoboot: %s\n",
227                         config->autoboot_enabled ? "enabled" : "disabled");
228 }
229
230 int platform_fini(void)
231 {
232         talloc_free(platform_ctx);
233         return 0;
234 }