<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="../assets/xml/rss.xsl" media="all"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>.:: Marcos Dione/StyXman's glob ::. (Posts about grub2)</title><link>https://www.grulic.org.ar/~mdione/glob/</link><description></description><atom:link href="https://www.grulic.org.ar/~mdione/glob/categories/grub2.xml" rel="self" type="application/rss+xml"></atom:link><language>en</language><copyright>Contents © 2025 &lt;a href="mailto:mdione@grulic.org.ar"&gt;Marcos Dione&lt;/a&gt; </copyright><lastBuildDate>Thu, 29 May 2025 15:41:16 GMT</lastBuildDate><generator>Nikola (getnikola.com)</generator><docs>http://blogs.law.harvard.edu/tech/rss</docs><item><title>Remotely upgrading CentOS6 to CentOS7</title><link>https://www.grulic.org.ar/~mdione/glob/posts/remotely-upgrading-centos6-centos7/</link><dc:creator>Marcos Dione</dc:creator><description>&lt;p&gt;At $WORK we're in the middle of a massive OS upgrade in all of our client's
appliances. Unfortunately, the OS is CentOS, which has no official method to do
it except for 'reinstall'. This of course is unacceptable for us, we need an automatic
system that can do it remotely. We have developed such a thing, mostly based
on RedHat's official tool, &lt;code&gt;redhat-upgrade-tool&lt;/code&gt; (&lt;code&gt;rut&lt;/code&gt;). There are plenty of
tutorials on how to do it on the web.&lt;/p&gt;
&lt;p&gt;All was fine until we hit clients using UEFI instead of BIOS&lt;sup id="fnref:1"&gt;&lt;a class="footnote-ref" href="https://www.grulic.org.ar/~mdione/glob/posts/remotely-upgrading-centos6-centos7/#fn:1"&gt;1&lt;/a&gt;&lt;/sup&gt;. Unluckily &lt;code&gt;rut&lt;/code&gt;
does not seem to handle the &lt;code&gt;grub&lt;/code&gt; to &lt;code&gt;grub2&lt;/code&gt; transition wery well in this case.
I had to create
a dummy &lt;code&gt;RPM&lt;/code&gt; package that, if it detects that the machine boots via EFI,
generates a &lt;code&gt;grub2&lt;/code&gt; config file in &lt;code&gt;/boot/efi/EFI/centos/grub.cfg&lt;/code&gt; and runs
&lt;code&gt;efibootmgr&lt;/code&gt; to tell the EFI system to use the EFI partition and file to boot.
Here's the whole &lt;code&gt;%posttrans&lt;/code&gt; section:&lt;/p&gt;
&lt;div class="code"&gt;&lt;pre class="code literal-block"&gt;&lt;span class="o"&gt;(&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nb"&gt;set&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;-x

&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nv"&gt;efi_parition&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="k"&gt;$(&lt;/span&gt;mount&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;|&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;awk&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s1"&gt;'$3 == "/boot/efi" { print $1 }'&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;|&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;cut&lt;span class="w"&gt; &lt;/span&gt;-d&lt;span class="w"&gt; &lt;/span&gt;/&lt;span class="w"&gt; &lt;/span&gt;-f&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="m"&gt;3&lt;/span&gt;&lt;span class="k"&gt;)&lt;/span&gt;

&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;-n&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="si"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;efi_parition&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;then&lt;/span&gt;
&lt;span class="w"&gt;        &lt;/span&gt;&lt;span class="c1"&gt;# create grub2 config file for the EFI module&lt;/span&gt;
&lt;span class="w"&gt;        &lt;/span&gt;mkdir&lt;span class="w"&gt; &lt;/span&gt;-pv&lt;span class="w"&gt; &lt;/span&gt;/boot/efi/EFI/centos
&lt;span class="w"&gt;        &lt;/span&gt;grub2-mkconfig&lt;span class="w"&gt; &lt;/span&gt;-o&lt;span class="w"&gt; &lt;/span&gt;/boot/efi/EFI/centos/grub.cfg

&lt;span class="w"&gt;        &lt;/span&gt;&lt;span class="c1"&gt;# it's a shame we have to this here and then efibootmgr it's going to do the inverse&lt;/span&gt;
&lt;span class="w"&gt;        &lt;/span&gt;&lt;span class="c1"&gt;# convert /boot/efi -&amp;gt; /dev/fooX -&amp;gt; foo + X&lt;/span&gt;
&lt;span class="w"&gt;        &lt;/span&gt;&lt;span class="nv"&gt;efi_disk&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="k"&gt;$(&lt;/span&gt;find&lt;span class="w"&gt; &lt;/span&gt;-H&lt;span class="w"&gt; &lt;/span&gt;/sys/block/*&lt;span class="w"&gt; &lt;/span&gt;-name&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="si"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;efi_parition&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;|&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;cut&lt;span class="w"&gt; &lt;/span&gt;-d&lt;span class="w"&gt; &lt;/span&gt;/&lt;span class="w"&gt; &lt;/span&gt;-f&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="m"&gt;4&lt;/span&gt;&lt;span class="k"&gt;)&lt;/span&gt;
&lt;span class="w"&gt;        &lt;/span&gt;&lt;span class="c1"&gt;# we can't just cut the last char (part number could be beyond 9)&lt;/span&gt;
&lt;span class="w"&gt;        &lt;/span&gt;&lt;span class="c1"&gt;# and we can't assume it's just after the disk name (disk: nvme0n1, partition: nvme0n1p1, part_num: 1)&lt;/span&gt;
&lt;span class="w"&gt;        &lt;/span&gt;&lt;span class="nv"&gt;efi_parition_number&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="k"&gt;$(&lt;/span&gt;&lt;span class="nb"&gt;echo&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="si"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;efi_parition&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;|&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;egrep&lt;span class="w"&gt; &lt;/span&gt;-o&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s1"&gt;'[0-9]+$'&lt;/span&gt;&lt;span class="k"&gt;)&lt;/span&gt;

&lt;span class="w"&gt;        &lt;/span&gt;&lt;span class="c1"&gt;# create an entry in the EFI boot manager and make it the only one to boot&lt;/span&gt;
&lt;span class="w"&gt;        &lt;/span&gt;efibootmgr&lt;span class="w"&gt; &lt;/span&gt;--create&lt;span class="w"&gt; &lt;/span&gt;--bootnum&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="m"&gt;0123&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;--label&lt;span class="w"&gt; &lt;/span&gt;grub2&lt;span class="w"&gt; &lt;/span&gt;--disk&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"/dev/&lt;/span&gt;&lt;span class="si"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;efi_disk&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="se"&gt;\&lt;/span&gt;
&lt;span class="w"&gt;            &lt;/span&gt;--part&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="si"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;efi_parition_number&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;--loader&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="se"&gt;\\&lt;/span&gt;EFI&lt;span class="se"&gt;\\&lt;/span&gt;centos&lt;span class="se"&gt;\\&lt;/span&gt;grubx64.efi&lt;span class="w"&gt; &lt;/span&gt;--bootorder&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="m"&gt;0123&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="k"&gt;fi&lt;/span&gt;
&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;&amp;amp;&lt;/span&gt;&amp;gt;&lt;span class="w"&gt; &lt;/span&gt;/tmp/upgrade-centos-6.9-7.2.log
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Another part of the automatic upgrade script detects EFI, installs &lt;code&gt;grub2-efi&lt;/code&gt;
and that other RPM, so the code is executed during the actual process of upgrading
to CentOS7.&lt;/p&gt;
&lt;p&gt;But the gist of this post is about how did I manage to test such a thing.
Luckily we had 3 Lenovo servers laying around, but I could only reach them via
&lt;code&gt;ssh&lt;/code&gt; and IPMI. If you don't know, BMC/IPMI is a 'second' computer in your
computer with which, among many, many other things, you can remotely turn on/off
your computer, access the console remotely and even upload an ISO image and mount
it in the 'main' machine as if it was a USB CD-ROM disk. This last one will come handy later.
Notice that this is the system in the middle of
&lt;a href="https://www.bloomberg.com/news/features/2018-10-04/the-big-hack-how-china-used-a-tiny-chip-to-infiltrate-america-s-top-companies"&gt;Bloomberg's article about China infiltrating into companies&lt;/a&gt;,
so you better get acquainted to it.&lt;/p&gt;
&lt;p&gt;These Lenovo machines already had CentOS7 installed, so the first step was to
install CentOS6. This should be possible by copying a CentOS6's installer and
booting it via &lt;code&gt;grub2&lt;/code&gt;. But before that I also had to enable UEFI boot. This
broke booting, because the existing system had &lt;code&gt;grbu2&lt;/code&gt; installed in a disk's MBR
instead on a EFI partition, and the disk had DOS partitions instead of GPT ones.&lt;/p&gt;
&lt;p&gt;So the steps I took to fix this were:&lt;/p&gt;
&lt;p&gt;On The client machine:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Install the &lt;code&gt;icedtea-plugin&lt;/code&gt; (the remote console is a java applet).&lt;/li&gt;
&lt;li&gt;Download &lt;a href="https://www.supergrubdisk.org/super-grub2-disk/"&gt;Super Grub2 Disk&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;Connect via IPMI to the server, request the remote console.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;On the remote server (all this through the IMPI remote console):&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Login, download
  &lt;a href="http://mirror.nsc.liu.se/centos-store/6.9/isos/x86_64/CentOS-6.9-x86_64-minimal.iso"&gt;CentOS6 installer iso&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;Tell the virtual device manager to add an ISO, using the SG2D ISO.&lt;/li&gt;
&lt;li&gt;Reboot the machine.&lt;/li&gt;
&lt;li&gt;Get into the Setup (another EFI module), enable EFI boot mode, reboot.&lt;/li&gt;
&lt;li&gt;Start the Boot Selection Menu, boot from the virtual CD.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;On SG2D/Grub2's interface:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Enable GRUB2's RAID and LVM support (the CentOS6 install ISO was in a LVM part).&lt;/li&gt;
&lt;li&gt;Selected &lt;code&gt;Print the devices/partitions&lt;/code&gt; to find my LVM part.&lt;/li&gt;
&lt;li&gt;Loosely followed &lt;a href="https://www.grulic.org.ar/~mdione/glob/posts/remotely-upgrading-centos6-centos7/"&gt;these instructions&lt;/a&gt; to boot from the ISO image:&lt;/li&gt;
&lt;li&gt;Open the Grub2 prompt (Press &lt;code&gt;c&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;&lt;code&gt;loopback loop (lvm/vg0-root)/root/CentOS-6.9-x86_64-minimal.iso&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;linux (loop)/images/pxeboot/vmlinuz&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;linux (loop)/images/pxeboot/initrd.img&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;boot&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;This boots the installer, but it's not able to find the source media, so install
from a URL. After setting up TCP/IP (fixed in my case), I used the URL
&lt;code&gt;http://mirrors.sonic.net/centos/6/os/x86_64/&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Freebie: &lt;a href="https://www.happyassassin.net/2014/01/25/uefi-boot-how-does-that-actually-work-then/"&gt;More than I ever wanted to know about UEFI&lt;/a&gt;.&lt;/p&gt;
&lt;div class="footnote"&gt;
&lt;hr&gt;
&lt;ol&gt;
&lt;li id="fn:1"&gt;
&lt;p&gt;In UEFI systems, the BIOS/Legacy mode is just an EFI module that provides
  the legacy boot method; that is, boots from a disk's MBR. &lt;a class="footnote-backref" href="https://www.grulic.org.ar/~mdione/glob/posts/remotely-upgrading-centos6-centos7/#fnref:1" title="Jump back to footnote 1 in the text"&gt;↩&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;</description><category>centos</category><category>grub2</category><category>impi</category><category>sysadmin</category><category>uefi</category><guid>https://www.grulic.org.ar/~mdione/glob/posts/remotely-upgrading-centos6-centos7/</guid><pubDate>Wed, 20 Feb 2019 08:05:52 GMT</pubDate></item></channel></rss>