Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
[SOLVED] Custom initramfs for LVM2
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Installing Gentoo
View previous topic :: View next topic  
Author Message
charles17
Advocate
Advocate


Joined: 02 Mar 2008
Posts: 2579

PostPosted: Tue Aug 22, 2017 4:11 pm    Post subject: [SOLVED] Custom initramfs for LVM2 Reply with quote

On working though the Custom initramfs wiki article I am stuck with copying the device nodes.
The article shows an example with /dev/{null,console,tty,sda1}
/usr/src/initramfs:
├── bin
├── dev
│   ├── console
│   ├── null
│   ├── sda1
│   └── tty
├── etc
├── lib
├── lib64
├── mnt
│   └── root
├── proc
├── root
├── sbin
└── sys

and says Please adapt to your own needs.

So which device nodes would I need for my own needs?
  • The root partition is on LVM like /dev/vg0/root -> /dev/mapper/vg0-root
  • The system is to support suspend and hibernation
  • All (except wifi) drivers are compiled into the kernel.
I can only guess, /dev/sda1 is meant to be the root partition and instead of it, I should put /dev/mapper/vg0-root, like
/usr/src/initramfs:
├── bin
├── dev
│   ├── console
│   ├── null
│   ├── mapper
│   │   └── vg0-root
│   └── tty
├── etc
├── lib
├── lib64
├── mnt
│   └── root
├── proc
├── root
├── sbin
└── sys

Is that correct? And are there other device nodes being useful to be copied?

Edit:
Mark this [SOLVED]


Last edited by charles17 on Fri Aug 25, 2017 8:29 am; edited 1 time in total
Back to top
View user's profile Send private message
szatox
Veteran
Veteran


Joined: 27 Aug 2013
Posts: 1707

PostPosted: Tue Aug 22, 2017 6:42 pm    Post subject: Reply with quote

Protip:
You _must_ have /dev/null in yout initramfs. Nothing will work without.
I'm not sure about /dev/console. I think I have managed to boot without it, though it's been quite a while ago (so I might be wrong). Copy it if you want to be on a safe side with limited attempts, or skip it to see what happens.

All the rest can be handled by kernel.
Code:
main () {
   mount -t proc proc /proc # <--- mount /proc first, before you do anything else.
   mount -t devtmpfs devtmpfs /dev/
   mount -t sysfs sysfs /sys

Even more funny, when you compile kernel, you can chose to make it mount /dev/ automatically during boot. Never really tried it, perhaps it would make /dev/null redundant too. It certainly should make one call to mount unnecessary.

Make sure you have LVM binary in your initramfs.
Back to top
View user's profile Send private message
NeddySeagoon
Administrator
Administrator


Joined: 05 Jul 2003
Posts: 41723
Location: 56N 3W

PostPosted: Tue Aug 22, 2017 7:57 pm    Post subject: Reply with quote

charles17,

Here's how to make an initrd without DEVTMPFS.
Using /sbin/lvm.static is the old way. I don't thinx lvm builds with USE=static any more, so you need to point lddtree at it and include everything listed there too.
_________________
Regards,

NeddySeagoon

Computer users fall into two groups:-
those that do backups
those that have never had a hard drive fail.
Back to top
View user's profile Send private message
charles17
Advocate
Advocate


Joined: 02 Mar 2008
Posts: 2579

PostPosted: Wed Aug 23, 2017 4:57 pm    Post subject: Reply with quote

NeddySeagoon wrote:
Using /sbin/lvm.static is the old way. I don't thinx lvm builds with USE=static any more,
Worksforme

NeddySeagoon wrote:
so you need to point lddtree at it and include everything listed there too.
Using lddtree --copy-to-tree looks like it doesn't need the libraries built with static. Is it preferable? I have the impression the initramfs gets somewhat bigger that way.
Back to top
View user's profile Send private message
charles17
Advocate
Advocate


Joined: 02 Mar 2008
Posts: 2579

PostPosted: Wed Aug 23, 2017 4:59 pm    Post subject: Reply with quote

Ok. Finally I got it working just with lvm.static and busybox both static in
/usr/src/initramfs:
/usr/src/initramfs/
├── bin
│   └── busybox
├── dev
│   ├── console
│   └── null
├── etc
├── init
├── lib
├── lib64
├── mnt
│   └── root
├── proc
├── root
├── sbin
│   └── lvm
└── sys

The init is taken from the wiki example:
init:
#!/bin/busybox sh

rescue_shell() {
   printf '\e[1;31m' # bold red foreground
   printf "$1 Dropping you to a shell."
   printf "\e[00m\n" # normal colour foreground
   # load the keymap
   [ -f /keymap ] && loadkmap < /keymap
   exec setsid cttyhack /bin/sh
}

# initialise
mount -t proc none /proc || rescue_shell "mount /proc failed."
mount -t sysfs none /sys || rescue_shell "mount /sys failed."
mount -t devtmpfs none /dev || rescue_shell "mount /dev failed."

# set hardcoded default values
crypt_root=""
root=""
resume=""
mount_ro_rw='ro'

# parse kernel command line
for p in $(cat /proc/cmdline); do
   case "${p}" in
#      crypt_root=*)
#         crypt_root="${p#*=}"
#         ;;
      root=*)
         root="${p#*=}"
         ;;
      resume=*)
         resume="${p#*=}"
         ;;
      ro|rw)
         mount_ro_rw="${p}"
         ;;
   esac
done


# # decrypt
# #  convert UUID or LABEL to device node
# crypt_root="$(findfs "${crypt_root}")"
# #  decryption is first tried using the key file /crypto_key.bin
# #  if this fails, prompt for a password
# cryptsetup open "${crypt_root}" lvm --type luks --key-file /crypto_key.bin || \
#    cryptsetup open "${crypt_root}" lvm --type luks || \
#    rescue_shell "Decryption failed."

# activate lvm
#  create /dev/mapper/control
lvm vgscan --mknodes || rescue_shell "vgscan failed."
#  activate all LVM volumes
lvm vgchange --sysinit -a ly || rescure_shell "vgchange failed."
#  create device nodes for the volumes
lvm vgscan --mknodes || rescue_shell "vgscan failed."

# mount the real root
#  convert UUID or LABEL to device node
root="$(findfs "${root}")"
mount -o "${mount_ro_rw}" "${root}" /mnt/root || rescue_shell "mount ${root} failed."

# enable resume from hibernate
if [ -n "${resume}" ]; then
   # copy the major:minor of the swap block device into /sys/power/resume
   printf '%u:%u\n' $(stat -L -c '0x%t 0x%T' "${resume}") > /sys/power/resume || \
      rescue_shell "Activating resume failed."
fi

# clean up
umount /proc
umount /sys
umount /dev

# boot the real system
exec switch_root /mnt/root /sbin/init

The initramfs is built into the kernel which is booting directly from EFI.

Next step should be to make the hibernation stuff working. Is it really that tricky as written in https://wiki.gentoo.org/wiki/Custom_Initramfs/Hibernation? I have emerged sys-power/suspend giving me commands like s2ram, s2disk and s2both. And I have a swap partition which I want to use for hibernation.
Back to top
View user's profile Send private message
frostschutz
Advocate
Advocate


Joined: 22 Feb 2005
Posts: 2968
Location: Germany

PostPosted: Wed Aug 23, 2017 9:44 pm    Post subject: Reply with quote

You really want to use devtmpfs and/or have vgscan --mknodes create the LVM devices for you.

The lddtree that has the --copy-to-tree feature is still only available for testing/~arch, the stable version doesn't have it. I was considering getting rid of all the static binary in favour of that once it hit stable...

LVM device names are very dynamic, their names don't matter, they are links to /dev/dm-{0,1,2,3,...} and those numbers are assigned on a first come, first serve basis - which means they might change as soon as you create another LV (and enable all LVs in one go...).

charles17 wrote:
Next step should be to make the hibernation stuff working. Is it really that tricky as written in https://wiki.gentoo.org/wiki/Custom_Initramfs/Hibernation?


I never got around to finishing the hibernation part... sorry about that.

Not sure if "tricky" is the right word, it's just a pitfall to avoid - you can either resume from hibernation, or mount/write to filesystems. Not change filesystems and then resume.

So preferably your hibernation image should be easy to reach (like a dedicated partition) and not somehow be hidden under layers and layers (like a swap file on a lvm raid) of stuff you should better not touch before resume.

charles17 wrote:
I have emerged sys-power/suspend giving me commands like s2ram, s2disk and s2both. And I have a swap partition which I want to use for hibernation.


If suspend to ram is an option for you, it is more reliable / does not need any special considerations (possible hardware problems and kernel bugs aside).
Back to top
View user's profile Send private message
charles17
Advocate
Advocate


Joined: 02 Mar 2008
Posts: 2579

PostPosted: Thu Aug 24, 2017 9:40 am    Post subject: Reply with quote

frostschutz wrote:
You really want to use devtmpfs and/or have vgscan --mknodes create the LVM devices for you.
It is in the init script shown above.

frostschutz wrote:
The lddtree that has the --copy-to-tree feature is still only available for testing/~arch, the stable version doesn't have it. I was considering getting rid of all the static binary in favour of that once it hit stable...
Here, on amd64 (all stable), the lddtree --copy-to-tree is provided by =app-misc/pax-utils-1.1.7[python]

frostschutz wrote:
So preferably your hibernation image should be easy to reach (like a dedicated partition) ...
I am going to try with the regular swap partition ( /dev/sda4 — parted says File system is linux-swap(v1).
Back to top
View user's profile Send private message
frostschutz
Advocate
Advocate


Joined: 22 Feb 2005
Posts: 2968
Location: Germany

PostPosted: Thu Aug 24, 2017 10:16 am    Post subject: Reply with quote

charles17 wrote:
Here, on amd64 (all stable), the lddtree --copy-to-tree is provided by =app-misc/pax-utils-1.1.7[python]


That's odd, could've sworn it wasn't in 1.1.7 ( previously discussed here https://wiki.gentoo.org/wiki/Talk:Custom_Initramfs#lddtree_secion ).

But if it's there, all the better...

@charles17 added a note about python useflag. thanks for pointing it out.
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Installing Gentoo All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum