Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I am doing a project in which I need to mount 100+ loop devices and merge it into AUFS mountpoint As an observation, for sequentially mounting 90 loop devices, it takes 25 seconds.

I am looking for a solution which will minimize time by mounting loop devices in parallel

share|improve this question
    
So what is your current code for this? –  Nils Aug 9 '13 at 10:27
add comment

2 Answers

up vote 5 down vote accepted
+50

I think this is obvious, but

typeset -i M=1
while [ $M -le 102 ]
  do
    mount mysourcedevice$M targetdir$M &
    let M++
done
wait

Should do the job. The wait will wait until all sub-processess are finished, before executing the next command.

share|improve this answer
    
If I have to add "wait" at the end of for loop then there is no point calling mount command in background –  SHW Aug 9 '13 at 12:28
2  
@SHW The wait is outside/after the loop. The loop starts all mounts in paralell. I assume you want to do something after the parallel mounts have finished. –  Nils Aug 9 '13 at 21:18
    
Yes...After parallel mounting,I am starting X. and to start X, all loop devices NEED to be mounted beforehand –  SHW Aug 10 '13 at 7:30
    
@SHW This code mounts them in parallel; you can do your other stuff after the wait. There isn't anything else to be done. While a C version may be slightly faster still (lacks the overhead of calling mount command a hundred times) it's usually not practical. –  frostschutz Aug 12 '13 at 11:04
    
agree with @frostschutz, try the C version only if performance is an issue. –  Alex Aug 13 '13 at 11:59
show 1 more comment

Maybe a threaded version could be a bit faster, you've to adjust the mount() parameters yourself.

#include <stdio.h>
#include <pthread.h>
#include <sys/mount.h>
#include <string.h>
#include <errno.h>

#define DEVS 100

static void *mountt(void *d)
{
    int i = (int)d;
    char loop[48], mp[48];

    snprintf(loop, 47, "/dev/loop%d", i);
    snprintf(mp, 47, "/mnt/%d", i);

    if (mount(loop, mp, "ext2", MS_MGC_VAL | MS_RDONLY | MS_NOSUID, "") < 0)
        fprintf(stderr, "mount[%d]: failed: %s\n", i, strerror(errno));

    return NULL;
}

int main(int argc, char **argv)
{
    int i;
    pthread_t tt[DEVS];

    for (i=0; i<DEVS; i++) {
        if (pthread_create( &tt[i], NULL, mountt, (void*)i) != 0)
            fprintf(stderr, "thread create[%d] failed: %s\n", i, strerror(errno));
    }

    for (i=0; i<DEVS; i++)
        pthread_join(tt[i], NULL);

    return 0;
}

gcc -O2 -Wall -o mountt mountt.c -lpthread

share|improve this answer
    
Thnx. I Will check first and then update you –  SHW Aug 12 '13 at 9:24
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.