Skip to content

Commit 48e1ada

Browse files
author
Julio Montes
committed
replace get_boot_info by is_first_boot
is_first_boot uses kernel boot id to identify the first boot modify save_instance_id in order to work together with is_first_boot
1 parent 1c97e18 commit 48e1ada

File tree

5 files changed

+71
-88
lines changed

5 files changed

+71
-88
lines changed

src/ccmodules/fbootcmd.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,9 @@ static void fbootcmd_item(GNode* node, gpointer command_line) {
5555
}
5656

5757
void fbootcmd_handler(GNode *node) {
58-
bool firstboot;
5958
GString* command_line = NULL;
6059
LOG(MOD "fbootcmd handler running...\n");
61-
get_boot_info(&firstboot, NULL);
62-
if (firstboot) {
60+
if (is_first_boot()) {
6361
LOG(MOD "Running first boot commands\n");
6462
command_line = g_string_new("");
6563
g_node_children_foreach(node, G_TRAVERSE_ALL, fbootcmd_item, command_line);

src/datasources/openstack.c

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -147,10 +147,10 @@ bool openstack_init(bool no_network) {
147147
return true;
148148
}
149149

150-
if (no_network) {
151-
LOG(MOD "config drive was not found and --no-network option was used\n");
152-
return false;
153-
}
150+
if (no_network) {
151+
LOG(MOD "config drive was not found and --no-network option was used\n");
152+
return false;
153+
}
154154

155155
if (!curl_common_init(&curl)) {
156156
LOG(MOD "Curl initialize failed\n");
@@ -426,7 +426,7 @@ static void openstack_run_handler(GNode *node, __unused__ gpointer null) {
426426
static void openstack_process_uuid(GNode* node, __unused__ gpointer *data) {
427427
if (node->data && g_strcmp0(node->data, "uuid") == 0) {
428428
openstack_metadata_uuid(node->children);
429-
g_node_unlink(node);
429+
g_node_unlink(node);
430430
g_node_destroy(node);
431431
}
432432
}
@@ -456,9 +456,7 @@ static int openstack_metadata_keys(GNode* node) {
456456

457457
static int openstack_metadata_hostname(GNode* node) {
458458
gchar command[LINE_MAX];
459-
bool firstboot = false;
460-
get_boot_info(&firstboot, NULL);
461-
if (firstboot) {
459+
if (is_first_boot()) {
462460
g_snprintf(command, LINE_MAX, HOSTNAMECTL_PATH " set-hostname '%s'", (char*)node->data);
463461
return exec_task(command);
464462
}

src/lib.c

Lines changed: 56 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,10 @@
6565
#define LOOP_MAJOR_ID 7
6666
#define SUDOERS_PATH SYSCONFDIR "/sudoers.d/"
6767
#define INSTANCE_ID_FILE DATADIR_PATH "/instance-id"
68-
#define LAST_INSTANCE_ID_FILE DATADIR_PATH "/last-instance-id"
68+
#define FIRST_BOOT_ID_FILE DATADIR_PATH "/first-boot-id"
69+
#define KERNEL_BOOT_ID_FILE "/proc/sys/kernel/random/boot_id"
70+
71+
G_LOCK_DEFINE(first_boot_id_file);
6972

7073

7174
void LOG(const char *fmt, ...) {
@@ -518,89 +521,74 @@ bool umount_filesystem(const gchar* mountdir, const gchar* loop_device) {
518521
return true;
519522
}
520523

521-
bool save_instance_id(const gchar* instance_id) {
522-
GString* id = g_string_new(instance_id);
523-
524-
LOG(MOD "Saving instance id '%s'\n", id->str);
525-
526-
if (!write_file(id, INSTANCE_ID_FILE, O_CREAT|O_TRUNC|O_WRONLY, S_IRWXU)) {
527-
LOG(MOD "Unable to save instance id\n");
528-
g_string_free(id, true);
529-
return false;
530-
}
531-
532-
g_string_free(id, true);
533-
return true;
534-
}
535-
536-
void get_boot_info(bool* firstboot, bool* snapshot) {
537-
gchar* instance_id = NULL;
524+
bool save_instance_id(const gchar* id) {
525+
bool result = false;
526+
GString* instance_id = g_string_new(id);
538527
gchar* last_instance_id = NULL;
539528
struct stat st;
540-
static int cache_firstboot = -1;
541-
static int cache_snapshot = -1;
542529

543-
if (cache_firstboot != -1 && cache_snapshot != -1 ) {
544-
if (snapshot) {
545-
*snapshot = (bool)cache_snapshot;
530+
if (stat(INSTANCE_ID_FILE, &st) != 0) {
531+
if (!write_file(instance_id, INSTANCE_ID_FILE, O_CREAT|O_TRUNC|O_WRONLY, S_IRUSR|S_IWUSR)) {
532+
LOG(MOD "Unable to save instance id\n");
533+
goto exit;
546534
}
547-
if (firstboot) {
548-
*firstboot = (bool)cache_firstboot;
549-
}
550-
return;
551-
}
552-
553-
if (snapshot) {
554-
*snapshot = false;
555-
}
556-
if (firstboot) {
557-
*firstboot = false;
558-
}
559-
560-
if (stat(LAST_INSTANCE_ID_FILE, &st) != 0) {
561-
LOG(MOD "first boot! - '%s' not found\n", LAST_INSTANCE_ID_FILE);
562-
if (!copy_file(INSTANCE_ID_FILE, LAST_INSTANCE_ID_FILE)) {
563-
LOG(MOD "Copy file failed\n");
535+
} else {
536+
if (!g_file_get_contents(INSTANCE_ID_FILE, &last_instance_id, NULL, NULL)) {
537+
LOG(MOD "Unable to read file '%s'\n", INSTANCE_ID_FILE);
538+
goto exit;
564539
}
565-
if (firstboot) {
566-
*firstboot = true;
540+
if (g_strcmp0(instance_id->str, last_instance_id) != 0) {
541+
g_free(last_instance_id);
542+
if (!write_file(instance_id, INSTANCE_ID_FILE, O_CREAT|O_TRUNC|O_WRONLY, S_IRUSR|S_IWUSR)) {
543+
LOG(MOD "Unable to save instance id\n");
544+
goto exit;
545+
}
546+
result = true;
547+
G_LOCK(first_boot_id_file);
548+
remove(FIRST_BOOT_ID_FILE);
549+
G_UNLOCK(first_boot_id_file);
567550
}
568-
cache_firstboot = 1;
569-
cache_snapshot = 0;
570-
return;
571551
}
572552

573-
if (!g_file_get_contents(INSTANCE_ID_FILE, &instance_id, NULL, NULL)) {
574-
LOG(MOD "Unable to read file '%s'\n", INSTANCE_ID_FILE);
575-
return;
576-
}
553+
exit:
554+
g_string_free(instance_id, true);
555+
return result;
556+
}
577557

578-
if (!g_file_get_contents(LAST_INSTANCE_ID_FILE, &last_instance_id, NULL, NULL)) {
579-
LOG(MOD "Unable to read file '%s'\n", LAST_INSTANCE_ID_FILE);
580-
goto fail1;
581-
}
558+
bool is_first_boot(void) {
559+
struct stat st;
560+
bool firstboot = false;
561+
gchar* boot_id;
562+
gchar* first_boot_id;
582563

583-
cache_firstboot = 0;
584-
cache_snapshot = 0;
564+
G_LOCK(first_boot_id_file);
585565

586-
if (g_strcmp0(instance_id, last_instance_id) != 0) {
587-
LOG(MOD "first boot!\n");
588-
if (!copy_file(INSTANCE_ID_FILE, LAST_INSTANCE_ID_FILE)) {
589-
LOG(MOD "Copy file failed\n");
566+
if (stat(FIRST_BOOT_ID_FILE, &st) != 0) {
567+
firstboot = true;
568+
if (!copy_file(KERNEL_BOOT_ID_FILE, FIRST_BOOT_ID_FILE)) {
569+
LOG(MOD "Copy file '%s' failed\n", KERNEL_BOOT_ID_FILE);
570+
return false;
571+
}
572+
} else {
573+
if (!g_file_get_contents(KERNEL_BOOT_ID_FILE, &boot_id, NULL, NULL)) {
574+
LOG(MOD "Unable to read file '%s'\n", KERNEL_BOOT_ID_FILE);
575+
goto exit;
590576
}
591-
if (snapshot) {
592-
*snapshot = true;
577+
if (!g_file_get_contents(FIRST_BOOT_ID_FILE, &first_boot_id, NULL, NULL)) {
578+
LOG(MOD "Unable to read file '%s'\n", FIRST_BOOT_ID_FILE);
579+
g_free(boot_id);
580+
goto exit;
593581
}
594-
if (firstboot) {
595-
*firstboot = true;
582+
if (g_strcmp0(first_boot_id, boot_id) == 0) {
583+
firstboot = true;
596584
}
597-
cache_firstboot = 1;
598-
cache_snapshot = 1;
585+
g_free(first_boot_id);
586+
g_free(boot_id);
599587
}
600588

601-
g_free(last_instance_id);
602-
fail1:
603-
g_free(instance_id);
589+
exit:
590+
G_UNLOCK(first_boot_id_file);
591+
return firstboot;
604592
}
605593

606594
bool gnode_free(GNode* node, __unused__ gpointer data) {

src/lib.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ void LOG(const char *fmt, ...) __attribute__((format(printf, 1, 2)));
5656
int make_dir(const char* pathname, mode_t mode) __warn_unused_result__;
5757
int chown_path(const char* pathname, const char* ownername, const char* groupname) __warn_unused_result__;
5858
bool save_instance_id(const gchar* instance_id) __warn_unused_result__;
59-
void get_boot_info(bool* firstboot, bool* snapshot);
59+
bool is_first_boot(void) __warn_unused_result__;
6060
bool write_file(const GString* data, const gchar* file_path, int oflags, mode_t mode) __warn_unused_result__;
6161
bool write_sudo_directives(const GString* data, const gchar* filename) __warn_unused_result__;
6262
bool write_ssh_keys(const GString* data, const gchar* username) __warn_unused_result__;

src/main.c

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,6 @@ int main(int argc, char *argv[]) {
135135
bool first_boot_setup = false;
136136
bool first_boot = false;
137137
bool no_network = false;
138-
bool snapshot = false;
139138
char* userdata_filename = NULL;
140139
char* tmp_metadata_filename = NULL;
141140
char* tmp_data_filesystem = NULL;
@@ -215,13 +214,13 @@ int main(int argc, char *argv[]) {
215214
fix_disk = true;
216215
break;
217216

218-
case OPT_FIRST_BOOT_SETUP:
219-
first_boot_setup = true;
220-
break;
217+
case OPT_FIRST_BOOT_SETUP:
218+
first_boot_setup = true;
219+
break;
221220

222-
case OPT_NO_NETWORK:
223-
no_network = true;
224-
break;
221+
case OPT_NO_NETWORK:
222+
no_network = true;
223+
break;
225224
}
226225
}
227226

@@ -290,7 +289,7 @@ int main(int argc, char *argv[]) {
290289
if (!datasource_handler->start()) {
291290
result_code = EXIT_FAILURE;
292291
} else if(first_boot_setup) {
293-
get_boot_info(&first_boot, &snapshot);
292+
first_boot = is_first_boot();
294293
}
295294
break;
296295
}

0 commit comments

Comments
 (0)