@@ -406,6 +406,31 @@ static void stop_all_remaining_app_threads()
406406 }
407407}
408408
409+ static void load_zfs_library_and_mount_zfs_root (const char * mount_error_msg, bool pivot_when_error = false )
410+ {
411+ // Load and initialize ZFS filesystem driver implemented in libsolaris.so
412+ const auto libsolaris_file_name = " libsolaris.so" ;
413+ // TODO: Consider calling dlclose() somewhere after ZFS is unmounted
414+ if (dlopen (libsolaris_file_name, RTLD_LAZY)) {
415+ zfsdev::zfsdev_init ();
416+ auto error = mount_zfs_rootfs (opt_pivot, opt_extra_zfs_pools);
417+ if (error) {
418+ debug (mount_error_msg);
419+ if (pivot_when_error) {
420+ // Continue with ramfs (already mounted)
421+ // TODO: Avoid the hack of using pivot_rootfs() just for
422+ // mounting the fstab entries.
423+ pivot_rootfs (" /" );
424+ }
425+ } else {
426+ bsd_shrinker_init ();
427+ boot_time.event (" ZFS mounted" );
428+ }
429+ } else {
430+ debug (" Could not load and/or initialize %s.\n " , libsolaris_file_name);
431+ }
432+ }
433+
409434void * do_main_thread (void *_main_args)
410435{
411436 auto app_cmdline = static_cast <char *>(_main_args);
@@ -424,7 +449,6 @@ void* do_main_thread(void *_main_args)
424449 if (opt_mount) {
425450 unmount_devfs ();
426451
427- const auto libsolaris_file_name = " libsolaris.so" ;
428452 if (opt_rootfs.compare (" rofs" ) == 0 ) {
429453 auto error = mount_rofs_rootfs (opt_pivot);
430454 if (error) {
@@ -437,20 +461,7 @@ void* do_main_thread(void *_main_args)
437461 }
438462 boot_time.event (" ROFS mounted" );
439463 } else if (opt_rootfs.compare (" zfs" ) == 0 ) {
440- // Initialize ZFS filesystem driver implemented in libsolaris.so
441- // TODO: Consider calling dlclose() somewhere after ZFS is unmounted
442- if (dlopen (libsolaris_file_name, RTLD_LAZY)) {
443- zfsdev::zfsdev_init ();
444- auto error = mount_zfs_rootfs (opt_pivot, opt_extra_zfs_pools);
445- if (error) {
446- debug (" Could not mount zfs root filesystem.\n " );
447- }
448-
449- bsd_shrinker_init ();
450- boot_time.event (" ZFS mounted" );
451- } else {
452- debug (" Could not load and/or initialize %s.\n " , libsolaris_file_name);
453- }
464+ load_zfs_library_and_mount_zfs_root (" Could not mount zfs root filesystem.\n " );
454465 } else if (opt_rootfs.compare (" ramfs" ) == 0 ) {
455466 // NOTE: The ramfs is already mounted, we just need to mount fstab
456467 // entries. That's the only difference between this and --nomount.
@@ -476,25 +487,7 @@ void* do_main_thread(void *_main_args)
476487 } else if (mount_virtiofs_rootfs (opt_pivot) == 0 ) {
477488 boot_time.event (" Virtio-fs mounted" );
478489 } else {
479- // Initialize ZFS filesystem driver implemented in libsolaris.so
480- // TODO: Consider calling dlclose() somewhere after ZFS is unmounted
481- if (dlopen (" libsolaris.so" , RTLD_LAZY)) {
482- zfsdev::zfsdev_init ();
483- auto error = mount_zfs_rootfs (opt_pivot, opt_extra_zfs_pools);
484- if (error) {
485- debug (" Could not mount zfs root filesystem (while "
486- " auto-discovering).\n " );
487- // Continue with ramfs (already mounted)
488- // TODO: Avoid the hack of using pivot_rootfs() just for
489- // mounting the fstab entries.
490- pivot_rootfs (" /" );
491- } else {
492- bsd_shrinker_init ();
493- boot_time.event (" ZFS mounted" );
494- }
495- } else {
496- debug (" Could not load and/or initialize %s.\n " , libsolaris_file_name);
497- }
490+ load_zfs_library_and_mount_zfs_root (" Could not mount zfs root filesystem (while auto-discovering).\n " , true );
498491 }
499492 }
500493 }
0 commit comments