# [HOWTO] lvm2 for root, swap and software suspend2 (edit)

## Raffi

Let me state up front that it will be an automatic deduction of 10 geek

points for anyone who asks me why.

Some day I might find a good boot loader with lvm2 support built in but,

for now, I will be using grub with a small boot partition not part of

lvm2. Everything else (root, swap, etc.) will all be in lvm2.

The basic steps are to

1) Configure your system with a small boot partition at the front and

add the rest of your disk space into lvm2. There is plenty of details

out there for setting up lvm2 so I will not go into it here.

2) Create logical partitions for root (/), and for swap and for anything

else you feel like having logical partitions for.

3) Install your system onto the lvm2 logical volumes.

4) Compile your kernel (suspend2 sources to use hibernate) with dm-mod

(either in kernel or as a module) and with ramdisk and initial ramdisk

support. The ram disk can't be compiled as a module.

5) Create your initial ram disk with the script provided in this how to.

6) Setup grub (that's what I use, if you use lilo it will work, but you

won't find the directions here) pointing to your kernel with parameters

lvm2root and lvm2resume2 pointing to your lvm2 root logical volume and

your lvm2 swap logical volume respectively

7) Enjoy the coolness of a fully lvm2 system with all that flexibility

and software suspend.

CONFIGURING THE DISK

I make my boot partition 50Megs. That gives me room for about 20 kernels

before I need to start cleaning things up. You can make yours any size

your want, just put it at the front of the disk and give it a partition

type of 83.

I made the rest of the disk (partition 2) with a type of 8e. That is

certainly not the only way to do it, but it works and makes pretty good

sense. There is no reason you can't add a second disk with 1 (or more)

partitions of type 8e and use them as part of this setup.

CREATING LOGICAL VOLUMES

With LVM2 you can use a different strategy than you might be used

to. Instead of building your partitions big enough for anything you

might throw at them, you want to build your logical volumes so that they

are pretty close to full. Leave enough room for day to day operations

but since you can grow the space any time you need to, don't lock it

into a partition until you really need it.

For example, 4G for slash on my laptop is good and snug. If you

separated out /usr/portage into another logical partition, I could make

that even smaller. Your swap partition can me made nice and small (just

enough room to run the larger programs you normally run) maybe less than

your current memory size. If you want to run something big that will

actually use a lot of swap, just create a second swap volume and add it

in on the fly.

INSTALLING YOUR SYSTEM

The install goes just the way it would on a normal system. Once you

chroot into your logical volumes, you won't be able to tell the

difference. Just be sure to install the lvm2 package.

COMPILING YOUR KERNEL

I would suggest using the suspend2-sources. While you can use the

vanilla-sources and patch it yourself, that loses it's charm after

about the second kernel upgrade.

The only things you need to have above and beyond what you need for

your system and for setting up the software suspend2 stuff is the

device mapper and initial ram disk. You can find the device mapper in

drivers->multi device and you can either compile it in the kernel or

add it as a module. You can find the ram disk support in devices->block

devices and you must compile it in the kernel. You also need to select

initial ram disk support.

Compile and install your kernel in the normal fashion.

CREATING THE INITIAL RAMDISK

This is the tricky part. Your initial ramdisk will need to activate

lvm2, initialize software suspend2 functionality and switch to the lvm2

root filesystem. For software suspend2 to work, it has to do all that

without using a read/write filesystem on your ramdisk.

I started with the lvm2create_initrd script that comes the lvm2 sources

and added Gentoo specific changes (such as using the full featured

statically compiled busybox in Gentoo called bb) and then added in

changes to deal with not mounting the ramdisk read/write.

This script will calculate the size of ramdisk needed to hold the

required utilities (currently /sbin/lvm and /bin/bb) as well as any

additional modules and then it creates the ramdisk and copies everything

needed into it.

The script also builds an /sbin/init script for the ramdisk that will

deal with activating lvm2 and setting up software suspend2.

Creating the ramdisk got a little tricky because busybox on Gentoo

compiles in so many of the applets that I ran out of inoes when linking

in all of the command names. The script now specified the number of

indoes needed as 500 and that seems to cover the current needs.

Creating a /sbin/init script that meets the requirements of lvm2 and

software suspend2 was also tricky. In order for software suspend2

to be willing to resume from a suspend, no device backed filesystem

can be mounted rw before the resume. The ram disk is a device backed

filesystem. In order to activate lvm2, the device mapper has to be

loaded and device files need to be created matching the current device

major and minor numbers. Those files can't be created in the /dev in the

ramdisk or you break the read/write rule. The solution turned out to be

mounting a tmpfs filesystem over /dev (tmpfs has no device associated

with it) and re-creating dev there and then building the rest of what is

needed.

Due to the dynamic nature of the device mapper's major and minor

numbers, there is no way for the kernel to deal with the normal

parameters of root= and resume2= while booting. For this reason,

the /sbin/init script will look for the parameters lvm2root= and

lvm2resume2= and use them to define the root and resume partitions after

lvm2 is activated.

To activate software suspend2, you need to provide a value for

resume2 in /proc/suspend2/resume2 and then create the file

/proc/suspend2/do_resume. This will not make any sense to the kernel

until after lvm2 is activated.

EDIT: As of 1.94-r3 of the hibernate scrips, I have had to put the line

EDIT: Added support for tuxonice (thanks to Heinzi)

EDIT: 3/19/08 Increased number of indoes in mke2fs command.

EDIT: 4/18/08 Re-worked script to use cpio based initrd instead of ramdisk.

EDIT: 4/19/08 Use switch_root for better cleanup.

EDIT: 5/27/08 Added flag for baselayout 2 (also needs makedev package installed) Also, xen support.

EDIT: 11/22/08 Added ability to create filesystems from rescue mode.

EDIT: 12/20/09 Move sysfs mount to make things work better with lvm update.

EDIT: 1/4/10 A little cleanup and version change.

EDIT: 8/26/10 Better module handling (looks up dependencies, does not need full path).

EDIT: 5/9/11 Fix issue with baselayout 2 and sysfs.

EDIT: 5/12/11 Deal with new version of modprobe

EDIT: 4/12/13 Added luks encryption and kernel suspend support

```

SuspendDevice /dev/vg00/swap

```

in my hibernate.conf in order for hibernate to work. Seems they have added some sanity checking code that does not take what we are doing here into account.

My modified script follows: (EDIT: Found some bugs when adding non static executables)

```

#!/bin/bash

#

# lvm2create_initrd: Version 3.0

#

# Blatantly stolen from code written by

#

# Miguel Cabeca cabeca (at) ist (dot) utl (dot) pt

#

# and modified for Gentoo and my own nefarious purposes. Don't blame the

# original author or even bug him about this script. If you have

# problems, try posting to the Gentoo forums

#

# Raffi

TMPMNT=/tmp/mnt.$$

DEVRAM=/tmp/initrd.$$

# set defaults

BINFILES=${BINFILES:-"/sbin/lvm /bin/bb"}

BASICDEVICES=${BASICDEVICES:-"std console fd"}

BLOCKDEVICES=${BLOCKDEVICES:-"md hda hdb hdc hdd sda sdb sdc sdd sde sdf sdg sdh sdi sdj sdk sdl"}

MAKEDEV=${MAKEDEV:-"gentoo-b2"}

# Uncomment this if you want to disable automatic size detection

PATH=/bin:/sbin:/usr/bin:/usr/sbin:$PATH

usage () {

        echo "Create an initial ramdisk image for LVM2 root filesystem"

        echo "$cmd: [-h] [-v] [-c lvm.conf] [-m modulelist] [-e extrafiles] [-f fstypes] -r [raiddevs] [-R mdadm.conf] [-M style] [kernel version]"

        echo "      -h|--help      print this usage message"

        echo "      -v|--verbose   verbose progress messages"

        echo "      -c|--lvmconf   path to lvm.conf (/etc/lvm/lvm.conf)"

        echo "      -k|--keyfile   Keyfile for luks"

        echo "      -l|--luks      Add support for luks encrypted disks"

        echo "      -m|--modules   modules to copy to initrd image"

        echo "      -e|--extra     extra files to add to initrd"

        echo "      -f|--fstools   Add filesystem tools"

        echo "      -r|--raid      raid devices to start in initrd"

        echo "      -R|--raidconf  location of mdadm.conf file to include"

        echo "      -M|--makedev   set MAKEDEV type (gentoo, debian or redhat)"

        echo "      -x|--xen       Add xen device support"

}

verbose () {

        [ "$VERBOSE" ] && echo "`echo $cmd | tr '[a-z0-9/_]' ' '` -- $1" || true

}

cleanup () {

        [ "`mount | grep $DEVRAM`" ] && verbose "unmounting $DEVRAM" && umount $DEVRAM

        [ -f $DEVRAM ] && verbose "removing $DEVRAM" && rm $DEVRAM

        [ -d $TMPMNT ] && verbose "removing $TMPMNT" && rmdir $TMPMNT

        verbose "exit with code $1"

        exit $1

}

trap "

        verbose 'Caught interrupt'

        echo 'Bye bye...'

        cleanup 1

" 1 2 3 15

create_init () {

        cat << 'INIT' > $TMPMNT/init

#!/bin/ash

# include in the path some dirs from the real root filesystem

# for chroot, blockdev

PATH="/sbin:/bin:/usr/sbin:/usr/bin:/lib/lvm-200:/initrd/bin:/initrd/sbin"

PRE="initrd:"

do_shell(){

        /bin/echo

        /bin/echo "*** Entering LVM2 rescue shell. Exit shell to continue booting. ***"

        /bin/echo

        /bin/setsid /bin/cttyhack /bin/ash

}

# We need /proc for device mapper and reading cmdline

echo "$PRE Mounting /proc"

mount -n -t proc none /proc

mount -n -t sysfs none /sys

# Read command line arguments.

#

# Device-Mapper dynamically allocates all device numbers. This means

# it is possible that the root volume specified to LILO or Grub may

# have a different number when the initrd runs than when the system

# was last running. In order to make sure the correct volume is

# mounted as root, the init script must determine what the desired

# root volume name is by getting the LVM2 root volume name from

# the kernel command line. In order for this to work correctly,

# "lvm2root=/dev/Volume_Group_Name/Root_Volume_Name" needs to be passed

# to the kernel command line (where Root_Volume_Name is replaced by your

# actual root volume's name. The same problem exists for resume2 and is

# fixed by passing "lvm2resume2=..."

for arg in `cat /proc/cmdline`; do

        case "$arg" in

        luks=*)

                encrypted=${arg#luks=}

        ;;

        lvm2root=*)

                rootvol=${arg#lvm2root=}

        ;;

        root=*)

                rootvol=${arg#root=}

        ;;

        lvm2usr=*)

                usrvol=${arg#lvm2usr=}

        ;;

        lvm2resume2=*)

                resume2=${arg#lvm2resume2=}

        ;;

        lvm2rescue)

                rescue="YES"

        ;;

esac

done

mount -o remount,rw rootfs /

# plug in modules listed in /etc/modules

if [ -f /etc/modules ]; then

        echo -n "$PRE plugging in kernel modules:"

        cat /etc/modules |

        while read module; do

                echo -n " $module"

                modprobe $module

        done

        echo '.'

fi

# start raid devices if raid_autostart file exists

if [ -f /etc/raid_autostart ]; then

        if [ ! -f /etc/mdadm/mdadm.conf ]; then

                #mdoptions='--super-minor=dev'

                # Remove super-minor. It does not work with metadata after .90

                mdoptions=

        fi

        cat /etc/raid_autostart|

        while read dev; do

                echo "Starting RAID device $dev"

                /sbin/mdadm --assemble $dev $mdoptions

        done

fi

# Create the /dev/mapper/control device for the ioctl

# interface using the major and minor numbers that have been allocated

# dynamically.

echo -n "$PRE Finding device mapper major and minor numbers "

MAJOR=$(sed -n 's/^ *\([0-9]\+\) \+misc$/\1/p' /proc/devices)

MINOR=$(sed -n 's/^ *\([0-9]\+\) \+device-mapper$/\1/p' /proc/misc)

if test -n "$MAJOR" -a -n "$MINOR" ; then

        mkdir -p -m 755 /dev/mapper

        mknod -m 600 /dev/mapper/control c $MAJOR $MINOR

fi

echo "($MAJOR,$MINOR)"

if grep -q xvd /proc/devices ; then

        echo "$PRE Adding xen block disk support"

        for dev in a b c d; do

                case $dev in

                a)

                        MINOR=0

                        ;;

                b)

                        MINOR=16

                        ;;

                c)

                        MINOR=32

                        ;;

                d)

                        MINOR=48

                        ;;

                esac

# Create xvd disk node

                device="/dev/xvd$dev"

                mknod -m 600 $device b 202 $MINOR

# Create partition nodes for each partition found.

                fdisk -l $device | sed -n "s|$device\([0-9][0-9]*\).*|\1|p" |

                while read part; do

                        mknod -m 600 $device$part b 202 `expr $MINOR + $part`

                done

        done

fi

for d in `echo $encrypted | sed 's/,/ /g'`; do

        DEV=`echo $d | sed 's/:.*//'`

        CNAM=`echo $d | sed 's/.*://'`

        [ "$CNAM" = "$DEV" ] && CNAM="`basename $DEV`-clear"

        if [ -f /.lukskey ]; then

                cryptsetup --key-file /.lukskey luksOpen $DEV $CNAM

        else

                cryptsetup luksOpen $DEV $CNAM

        fi

        if [ $? != 0 ]; then

                echo "\t*FAILED*";

                do_shell

        fi

done

echo "$PRE Activating LVM2 volumes"

lvm vgscan

# run a shell if we're passed lvm2rescue on commandline

if [ "$rescue" = "YES" ]; then

        lvm vgchange --ignorelockingfailure -P -a y

        do_shell

else

        lvm vgchange --ignorelockingfailure -a y

fi

# Setup software suspend2

if [ -n "$resume2" ]; then

        # Create kernel swapsuspend device

        if [ -f /sys/class/misc/snapshot/dev ]; then

                MAJOR=$(sed -n 's/^\([0-9]\+\):.*/\1/p' /sys/class/misc/snapshot/dev)

                MINOR=$(sed -n 's/^.*:\([0-9]\+\)$/\1/p' /sys/class/misc/snapshot/dev)

                mknod -m 600 /dev/snapshot c $MAJOR $MINOR

        fi

        if [ -f /etc/suspend.conf -a -f /usr/lib64/suspend/resume ]; then

                echo "Setup kernel hibernation"

                /usr/lib64/suspend/resume -r "$resume2"

        elif [ -f /proc/suspend2/resume2 ]; then

                echo "Setup suspen2 hibernation"

                echo $resume2 >/proc/suspend2/resume2

                echo > /proc/suspend2/do_resume

        elif [ -f /sys/power/suspend2/resume2 ]; then

                echo "Setup suspen2 hibernation"

                echo $resume2 >/sys/power/suspend2/resume2

                echo > /sys/power/suspend2/do_resume

        elif [ -f /sys/power/tuxonice/resume ]; then

                echo "Setup suspen2 hibernation"

                echo $resume2 >/sys/power/tuxonice/resume

                echo > /sys/power/tuxonice/do_resume     

                else

                echo "Can't resume!"

        fi

fi

echo "$PRE Mounting root filesystem $rootvol ro"

if ! mount -n -t auto -o ro $rootvol /rootvol; then

        echo "\t*FAILED*";

        do_shell

fi

# Mount usr if separate and specified.

if [ -n "$usrvol" ]; then

        echo "$PRE Mounting usr filesystem $usrvol ro"

        if ! mount -n -t auto -o ro $usrvol /rootvol/usr; then

                echo "\t*FAILED*";

                do_shell

        fi

fi

echo "$PRE Umounting /proc"

umount /proc

echo "$PRE Umounting /sys"

umount /sys

echo "$PRE Changing roots and proceeding with boot..."

exec switch_root -c /dev/console /rootvol /sbin/init $*

INIT

        chmod 555 $TMPMNT/init

}

# create lvm.conf file from dumpconfig. Just use filter options

create_lvmconf () {

        echo 'devices {' > $TMPMNT/etc/lvm/lvm.conf

        lvm dumpconfig | grep 'filter=' >> $TMPMNT/etc/lvm/lvm.conf

        [ "$XEN" = "y" ] && echo '      types = ["xvd", 16]' >>$TMPMNT/etc/lvm/lvm.conf

        echo '}' >> $TMPMNT/etc/lvm/lvm.conf

}

#

# Main

#

cmd=`basename $0`

VERSION=`uname -r`

while [ $# -gt 0 ]; do

        case $1 in

        -h|--help) usage; exit 0;;

        -v|--verbose)  VERBOSE="y";;

        -c|--lvmconf)  LVMCONF=$2; shift;;

        -k|--keyfile)  KEYFILE=$2; shift;;

        -l|--luks)     BINFILES="$BINFILES /sbin/cryptsetup";;

        -m|--modules)  MODULES=$2; shift;;

        -e|--extra)    EXTRAFILES=$2; shift;;

        -f|--fstools)  FS=$2; shift;;

        -r|--raid)     RAID=$2; shift;;

        -R|--raidconf) RAIDCONF=$2; shift;;

        -M|--makedev)  MAKEDEV=$2; shift;;

        -x|--xen)      XEN="y";;

        [2-9].[0-9]*.[0-9]*) VERSION=$1;;

        *) echo "$cmd -- invalid option '$1'"; usage; exit 0;;

        esac

        shift

done

INITRD=${INITRD:-"/boot/initrd-$VERSION.gz"}

echo "$cmd -- make LVM initial ram disk $INITRD"

echo ""

if [ -n "$RAID" ]; then

        BINFILES="$BINFILES /sbin/mdadm"

        RAIDCONF=${RAIDCONF:-"/etc/mdadm.conf"}

        if [ -r $RAIDCONF ]; then

                EXTRAFILES="$EXTRAFILES $RAIDCONF"

        else

                echo "$cmd -- WARNING: No $RAIDCONF! Your RAID device minor numbers must match their superblock values!"

        fi

fi

if [ -n "$FS" ]; then

        for fstype in $FS; do

                case "$fstype" in

                btrfs)

                        BINFILES="$BINFILES /sbin/btrfs /sbin/btrfs-convert /sbin/btrfsck /sbin/mkfs.btrfs"

                        ;;

                reiser*)

                        BINFILES="$BINFILES /sbin/reiserfsck /sbin/resize_reiserfs /sbin/mkreiserfs"

                ;;

                ext*)

                        BINFILES="$BINFILES /sbin/resize2fs /sbin/e2fsck /sbin/mke2fs"

                ;;

                xfs*)

                        BINFILES="$BINFILES /usr/sbin/xfs_check /usr/sbin/xfs_growfs /sbin/xfs_repair /usr/sbin/xfs_db /sbin/mkfs.xfs"

                ;;

                esac

        done

fi

# Kernel swap suspend executable

if [ -f /usr/lib64/suspend/resume ]; then

        BINFILES="$BINFILES /usr/lib64/suspend/resume"

fi

for a in $BINFILES $EXTRAFILES; do

        if [ ! -r "$a" ] ; then

                echo "$cmd -- ERROR: you need $a"

                exit 1;

        fi;

done

# Figure out which shared libraries we actually need in our initrd

echo "$cmd -- finding required shared libraries"

verbose "BINFILES: `echo $BINFILES`"

LIBFILES=`ldd $BINFILES 2>/dev/null | awk '/=>.*lib/ {print $3} /ld-linux/ {print $1}' | sort -u`

if [ $? -ne 0 ]; then

        echo "$cmd -- ERROR figuring out needed shared libraries"

        exit 1

fi

verbose "Shared libraries needed: `echo $LIBFILES`"

INITRDFILES="$BINFILES $LIBFILES $EXTRAFILES"

# tack on stuff for modules if we declared any and the files exist

if [ -n "$MODULES" ]; then

        if [ -f "/etc/modprobe.conf" ]; then

                INITRDFILES="$INITRDFILES /etc/modprobe.conf"

        fi

        if [ -f "/lib/modules/modprobe.conf" ]; then

                INITRDFILES="$INITRDFILES /lib/modules/modprobe.conf"

        fi

        INITRDFILES="$INITRDFILES

        `for module in $MODULES; do

                modprobe --ignore-install --show-depends --set-version $VERSION $module | grep '^insmod' |

                while read cmd; do

                        echo $cmd | sed 's/insmod //'

                done

        done | sort -u`"

fi

# Copy kernel suspend config

if [ -f "/etc/suspend.conf" ]; then

        INITRDFILES="$INITRDFILES /etc/suspend.conf"

fi

INITRDFILES="$INITRDFILES /lib/modules/$VERSION/modules.order /lib/modules/$VERSION/modules.builtin"

verbose "Add the following files to ramdisk"

verbose "$INITRDFILES"

verbose "creating mountpoint $TMPMNT"

mkdir $TMPMNT

if [ $? -ne 0 ]; then

        echo "$cmd -- ERROR making $TMPMNT"

        cleanup 1

fi

verbose "creating basic set of directories in $TMPMNT"

(cd $TMPMNT; mkdir bin dev etc lib proc sbin var)

if [ $? -ne 0 ]; then

        echo "$cmd -- ERROR creating directories in $TMPMNT"

        cleanup 1

fi

if echo $LIBFILES | grep -q /lib64/; then

        (cd $TMPMNT; ln -s lib lib64)

        if [ $? -ne 0 ]; then

                echo "$cmd -- ERROR creating lib64 link in $TMPMNT"

                cleanup 1

        fi

fi

# Add some /dev files. We have to handle different types of MAKEDEV invocations

# here, so this is rather messy.

RETCODE=0

echo "$cmd -- adding required /dev files"

verbose "BASICDEVICES: `echo $BASICDEVICES`"

verbose "BLOCKDEVICES: `echo $BLOCKDEVICES`"

[ "$VERBOSE" ] && OPT_Q="-v" || OPT_Q=""

case "$MAKEDEV" in

gentoo)

        (cd $TMPMNT/dev; /usr/sbin/MAKEDEV $OPT_Q $BASICDEVICES $BLOCKDEVICES)

        RETCODE=$?

        ;;

gentoo-b2)

        (/sbin/MAKEDEV -d $TMPMNT/dev $OPT_Q $BASICDEVICES $BLOCKDEVICES)

        RETCODE=$?

        ;;

debian)

        (cd $TMPMNT/dev; /dev/MAKEDEV $OPT_Q $BASICDEVICES $BLOCKDEVICES)

        RETCODE=$?

        ;;

redhat)

        (cd $TMPMNT/dev; /dev/MAKEDEV $OPT_Q -d $TMPMNT/dev -m 2)

        RETCODE=$?

        ;;

*)

        echo "$cmd -- ERROR: $MAKEDEV is not a known MAKEDEV style."

        RETCODE=1

        ;;

esac

if [ $RETCODE -ne 0 ]; then

        echo "$cmd -- ERROR adding /dev files"

        cleanup 1

fi

mkdir $TMPMNT/rootvol

mkdir $TMPMNT/sys

# copy necessary files to ram disk

echo "$cmd -- copying initrd files to ram disk"

[ "$VERBOSE" ] && OPT_Q="-v" || OPT_Q="--quiet"

verbose "find \$INITRDFILES | cpio -pdmL $OPT_Q $TMPMNT"

find $INITRDFILES | cpio -pdmL $OPT_Q $TMPMNT

if [ $? -ne 0 ]; then

        echo "$cmd -- ERROR cpio to ram disk"

        cleanup 1

fi

# Rename bb as busybox to use the gentoo static compiled busybox.

if [ -f $TMPMNT/bin/bb -a ! -f $TMPMNT/bin/busybox ]; then

        mv $TMPMNT/bin/bb $TMPMNT/bin/busybox

fi

echo "$cmd -- creating symlinks to busybox"

shopt -s extglob

[ "$VERBOSE" ] && OPT_Q="-v" || OPT_Q=""

BUSYBOXSYMLINKS=`busybox 2>&1| awk '/^Currently defined functions:$/ {i++;next} i'|tr ',\t\n' ' '`

for link in ${BUSYBOXSYMLINKS//@(linuxrc|init|busybox)}; do

        ln -s $OPT_Q busybox $TMPMNT/bin/$link;

done

shopt -u extglob

echo "$cmd -- creating new $TMPMNT/sbin/init"

create_init

if [ $? -ne 0 ]; then

        echo "$cmd -- ERROR creating init"

        cleanup

        exit 1

fi

# copy LVMCONF into place or create a stripped down one from lvm dumpconfig

mkdir -p $TMPMNT/etc/lvm

if [ -n "$LVMCONF" ]; then

        echo "$cmd -- copying $LVMCONF to $TMPMNT/etc/lvm/lvm.conf"

        if [ -f "$LVMCONF" ]; then

                cp $LVMCONF $TMPMNT/etc/lvm/lvm.conf

        else

                echo "$cmd -- ERROR: $LVMCONF does not exist!"

                cleanup

                exit 1

        fi

else

        echo "$cmd -- creating new $TMPMNT/etc/lvm/lvm.conf"

        create_lvmconf

fi

if [ -n "$RAID" ]; then

        RAIDLIST="$TMPMNT/etc/raid_autostart"

        echo "$cmd -- creating $RAIDLIST file."

        for device in $RAID; do

                echo $device >> $RAIDLIST

        done

fi

# create modules.dep and /etc/modules files if needed

if [ -n "$MODULES" ]; then

        echo "$cmd -- creating $MODDIR/modules.dep file and $TMPMNT/etc/modules"

        depmod -b $TMPMNT $VERSION

        # The individual modules are probably already in the correct form, but this

        # code does not hurt them. Might even add functionality of specifying paths.

        for module in $MODULES; do

                basename $module | sed 's/\.k\{0,1\}o$//' >> $TMPMNT/etc/modules

        done

fi

# Copy luks keyfile is luks setup and keyfile provide.

if [ -f $TMPMNT/sbin/cryptsetup -a -n "$KEYFILE" ]; then

        cp "$KEYFILE" $TMPMNT/.lukskey

fi

umask 077

(cd $TMPMNT && find . -print | cpio -H newc -o ) | gzip -n -9 > $INITRD

rm -r $TMPMNT

if [ $? -ne 0 ]; then

        echo "$cmd -- ERROR creating $INITRD"

        cleanup 1

fi

cat << FINALTXT

--------------------------------------------------------

Your initrd is ready in $INITRD

Don't forget to set lvm2root=/dev/VG/LV in kernel parameters, where LV is

your root volume. If you use lilo try adding/modifying an entry similar to

this one in lilo.conf:

image=/boot/vmlinuz-lvm2-$VERSION

        label="ramdisk_LVM"

        initrd=/boot/initrd-lvm2-$VERSION.gz

        append="lvm2root=/dev/system/root <other parameters>"

If using grub try adding/modifying an entry similar to this one in menu.lst

title ramdisk LVM

        kernel /boot/vmlinuz-lvm2-$VERSION lvm2root=/dev/system/root <other parameters>

        initrd /boot/initrd-lvm2-$VERSION.gz

You can also pass lvm2rescue to the kernel to get a shell

--------------------------------------------------------

FINALTXT

cleanup 0

```

To use the script, make sure you have the loopback device compiled into

your kernel or load it via 

```
modprobe loop
```

 and then run the

script. If you (like me) have not compiled the device mapper into the

kernel, you will need to do something like

```

./lvm2create_initrd -m /lib/modules/2.6.17-suspend2-r2/kernel/drivers/md/dm-mod.

ko 2.6.17-suspend2-r2

```

This should create the file initrd-lvm2-2.6.17-suspend2-r2.gz in your

/boot directory (be sure it is mounted first).

SETTING UP GRUB

You will need to provide a few non standard options to the kernel by

appending them on the kernel command line in grub. You will also need

to change the standard root= to be root=/dev/ram0, while I believe that

should be the default when using a initial ramdisk, I have had much

better luck when specifying it explicitly on the command line. Also, it

won't hurt to add ro to be sure your ramdisk is mounted read only.

The first non standard option to add is lvm2root. Have this point to

your root partition in lvm2 for example, mine is

```

lvm2root=/dev/vg00/root

```

The second non standard option is lvm2resume2. This should point to your

swap partition in lvm2.

You can also add lvm2rescue to drop into a shell after activating lvm2

so that you can do any system maintenance or repair before finishing the

boot.

Your kernel line should look something like

```

kernel /suspend2-2.6.x ro root=/dev/ram0 lvm2root=/dev/v00/root lvm2resume2=/dev

/vg00/swap

```

Your initrd should simply point to the initial ramdisk created in the

previous step.

```

initrd /initrd-lvm2-2.6.x-suspend2.gz

```

ENJOY

Now sit back and bask in the flexibility (and uber geekyness) of a fully

lvm2 system including software suspend2 to an lvm swap partition.Last edited by Raffi on Fri Apr 12, 2013 7:45 pm; edited 20 times in total

----------

## lixer

Bookmarked!!   :Very Happy:  . It will come handy on my next rebuild   :Cool:   Thanks for putting in the hard work  :Very Happy: 

----------

## Mounir Lamouri

I've an issue with my initrd for lvm2. I don't use suspend because it is for a server but I used the following script and follow instructions as I can.

So, my issue is about pivot_root. I've the following output when my server boot :

 *Quote:*   

> pivot_root: pivot_root : No such file or directory
> 
> \t *FAILED*
> 
> *** Entering lvm2 rescue shell. Exit shell to continue booting. ***
> ...

 

So, there, I exit the shell and my box boot as usual, but I notice, I got :

 *Quote:*   

> umount: /initrd not found

 

To fix it, I taked many ways.

In a first time, I add pivot_root with the -e option. I also use $BINUTILS variable to add it. But this doesn't resolv anything.

After that, I verfiy if pivot_root was on my initrd and it is. There is a symlink to busybox bin. So, I turn my attention to busybox and I try to emerge it with static use flag. And like you can imagine, if I've always this issue : this doesn't change anything.

So, is there someone who may help me ?

Thanks by advance.

----------

## Raffi

I assume your pivot_root looks something like

```

pivot_root /lvmroot /initrd

```

You need to have an initrd directory in the root your are pivoting to. Otherwise, it can't mount the old root on it.

----------

## Mounir Lamouri

I'm so stupid ! I was thinking /initrd was optional...

Where could I hide ?

Now, my server boot perfectly ! Thanks to you and your script, you save me to genkernel  :Wink: 

----------

## huhn_m

Are you sure the updated script works?

I tried it and it says unexpected end of file!

The original version worked however. I don't actually see where the error should be, so could you

please verify if it also exists on your machine? If there is a mistake please correct it, since I built my machine

on your script. It is verry nice, but I need it after kernel updates for the system to come up with the new kernel (and ramdisk!)

Thank you verry much for your work. it is much appreciated.

----------

## Raffi

The script that I wrote works. Did my cut and paste? Not sure. Let me try pasting it back and see what happens.

----------

## Raffi

I grabbed the script, pasted back to my system and did a diff  -b with the original. No diffs. Can you cut and paste the output you got? Are you sure you copied the entire thing?

----------

## huhn_m

Here is the output I get from running the script:

```

localhost ~ # ./lvm2create_initrd --help

./lvm2create_initrd: line 512: syntax error: unexpected end of file

```

I really don't know what I do wrong.

I copied the whole script once again and still get the same error.

Here is the script I do use:

```

#!/bin/bash

 #

 # lvm2create_initrd

 #

 # Blatantly stolen from code written by

 #

 # Miguel Cabeca cabeca (at) ist (dot) utl (dot) pt

 #

 # and modified for Gentoo and my own nefarious purposes. Don't blame the

 # original author or even bug him about this script. If you have

 # problems, try posting to the Gentoo forums

 #

 # Raffi

 TMPMNT=/tmp/mnt.$$

 DEVRAM=/tmp/initrd.$$

 # set defaults

 BINFILES=${BINFILES:-"/sbin/lvm /bin/bb"}

 BASICDEVICES=${BASICDEVICES:-"std consoleonly fd"}

 BLOCKDEVICES=${BLOCKDEVICES:-"md hda hdb hdc hdd sda sdb sdc sdd"}

 MAKEDEV=${MAKEDEV:-"gentoo"}

 # Uncomment this if you want to disable automatic size detection

 #INITRDSIZE=4096

 PATH=/bin:/sbin:/usr/bin:/usr/sbin:$PATH

 usage () {

         echo "Create an initial ramdisk image for LVM2 root filesystem"

         echo "$cmd: [-h] [-v] [-c lvm.conf] [-m modulelist] [-e extrafiles] [-f fstypes] -r [raiddevs] [-R mdadm.conf] [-M style] [kernel version]"

         echo "      -h|--help      print this usage message"

         echo "      -v|--verbose   verbose progress messages"

         echo "      -c|--lvmconf   path to lvm.conf (/etc/lvm/lvm.conf)"

         echo "      -m|--modules   modules to copy to initrd image"

         echo "      -e|--extra     extra files to add to initrd"

         echo "      -f|--fstools   Add filesystem tools"

         echo "      -r|--raid      raid devices to start in initrd"

         echo "      -R|--raidconf  location of mdadm.conf file to include"

         echo "      -M|--makedev   set MAKEDEV type (gentoo, debian or redhat)"

 }

 verbose () {

    [ "$VERBOSE" ] && echo "`echo $cmd | tr '[a-z0-9/_]' ' '` -- $1" || true

 }

 cleanup () {

   [ "`mount | grep $DEVRAM`" ] && verbose "unmounting $DEVRAM" && umount $DEVRAM

   [ -f $DEVRAM ] && verbose "removing $DEVRAM" && rm $DEVRAM

   [ -d $TMPMNT ] && verbose "removing $TMPMNT" && rmdir $TMPMNT

   verbose "exit with code $1"

   exit $1

 }

 trap "

   verbose 'Caught interrupt'

   echo 'Bye bye...'

   cleanup 1

 " 1 2 3 15

 create_init () {

    cat << 'INIT' > $TMPMNT/sbin/init

 #!/bin/ash

 # include in the path some dirs from the real root filesystem

 # for chroot, blockdev

 PATH="/sbin:/bin:/usr/sbin:/usr/bin:/lib/lvm-200:/initrd/bin:/initrd/sbin"

 PRE="initrd:"

 do_shell(){

     /bin/echo

     /bin/echo "*** Entering LVM2 rescue shell. Exit shell to continue booting. ***"

     /bin/echo

     /bin/ash

 }

 # We need /proc for device mapper and reading cmdline

 echo "$PRE Mounting /proc"

 mount -n -t proc none /proc

 # Read command line arguments.

 #

 # Device-Mapper dynamically allocates all device numbers. This means

 # it is possible that the root volume specified to LILO or Grub may

 # have a different number when the initrd runs than when the system

 # was last running. In order to make sure the correct volume is

 # mounted as root, the init script must determine what the desired

 # root volume name is by getting the LVM2 root volume name from

 # the kernel command line. In order for this to work correctly,

 # "lvm2root=/dev/Volume_Group_Name/Root_Volume_Name" needs to be passed

 # to the kernel command line (where Root_Volume_Name is replaced by your

 # actual root volume's name. The same problem exists for resume2 and is

 # fixed by passing "lvm2resume2=..."

 for arg in `cat /proc/cmdline`; do

         case "$arg" in

         lvm2root=*)

                 rootvol=${arg#lvm2root=}

                 ;;

         lvm2resume2=*)

                 resume2=${arg#lvm2resume2=}

                 ;;

         lvm2rescue)

                 rescue="YES"

                 ;;

         esac

 done

 # Only worry about rw state of ramdisk if using software suspend.

 if [ -n "$resume2" ]; then

         mount -n -t tmpfs none /dev

         (cd /dev; cpio -im </dev.cpio)

 else

         echo "$PRE Remounting / read/write"

         mount -t ext2 -o remount,rw /dev/ram0 /

 fi

 # plug in modules listed in /etc/modules

 if [ -f /etc/modules ]; then

     echo -n "$PRE plugging in kernel modules:"

     cat /etc/modules |

     while read module; do

         echo -n " $module"

         modprobe $module

     done

     echo '.'

 fi

 # start raid devices if raid_autostart file exists

 if [ -f /etc/raid_autostart ]; then

     if [ ! -f /etc/mdadm/mdadm.conf ]; then

         mdoptions='--super-minor=dev'

     fi

     cat /etc/raid_autostart|

     while read dev; do

         echo "Starting RAID device $dev"

         /sbin/mdadm --assemble $dev $mdoptions

     done

 fi

 # Create the /dev/mapper/control device for the ioctl

 # interface using the major and minor numbers that have been allocated

 # dynamically.

 echo -n "$PRE Finding device mapper major and minor numbers "

 MAJOR=$(sed -n 's/^ *\([0-9]\+\) \+misc$/\1/p' /proc/devices)

 MINOR=$(sed -n 's/^ *\([0-9]\+\) \+device-mapper$/\1/p' /proc/misc)

 if test -n "$MAJOR" -a -n "$MINOR" ; then

         mkdir -p -m 755 /dev/mapper

         mknod -m 600 /dev/mapper/control c $MAJOR $MINOR

 fi

 echo "($MAJOR,$MINOR)"

 echo "$PRE Activating LVM2 volumes"

 lvm vgscan

 # run a shell if we're passed lvm2rescue on commandline

 if [ "$rescue" = "YES" ]; then

     lvm vgchange --ignorelockingfailure -P -a y

     do_shell

 else

     lvm vgchange --ignorelockingfailure -a y

 fi

 # Setup software suspend2

 if [ -n "$resume2" ]; then

         echo "Setup suspen2 hibernation"

         echo $resume2 >/proc/suspend2/resume2

         echo > /proc/suspend2/do_resume

 fi

 echo "$PRE Mounting root filesystem $rootvol ro"

 if ! mount -n -t auto -o ro $rootvol /rootvol; then

         echo "\t*FAILED*";

         do_shell

 fi

 echo "$PRE Umounting /proc"

 umount /proc

 echo "$PRE Changing roots"

 cd /rootvol

 if ! pivot_root . initrd ; then

         echo "\t*FAILED*"

         do_shell

 fi

 if [ -n "$resume2" ]; then

         umount /initrd/dev

 fi

 echo "$PRE Proceeding with boot..."

 exec chroot . /bin/sh -c "umount /initrd; blockdev --flushbufs /dev/ram0 ; exec /sbin/init $*" < dev/console > dev/console 2>&1

 INIT

    chmod 555 $TMPMNT/sbin/init

 }

 # create lvm.conf file from dumpconfig. Just use filter options

 create_lvmconf () {

     echo 'devices {' > $TMPMNT/etc/lvm/lvm.conf

     lvm dumpconfig | grep 'filter=' >> $TMPMNT/etc/lvm/lvm.conf

     echo '}' >> $TMPMNT/etc/lvm/lvm.conf

 }

 #

 # Main

 #

 cmd=`basename $0`

 VERSION=`uname -r`

 while [ $# -gt 0 ]; do

    case $1 in

    -h|--help) usage; exit 0;;

    -v|--verbose)  VERBOSE="y";;

    -c|--lvmconf)  LVMCONF=$2; shift;;

    -m|--modules)  MODULES=$2; shift;;

    -e|--extra)    EXTRAFILES=$2; shift;;

    -f|--fstools)  FS=$2; shift;;

    -r|--raid)     RAID=$2; shift;;

    -R|--raidconf) RAIDCONF=$2; shift;;

    -M|--makedev)  MAKEDEV=$2; shift;;

    [2-9].[0-9]*.[0-9]*) VERSION=$1;;

    *) echo "$cmd -- invalid option '$1'"; usage; exit 0;;

    esac

    shift

 done

 INITRD=${INITRD:-"/boot/initrd-lvm2-$VERSION.gz"}

 echo "$cmd -- make LVM initial ram disk $INITRD"

 echo ""

 if [ -n "$RAID" ]; then

     BINFILES="$BINFILES /sbin/mdadm"

     RAIDCONF=${RAIDCONF:-"/etc/mdadm.conf"}

     if [ -r $RAIDCONF ]; then

         EXTRAFILES="$EXTRAFILES $RAIDCONF"

     else

         echo "$cmd -- WARNING: No $RAIDCONF! Your RAID device minor numbers must match their superblock values!"

     fi

 fi

 if [ -n "$FS" ]; then

         for fstype in $FS; do

                 case "$fstype" in

                 reiser*)

                         BINFILES="$BINFILES /sbin/reiserfsck /sbin/resize_reiserfs"

                         ;;

                 ext*)

                         BINFILES="$BINFILES /sbin/resize2fs"

                         ;;

                 xfs*)

                         BINFILES="$BINFILES /usr/sbin/xfs_check /usr/sbin/xfs_growfs"

                         ;;

                 esac

         done

 fi

 for a in $BINFILES $EXTRAFILES; do

     if [ ! -r "$a" ] ; then

         echo "$cmd -- ERROR: you need $a"

         exit 1;

     fi;

 done

 # Figure out which shared libraries we actually need in our initrd

 echo "$cmd -- finding required shared libraries"

 verbose "BINFILES: `echo $BINFILES`"

 LIBFILES=`ldd $BINFILES 2>/dev/null | awk '/=>.*lib/ {print $3} /ld-linux/ {print $1}' | sort -u`

 if [ $? -ne 0 ]; then

    echo "$cmd -- ERROR figuring out needed shared libraries"

    exit 1

 fi

 verbose "Shared libraries needed: `echo $LIBFILES`"

 INITRDFILES="$BINFILES $LIBFILES $MODULES $EXTRAFILES"

 # tack on stuff for modules if we declared any and the files exist

 if [ -n "$MODULES" ]; then

     if [ -f "/etc/modprobe.conf" ]; then

         INITRDFILES="$INITRDFILES /etc/modprobe.conf"

     fi

     if [ -f "/lib/modules/modprobe.conf" ]; then

         INITRDFILES="$INITRDFILES /lib/modules/modprobe.conf"

     fi

 fi

 # Calculate the the size of the ramdisk image.

 # Don't forget that inodes take up space too, as does the filesystem metadata.

 echo "$cmd -- calculating initrd filesystem parameters"

 if [ -z "$INITRDSIZE" ]; then

    echo "$cmd -- calculating loopback file size"

    verbose "finding size"

    INITRDSIZE="`du -Lck $INITRDFILES | tail -1 | cut -f 1`"

    verbose "minimum: $INITRDSIZE kB for files + inodes + filesystem metadata"

    INITRDSIZE=`expr $INITRDSIZE + 512`  # enough for ext2 fs + a bit

 fi

 echo "$cmd -- making loopback file ($INITRDSIZE kB)"

 verbose "using $DEVRAM as a temporary loopback file"

 dd if=/dev/zero of=$DEVRAM count=$INITRDSIZE bs=1024 > /dev/null 2>&1

 if [ $? -ne 0 ]; then

    echo "$cmd -- ERROR creating loopback file"

    cleanup 1

 fi

 echo "$cmd -- making ram disk filesystem"

 verbose "mke2fs -F -m0 -L LVM-$VERSION -N 500 $DEVRAM $INITRDSIZE"

 [ "$VERBOSE" ] && OPT_Q="" || OPT_Q="-q"

 mke2fs $OPT_Q -F -m0 -L LVM-$VERSION -N 500 $DEVRAM $INITRDSIZE

 if [ $? -ne 0 ]; then

    echo "$cmd -- ERROR making ram disk filesystem"

    echo "$cmd -- ERROR you need to use mke2fs >= 1.14 or increase INITRDSIZE"

    cleanup 1

 fi

 verbose "creating mountpoint $TMPMNT"

 mkdir $TMPMNT

 if [ $? -ne 0 ]; then

    echo "$cmd -- ERROR making $TMPMNT"

    cleanup 1

 fi

 if [ ! -d /dev/loop ]; then

    verbose "Loading loopback module"

    modprobe loop

    sleep 1

 fi

 echo "$cmd -- mounting ram disk filesystem"

 verbose "mount -o loop $DEVRAM $TMPMNT"

 mount -oloop $DEVRAM $TMPMNT

 if [ $? -ne 0 ]; then

    echo "$cmd -- ERROR mounting $DEVRAM on $TMPMNT"

    cleanup 1

 fi

 verbose "creating basic set of directories in $TMPMNT"

 (cd $TMPMNT; mkdir bin dev etc lib proc sbin var)

 if [ $? -ne 0 ]; then

    echo "$cmd -- ERROR creating directories in $TMPMNT"

    cleanup 1

 fi

 if echo $LIBFILES | grep -q /lib64/; then

         (cd $TMPMNT; ln -s lib lib64)

         if [ $? -ne 0 ]; then

            echo "$cmd -- ERROR creating lib64 link in $TMPMNT"

            cleanup 1

         fi

 fi

 # Add some /dev files. We have to handle different types of MAKEDEV invocations

 # here, so this is rather messy.

 RETCODE=0

 echo "$cmd -- adding required /dev files"

 verbose "BASICDEVICES: `echo $BASICDEVICES`"

 verbose "BLOCKDEVICES: `echo $BLOCKDEVICES`"

 [ "$VERBOSE" ] && OPT_Q="-v" || OPT_Q=""

 case "$MAKEDEV" in

 gentoo)

     (cd $TMPMNT/dev; /usr/sbin/MAKEDEV $OPT_Q $BASICDEVICES $BLOCKDEVICES)

     RETCODE=$?

     ;;

 debian)

     (cd $TMPMNT/dev; /dev/MAKEDEV $OPT_Q $BASICDEVICES $BLOCKDEVICES)

     RETCODE=$?

     ;;

 redhat)

     (cd $TMPMNT/dev; /dev/MAKEDEV $OPT_Q -d $TMPMNT/dev -m 2)

     RETCODE=$?

     ;;

 *)

     echo "$cmd -- ERROR: $MAKEDEV is not a known MAKEDEV style."

     RETCODE=1

     ;;

 esac

 if [ $RETCODE -ne 0 ]; then

    echo "$cmd -- ERROR adding /dev files"

    cleanup 1

 fi

 (cd $TMPMNT/dev; find . -print | cpio -H newc -o >$TMPMNT/dev.cpio)

 mkdir $TMPMNT/rootvol

 # copy necessary files to ram disk

 echo "$cmd -- copying initrd files to ram disk"

 [ "$VERBOSE" ] && OPT_Q="-v" || OPT_Q="--quiet"

 verbose "find \$INITRDFILES | cpio -pdmL $OPT_Q $TMPMNT"

 find $INITRDFILES | cpio -pdmL $OPT_Q $TMPMNT

 if [ $? -ne 0 ]; then

    echo "$cmd -- ERROR cpio to ram disk"

    cleanup 1

 fi

 # Rename bb as busybox to use the gentoo static compiled busybox.

 if [ -f $TMPMNT/bin/bb -a ! -f $TMPMNT/bin/busybox ]; then

    mv $TMPMNT/bin/bb $TMPMNT/bin/busybox

 fi

 echo "$cmd -- creating symlinks to busybox"

 shopt -s extglob

 [ "$VERBOSE" ] && OPT_Q="-v" || OPT_Q=""

 BUSYBOXSYMLINKS=`busybox 2>&1| awk '/^Currently defined functions:$/ {i++;next} i'|tr ',\t\n' ' '`

 for link in ${BUSYBOXSYMLINKS//@(linuxrc|init|busybox)}; do

         ln -s $OPT_Q busybox $TMPMNT/bin/$link;

 done

 shopt -u extglob

 echo "$cmd -- creating new $TMPMNT/sbin/init"

 create_init

 if [ $? -ne 0 ]; then

    echo "$cmd -- ERROR creating init"

    cleanup

    exit 1

 fi

 # copy LVMCONF into place or create a stripped down one from lvm dumpconfig

 mkdir -p $TMPMNT/etc/lvm

 if [ -n "$LVMCONF" ]; then

     echo "$cmd -- copying $LVMCONF to $TMPMNT/etc/lvm/lvm.conf"

     if [ -f "$LVMCONF" ]; then

         cp $LVMCONF $TMPMNT/etc/lvm/lvm.conf

     else

         echo "$cmd -- ERROR: $LVMCONF does not exist!"

         cleanup

         exit 1

     fi

 else

     echo "$cmd -- creating new $TMPMNT/etc/lvm/lvm.conf"

     create_lvmconf

 fi

 if [ -n "$RAID" ]; then

     RAIDLIST="$TMPMNT/etc/raid_autostart"

     echo "$cmd -- creating $RAIDLIST file."

     for device in $RAID; do

         echo $device >> $RAIDLIST

     done

 fi

 # create modules.dep and /etc/modules files if needed

 if [ -n "$MODULES" ]; then

     echo "$cmd -- creating $MODDIR/modules.dep file and $TMPMNT/etc/modules"

     depmod -b $TMPMNT $VERSION

     for module in $MODULES; do

         basename $module | sed 's/\.k\{0,1\}o$//' >> $TMPMNT/etc/modules

     done

 fi

 verbose "removing $TMPMNT/lost+found"

 rmdir $TMPMNT/lost+found

 echo "$cmd -- ummounting ram disk"

 umount $DEVRAM

 if [ $? -ne 0 ]; then

    echo "$cmd -- ERROR umounting $DEVRAM"

    cleanup 1

 fi

 echo "$cmd -- creating compressed initrd $INITRD"

 verbose "dd if=$DEVRAM bs=1k count=$INITRDSIZE | gzip -9"

 dd if=$DEVRAM bs=1k count=$INITRDSIZE 2>/dev/null | gzip -9 > $INITRD

 if [ $? -ne 0 ]; then

    echo "$cmd -- ERROR creating $INITRD"

    cleanup 1

 fi

 cat << FINALTXT

 --------------------------------------------------------

 Your initrd is ready in $INITRD

 Don't forget to set root=/dev/ram0 in kernel parameters

 Don't forget to set lvm2root=/dev/VG/LV in kernel parameters, where LV is your root volume

 If you use lilo try adding/modifying an entry similar to this one in lilo.conf:

 image=/boot/vmlinuz-lvm2-$VERSION

         label="ramdisk_LVM"

         initrd=/boot/initrd-lvm2-$VERSION.gz

         append="root=/dev/ram0 lvm2root=/dev/system/root ramdisk_size=$INITRDSIZE <other parameters>"

 If using grub try adding/modifying an entry similar to this one in menu.lst

 title ramdisk LVM

         kernel /boot/vmlinuz-lvm2-$VERSION root=/dev/ram0 lvm2root=/dev/system/root ramdisk_size=$INITRDSIZE <other parameters>

         initrd /boot/initrd-lvm2-$VERSION.gz

 You can also pass lvm2rescue to the kernel to get a shell

 --------------------------------------------------------

 FINALTXT

 cleanup 0

```

The whitespace at the beginning of each line does not matter. konqueror inserts them upon cutting (whyever it does this) but removing them does not solve the problem at all..

----------

## Raffi

I copied what you posted, it gets the same error. I removed the white space at the front (shifted left with vi), it worked. Looks like the white space is the problem.

----------

## nato.Otan

Congratulations for this scrips as I was looking for a guide on how to set up root on lvm.

however is just working half way through. I created the initrd like this:

 *Quote:*   

> /lvm2create_initrd -c /etc/lvm/lvm.conf (kernerl version)

 

I don't know if this is scrip is only for a current installation or one from scratch. Perhaps my error is because I am building a new installation.

Also I build busybox and lvm2 with like this:

 *Quote:*   

> USE="static -nolvmstatic" emerge busybox lvm2

 

Although you don't mention emerging lvm2 statically.

Before I use to get

 *Quote:*   

> No volumes found

 

Now it appears this, I don't know whether is an improvement or not:

 *Quote:*   

>  Locking type 1 initialisation failed.
> 
>  Locking type 1 initialisation failed.

 

 *Quote:*   

> vFS: Mounted root (ext2 filesystem) readonly
> 
> Freeind unused kernel memory: 228k feed
> 
> initrd: Mounting /proc
> ...

 

I found on google that a possible repair is this"

 *Quote:*   

> The simple fix is to add --ignorelockingfailure to the vgchange command

 

I do this in LVM2 rescue shell and I get:

 *Quote:*   

> No volume groups found

 

But I can activate them regularly with LiveCD in order to chroot and try different workarounds.

Also I've palyed around with modifying your scrip in here:

 *Quote:*   

> echo "$PRE Activating LVM2 volumes"
> 
> lvm vgscan
> 
> # run a shell if we're passed lvm2rescue on commandline
> ...

 

And change:

 *Quote:*   

> echo "$PRE Activating LVM2 volumes"
> 
> lvm vgscan
> 
> # run a shell if we're passed lvm2rescue on commandline
> ...

 

for

 *Quote:*   

> echo "$PRE Activating LVM2 volumes"
> 
> lvm vgscan --ignorelockingfailure -a y
> 
> # run a shell if we're passed lvm2rescue on commandline
> ...

 

And adding with --mknodes because Gentoo LVM2 guide mentions that if the installation has been interrupted, as is in my case many times, that need to be added using LiveCD. So I put it on the scripe as well.

 *Quote:*   

> echo "$PRE Activating LVM2 volumes"
> 
> lvm vgscan --mknodes --ignorelockingfailure -a y
> 
> # run a shell if we're passed lvm2rescue on commandline
> ...

 

Finally I created swap with mkswap /dev/vg/swap. It gave me some error but then i did mkswap -f .... Now I have this message when boot and when activating swap partition...

 *Quote:*   

> BIT FAT WARNING!! Failed to translate "/dev/kewl/swap" into a device id.

 

I can't put swap type with fdisk because I need to put as lvm2.

Thanks in advanced...

byeLast edited by nato.Otan on Mon Oct 23, 2006 10:11 pm; edited 1 time in total

----------

## Raffi

Do you have the device mapper compiled into your kernel or is it a module? If it is a modules, you will need to specify the path to that modules using the -m flag.

There is no need to compile either busybox or lvm2 statically. Lvm2 will ignore the flag and compile itself statically anyway and busybox on gentoo always puts a static copy call bb in /bin.

As far as the swap problem goes, what error did you get when you made your swap partition?

----------

## nato.Otan

hi,

I've compiled everything in kernel nothing as modules.

Some people suggested me to create the /rootvol directory in response to this error message:

 *Quote:*   

> initrd: Mounting root filesystem /dev/kewl/root ro
> 
> mount: Mounting /dev/kewl/root on /rootvol failed: No such file or directory

 

and apparently this time it did recognize the volume!!

 *Quote:*   

> mount: Mounting /dev/kewl/root on..

 

but why do I get this message?

 *Quote:*   

> Locking type 1 initialisation failed.
> 
> Locking type 1 initialisation failed.

 

This is the the message when I do:

 *Quote:*   

> mkswap /dev/kewl/swap
> 
> Setting up swapspace version 1, size = 511700 kB
> 
> no label, UUID=(long number)

 

I wonder whether this is related to:

 *Quote:*   

> BIT FAT WARNING!! Failed to translate "/dev/kewl/swap" into a device id.

 

Maybe because I didn't set it with fdisk?

----------

## Raffi

I'm not sure I'm understanding the problem you are having. What kernel options are you feeding to grub?

----------

## nato.Otan

me again... I was thinking why I am the only one getting this message about;

 *Quote:*   

> mount: Mounting /dev/kewl/root on /rootvol failed: No such file or directory

 

I thought it was supposed to use /initrd directory in / . Where does /rootvol need to be located?

----------

## nato.Otan

I believe my main PROBLEM is due to this one because there is where it stops:

 *Quote:*   

> initrd: Mounting root filesystem /dev/kewl/root ro
> 
> mount: Mounting /dev/kewl/root on /rootvol failed: No such file or directory
> 
> \t*FAILEd*
> ...

 

And this is grub:

 *Quote:*   

> kernel /boot/vmlinuz-2.6.18-no2 ro root=/dev/ram0 lvm2root=/dev/kewl/root ramdisk_size=4096 lvm2resume2=dev/kewl/swap
> 
> initrd /boot/initrd-lvm2-2.6.18-no2

 

----------

## Raffi

/rootvol is part of the initrd image. If it is not there, your image might be bad. This brings me to your ramdisk size, I find the 4096 suspect. Mine is 5152. The script that creates the ramdisk tells you what size to use. What did it say?

----------

## nato.Otan

I am most likely to be at fault.   :Embarassed: 

Because I saw in the scrip this:

 *Quote:*   

> # Uncomment this if you want to disable automatic size detection
> 
>  #INITRDSIZE=4096

 

And in my kernel was set to that number as well I thought I should've uncommented it. But if the size is greatet than 4096 what about the kernel? Should I change the kernel and put more? And what multiple should I use?

Well now I am not sure what to follow next, whether uncomment that line first and re-run the scrip or change the kernel value to something higher.

----------

## Raffi

The script will spit out a suggested command line that you can modify the way you want. I don't change the size in the kernel config since I won't know the size until after I copy in what I need and that might change from time to time.

----------

## nato.Otan

For some reason I thought anything above the kernel's value won't work unless recompiling the kernel again with a higher number.

Ok, so whatever the scrip suggests I would have to put in the kernel after beind done with script? For instance if you said you have 5152, then you went on changing the kernel value to EXACTLY 5152?

I am confused now   :Confused:  , perhaps I shouldn't have thouched the script to begin with.

----------

## Raffi

The kernel config option is for the default size. You can override it on the command line. When the script suggested 5152, I added ramdisk_size=5152 to the kernel command line.

Nothing that you said you did looked like it would damage anything.

----------

## nato.Otan

Raffi thank very much for your support I will try this right now and see the results. This is an excellent script I wish instalation on LVM2 would be mainstream instead of a dark practice. Also I have a proposal to add this into Gentoo's LVM2 guide and put in the wiki, with your permision if you agree of course. Perhaps give directions on how to set up the script for laptops and a section for desktops. The Gentoo LVM2 installation has a  resources  section where they provide an appaling and pathetic link on how to set up root on LVM. It just doesn't make any sense, while Gentoo's guide explicitly talks about LVM2 which now is at version LVM2-2.02.10, the outdated link, "Date: Fri, 20 Oct 2000 23:41:44 +0200" is using LVM 0.7 . To make matters worst it only talks about setting lilo and the freaking guy doesn't even run Gentoo!!!

 *Quote:*   

> I was using a SuSE 6.4 install

 

WTF??!!

What do you think about proposing this to the people in charge of LVM2 guide?

Author: Avi Schwartz

Contributor: Rajiv Manglani

Editor: Xavier Neys

----------

## Raffi

I have thought about updating the wiki, but I wanted the script to get a bit of use by more people than just me first.

The lack of information in the wiki is why I wrote this script and howto.

----------

## nato.Otan

hi Raffi,

I did solve the problem, just by doing what the message spitted out:

 *Quote:*   

> initrd: Mounting root filesystem /dev/kewl/root ro 
> 
> mount: Mounting /dev/kewl/root on /rootvol failed: No such file or directory 
> 
> \t*FAILEd*

 

So I created in / this directory:

 *Quote:*   

> #mkdir rootvol

 

This solve everything and I was able to boot without an issue. Now this left me scratching my head over the reason why the script didn't give you this problem and why it did for me. Although I am not a coder I peered inside your script in order to see if I could relate something to that failed message. These are the parts where I found rootvol and I did not see "mkdir rootvol" anywhere.

 *Quote:*   

> for arg in `cat /proc/cmdline`; do 
> 
>         case "$arg" in 
> 
>         lvm2root=*) 
> ...

 

and,

 *Quote:*   

> echo "$PRE Mounting root filesystem $rootvol ro" 
> 
> if ! mount -n -t auto -o ro $rootvol /rootvol; then 
> 
>         echo "\t*FAILED*"; 
> ...

 

I thought the initrd directory in / would be used by the script.

And in order to avoid the following message during boot,

 *Quote:*   

> initrd: Finding device mapper major and minor numbers 910.63) 
> 
> initrd: Activating LVM@ volumes 
> 
> Locking type 1 initialisation failed. 
> ...

 

I added to your scrip what's highlited,

 *Quote:*   

> echo "$PRE Activating LVM2 volumes" 
> 
> lvm vgscan --ignorelockingfailure -a y 
> 
> # run a shell if we're passed lvm2rescue on commandline 
> ...

 

After this everyhting went smoothly. I hope this helps in order to refine the script!

And again thank you very much  :Smile: 

PD: One last question, is there a way to boot in verbose mode and latter on  look at the log? This would be interesting in order to check how the script is acting.

----------

## nato.Otan

sorry I am here again...

 :Crying or Very sad:   :Crying or Very sad:   :Crying or Very sad:   :Crying or Very sad:   :Crying or Very sad: 

I just rebuild system and world and re-compiled the kernel 'cause I wanted new features. Then I rebuild the initrd image with lvm2create_initrd. The kernel boots and when it's done I get this message:

 *Quote:*   

> VFS: Mounted root (ext2 filesystem) readonly.
> 
> Freeing unused kernel memory: 224k freed
> 
> Kernel panic - not syncing: Not init found. Try passing ini= option to kernel

 

I did pass several combinations of options in the kernel including "noapic", because googling had a solution doing this, however it stops all the after kernel boot with that message!!!

Are there general rules for re-compiling the system/world/kernel in order to create the initrd image?

Googling actually didn't yield any meaningful results for this that applies to Gentoo.

Thanks

[EDIT] I just wanted to make sure that it doesn't matter whether lvm2 and busybox aren't compiled with the STATICALLY. You said before that busybox automatically creates a static version of itself.

[EDIT 2] By the way something really weird happened, when I used LiveCD and mounted  / I went to check /proc directory and it was completely empty!!! As far as I know that there's been always stuff on /proc directory. I am truly perplexed as to what might've cause this!!!

----------

## Raffi

The only time I have seen the missing rootvol message is when, for some reason, I can't find my lvm. The last time was when I forgot to tell it to add the raid setup on a system that was using raid.

The no init found sounds like you are not getting the ramdisk loaded. What command did you use to build the ramdisk? What kernel were you booted under at the time? What does your grub entry look like?

----------

## weedy

 *nato.Otan wrote:*   

> sorry I am here again...
> 
>     
> 
> I just rebuild system and world and re-compiled the kernel 'cause I wanted new features. Then I rebuild the initrd image with lvm2create_initrd. The kernel boots and when it's done I get this message:
> ...

 

I got this bug, in the initrd /bin/busybox does not have the execute bit set:

-rw-------  1 root root 1707168 2006-11-11 23:59 busybox

it needs to be at least:

-rwxr-xr-x 1 root root 1707168 2006-11-11 23:59 busybox

I solved this with:

```
   cleanup 1

fi

# Make sure files in /sbin are executable.

for file in $TMPMNT/sbin/*

do

   if [ ! -x $file ]; then

      chmod 0755 $file

      echo "$cmd -- INFO Execute bit set for $file"

   fi

done

# Rename bb as busybox to use the gentoo static compiled busybox.

# Also make sure it's executable.

if [ -f $TMPMNT/bin/bb -a ! -f $TMPMNT/bin/busybox ]; then

   mv $TMPMNT/bin/bb $TMPMNT/bin/busybox

   if [ ! -x $TMPMNT/bin/busybox ]; then

      chmod 0755 $TMPMNT/bin/busybox

      echo "$cmd -- INFO Execute bit set for $TMPMNT/bin/busybox"

   fi

fi

echo "$cmd -- creating symlinks to busybox"
```

any time a programmer gets lazy and takes things for granted bugs like this will come up. god only know how many times i've been bitten because I was lazy  :Laughing: 

----------

## Raffi

The error you got sounds like you either named the initrd incorrectly or you did not have /boot mounted when  you created it.

What options did you use to rebuild the initrd? Did you have /boot mounted when you did it?

Last time I checked, busybox still builds both a static and dynamic executable.

Since /proc is another (pseudo) filesystem, you will need to have it mounted for anything to be in there.

----------

## weedy

 *Raffi wrote:*   

> The error you got sounds like you either named the initrd incorrectly or you did not have /boot mounted when  you created it.
> 
> What options did you use to rebuild the initrd? Did you have /boot mounted when you did it?
> 
> Last time I checked, busybox still builds both a static and dynamic executable.
> ...

 

grub is correct, /boot was mounted, and /bin/bb is there. Inside the initrd /bin/busybox (the renamed /bin/bb) did not have the execute bit set, thus theres no awk. So /sbin/init doesn't work. That's why you need the checks added to the script, to make sure all needed executables are infact executable. Once I ran chmod 0755 /path/to/mounted/initrd-lvm2/bin/busybox the box booted with out fail, you can see a copy of the script I use here. It has 2 more edits that allow for proper vmlinuz operation. https://weedy.1.vg/lvm2create_initrd

----------

## Raffi

That's pretty odd. Is your /bin/bb executable? I have not run into any of these problems on the 4 machines I'm running this script on.

----------

## weedy

 *Raffi wrote:*   

> That's pretty odd. Is your /bin/bb executable? I have not run into any of these problems on the 4 machines I'm running this script on.

  Yes my /bin/bb is fine, i've done lvm2 installs for 6 people (I guess it's only fitting the one that install i get weird bugs would be mine) and mine is the 7th. But as you can see i'm not alone with this. It could be related to time, both our installs are recent. Code changes in cpio?

----------

## Raffi

That's a thought. I will try to find some time to play around with the script today.

----------

## Raffi

I can't see how the execute bit could have changed. Can you send me the following information

ls -l /bin/bb

uname -a

mount | grep ' / '

equery l cpio

----------

## weedy

here you are

```
kamo-chan ~ # ls -l /bin/bb

-rwxr-xr-x 1 root root 1731420 Nov 12 20:41 /bin/bb

kamo-chan ~ # uname -a

Linux kamo-chan 2.6.18-gentoo-r2 #7 SMP PREEMPT Mon Nov 13 21:41:30 EST 2006 i686 Intel(R) Pentium(R) 4 CPU 2.60GHz GenuineIntel GNU/Linux

kamo-chan ~ # mount | grep ' / '

/dev/mapper/system-root on / type ext3 (rw,noatime)

kamo-chan ~ # equery l cpio

[ Searching for package 'cpio' in all categories among: ]

 * installed packages

[I--] [ ~] app-arch/cpio-2.7-r1 (0)

```

----------

## Raffi

The only differences from what I have are you are using ext3 (I'm using reiser) that should not be an issue and you have a different version of cpio (I have cpio-2.6-r5). The cpio version could be the difference. Any way you can drop back to the version I'm using and see if things change?

----------

## weedy

 *Raffi wrote:*   

> The only differences from what I have are you are using ext3 (I'm using reiser) that should not be an issue and you have a different version of cpio (I have cpio-2.6-r5). The cpio version could be the difference. Any way you can drop back to the version I'm using and see if things change?

 arg that sounds like work

----------

## nato.Otan

 *weedy wrote:*   

>  *Raffi wrote:*   The error you got sounds like you either named the initrd incorrectly or you did not have /boot mounted when  you created it.
> 
> What options did you use to rebuild the initrd? Did you have /boot mounted when you did it?
> 
> Last time I checked, busybox still builds both a static and dynamic executable.
> ...

 

You are a genious, not in a 1000 years I would've figure this out. But your script did the job, I was breathless. Breathless because I spent countless hours chrooting and recompiling in differen fashions: system, world, kernel and what not. After that I though about just doing a re-install until I checked this thread. How in the world did you manage to figured that out?

The odd thing is that first it worked with Raffi's script and after I did some emerges suddenly blacked out. So why at some point Raffi's is fine and at another time is weedy's is the correct one?

Anyways thanks a lot for this.

By the way, busybox HAS to be compiled statically?

----------

## Raffi

 *nato.Otan wrote:*   

> 
> 
> By the way, busybox HAS to be compiled statically?

 

Yes and no. Yes, because things will work a little smoother that way. No because gentoo already compiles a static version which my script uses.

----------

## weedy

 *nato.Otan wrote:*   

>  *weedy wrote:*    *Raffi wrote:*   The error you got sounds like you either named the initrd incorrectly or you did not have /boot mounted when  you created it.
> 
> What options did you use to rebuild the initrd? Did you have /boot mounted when you did it?
> 
> Last time I checked, busybox still builds both a static and dynamic executable.
> ...

 well when I did the install this time and it didn't work I mounted the initrd and poked around. I have hand crafted initrds before but using Raffi's script is WAAAAAAAAAAAAAAAAAAAAAAAAAAY easier. And since I'm lazy easy == good

----------

## synss

I want to tell first that I did not go through this install yet, but I would like to achieve sth similar (that + encryption for another 10 geek points) but, 

instead of using initrd + tmpfs, would not an initramfs solve the read-write problem and the inode number problem all at once?

you can personalized your busybox install and remove everything you do not need, read the ebuild for details on how to do that.But well anyway, I will sure go through what you have done in details even if I go the initramfs route.

----------

## Raffi

Not sure about the initramfs helping. The reason I used the tmpfs is that it is not a block device and therefor did not break suspend restore when it was written to. My first pass at doing this did include a customized busybox, I just like using what is already there because it is both easier and leaves you with more flexibility.

----------

## dougm

I just wanted to let anyone reading this thread here know that I've managed to get Gentoo support into the official LVM2 distribution for lvm2create_initrd. I've updated the LVM2 Root Wiki entry to reflect the changes in "The Second Easiest Way" section.  This doesn't require static binaries, it automatically sizes the initial ram disk, and uses busybox by default.  The only things you have to make sure to do are to create /initrd in the target root partition, and make sure you support for a large enough image in your kernel.

----------

## Raffi

That's cool. Now we just need to get support for swap suspend into there as well.

----------

## /root

@raffi: First things first: Great Work!

Here is my piece of advice from the work with this script:

Change the devicelist in lvm2create_initrd script to fit your needs (e.g. hde for the first IDE device on a second controller).

```
BLOCKDEVICES=${BLOCKDEVICES:-"md hda hdb hdc hdd sda sdb sdc sdd"}
```

Changes in grub.conf (lvm2root-variable) -> Don't use a trailing slash

```
lvm2root=/dev/vg00/lv00
```

Change root after booting initrd-image -> There must exist an initrd directory in your /-partition

Bye!

----------

## Raffi

 */root wrote:*   

> 
> 
> Change the devicelist in lvm2create_initrd script to fit your needs (e.g. hde for the first IDE device on a second controller).
> 
> ```
> ...

 

Good suggestion, I should add a comment in the script to let people know they can edit that for additional hardware.

 */root wrote:*   

> 
> 
> Changes in grub.conf (lvm2root-variable) -> Don't use a trailing slash
> 
> ```
> ...

 

I'm confused here, I dont' see a trailing / on anything in my post or my script.

 */root wrote:*   

> 
> 
> Change root after booting initrd-image -> There must exist an initrd directory in your /-partition
> 
> 

 

Yep, I keep running into this. I will look into the best way to address this. Maybe just a reminder from the create initrd script.

----------

## /root

 *Raffi wrote:*   

>  */root wrote:*   Changes in grub.conf (lvm2root-variable) -> Don't use a trailing slash
> 
> ```
> lvm2root=/dev/vg00/lv00
> ```
> ...

 

That's right, this was my fault. This accident leads to an "File not found"/"Not a directory" error when the script tries to mount the vg to the /rootvol dir which possible leads the user to a wrong direction.

 *Raffi wrote:*   

>  */root wrote:*   Change root after booting initrd-image -> There must exist an initrd directory in your /-partition 
> 
> Yep, I keep running into this. I will look into the best way to address this. Maybe just a reminder from the create initrd script.

 

A simple 

```
if ![ -d /initrd ]; then mkdir /initrd; fi
```

 should do the trick in the lvm2create_initrd-script.

Bye.

----------

## Raffi

 */root wrote:*   

> 
> 
> A simple 
> 
> ```
> ...

 

My only concern about that solution is that my script mounts the root filesystem read only. I'm not sure what the consequences of mounting it read/write would be.

----------

## /root

 *Raffi wrote:*   

>  */root wrote:*   
> 
> A simple 
> 
> ```
> ...

 Right. But you have to run the script once before booting of from ramdisk to create initrd and so the root-filesystem that resides on the lvm2-partition should be read/write.

Another solution would be mounting the root filesystem somewhere else and call 

```
pivot_root . /path/that/exist/common/on/all/gentoo/based/systems/and/couldnt/be/accidently/deleted
```

A simple note after creation of the initrd image should leave the /initrd dir untouched by the user. Maybe he doublechecks the existance of it.

Bye.

----------

## Raffi

I edited the script to add a warning. This seems safest.

----------

## /root

I encountered some odd behaviour of the lvm2create_initrd script.

After changing my kernel and rebuilding the initrd image my installation was unable to boot because the kernel couldn't find the init-script.

After some research on this, I found out, that the executables in the initrd image under /bin and /sbin aren't executable, except /sbin/init

As a workaround I'm mounting the initrd image and do a

```
chmod u+x bin/busybox && chmod u+x sbin/lvm
```

inside the mounted dir.

What kind of change can have this effect? The script have been retained unchanged.   :Shocked: 

Bye.

----------

## Raffi

Your fix should not cause any problems, they are supposed to be executable. Since I copy the files with cpio from your system, I can't see how those files did not get the correct permissions set unless your system has them wrong.

What file system are you running on /? What is the permissions on /bin/bb and /sbin/lvm?

----------

## /root

Wow. Really fast reply  :Smile: 

Here are the requested Infos:

Filesystems:

```

morpheus ~ # mount

/dev/mapper/gentoo-system on / type ext3 (rw,noatime)

proc on /proc type proc (rw)

sysfs on /sys type sysfs (rw,nosuid,nodev,noexec)

udev on /dev type tmpfs (rw,nosuid)

devpts on /dev/pts type devpts (rw,nosuid,noexec)

/dev/hde1 on /boot type ext2 (rw,noatime)

/dev/mapper/gentoo-portage on /usr/portage type ext2 (rw,noatime)

/dev/mapper/gentoo-data on /mnt/data type ext3 (rw,noatime)

/dev/mapper/gentoo-media on /mnt/media type ext3 (rw,noatime)

shm on /dev/shm type tmpfs (rw,noexec,nosuid,nodev)

usbfs on /proc/bus/usb type usbfs (rw,noexec,nosuid,devmode=0664,devgid=85)

```

Files:

```

morpheus ~ # ls /sbin/lvm -la

-r-xr-xr-x 1 root root 1352628 Jan 26 18:13 /sbin/lvm

morpheus ~ # ls /bin/busybox* -la

-rwxr-xr-x 1 root root 1059140 Jan 26 17:49 /bin/busybox

lrwxrwxrwx 1 root root       2 Jan 26 17:49 /bin/busybox.static -> bb

morpheus ~ # ls /bin/bb -la

-rwxr-xr-x 1 root root 1757020 Jan 26 17:49 /bin/bb

```

cpio:

```

morpheus ~ # cpio --version

cpio (GNU cpio) 2.7

```

The last big emerge was bootstrapping my system and do an emerge -e system && emerge -e world in the middle of january.

Bye.

[EDIT] I've tried the script step-by-step. The cpio command gives an error 

```

cpio: cannot link /bin/bb to /mnt/initrd-image/bin/bb: Invalid cross-device link

```

and google tell me it has something to do with different filesystems (ex3/ex2). But this is the point where the execution bit is gone.

----------

## Raffi

I'm using 2.6-r5 of cpio. I wonder if the syntax changed. What does  your info page say that -L does?

----------

## /root

cpio options:

```

`-p, --pass-through'

     Run in copy-pass mode.  *Note Copy-pass mode::.

`-d, --make-directories'

     Create leading directories where needed.

`-m, --preserve-modification-time'

     Retain previous file modification times when creating files.

`-L, --dereference'

     Copy the file that a symbolic link points to, rather than the

     symbolic link itself.

```

Seems reasonable to me.

----------

## Raffi

That's what mine says.

Have you tried playing around with cpio? Does the following give an error?

```

echo /bin/bb | cpio -pvdm /tmp

```

----------

## /root

Nothing changed with different options of cpio.

The error I noticed in my previous post edit comes from "-l" instead of "-L"  :Embarassed: 

Every file or directory created by

```
echo /bin/bb | cpio -pvdm /tmp

```

has got only user rights set. rwx for dirs and rw for files.

```
morpheus initrd-image # echo /bin/bb | cpio -pdmLv /mnt/initrd-image

/mnt/initrd-image/bin/bb

3432 blocks

morpheus initrd-image # mkdir sbin

morpheus initrd-image # echo /sbin/lvm | cpio -pdmLv /mnt/initrd-image

/mnt/initrd-image/sbin/lvm

2642 blocks

morpheus initrd-image # ls * -la

bin:

total 1726

drwx------ 2 root root    1024 Feb  2 23:28 .

drwxr-xr-x 5 root root    1024 Feb  2 23:29 ..

-rw------- 1 root root 1757020 Feb  2 23:28 bb

sbin:

total 1330

drwxr-xr-x 2 root root    1024 Feb  2 23:29 .

drwxr-xr-x 5 root root    1024 Feb  2 23:29 ..

-rw------- 1 root root 1352628 Feb  2 23:29 lvm

```

At the moment, changing the rights with chmod works for me.

----------

## Raffi

That's very strange. It is acting like you are not copying into an ext2 file system.

Can you try to create a loopback ext2 filesytem by hand and see what happens when you copy to it using cpio? How are you looking at the files in the initrd that my script created?

----------

## /root

I've tried in different ways, even copying in the same ext3 filesystem gives the same results.

This way, I'm looking in the created initrd image:

```
morpheus mnt # mount -o loop /boot/initrd-lvm2-2.6.19-gentoo-r4 /mnt/initrd-image

morpheus mnt # ls initrd-image/ -la

total 35

drwxr-xr-x 10 root root  1024 Jan 29 17:59 .

drwxr-xr-x  7 root root  4096 Feb  2 23:28 ..

drwxr-xr-x  2 root root  4096 Jan 29 17:59 bin

drwxr-xr-x  2 root root  2048 Jan 29 17:58 dev

-rw-r--r--  1 root root 16896 Jan 29 17:58 dev.cpio

drwxr-xr-x  3 root root  1024 Jan 29 17:59 etc

drwxr-xr-x  2 root root  1024 Jan 29 17:58 lib

drwxr-xr-x  2 root root  1024 Jan 29 17:58 proc

drwxr-xr-x  2 root root  1024 Jan 29 17:58 rootvol

drwxr-xr-x  2 root root  1024 Jan 29 17:59 sbin

drwxr-xr-x  2 root root  1024 Jan 29 17:58 var

morpheus mnt # ls initrd-image/sbin/* -la

-r-xr-xr-x 1 root root    3810 Jan 29 17:59 initrd-image/sbin/init

-rw------- 1 root root 1352628 Jan 29 17:58 initrd-image/sbin/lvm

```

----------

## Raffi

Sounds like either your kernel or your mkext2 command is doing something funny. What kernel version? What ext utils version.

----------

## /root

Current kernel version and e2fsprogs:

```

morpheus ~ # equery b mke2fs

[ Searching for file(s) mke2fs in *... ]

sys-fs/e2fsprogs-1.39-r1 (/sbin/mke2fs)

morpheus ~ # uname -r

2.6.19-ck2-r4

```

Also tried kernel 2.6.19-gentoo-r4 with almost same config as 2.6.19-ck2-r3 (initial kernel, no "execute" problems while building initrd image) but slightly changes in device driver section -> video for linux, character device, i2c.

----------

## Raffi

I'm running sys-fs/e2fsprogs-1.39.

I'm also using a vanilla 2.6.19.2 on one machine and a suspend2 sources (not sure which version) on another. This is really strange, you do seem to have a permissions problem when you copy files. It also looks like you might be running an ~arch version of everything. If you have verified that creating a initrd ext2 loopback by and and maybe even trying it straight on a ramdisk /dev/ram0 gives you problems just copying files and trying to preserve permission, I would suggest starting a new thread on that.

----------

## /root

I've opened a thread in "Other things Gentoo" on the cpio problem in general.

----------

## /root

 */root wrote:*   

> I've opened a thread in "Other things Gentoo" on the cpio problem in general.

 

Problem solved.

Downgrading to cpio 2.6-r5 does the trick.

```
morpheus initrd-image # echo /bin/bb | cpio -dpmL /mnt/initrd-image/

3432 blocks

morpheus initrd-image # ls bin/* -la

-rwxr-xr-x 1 root root 1757020 Jan 26 17:49 bin/bb

morpheus initrd-image # cpio --version

cpio (GNU cpio) 2.6

```

----------

## Raffi

I just noticed that my laptop stopped being able to hibernate. Turns out when I upgraded to the latest hibernate-scripts (1.94-r3) the sanity checking decided my system was not setup to hibernate. The new scripts don't detect this setup because we purposefully avoided all of the normal ways of telling the kernel about our resume device (since the kernel would not understand them until we finish setting things up)The fix is to add the line

```

SuspendDevice /dev/vg/swap

```

to your hibernate.conf. This tells the script not to worry about not finding a resume device in the normal places. I have edit my original howto to add this detail.

----------

## Raffi

If you ever have the resume fail because it did not see your hibernated data in your swap partition (as opposed to seeing and rejecting it) you will want to be sure to invalidate the hibernated image before your next reboot. At least if you value your disks staying in an uncorrupted state.

If your image was not detected (as opposed to rejected) your swap will probably not activate since it will contain the hibernate image instead of a valid swap signature. In this case, a simple mkswap will take care of the problem and prevent what happened to me.

I was trying to bring my script up to date to deal with changes to the kernel and the hibernate scripts and when I thought I had it, I hibernated my laptop. The next reboot showed that I had not successfully dealt with the change from /proc to /sys in activating the resume. The machine booted up all the way ignoring my hibernate images and not activating my swap. I did not pay attention, fixed my script and rebooted with the intent of hibernating from a clean boot. I knew I was not going to be happy when instead of a clean boot, it resumed from the previous hibernate.

Fortunately, a reiserfsck --rebuild-tree from the lvm2 rescue on a couple of my logical volumes got me up far enough to be able to start the emerge -e world. It could have been worse.

Anyway, I updated my script and it should correctly deal with using /sys as well as /proc for resuming and it my earlier post about updating the .conf file for the hibernate script should deal with those changes as well.

----------

## weedy

with all these permission problems why not use rsync to copy the files?

----------

## Raffi

The only difference between cpio and rsync is on directories and this is only on the timestamp. I could have used rsync, but I'm an old unix guy from way back before such new fangled tools.   :Smile: 

----------

## Raffi

Looks like the broken version of cpio has made it to portage stable. I have reported the bug. If it is not fixed soon, I will post a workaround.

----------

## Raffi

From bugzilla

fixed in 2.7-r2

https://bugs.gentoo.org/show_bug.cgi?id=174921

----------

## lkraav

hi raffi

thanks for the great script, i have converted regular setups into raid1+lvm2 setups running in no time.

my question is, do you know if adding LUKS support for mounting the root filesystem inside LVM2 volume suitable to be integrated in your script?

i am running the setup described here http://gentoo-wiki.com/SECURITY_System_Encryption_DM-Crypt_with_LUKS#Initramfs.27s_init on one machine, 

it works well also. is it possible to bring these two scripts together?

----------

## Raffi

 *lkraav wrote:*   

> 
> 
> my question is, do you know if adding LUKS support for mounting the root filesystem inside LVM2 volume suitable to be integrated in your script?
> 
> 

 

Until your post, I had not heard of LUKS. A quick look through the howto seems like it would not be too hard to integrate into the lvm script. Since I don't have any current plans to encrypt my system, it is unlikely that I will get to it any time soon, but if you want to do it, I will be happy to try an help.

----------

## lkraav

thanks for your reply. the problem is, i would have to really start digging into both scripts without being the author. i think either of you guys could get the result in half or less time and effort. perhaps you have a virtual (vmware or other) machine you could experiment without tampering with your own system?

while encrypting the root filesystem usually isnt crucial anyway, i still think it would make for a coherent approach to designing a system, such as this:

raid1 -> lvm2 -> luks (swap, root, secure-data) -> ext/xfs/reiser...

i'll go ahead and start taking a look at both scripts to start.

----------

## Raffi

 *lkraav wrote:*   

> thanks for your reply. the problem is, i would have to really start digging into both scripts without being the author. i think either of you guys could get the result in half or less time and effort. perhaps you have a virtual (vmware or other) machine you could experiment without tampering with your own system?
> 
> 

 

I'm certain that you are correct on the speed part. However, I see the above as a reason for you to dig in and do it. In open source, everyone gives a little and we all gain a lot   :Smile:  . Since I would not be using that system, just setting up a test system, I would not be in a good position to maintain the code nor would my motivation be as high as it should be to produce the best result. You will find by digging in, that you will gain a much fuller understanding of all the processes involved and will be in a position to help others gain that understanding.

The combined script is different enough from both my script and the luks script that it really should be its own howto. Once you get a working script, posting it as a new topic will generate some interest that will allow you to refine the script over time. As I said before, I will be happy to help you with the script, but I think you should take ownership of it.

 *lkraav wrote:*   

> 
> 
> while encrypting the root filesystem usually isnt crucial anyway, i still think it would make for a coherent approach to designing a system, such as this:
> 
> raid1 -> lvm2 -> luks (swap, root, secure-data) -> ext/xfs/reiser...
> ...

 

I agree and am looking forward to your final product. It may even convince me to dive into encrypting my system   :Smile:  .

----------

## dexxxter

Hi!

I'm currently trying to make gentoo boot from root partition, located on LV over RAID mirror, but during boot i get following message:

initrd: Mounting /proc

initrd: Remounting / read/write

Starting RAID device /dev/md6

/sbin/init: /sbin/init: 78: /sbin/mdadm: Permission denied

...later...

*** Entering LVM2 rescue shell. Exit shell to continue booting. ***

/ $ mdadm

/bin/ash: mdadm: Permission denied

/ $ ls -la /sbin

drwxr-xr-x    2 0        0            1024 Jul 22 07:22 .

drwxr-xr-x   10 0        0            1024 Jul 22 07:22 ..

-r-xr-xr-x    1 0        0            3811 Jul 22 07:22 init

-rwxr-xr-x    1 0        0         1362680 Jul 22 07:22 lvm

-rwsr-xr-x    1 0        0          177352 Jul 22 07:22 mdadm

Chmoding mdadm to 0777, 0755, 4755 (as above) with no result.

Any Ideas why it doesn't start md-devices?

initrd was created with:

lvm2create_initrd.sh -M gentoo -c /etc/lvm/lvm.conf -r /dev/md6 -R /etc/mdadm.conf 2.6.20-gentoo-r7

kernel compiled with

CONFIG_MD=y

CONFIG_BLK_DEV_MD=y

CONFIG_MD_LINEAR=y

CONFIG_MD_RAID0=y

CONFIG_MD_RAID1=y

CONFIG_MD_RAID10=y

CONFIG_MD_RAID456=y

CONFIG_MD_RAID5_RESHAPE=y

# CONFIG_MD_MULTIPATH is not set

# CONFIG_MD_FAULTY is not set

CONFIG_BLK_DEV_DM=y

# CONFIG_DM_DEBUG is not set

CONFIG_DM_CRYPT=y

CONFIG_DM_SNAPSHOT=y

CONFIG_DM_MIRROR=y

# CONFIG_DM_ZERO is not set

# CONFIG_DM_MULTIPATH is not set

CONFIG_BLK_DEV_DM_BBR=m

Disk layout:

/dev/sda(b)1 -> /dev/md0 (raid1) -> /boot

/dev/sda(b)2 -> extended

/dev/sda3 -> current '/'

/dev/sda(b)4 -> /dev/md4 (raid0) -> swap

/dev/sda(b)5 -> /dev/md5 (raid0) -> LVM VG stripe

/dev/sda(b)6 -> /dev/md6 (raid1) -> LVM VG mirror

grub.conf:

title=GeNtOoOoOo LVM (2.6.20-7)

root (hd0,0)

kernel /kernel-genkernel-x86_64-2.6.20-gentoo-r7 root=/dev/ram0 lvm2root=/dev/mirror/root ramdisk_size=5456

initrd /initrd-lvm2-2.6.20-gentoo-r7.gz

also tried to add  dodmraid dolvm2 to kernel options.

----------

## Raffi

 *dexxxter wrote:*   

> Hi!
> 
> I'm currently trying to make gentoo boot from root partition, located on LV over RAID mirror, but during boot i get following message:
> 
> initrd: Mounting /proc
> ...

 

There is a good chance you are running a broken version of cpio on the machine you created your initrd. What version are you running? Try upgrading, I'm using version 2.8

----------

## Heinzi

Thank you very much!

I already thought about doing without lvm2 or without suspend2 - until I found your howto  :Smile: 

----------

## Raffi

Glad to see it is of use. Please post how your install goes. There have been enough changes since I first wrote this that it may be time for some updates.

----------

## the.ant

That script seems to address exactly the problem I have been dealing with. 

It's exactly how my ubuntu machine is set up, in the long term I would like to

replace ubuntu with gentoo but I did not find out how to set this up. 

However, I could not try it yet because my current machine does not have 

support for loop devices and right now, I don't feel like recompiling the kernel. 

But eventually I will try it and post the results here.

----------

## Heinzi

It doesn't work as it should  :Sad: 

I went through the points 4,5 and 6 of your tutorial to add suspend2 to my already existing gentoo lvm2 system.

The new kernel booted and I stopped the x-server and used the console to log in. I got the tip to try hibernate first without X in console.

I typed "hibernate -n" and everything went well. So I typed "hibernate" and also everything seemed to go well. He wrote the image, went atomic and switched power off. I switched power on and selected again the TuxOnIce-grub and now my computer should recognize the image and load that, but it doesn't.

He boots as if there were no image. Mysqld for example is wondering why the socket-file already exists.

What can I do?

I have a little boot partition and a windows-partition on one sata-harddrive. The rest of this harddrive is filled by a lvm2-partition. On a second harddrive I have only a lvm2-partition.

Here my lvm2-configuration:

```
$ lvm lvs

  LV   VG     Attr   LSize   Origin Snap%  Move Log Copy%

  home system -wi-ao 239.90G

  root system -wi-ao  36.00G

  swap system -wi-a-   2.00G
```

Here the way I tried to install hibernation:

I emerged hibernate-script and added hibernate-cleanup to the boot runlevel.

I redirected the /usr/src/linux-symlink to /usr/src/linux-2.6.23-tuxonice-r6

I compiled the tuxonice-2.6.23-r6 kernel, copied /usr/src/linux/boot/bzImage to /boot/bzImage-tuxonicehowto.

I created the initramdisk with your script - scriptoptions: "-M gentoo 2.6.23-tuxonice-r6"

I ran module-rebuild, so that my ati-card will work.

I added a TuxOnIce-Grub-Entry into menu.lst:

```
title TuxOnIce

    root (hd0,1)

    kernel /bzImage-tuxonicehowto ro root=/dev/ram0 lvm2root=/dev/system/root lvm2resume2=/dev/system/swap

    initrd /initrd-lvm2-2.6.23-tuxonice-r6.gz
```

My hibernate.conf:

```
TryMethod suspend2.conf

#TryMethod disk.conf

#TryMethod ram.conf
```

suspend2.conf:

```
# Example suspend2.conf file.

# 

# See hibernate.conf(5) for help on the configuration items.

#

# NOTE: Suspend2 is an improved version of suspend-to-disk which currently

#       requires patching your kernel. For more information, see www.suspend2.net

# 

#       If you do not wish to patch your kernel but still be able to suspend to

#       disk, see disk.conf instead.

### suspend2 (for Software Suspend 2)

UseSuspend2 yes

Reboot no

EnableEscape yes

DefaultConsoleLevel 1

Compressor lzf

Encryptor none

# ImageSizeLimit 200

## useful for initrd usage:

SuspendDevice /dev/system/swap

## Powerdown method - 3 for suspend-to-RAM, 4 for ACPI S4 sleep, 5 for poweroff

PowerdownMethod 5

## Any other /proc/software_suspend setting can be set like so:

ProcSetting expected_compression 50

## Or traditionally like this:

# Suspend2AllSettings 0 0 2056 65535 5

## Or even from the results of hibernate --save-settings with this:

# Suspend2AllSettingsFile /etc/hibernate/suspend-settings.conf

## For filewriter:

# FilewriterLocation /suspend_file 1000

# VerifyFilewriterResume2 yes

## Specify a userui like this:

# ProcSetting userui_program /usr/local/sbin/suspend2ui_text

# Scale CPU to full speed to make sure we suspend as fast as possible.

FullSpeedCPU yes

#Partitionen, auf die (waehrend Linux suspended ist) zugegriffen werden soll, muessen unmounted und remounted werden

Unmount /windows

OnResume 20 mount /windows

Include common.conf
```

Lines without # as first char in common.conf:

```
Verbosity 0

LogFile /var/log/hibernate.log

LogVerbosity 1

Distribution gentoo

SaveClock restore-only

RadeonTool yes

FullSpeedCPU yes

LockKDE yes

OnSuspend 20 echo "Good night!"

OnResume 20 echo "Good morning!"

UnloadBlacklistedModules yes

LoadModules auto

DownInterfaces eth0

UpInterfaces auto

SwitchToTextMode yes

XStatus kde

XSuspendText Preparing to suspend...

XResumeText Resuming from suspend...
```

When booting I get a message like "BLKFSBUF: Device or resource busy". But the message is too quick out of sight and I have no idea how to slow down boot process or where I can find a boot protocol.

----------

## Raffi

Well that looks correct. Here is something you can try. This might give you a usable error message. First boot with the option lvm2rescue. This should drop you into a shell before anything has happened. Then try the following.

```

echo /dev/system/swap >/proc/suspend2/resume2

echo > /proc/suspend2/do_resume

```

----------

## Heinzi

The folder /proc/suspend2 doesn't exists - neither in the lvm2rescue-shell nor in the booted system.

I also searched a folder named tuxonice and didn't found any in /proc.

But tuxonice should be compiled into my kernel. Here the important config parts:

```
CONFIG_HIBERNATION_SMP_POSSIBLE=y

CONFIG_HIBERNATION=y

CONFIG_PM_STD_PARTITION="/dev/system/swap"

CONFIG_TOI_CORE=y

CONFIG_TOI_FILE=y

CONFIG_TOI_SWAP=y

CONFIG_TOI_CRYPTO=y

CONFIG_TOI_USERUI=y

CONFIG_TOI_USERUI_DEFAULT_PATH="/usr/local/sbin/tuxonice_fbsplash"

# CONFIG_TOI_KEEP_IMAGE is not set

CONFIG_TOI_REPLACE_SWSUSP=y

# CONFIG_TOI_CLUSTER is not set

CONFIG_TOI_CHECKSUM=y

CONFIG_TOI_DEFAULT_WAIT=-1

# CONFIG_TOI_PAGEFLAGS_TEST is not set

CONFIG_TOI=y

CONFIG_SUSPEND_SMP_POSSIBLE=y

CONFIG_SUSPEND=y

# CONFIG_USB_SUSPEND is not set
```

----------

## Raffi

OK. That was for the older kernels. I should have said do

```

echo /dev/system/swap >/sys/power/suspend2/resume2

echo > /sys/power/suspend2/do_resume 

```

----------

## Heinzi

There is no /sys/power/suspend2, but I found in the booted system a folder /sys/power/tuxonice with the files do_resume and resume (not resume2) in it.

The content of /sys/power/tuxonice/resume is /dev/system/swap.

But when I boot the lvm2rescue - shell, there aren't any files in /sys. /sys is just an empty folder. Strange

Another strange thing is the part of the boot-log I can see above the cursor, when the lvm2rescue-shell is started. There is a message "TuxOnIce: Can't translate "/dev/system/swap" into a device id yet." and a few lines later "initrd: Activating LVM2 volumes". Isn't that the wrong order?

----------

## Raffi

I'm not sure what tuxonice is, but from what you are telling me, I'm pretty sure my script is not supporting it.

I guess you will need to mount /sys from the rescue shell. You can do that with mount -t sysfs none /sys

----------

## Heinzi

Suspend2 was renamed to tuxonice - it's just another name for the newer versions of suspend2.

I modified your script, so that it also works with tuxonice. With that modification, it works now. Thanks for your help!

In your script, there is a section where you distinguish between /proc and /sys/power. There I added a if-statement for tuxonice.

From line 169 on:

```
# Setup software suspend2

if [ -n "$resume2" ]; then

        mount -t sysfs none /sys

        if [ -f /proc/suspend2/resume2 ]; then

                echo "Setup suspen2 hibernation"

                echo $resume2 >/proc/suspend2/resume2

                echo > /proc/suspend2/do_resume

        elif [ -f /sys/power/suspend2/resume2 ]; then

                echo "Setup suspen2 hibernation"

                echo $resume2 >/sys/power/suspend2/resume2

                echo > /sys/power/suspend2/do_resume

        elif [ -f /sys/power/tuxonice/resume ]; then

                echo "Setup suspen2 hibernation"

                echo $resume2 >/sys/power/tuxonice/resume

                echo > /sys/power/tuxonice/do_resume

        else

                echo "Can't resume!"

        fi

fi
```

Maybe you can update your script that way so that it works for newer versions too.

----------

## Raffi

Cool. I will update my script.

----------

## Raffi

I have re-worked the script to use a cpio initrd instead of a ramdisk. This makes it easier to build the initrd. Since you no longer need to mount a loopback device  you can probably run the script as a non root user (other than it wanting to put the initrd in /boot).

There is no longer a need for ramdisk_size, since we are not using the ramdisk anymore and you can compile the kernel with ramdisk as a module. Just be sure to turn on the initrd stuff.

----------

## Halahad

it works for baselayout2?Last edited by Halahad on Sun May 11, 2008 2:21 pm; edited 2 times in total

----------

## Raffi

 *Halahad wrote:*   

> Is it works for baselayout2?

 

No idea. I won't be playing with baselayout2 until it is in the stable tree.

----------

## Halahad

It seems like that cpio is much faster than the original ramdisk one. thx for your great work.

----------

## weedy

 *Raffi wrote:*   

>  *Halahad wrote:*   Is it works for baselayout2? 
> 
> No idea. I won't be playing with baselayout2 until it is in the stable tree.

 

baselayout2 does not have MAKEDEV

----------

## ali3nx

Most definitely verified that makedev does not exist at least on my system...

https://forums.gentoo.org/viewtopic-p-5095275.html#5095275

----------

## Raffi

Presumably there is a MAKEDEV equivalent available. Does anyone know what it is?

----------

## ali3nx

 *Raffi wrote:*   

> Presumably there is a MAKEDEV equivalent available. Does anyone know what it is?

 

It would seem that someone anticipated various legacy apps needing MAKEDEV with baselayout 2. 

```
anvil ~ # emerge -pv sys-apps/makedev

These are the packages that would be merged, in order:

Calculating dependencies... done!

[ebuild   R   ] sys-apps/makedev-3.23.1  USE="-build (-selinux)" 0 kB

Total: 1 package (1 reinstall), Size of downloads: 0 kB
```

----------

## Halahad

 *ali3nx wrote:*   

>  *Raffi wrote:*   Presumably there is a MAKEDEV equivalent available. Does anyone know what it is? 
> 
> It would seem that someone anticipated various legacy apps needing MAKEDEV with baselayout 2. 
> 
> ```
> ...

 

This kind of makedev has a completely deferent syntax and configurations.

----------

## Raffi

It is starting to sound like I should bite the bullet and upgrade one of my machine to baselayout2. Not sure when  I will have the time, but I will try to get to it this week.

----------

## Raffi

I have updated the script. Use the -M gentoo-b2 option if you are using baselayout 2. Be sure to emerge makedev first. I have also added support for xen systems, the script this was tested on is a xen domu with /dev/xvda being a pv and the entire system using lvm.

----------

## dantrell

 *Raffi wrote:*   

> I have updated the script. Use the -M gentoo-b2 option if you are using baselayout 2. Be sure to emerge makedev first. I have also added support for xen systems, the script this was tested on is a xen domu with /dev/xvda being a pv and the entire system using lvm.

 

I used the updated script like so:

```
./lvm2ci -M gentoo-b2 -c /etc/lvm/lvm.conf
```

Important output was as follows:

```
lvm2ci -- make LVM initial ram disk /boot/initrd-lvm2-2.6.24-gentoo-r5.gz

lvm2ci -- finding required shared libraries

lvm2ci -- adding required /dev files

don't know how to make device "consoleonly"

lvm2ci -- copying initrd files to ram disk

lvm2ci -- creating symlinks to busybox

lvm2ci -- creating new /tmp/mnt.16998/sbin/init

lvm2ci -- copying /etc/lvm/lvm.conf to /tmp/mnt.16998/etc/lvm/lvm.conf

8821 blocks

--------------------------------------------------------

Your initrd is ready in /boot/initrd-lvm2-2.6.24-gentoo-r5.gz
```

I noticed something failed during bootup but it scrolled by to fast to see, I'll check it out later. Regardless everything apparently works.

Thanks.  :Smile: 

----------

## Raffi

The error in my script of

don't know how to make device "consoleonly"

is nothing to worry about, I was just too lazy to make the script not ask for something that the new makedev could not do. Any errors in your normal startup cause by what my script does (or does not do) is something I do want to hear about.

----------

## dantrell

 *Raffi wrote:*   

> [...] Any errors in your normal startup cause by what my script does (or does not do) is something I do want to hear about.

 

Turned out to be nothing related to your script (as far as I could tell).

Thanks again.  :Smile: 

----------

## swingkyd

maybe this is a dumb question but... why must we use initrd? I have never used it for my gentoo box...so why would I need it for this?

----------

## Raffi

You don't need to unless you want to use something non-standard/built in for your root. In my case, I wanted everything on my system to be in lvm. That included swap and suspending/resuming from that swap. To do this, you need to use an initrd.

----------

## 188562

Thank you for the excellent work Raffi  !

It is unfortunate that your script is not working under baselayout2   :Crying or Very sad: 

----------

## Raffi

It is for me. If you are having problems, please send me the error.

----------

## 188562

I tried your script in a stable system and everything worked perfectly afterwards was an attempt to implement all the same under baselayout2. Unfortunately, under baselayout2 I have nothing left. If memory serves me is not the error was 

No volume groups found

and before that even when the assembly initrd-lvm2 script could not create the necessary device. 

But if it still works then I will just glad. This means that where I had committed a mistake. 

In any case, thanks.

----------

## Raffi

Be sure to re-grab the script as it changed when I added support for baselayout2. The support for baselayout2 was only necessary for creating the initrd on a baselayout2 machine. The old script could boot a machine that had baselayout2 on it as long as the initrd was created on a baselayout1 machine.

If you get errors when running the script, you can assume that the script will probably not work right when booting the machine. In this case, send me the errors and I can try to help.

----------

## saberspark77

I had to make a slight modification to the script.  Apparently TuxOnIce now suggests that its options be passed as resume=, rather than resume2=, and noresume rather than noresume2.  I simply changed all occurrences of resume2 to resume, but since I believe both are still valid, you may want to write in a test for either of them...

----------

## Raffi

This should probably be an option. Last time I installed it on my tuxonice, the docs said resume instead of resume2 and that did not work at all.

I think there ha been a recent update to tuxonice, I will look again.

----------

## Raffi

Please post the changes you made so that I can be sure I'm addressing your issue. Looking at my script, it does look like I'm already doing what you suggested.

----------

## lkraav

hi raffi,

would you consider putting your script under some version control, just mark each edit with a new version nr with some # comment line? i am assuming right now the script in the first post is the latest modified version, right.

----------

## Raffi

The script in the first post is the latest. I have arbitrarily assigned a version of 2.0 to the script. I will try to remember to update the number when I change things.

----------

## lkraav

as a side note - i now have a situation where i have some different configurations, some with raid, some with lvm2, some with both, some with luks included and i decided to move everything to tuxonice init script architecture - http://wiki.tuxonice.net/EncryptedSwapAndRoot

unfortunately this mechanism still lacks a good manual initramfs creation mechanism. right now i compile my initramfs into the kernel with the builtin mech, this lets me do a good build mechanism, but really sucks when you have to change something small in the init script.

but at the same time, stuff like uvesafb+v86d now uses the kernel builtin mechanism, so its kinda hard to move away from this too.

raffi, my vision is, that your script has been largely superseded in basic functionality when it comes to init, but it definitely has unique value in simply letting one create an initramfs quickly. ideally, that functionality would be integrated with tuxonice init mechanism, which in my view is the best (most functional) one available at the moment. it also needs only minor modifications for lvm2 + raid handling (which i already did on my systems). i dont see a point in having many of these different but same init scripts, i think everyone would benefit if there was an effort merger. ive already contacted alon lev bar (tuxonice) about this and he says all patches are welcome. the bad thing is i am not seeing any shared development tools, like repository and stuff, but perhaps all that could be worked out.

what are your thoughts on this, raffi?

----------

## Raffi

I have never looked at the tuxonice init stuff. I will have to find time to take a look.

----------

## Raffi

A quick look at the tuxonice page you posted a link to seems to me that we simply have two different goals. I'm using the same kernel mechanism that the tuxonice stuff is using. I just started with the lvm init scripts and modified things for my needs.

I have not been very interested in writing a generic do every possible thing initramfs, I've just added features that I have need of when I run into problems. Feel free to borrow/steal/re-write any portion as patches or add ons to the the tuxonice encryption stuff. If you come up with a generic way of adding features, I might end up switching  :Smile: 

----------

## tekknokrat

Using this script with current baselayout2 and gentoo-source's kernel the initrd fails with this error message:

 *Quote:*   

> mdadm: no devices found for /dev/md0

 

I want to use a lvm2 root within a degraded soft-raid1 array.

initrd was created via this cmd:

```
./create_lvminitrd.sh --raid /dev/md0 --raidconf /etc/mdadm.conf -M gentoo-b2 -m /lib/modules/2.6.29-gentoo-r5/kernel/drivers/md/dm-mod.ko -c /etc/lvm/lvm.conf 2.6.29-gentoo-r5
```

boot option:

```
title Gentoo Linux 2.6.29-r5

root (hd0,0)

        kernel /kernel-genkernel-x86_64-2.6.29-gentoo-r5 root=/dev/ram0 lvm2root=/dev/vg/gentoo-root

        initrd /initrd-lvm2-2.6.29-gentoo-r5.gz

```

mdadm.conf:

```
DEVICE partitions

MAILADDR root

ARRAY /dev/md0 level=raid1 num-devices=2 UUID=b3ddb736:02e9ccd6:ef7779b1:049da636

```

kernel config based on genkernel default config, only raid is builtin:

```

# grep RAID /usr/src/linux/.config

CONFIG_RAID_ATTRS=m

CONFIG_BLK_DEV_3W_XXXX_RAID=m

CONFIG_SCSI_AACRAID=m

CONFIG_MEGARAID_NEWGEN=y

CONFIG_MEGARAID_MM=m

CONFIG_MEGARAID_MAILBOX=m

CONFIG_MEGARAID_LEGACY=m

CONFIG_MEGARAID_SAS=m

# CONFIG_MD_RAID0 is not set

CONFIG_MD_RAID1=y

# CONFIG_MD_RAID10 is not set

# CONFIG_MD_RAID456 is not set

# grep DM_ /usr/src/linux/.config

# CONFIG_DM_DEBUG is not set

CONFIG_DM_CRYPT=m

CONFIG_DM_SNAPSHOT=m

CONFIG_DM_MIRROR=m

CONFIG_DM_ZERO=m

CONFIG_DM_MULTIPATH=m

# CONFIG_DM_DELAY is not set

CONFIG_DM_UEVENT=y

CONFIG_BLK_DEV_DM_BBR=m

CONFIG_LDM_PARTITION=y

# CONFIG_LDM_DEBUG is not set

```

 - sys-apps/baselayout-2.0.1 was built with the following:

USE="(multilib) -build"

 - sys-kernel/gentoo-sources-2.6.29-r5 was built with the following:

USE="(multilib) -build -symlink"

----------

## Raffi

It sounds like you don't have the correct disk drivers loaded. What do you get if you type the following in the rescue mode

fdisk -l

----------

## tekknokrat

 *Raffi wrote:*   

> It sounds like you don't have the correct disk drivers loaded. What do you get if you type the following in the rescue mode
> 
> fdisk -l

 

does fdisk -l from fedora installation on the same box suffice?

```

$ sudo fdisk -l

[sudo] password for gthielebein: 

Disk /dev/sda: 640.1 GB, 640135028736 bytes

255 heads, 63 sectors/track, 77825 cylinders

Units = cylinders of 16065 * 512 = 8225280 bytes

Disk identifier: 0xe7ace7ac

   Device Boot      Start         End      Blocks   Id  System

/dev/sda1   *           1          63      506016   83  Linux

Partition 1 does not end on cylinder boundary.

/dev/sda2              64         586     4193784   83  Linux

Partition 2 does not end on cylinder boundary.

/dev/sda3           17086       77825   487894050    5  Extended

Partition 3 does not end on cylinder boundary.

/dev/sda5           17086       77825   487894018+  fd  Linux raid autodetect

```

You just reminded me to include the sata_sil drivers because they are also build as module only:

```
# grep SIL /usr/src/linux/.config

CONFIG_SATA_SIL24=m

CONFIG_SATA_SIL=m

CONFIG_PATA_SIL680=m

# CONFIG_SSB_SILENT is not set

# CONFIG_FB_ASILIANT is not set

```

----------

## Raffi

 *tekknokrat wrote:*   

> 
> 
> does fdisk -l from fedora installation on the same box suffice?
> 
> 

 

Nope, we are looking to see if you have the drivers in this install.

 *tekknokrat wrote:*   

> 
> 
> You just reminded me to include the sata_sil drivers because they are also build as module only:
> 
> 

 

That would probably be the problem.

----------

## tekknokrat

 *Raffi wrote:*   

>  *tekknokrat wrote:*   
> 
> does fdisk -l from fedora installation on the same box suffice?
> 
>  
> ...

 

Tried with integrating the sil_ata but the error remains, I can't access the shell because of this:

```
/bin/ash: can't access tty: job control turned off
```

I wonder what is now missing in default gentoo-sources config.

----------

## Raffi

The job control message should not be a problem. If you add lvm2rescue to the command line, you should be able to type. You will need to either load the correct modules with the script, or compile in the correct disk drivers into your kernel. Not knowing what you are running, I can't tell you what that would be.

----------

## Raffi

The job control message should not be a problem. If you add lvm2rescue to the command line, you should be able to type. You will need to either load the correct modules with the script, or compile in the correct disk drivers into your kernel. Not knowing what you are running, I can't tell you what that would be.

----------

## Fog_Watch

 *Raffi wrote:*   

> 2) Create logical partitions for [...] swap

 

But:

"Try to put your swap partitions near the beginning of your drive".

And, Gentoo documents creating swap immediately after boot.

Given the above I can think of a reason to have swap not in a logical volume, but I can't think of a reason to have it in a LV.  Root?  Yes.  Swap?  ?

Regards

Fog Watch.

----------

## Raffi

 *Fog_Watch wrote:*   

>  *Raffi wrote:*   2) Create logical partitions for [...] swap 
> 
> Given the above I can think of a reason to have swap not in a logical volume, but I can't think of a reason to have it in a LV.  Root?  Yes.  Swap?  ?
> 
> 

 

I did it because I can. Kind of like why people climb mountains.

As for how well it works, I usually have a small swap partition for emergencies only, ANY swap is way too slow. Memory is cheap enough now you should probably not be using it much any  more. Not like the days when you wanted 2x swap to memory.

----------

## Moriah

I have been using gentoo-sources for 6 years now.  I am working on my first gentoo laptop install, details are at:

https://forums.gentoo.org/viewtopic-t-809431-postdays-0-postorder-asc-start-0.html

I am wondering if I still need to use suspend2-sources to use your method.

I also plan to eventually go with RAID-1 as well as LVM2 on the root filesystem, but for now I am just using a simple 320 GB drive.  Since I have 8 GB, it is usually easier to sleep than to hibernate.  Does your method support both of these?

Thanks!    :Very Happy: 

----------

## Raffi

 *Moriah wrote:*   

> I have been using gentoo-sources for 6 years now.  I am working on my first gentoo laptop install, details are at:
> 
> https://forums.gentoo.org/viewtopic-t-809431-postdays-0-postorder-asc-start-0.html
> 
> I am wondering if I still need to use suspend2-sources to use your method.
> ...

 

suspend2 has become tuxonice, so if you wanted to use it you could use tuxonice. As to NEED, no use whatever works for you. The tuxonice stuff allows suspend to work well, I have not tried with other kernels.

----------

## Moriah

So is suspend2 to ram or to disk?  I like both, but to ram is more important for me.

----------

## Raffi

Suspend to or tuxonice support both.

----------

## Raffi

I got tired of the way you had to add modules. The script will now lookup module paths and dependencies based on the module name. Make things a lot easier.

----------

## Raffi

With the stabilization of baselayout 2, things did not go well with the ramdisk. Turns out to be a simple fix, but it looked pretty bad. I have posted an edited script that should fix the problem, but if you have done the upgrade and everything fell apart, you can bring the system up using the following steps

Boot system into the lvm2rescue mode. (add lvm2rescue to the kernel line in grub).

From the lvm2rescue command prompt type

umount /sys

exit

----------

## Raffi

Another update to deal with changes to the output of the modprobe command.

----------

## smparkes

Baselayout-2 did seem to do me in. Up until then I had been using my own fixes to the LVM2-included script. But it sounds like I'm running into what was mentioned: I could build an initrd under BL1 and boot it under BL2, but anything I build under BL2 won't boot. (I have working kernels/initrds so I can try the new and still fall back and prove everything is, in some sense, sane).

I use both md and lvm2. The md stuff seems fine but then LVM2 says "no volume groups found" and falls over to the fallback shell.

Looking at the rc.log on success and what you've mentioned, I think I've got something wrong related to udev/device mapper.

Two things I noticed but don't entirely understand:

- With my old setup, I see errors related to /dev/vg/* not being created by udev but everything works okay. I'm afraid I don't quite understand the relationship between /dev/vg/name and /dev/mapper/vg-name.

- In the past I had lvm2root set to the /dev/mapper path but it sounds like I should be using a /dev/vg path (if that matters)?

Any pointers?

Thanks!

----------

## Raffi

Not sure what you are actually running into, but I solved the /dev/vg/... vs. /dev/mapper/... conflict by only using the /dev/mapper/... ones. They seem to always be there whereas the /dev/vg/... ones get added at some later time. They are easy to type and I use them from the command line, but scripts and fstab files get the mapper version.

As to no volume groups found, you might need to add a lvm.conf and/or add the device files into your initrd that contain your pvs.

----------

## smparkes

Very strange.

I noticed that with this initrd, /proc/mdstat doesn't show the md devices, though they seem to be detected via mdadm -Es.

I'm not sure if that's related or not, though, since with both this and the 'classic' initrd create, whether anything is in mdstat or not, pvscan/pvs returns nothing. That I'm finding hard to track backwards. Can't tell exactly what it's doing behind the scenes.

Seems like the device major/minors are different but I'm not sure what to make of that ...

----------

## Raffi

Something to try from your initrd environment is an fdisk -l. If that shows your devices, then things should work. I'm guessing you won't find your disks correctly with that which means you will need to add some devices files either for the md devices or something else.

----------

## smparkes

Okay. Getting to understand this a bit more. I'll walk through the init script and see where things seem to go south ...

----------

## smparkes

Meh. Pretty much user error. I wasn't asking for the necessary md devices to get started. Not sure why it worked in the past since the old script had pretty much the same format. (Then on top of that, I messed up the md conf while trying to debug.)

I do get

```
The link /dev/vg/root should had been created by udev but it was not found. Falling back to direct link creation.

```

on boot for all the logical volumes. Appears to be benign and all the search results seem inconclusive ...

Thanks!

----------

## Raffi

The fall back message you got is why I use the /dev/mapper/... version of the device files.

I could not tell from your post if you have solved your problem or now.

----------

## smparkes

Yeah, sorry, aside from the weird warnings, it's working fine. I just migrated a more complex server and it worked fine.

Thanks for maintaining the script. Takes a load off my mind; I'd been worried about my own hacks.

One comment for posterity: cutting and pasting the script caused a minor problem for me: for some reason I was getting a space at the end of the lines that end the here documents which kept bash from recognizing them. Not a big deal, but it'd be a little easier if you could stuff the script into a pastie or github gist.

Thanks for all the effort.

----------

## Raffi

Been a while since I've updated this script.

I've switched to using the kernel swap suspend stuff and I currently have a encrypted disk on my laptop. Using grub2 and this initrd script, I'm able to have a full encrypted system (minus the MBR and grubs module partition (8MB)).

----------

