int diskutil_init(boolean require_grub)
{
    int i = 0;
    int ret = EUCA_OK;
    int missing_handlers = 0;

    // Just in case
    if (require_grub > FALSE)
        require_grub = TRUE;

    // if init was called without grub requirement, it will run again if grub is needed now
    if (initialized < (1 + require_grub)) {
        bzero(helpers_path, sizeof(helpers_path));  //! @fixme: chuck - this does not reset the whole array
        missing_handlers = verify_helpers(helpers, helpers_path, LASTHELPER);
        if (helpers_path[GRUB])
            grub_version = 1;
        else
            missing_handlers--;

        if (helpers_path[GRUB_SETUP]) {
            // don't need it, but grub-setup only exists on v2
            if (grub_version != 1) {
                // prefer 1 until 2 is implemented
                grub_version = 2;
            }
        } else
            missing_handlers--;

        if (require_grub && grub_version == 0) {
            LOGERROR("cannot find either grub 1 or grub 2\n");
            ret = EUCA_ERROR;
        } else if (grub_version == 1) {
            // grub 1 commands seem present, check for stage files, which we will be copying
            if (!try_stage_dir("/usr/lib/grub/x86_64-pc") || !try_stage_dir("/usr/lib/grub/i386-pc") || !try_stage_dir("/usr/lib/grub")
                || !try_stage_dir("/boot/grub")) {
                LOGINFO("found grub 1 stage files in %s\n", stage_files_dir);
            } else if (require_grub) {
                LOGERROR("failed to find grub 1 stage files (in /boot/grub et al)\n");
                ret = EUCA_ERROR;
            }
        } else if (grub_version == 2) {
            LOGINFO("detected grub 2\n");
        }
        // flag missing handlers
        if (missing_handlers) {
            for (i = 0; i < LASTHELPER; i++) {
                if (helpers_path[i] == NULL && i != GRUB && i != GRUB_SETUP && i != GRUB_INSTALL) {
                    LOGERROR("missing a required handler: %s\n", helpers[i]);
                    ret = EUCA_ERROR;
                }
            }
        }

        if ((initialized < 1) && (loop_sem == NULL))
            loop_sem = sem_alloc(1, IPC_MUTEX_SEMAPHORE);
        initialized = 1 + require_grub;
    }

    return (ret);
}
int diskutil_init(boolean require_grub)
{
    int i = 0;
    int ret = EUCA_OK;
    int missing_handlers = 0;

     
    if (require_grub > FALSE)
        require_grub = TRUE;

     
    if (initialized < (1 + require_grub)) {
        bzero(helpers_path, sizeof(helpers_path));   
        missing_handlers = verify_helpers(helpers, helpers_path, LASTHELPER);
        if (helpers_path[GRUB])
            grub_version = 1;
        else
            missing_handlers--;

        if (helpers_path[GRUB_SETUP]) {
             
            if (grub_version != 1) {
                 
                grub_version = 2;
            }
        } else
            missing_handlers--;

        if (require_grub && grub_version == 0) {
            LOGERROR("cannot find either grub 1 or grub 2\n");
            ret = EUCA_ERROR;
        } else if (grub_version == 1) {
             
            if (!try_stage_dir("/usr/lib/grub/x86_64-pc") || !try_stage_dir("/usr/lib/grub/i386-pc") || !try_stage_dir("/usr/lib/grub")
                || !try_stage_dir("/boot/grub")) {
                LOGINFO("found grub 1 stage files in %s\n", stage_files_dir);
            } else if (require_grub) {
                LOGERROR("failed to find grub 1 stage files (in /boot/grub et al)\n");
                ret = EUCA_ERROR;
            }
        } else if (grub_version == 2) {
            LOGINFO("detected grub 2\n");
        }
         
        if (missing_handlers) {
            for (i = 0; i < LASTHELPER; i++) {
                if (helpers_path[i] == NULL && i != GRUB && i != GRUB_SETUP && i != GRUB_INSTALL) {
                    LOGERROR("missing a required handler: %s\n", helpers[i]);
                    ret = EUCA_ERROR;
                }
            }
        }

        if ((initialized < 1) && (loop_sem == NULL))
            loop_sem = sem_alloc(1, IPC_MUTEX_SEMAPHORE);
        initialized = 1 + require_grub;
    }

    return (ret);
}