# mdev discovery and "mount by label" for initramfs, too slow

## m27315

Hi,

I move SATA (and eSATA) disks around frequently as part of my backup scheme.  However, this messes up the drive lettering (sda, sdb, sdc, ...), which breaks GRUB's and /etc/fstab's partitiion assignment.  Consequently, I am trying to move to a persistent drive naming scheme, using assigned disk labels.

This scheme requires an initrd or initramfs file to read the kernel command-line label, locate the correct partition, and mount it.  I followed these instructions here:

http://en.gentoo-wiki.com/wiki/Initramfs#UUID.2FLABEL_Root_Mounting

And, it works, but it is slow!  Boot-up requires an additional 10-60 seconds or more, depending on the state of my external HDD (eSATA). 

My init file contents are:

```
$ cat /usr/src/initramfs/init 

#!/bin/busybox sh

rescue_shell() {

    echo "Something went wrong. Dropping you to a shell."

    busybox --install -s

    exec /bin/sh

}

mini_udev() {

    echo /sbin/mdev > /proc/sys/kernel/hotplug

    mdev -s

}

uuidlabel_root() {

    for cmd in $(cat /proc/cmdline) ; do

        case $cmd in

        root=*)

            type=$(echo $cmd | cut -d= -f2)

            if [ $type == "LABEL" ] || [ $type == "UUID" ] ; then

                uuid=$(echo $cmd | cut -d= -f3)

                mount -o ro $(findfs "$type"="$uuid") /mnt/root

            else

                mount -o ro $(echo $cmd | cut -d= -f2) /mnt/root

            fi

            ;;

        esac

    done

}

# Mount the /proc and /sys filesystems.

mount -t proc none /proc

mount -t sysfs none /sys

# Launch mdev (mini_udev)

mini_udev

# Do your stuff here.

echo "This script mounts rootfs and boots it up, nothing more!"

# Mount the root filesystem.

#mount -o ro /dev/sda1 /mnt/root

uuidlabel_root || rescue_shell

# Clean up.

umount /proc

umount /sys

# Boot the real thing.

exec switch_root /mnt/root /sbin/init
```

As I said, this works, but it is very slow.

Does anybody know of a more recent, more preferred, or better performing technique?  I really like the assign-by-drive-label scheme.  It is very robust, assuming your labels are sensibly unique.  It's just soo slow.    :Sad: 

Any thoughts?

Thanks!

FWIW, here is a simplified version of my GRUB and /etc/fstab files:

```
$ cat /boot/grub/grub.conf 

# Which listing to boot as default. 0 is the first, 1 the second etc.

default 1

# How many seconds to wait before the default listing is booted.

timeout 3

# Nice, fat splash-image to spice things up :)

# Comment out if you don't have a graphics card installed

#splashimage=(hd0,1)/boot/grub/splash.xpm.gz

splashimage=(hd0,1)/boot/grub/gentoo.xpm.gz

title Windows XP Pro SP3

rootnoverify (hd0,0)

makeactive

chainloader +1

title Gentoo Linux 2.6.32-gentoo-r1 MOUNT BY LABEL

root (hd0,1)

kernel /boot/kernel-2.6.32-gentoo-r1b root=LABEL=/ splash=silent,fadein,theme:livecd-2010.0 video=uvesafb:1600x1200-32,mtrr:3,ywrap quiet CONSOLE=/dev/tty1 fbcon=scrollback:128K

initrd (hd0,1)/boot/fbsplash-label-initramfs.cpio.gz

title Gentoo Linux 2.6.32-gentoo-r1 MOUNT BY DEV

root (hd0,1)

kernel /boot/kernel-2.6.32-gentoo-r1b root=/dev/sdb3 splash=silent,fadein,theme:livecd-2010.0 video=uvesafb:1600x1200-32,mtrr:3,ywrap quiet CONSOLE=/dev/tty1 fbcon=scrollback:128K

initrd (hd0,1)/boot/fbsplash-livecd-2010.0-1600x1200
```

```
$ cat /etc/fstab 

# /etc/fstab: static file system information.

# <fs>                  <mountpoint>    <type>          <opts>          <dump/pass>

# Static mounts by device nodes

#/dev/sda1                              /winxp                          ntfs-3g uid=1068,gid=88                 0 0

#/dev/sda2                              /boot                                   ext2            noauto,noatime                 0 2

#/dev/sdb2                              none                                    swap            sw                             0 0

#/dev/sdb3                              /                                               ext3            noatime                0 1

# More flexible mounts by device label

LABEL=WINXP                             /winxp                          ntfs-3g uid=1068,gid=88                 0 0

LABEL=BACKUP_750                /backup_ext                     ntfs-3g noauto,uid=1068,gid=88  0 0

LABEL=/boot                             /boot                                   ext2            noauto,noatime                 0 2

LABEL=SWAP                              none                                    swap            sw                             0 0

LABEL=/                                 /                                               ext3            noatime                0 1

/dev/cdrom                              /mnt/cdrom                      auto            noauto,user                            0 0

# Floppy drive

#/dev/fd0                               /mnt/floppy                     auto            noauto                                 0 0

# Fast RAM drives

tmpfs                                  /var/tmp/portage        tmpfs           size=2048M,mode=0777         0 0

tmpfs                          /tmp                                    tmpfs           size=2048M,mode=0777         0 0

shm                                             /dev/shm                                tmpfs           nodev,nosuid,noexec    0 0
```

----------

## EzInKy

I think I'd start by deferring the ntfs mounting until after the machine is booted and see if that doesn't speed things up a bit.

EDIT: And it does seem logical that it would take more time to search for a label than it would to mount a known location.

----------

## m27315

During busybox's mdev-enabled start-up, I had noticed these error messages:

```
end_request: I/O error, dev fd0, sector 0

end_request: I/O error, dev fd0, sector 0

Buffer I/O error on device fd0, logical block 0

end_request: I/O error, dev fd0, sector 0

Buffer I/O error on device fd0, logical block 0
```

My splash screen was hiding these message, so I did not see them at first.  Anyway, I disabled the floppy drive in the BIOS, because I was not using the floppy drive anyway.  This eliminated the hang-up entirely.  Start-up times are identical using mdev or not, at least within the resolution of my thumb on a stop-watch.  

Unfortunately, GRUB now takes an extra 5-7 seconds to start.  And, kernel initialization takes an extra 5 seconds.   These extra delays occur regardless if mdev is used or not!   :Shocked:   Why would disabling the floppy drive add more time to GRUB and kernel initialization?  Weird!    :Rolling Eyes:   At least, that is much better than the 37+ second increase I was seeing previously.  I really like this new scheme and plan to use it from now on...

Any guesses as to why GRUB and kernel init would take longer with the floppy drive disabled in the BIOS?  There should be some reason for this delay, which should be treatable.

(@EzInKy:  BTW, I checked mounting and not mounting the NTFS drives, and it did not make any difference.  This makes sense to me, because the hang-up was occurring before the kernel was even initialized.)

----------

## Gentree

there's also Floppy boot check in most BIOS, thought I would have hoped that bios would not check a non-existant floppy , you may want to deselect that as well. Maybe it's checking for a possible USB floppy or something.

 :Cool: 

----------

## frostschutz

If you believe mdev to be the culprit, you can try devtmpfs instead, it's available in recent kernels and covered in the Initramfs wiki page. It should be faster than mdev. However I find it odd why it takes so long for you. Usually with drives that are not detected right away, you'd have to specifically instruct your init script (or the kernel) to wait for the drive appear. It should not induce delay on its own accord, after all, at the mdev point of initialization it doesn't really know yet which drive you're waiting for.

If all else fails you should add timing information, or echo a debug message before and after every command, to determine what exactly the culprit is that leaves you sitting around for 60 seconds... it's not exactly clear from your description where this additional delay occurs.

EDIT:

BLARGH, why do people revive old threads really... whatever the problem was, it was probably solved ages ago   :Evil or Very Mad: 

----------

## m27315

As I mentioned in my last post, the only slow down that I have now is in Grub itself.  The time it takes to show the grub menu is a good 2-3 seconds slower, and the "kernel initialization" phase after grub is a good 3-4 seconds slower.  It's really not that big of a deal now, but I still get some weird error messages during kernel init about not being able to create temp drives because it is read only and not being able to mount udev/mapper because it is already mounted.

It would be nice to get the 5-7 seconds back and hide the annoying error messages, but it is not a big deal to me now...  I'll post the exact error messages later...

BTW, I can't print any debug messages, because the delays occur in grub and kernel-init, before I have control in the initramfs script...

----------

## frostschutz

If grub was originally installed with floppy drive in mind, you could try reinstalling it (with a devices map that does not contain fd0). Maybe it spends time scanning for a drive that just is not there. I rid myself of floppy drives a long long time ago so I do not recall if there ever was an issue with grub and floppy drives. If the kernel is compressed with bzip2, try gzip instead. It's a bit larger, but decompresses a lot faster. Although if you have a fast machine it shouldn't really be noticable - it makes a difference of several seconds for me though if I run it in a VM. Also check if your initramfs contains unnecessary stuff, and remove that - for example if the binaries contain debugging symbols you could strip em. Large initramfs also takes a fair amount of extra time to load, again, probably not noticable on a really fast machine, but already makes a difference for example if you boot from USB.

Other than that, I have no idea what could induce delay in the grub setup phase.

----------

