Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
[SOLVED] EFISTUB kernel upgrading script?
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
fcl
Tux's lil' helper
Tux's lil' helper


Joined: 31 Dec 2016
Posts: 77

PostPosted: Sun Feb 12, 2017 11:55 am    Post subject: [SOLVED] EFISTUB kernel upgrading script? Reply with quote

I've got an EFISTUB kernel and no extra boot loaders. Everything works dandy but I want to make upgrading kernels a bit "easier".
After I compile a new kernel, I always make install which copies it to /boot as vmlinuz-${PV}-gentoo. This is nice but isn't good enough because the name always changes and my EFI executable is hard coded as \efi\boot\bootx64.efi.
Basically I currently just manually cp the new kernel to /boot/EFI/Boot/bootx64.efi (and ditto for the old kernel). I want to automate this with a script of some kind.

Here's my current EFI configuration straight from efibootmgr -v
Code:
Boot0000* Gentoo   HD(1,GPT,5a0dea9b-c63f-41a1-a8ab-ee73396cdc80,0x800,0x40000)/File(\efi\boot\bootx64.efi)
Boot0001* Gentoo Old   HD(1,GPT,5a0dea9b-c63f-41a1-a8ab-ee73396cdc80,0x800,0x40000)/File(\efi\boot\bootx64.old.efi)


Is it okay to directly modify /sbin/installkernel ? I just removed some -$vers from it and it seemed to kinda work but I feel like this isn't the most ideal hack. :D
Code:
--- installkernel   2017-02-12 13:39:52.373637336 +0200
+++ installkernel2   2017-02-12 13:46:48.621640992 +0200
@@ -35,11 +35,11 @@
 
 # Create backups of older versions before installing
 updatever () {
-  if [ -f "$dir/$1-$ver" ] ; then
-    mv "$dir/$1-$ver" "$dir/$1-$ver.old"
+  if [ -f "$dir/$1" ] ; then
+    mv "$dir/$1" "$dir/$1.old"
   fi
 
-  cat "$2" > "$dir/$1-$ver"
+  cat "$2" > "$dir/$1"
 
   # This section is for backwards compatibility only
   if test -f "$dir/$1" ; then


Now make install creates vmlinuz and vmlinuz.old which should work fine as EFI executables. Please guide me


Last edited by fcl on Sun Feb 12, 2017 6:34 pm; edited 1 time in total
Back to top
View user's profile Send private message
Roman_Gruber
Advocate
Advocate


Joined: 03 Oct 2006
Posts: 3846
Location: Austro Bavaria

PostPosted: Sun Feb 12, 2017 12:32 pm    Post subject: Reply with quote

I'd stick to the FHS standard.

A better idea is to keep custom scripts in /etc or /root
/sbin maybe overwritten with next time you build that related package which owns that file
Back to top
View user's profile Send private message
jonathan183
Guru
Guru


Joined: 13 Dec 2011
Posts: 318

PostPosted: Sun Feb 12, 2017 1:36 pm    Post subject: Reply with quote

Personally I'd use a separate script for this and call the standard install script then mv or cp things after, /opt/user-scripts is my location of choice but Roman_Gruber has already pointed out /sbin is probably not the best location for a custom script ... also you may want either user interaction or some other mechanism preventing two failed kernel compiles leaving the system unbootable ;)
Back to top
View user's profile Send private message
Roman_Gruber
Advocate
Advocate


Joined: 03 Oct 2006
Posts: 3846
Location: Austro Bavaria

PostPosted: Sun Feb 12, 2017 1:42 pm    Post subject: Reply with quote

Second thought:

Custom overlay => custom ebuild for that patch.
Back to top
View user's profile Send private message
fcl
Tux's lil' helper
Tux's lil' helper


Joined: 31 Dec 2016
Posts: 77

PostPosted: Sun Feb 12, 2017 2:20 pm    Post subject: Reply with quote

Roman_Gruber wrote:
I'd stick to the FHS standard.

A better idea is to keep custom scripts in /etc or /root
/sbin maybe overwritten with next time you build that related package which owns that file

Yeah, I know it's bad form. I could actually just but /sbin to CONFIG_PROTECT or whatever it was called. Although that would create another problem (I don't know how often the executables there are modified but I'd reckon too often for my taste). Is there really no CONFIG_PROTECT_FILE? :roll:

jonathan183 wrote:
Personally I'd use a separate script for this and call the standard install script then mv or cp things after, /opt/user-scripts is my location of choice but Roman_Gruber has already pointed out /sbin is probably not the best location for a custom script ...

I'm really bad at scripting. Otherwise I'd do just that.

eg. 1. check that current dir is /usr/src/linux 2. issue make install 3. somehow handle the files in boot, ie. strip versions, but make sure to only touch _gentoo_ kernels (I dual boot and test other Linuxes).
I might be able to crack it in a week or two.. Or just do it manually like I've done until now and come back to it in a year when it finally drives me nuts. :lol:
Quote:
also you may want either user interaction or some other mechanism preventing two failed kernel compiles leaving the system unbootable ;)

Oh, I won't automate the whole kernel upgrade prosess, only the final part of copying/modifying the end result in /boot

Roman_Gruber wrote:
Second thought:

Custom overlay => custom ebuild for that patch.

Hmm... tempting. I think I'd be able to do that relatively easily. Bash isn't my forte but I do know the basics of ebuilds and patching.
Back to top
View user's profile Send private message
khayyam
Watchman
Watchman


Joined: 07 Jun 2012
Posts: 6227
Location: Room 101

PostPosted: Sun Feb 12, 2017 3:00 pm    Post subject: Reply with quote

Roman_Gruber wrote:
I'd stick to the FHS standard.

Roman_Gruber ... please show how anything in the OP provided was contary to the FHS ... or stop providing this form of nonsense.

@fcl ... there isn't a lot to it, you can place your modified 'installkernel' in /root/bin and as long as /root/bin is before /sbin in $PATH then this will be executed in place of /sbin/installkernel. That however means that you don't get updates and so it's probably better to do as jonathan183 suggested and wrap /sbin/installkernel in a seperate script (ie, /usr/local/sbin/myinstallkernel). I don't really know what needs to be done but I imagine the following will give you some idea (untested, so please test the various parts before assuming it'll work as expected).

Code:
#/bin/sh

# get the kernel version
# (based on what eselect shows as the currently selected kernel)
kver="$(eselect kernel list | awk '{gsub("linux-","") ; if ($3 ~ "*") print $2}')"

# check boot is mounted
if [ mountpoint -q -- "/boot" ] ; then
    cd /usr/src/linux-$kver
    /sbin/installkernel &&
    mv -f /boot/vmlinuz-$kver /boot/efi/boot/bootx64.efi
else
   echo "* boot isn't mounted, mount before running this script"
fi

You could probably do more there (ie, backing up the previous kernel, or what-have-you).

edit: corrected closing single quote

HTH & best ... khay


Last edited by khayyam on Sun Feb 12, 2017 7:49 pm; edited 1 time in total
Back to top
View user's profile Send private message
Ant P.
Watchman
Watchman


Joined: 18 Apr 2009
Posts: 6920

PostPosted: Sun Feb 12, 2017 5:24 pm    Post subject: Reply with quote

The kernel already provides the necessary hooks to do this without butchering files in /sbin/:
/etc/kernel/postinst.d/efi:
#!/bin/sh
mount /dev/sda1 /boot/efi

for kern in /boot/vmlinuz{,.old,.good}; do
    [ -f $kern ] || continue
    cp -Luv $kern /boot/efi/EFI/kernel/$(basename $kern).efi
done

umount /boot/efi

Code:
~ # cd /usr/src/linux && make install
Back to top
View user's profile Send private message
fcl
Tux's lil' helper
Tux's lil' helper


Joined: 31 Dec 2016
Posts: 77

PostPosted: Sun Feb 12, 2017 6:34 pm    Post subject: Reply with quote

Thank you khayyam and Ant. P. I combined parts of your scripts and put it in postinst.d/ Although it lacks some polish, it still works. I'll have to learn shell scripting and eventually make it better :-)

edit: btw khayy, your script had a little typo
Code:
kver="$(eselect kernel list | awk '{gsub("linux-","") ; if ($3 ~ "*") print $2}')"
                                                                               ^--- missed this
Back to top
View user's profile Send private message
Roman_Gruber
Advocate
Advocate


Joined: 03 Oct 2006
Posts: 3846
Location: Austro Bavaria

PostPosted: Sun Feb 12, 2017 6:56 pm    Post subject: Reply with quote

khayyam wrote:
Roman_Gruber wrote:
I'd stick to the FHS standard.

Roman_Gruber ... please show how anything in the OP provided was contary to the FHS ... or stop providing this form of nonsense.



Laziness?

Quote:
Locally installed system administration programs should be placed in /usr/local/sbin.


Was always that loc since kernel 2.x.x
Back to top
View user's profile Send private message
Hu
Moderator
Moderator


Joined: 06 Mar 2007
Posts: 21586

PostPosted: Sun Feb 12, 2017 7:12 pm    Post subject: Reply with quote

fcl wrote:
Thank you khayyam and Ant. P. I combined parts of your scripts and put it in postinst.d/ Although it lacks some polish, it still works. I'll have to learn shell scripting and eventually make it better :-)
Would you be willing to post the composite script? We may be able to help you polish it, and there is a chance someone else will find this thread and want the same functionality as your composite provides.
Back to top
View user's profile Send private message
khayyam
Watchman
Watchman


Joined: 07 Jun 2012
Posts: 6227
Location: Room 101

PostPosted: Sun Feb 12, 2017 7:17 pm    Post subject: Reply with quote

Roman_Gruber wrote:
khayyam wrote:
Roman_Gruber wrote:
I'd stick to the FHS standard.

Roman_Gruber ... please show how anything in the OP provided was contary to the FHS ... or stop providing this form of nonsense.

Laziness?

Quote:
Locally installed system administration programs should be placed in /usr/local/sbin.

Roman_Gruber ... that doesn't answer the question. They asked "[i]s it okay to directly modify /sbin/installkernel?" The answer, of course, is no, but you somehow think this warrents some comment about the FHS, which is only related should they create their own script in some non-standard location. The point being, why not answer the question asked, and not the question you decided it was.

best ... khay
Back to top
View user's profile Send private message
fcl
Tux's lil' helper
Tux's lil' helper


Joined: 31 Dec 2016
Posts: 77

PostPosted: Sun Feb 12, 2017 7:30 pm    Post subject: Reply with quote

Hu wrote:
fcl wrote:
Thank you khayyam and Ant. P. I combined parts of your scripts and put it in postinst.d/ Although it lacks some polish, it still works. I'll have to learn shell scripting and eventually make it better :-)
Would you be willing to post the composite script? We may be able to help you polish it, and there is a chance someone else will find this thread and want the same functionality as your composite provides.

Sure, it's pretty embarassing but what can you do. :oops:

Code:
#!/bin/sh

# get the kernel version
# (based on what eselect shows as the currently selected kernel)
kver="$(eselect kernel list | awk '{gsub("linux-","") ; if ($3 ~ "*") print $2}')"

cd /boot
cp -f vmlinuz-$kver /boot/efi/gentoo.efi
cp -f vmlinuz-*-gentoo.old /boot/efi/gentoo.old.efi


It could use "some" sanitization...

Note: this assumes that you use efibootmgr or some other mean to add the gentoo.efi boot entry.
This works for me
Code:
# efibootmgr -c -L "Gentoo" -l "\efi\gentoo.efi"

Obviously, sda1 is my /boot, and it also happens to be my $ESP.
Back to top
View user's profile Send private message
khayyam
Watchman
Watchman


Joined: 07 Jun 2012
Posts: 6227
Location: Room 101

PostPosted: Sun Feb 12, 2017 8:15 pm    Post subject: Reply with quote

fcl wrote:
It could use "some" sanitization...

fcl ... it is always a good idea to check the presence (or contents) of vars, files, or mounts, eg:

Code:
#!/bin/sh

kver="$(eselect kernel list | awk '{gsub("linux-","") ; if ($3 ~ "*") print $2}')"

if [ -n $kver ] && (mountpoint -q -- "/boot") ; then
    if [ -f /boot/vmlinuz-$kver ]; then
        cp -f /boot/vmlinuz-$kver /boot/efi/gentoo.efi
    else
        echo "* copy of /boot/vmlinuz-$kver failed"
    fi
    if ls /boot/vmlinuz-*-gentoo.old 1> /dev/null 2>&1 ; then
        cp -f /boot/vmlinuz-*-gentoo.old /boot/efi/gentoo.old.efi
    else
        echo "* copy of /boot/vmlinuz-*-gentoo.old failed"
    fi
fi

fcl wrote:
Note: this assumes that you use efibootmgr or some other mean to add the gentoo.efi boot entry. This works for me

Code:
# efibootmgr -c -L "Gentoo" -l "\efi\gentoo.efi"

If you're running this every time you update {ESP}/efi/gentoo.efi you don't need to.

best ... khay
Back to top
View user's profile Send private message
Hu
Moderator
Moderator


Joined: 06 Mar 2007
Posts: 21586

PostPosted: Sun Feb 12, 2017 8:37 pm    Post subject: Reply with quote

fcl wrote:
Sure, it's pretty embarassing but what can you do. :oops:
Learn from the advice of longtime users, improve, and be proud of what you write next. :)

khayyam provided good overall improvements. I want to touch on a few specific issues.
fcl wrote:
Code:
#!/bin/sh
Unless you know otherwise, it is often a good idea to set -e near the top of the script, which is a shorthand for adding || exit $? to most lines of the script. This will cause it to bail out early if a called program fails. Usually, it is less trouble to clean up the result of an early exit than to clean up the result of letting the script blunder on, relying on the outcome of events that never happened (due to earlier errors).

See info bash section (bash.info.bz2)The Set Builtin option `-e' for full details of how this works.
fcl wrote:
Code:
cp -f vmlinuz-$kver /boot/efi/gentoo.efi
cp -f vmlinuz-*-gentoo.old /boot/efi/gentoo.old.efi
This glob may expand to multiple kernels if you do not aggressively prune /boot. If it expands to multiple kernels, then that will probably cause the cp to fail because the rightmost argument is a file, not a directory.
Back to top
View user's profile Send private message
khayyam
Watchman
Watchman


Joined: 07 Jun 2012
Posts: 6227
Location: Room 101

PostPosted: Sun Feb 12, 2017 10:33 pm    Post subject: Reply with quote

Hu wrote:
fcl wrote:
Code:
cp -f vmlinuz-*-gentoo.old /boot/efi/gentoo.old.efi

This glob may expand to multiple kernels if you do not aggressively prune /boot. If it expands to multiple kernels, then that will probably cause the cp to fail because the rightmost argument is a file, not a directory.

Hu ... good catch. I wasn't sure why 'cp' was used, and thought it'd probably best if 'mv' were used in it's place ... but that brief thought didn't end up being reflected in the script suggestions. I'm not sure what 'make install' does, but the glob is probably the version of the currently running kernel, and so the following should remedy the need to use a glob:

Code:
ckver="$(uname -r)"
[...]
    if [ -f /boot/vmlinuz-${ckver}.old ] ; then
        cp -f /boot/vmlinuz-${ckver}.old /boot/efi/gentoo.old.efi
    else
        echo "* copy of /boot/vmlinuz-${ckver}.old failed"
    fi

That said, I don't know why this is being copied to {ESP}/efi/ ... there doesn't seem to be entry in efivars.

best ... khay
Back to top
View user's profile Send private message
fcl
Tux's lil' helper
Tux's lil' helper


Joined: 31 Dec 2016
Posts: 77

PostPosted: Mon Feb 13, 2017 12:58 pm    Post subject: Reply with quote

Thank you all for the help. I should've clarified that I changed the boot entry so it points to /efi/gentoo.efi now. /efi/boot/bootx64.efi might be overwritten by other distros/whatever because it's one of the defaults AFAIK.

To wrap things up, here's the current script.

Code:
$ cat /etc/kernel/postinst.d/efi
#!/bin/sh
set -e

kver="$(eselect kernel list | awk '{gsub("linux-","") ; if ($3 ~ "*") print $2}')"
ckver="$(uname -r)"

if [ -n $kver ] && (mountpoint -q -- "/boot") ; then
    if [ -f /boot/vmlinuz-${kver} ]; then
        cp -f /boot/vmlinuz-${kver} /boot/efi/gentoo.efi
    else
        echo "* copy of /boot/vmlinuz-${kver} failed"
    fi
    if [ -f /boot/vmlinuz-${ckver}.old ] ; then
        cp -f /boot/vmlinuz-${ckver}.old /boot/efi/gentoo.old.efi
    else
        echo "* copy of /boot/vmlinuz-${ckver}.old failed"
    fi
fi


/boot is my EFI System Partition and the first partition on /dev/sda, hence I added a boot entry with efibootmgr like so:
Code:
# efibootmgr -c -d /dev/sda -p 1 -L "Gentoo" -l "\efi\gentoo.efi"

(actually, -d and -p can be omitted in this case as sda1 is the default in efibootmgr)
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