Skip to content

Commit

Permalink
Add debugging aides (compile-time optional)
Browse files Browse the repository at this point in the history
To help with debugging of the initramfs, add #ifdef'd debug statements.
When compiled with CPPFLAGS="-DDEBUG_INITRAMFS" the initramfs will now
be verbose - and sleep for 5s at the end before executing init.
  • Loading branch information
chris-se committed Jan 7, 2016
1 parent 79bd8b4 commit f143343
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions tiny_initramfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,27 +45,51 @@ int main(int argc, char **argv)
fstab_info usrfs_info;
char real_device_name[MAX_PATH_LEN];

#ifdef DEBUG_INITRAMFS
warn(LOG_PREFIX, "[ 0 ] Startup", NULL);
#endif

r = mount("proc", "/proc", "proc", MS_NODEV | MS_NOEXEC | MS_NOSUID, NULL);
if (r < 0)
panic(errno, LOG_PREFIX, "Could not mount /proc", NULL);

#ifdef DEBUG_INITRAMFS
warn(LOG_PREFIX, "[ 1 ] /proc mounted", NULL);
#endif

r = mount("udev", "/dev", "devtmpfs", 0, DEVTMPFS_MOUNTOPTS);
if (r < 0)
panic(errno, LOG_PREFIX, "Could not mount /dev (as devtmpfs)", NULL);

#ifdef DEBUG_INITRAMFS
warn(LOG_PREFIX, "[ 2 ] /dev mounted", NULL);
#endif

parse_cmdline();

#ifdef DEBUG_INITRAMFS
warn(LOG_PREFIX, "[ 3 ] ", PROC_CMDLINE_FILENAME, " parsed", NULL);
#endif

if (!strlen(root_device))
panic(0, LOG_PREFIX, "No root filesystem (root=) specified", NULL);

if (root_wait_indefinitely)
timeout_togo = -1;
wait_for_device(real_device_name, &timeout_togo, root_device, root_delay);

#ifdef DEBUG_INITRAMFS
warn(LOG_PREFIX, "[ 4 ] waited for root device", NULL);
#endif

r = mount_filesystem(real_device_name, TARGET_DIRECTORY, strlen(root_fstype) ? root_fstype : NULL, root_options, global_rw ? 0 : MS_RDONLY, global_rw ? MS_RDONLY : 0);
if (r < 0)
panic(-r, LOG_PREFIX, "Failed to mount root filesystem from ", root_device, NULL);

#ifdef DEBUG_INITRAMFS
warn(LOG_PREFIX, "[ 5 ] mounted root filesystem", NULL);
#endif

/* We need these regardless of /usr handling */
if (access(TARGET_DIRECTORY "/dev", F_OK) != 0)
panic(errno, LOG_PREFIX, "/dev doesn't exist on root filesystem", NULL);
Expand All @@ -81,22 +105,42 @@ int main(int argc, char **argv)
if (r == -ENODEV)
panic(0, LOG_PREFIX, "Entry in /etc/fstab for /usr must be a (non-symlink) kernel device path, or of the form UUID=.", NULL);

#ifdef DEBUG_INITRAMFS
warn(LOG_PREFIX, "[ 6 ] parsed /etc/fstab", NULL);
#endif

if (r == 0) {
/* wait for /usr filesystem device */
wait_for_device(real_device_name, &timeout_togo, usrfs_info.source, 0);

#ifdef DEBUG_INITRAMFS
warn(LOG_PREFIX, "[ 6.1] waited for /usr device", NULL);
#endif

/* mount it */
r = mount_filesystem(real_device_name, TARGET_DIRECTORY "/usr", usrfs_info.type, usrfs_info.options, global_rw ? 0 : MS_RDONLY, global_rw ? MS_RDONLY : 0);
if (r < 0)
panic(-r, LOG_PREFIX, "Failed to mount /usr filesystem from ", usrfs_info.source, NULL);

#ifdef DEBUG_INITRAMFS
warn(LOG_PREFIX, "[ 6.2] mounted /usr filesystem", NULL);
#endif
}

/* move mounts */
r = mount("/dev", TARGET_DIRECTORY "/dev", NULL, MS_MOVE, NULL);

#ifdef DEBUG_INITRAMFS
warn(LOG_PREFIX, "[ 7 ] moved /dev", NULL);
#endif

if (!r)
r = mount("/proc", TARGET_DIRECTORY "/proc", NULL, MS_MOVE, NULL);

#ifdef DEBUG_INITRAMFS
warn(LOG_PREFIX, "[ 8 ] moved /proc", NULL);
#endif

if (r < 0)
panic(errno, LOG_PREFIX, "Couldn't move /dev or /proc from initramfs to root filesystem", NULL);

Expand All @@ -109,6 +153,11 @@ int main(int argc, char **argv)
if (r < 0)
panic(errno, LOG_PREFIX, "Couldn't switch root filesystem", NULL);

#ifdef DEBUG_INITRAMFS
warn(LOG_PREFIX, "[ 9 ] switched root, sleeping for 5s", NULL);
sleep(5);
#endif

if (strlen(init_binary)) {
try_exec(argc, argv, init_binary);
} else {
Expand Down

0 comments on commit f143343

Please sign in to comment.