Skip to contentSkip to conversation

Arrange to upgrade microcode on x86 test hosts.

PATCH Superseded Ian Campbell 3 participants 6 messages Jun 11, 2015 – Jun 12, 2015

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Arrange to upgrade microcode on x86 test hosts.

Both Xen and Linux support extracting a microcode update from an initramfs early during boot. This requires prepending a suitable uncompressed cpio archive containing the necessary files to the initrd.

Xen also supports loading the microcode cpio from any multiboot module, but for in order to allow the possibility of loading on native boots (e.g. for build jobs) we prefer the prepend method.

This patch provides mg-cpu-microcode-update which creates a suitable microcode cpio in the images directory and arranges for it to be added to the host during installation (so it is done before the kernel is installed and initramfs generated etc, saving faff). It also adds "ucode=scan" to the Xen command line when necessary.

The version of initramfs-tools in Wheezy does not yet support prepending things to the initrm, so we use a custom compression command which sneakily does it for us. This could be done better from Jessie onwards.

Note that Linux only supports this from v3.8 onwards, so this doesn't work for Wheezy (which uses v3.2). From Jessie onwards we should benefit from microcode updates even for native (build) jobs.

Tested on both Intel and AMD where it appeared to have the desired effect under Xen. Under native I only tried Wheezy which doesn't support early microcode.

Note that I've not bothered to implement late microcode updates, which would work on v3.2 too, since that would need a different set of files etc and it doesn't seem especially pressing.

---
Upon commit please run mg-cpu-microcode-update and set MicrocodeUpdateAmd64 and MicrocodeUpdateI386 to the resulting file in both production-config and production-config-cambridge (I will copy the binary to the Cambridge location)

---
 Osstest/Debian.pm           | 44 ++++++++++++++++++++++++
 mg-cpu-microcode-update     | 82 +++++++++++++++++++++++++++++++++++++++++++++
 production-config           |  4 +++
 production-config-cambridge |  4 +++
 ts-xen-install              |  7 ++++
 5 files changed, 141 insertions(+)
 create mode 100755 mg-cpu-microcode-update
Osstest/Debian.pm +44 -0
index f09cad6..5b330c7 100644
@@ -626,6 +626,48 @@ chroot /target chown -R \$u.\$u \$h/.ssh
 END
 }
 
+sub preseed_microcode($$)
+{
+    my ($ho,$sfx) = @_;
+    my $prop = "MicrocodeUpdate".ucfirst($r{arch});
+    logm("ucode=$prop $c{$prop}");
+    return unless $c{$prop};
+    my $ucode = get_filecontents("$c{Images}/$c{$prop}");
+    my $cpio_url = create_webfile($ho, "microcode-cpio",
+	sub {
+	    my $f = $c{Images}."/".$c{$prop};
+	    copy($f, $_[0]) or die "Copy $f failed: $!";
+    });
+
+    # The ability to prepend from an initramfs-hook was not added
+    # until Jessie, therefore for Wheezy we use a custom compression
+    # method which sneaks the necessary cpio onto the front.
+    my $gzip_url = create_webfile($ho, "microcode-gzip",<<END);
+#!/bin/bash
+if [ -f /boot/microcode.cpio ]; then
+    cat /boot/microcode.cpio
+fi
+exec gzip
+END
+
+    preseed_hook_installscript($ho, $sfx,
+       '/usr/lib/base-installer.d/','osstest-microcode', <<END);
+#!/bin/sh
+set -ex
+
+mkdir -p /target/boot
+wget -O /target/boot/microcode.cpio $cpio_url
+
+mkdir -p /target/usr/sbin
+wget -O /target/usr/sbin/osstest-initramfs-gzip $gzip_url
+chmod +x /target/usr/sbin/osstest-initramfs-gzip
+
+mkdir -p /target/etc/initramfs-tools/conf.d/
+echo COMPRESS=/usr/sbin/osstest-initramfs-gzip >> \\
+	/target/etc/initramfs-tools/conf.d/osstest-initramfs-gzip.conf
+END
+}
+
 sub preseed_base ($$$$;@) {
     my ($ho,$suite,$sfx,$extra_packages,%xopts) = @_;
 
@@ -941,6 +983,8 @@ d-i partman-auto/expert_recipe string					\\
 
 END
 
+    preseed_microcode($ho,$sfx);
+
     $preseed_file .= preseed_hook_cmds();
 
     if ($ho->{Flags}{'no-di-kernel'}) {
mg-cpu-microcode-update new file +82 -0

82 lines not on this page · raw message

production-config +4 -0
index 3a0b768..449bb58 100644
@@ -87,6 +87,10 @@ TftpPxeTemplatesReal pxelinux.cfg/%ipaddrhex%
 TftpPxeGroup osstest
 TftpDiVersion 2015-01-10
 
+# These should normally be the same. XXX update on commit.
+MicrocodeUpdateAmd64 microcode.x86.YYYY-MM-DD.cpio
+MicrocodeUpdateI386 microcode.x86.YYYY-MM-DD.cpio
+
 XenUsePath /usr/groups/xencore/systems/bin/xenuse
 XenUseUser osstest
 
production-config-cambridge +4 -0
index f32cd84..8517233 100644
@@ -73,6 +73,10 @@ TftpPxeTemplates %ipaddrhex%/pxelinux.cfg
 TftpPxeGroup osstest
 TftpDiVersion 2015-01-10
 
+# These should normally be the same. XXX update on commit.
+MicrocodeUpdateAmd64 microcode.x86.YYYY-MM-DD.cpio
+MicrocodeUpdateI386 microcode.x86.YYYY-MM-DD.cpio
+
 XenUsePath /usr/groups/xencore/systems/bin/xenuse
 XenUseUser osstest
 
ts-xen-install +7 -0
index b2f667f..d55401c 100755
@@ -145,6 +145,13 @@ sub setupboot () {
     if (toolstack($ho)->{Dom0MemFixed}) {
         $xenhopt .= " dom0_mem=512M,max:512M";
     }
+
+    # If /boot/microcode.cpio is present then ts-host-install has
+    # arranged that it will be prepended to the initrd already, all we
+    # need to do is look for it.
+    $xenhopt .= " ucode=scan"
+	if target_file_exists($ho, "/boot/microcode.cpio");
+
     my $append= $r{xen_boot_append};
     $xenhopt .= " $append" if defined $append;
     $append = get_host_property($ho, 'xen-commandline-append', undef);
Signature · 1 line
2.1.4
#1 lore raw
Quoted 29 lines
Quoted 1 line
Quoted 1 line · On 11.06.15 at 12:52, <[email protected]> wrote:

Both Xen and Linux support extracting a microcode update from an initramfs early during boot. This requires prepending a suitable uncompressed cpio archive containing the necessary files to the initrd.

Xen also supports loading the microcode cpio from any multiboot module, but for in order to allow the possibility of loading on native boots (e.g. for build jobs) we prefer the prepend method.

This patch provides mg-cpu-microcode-update which creates a suitable microcode cpio in the images directory and arranges for it to be added to the host during installation (so it is done before the kernel is installed and initramfs generated etc, saving faff). It also adds "ucode=scan" to the Xen command line when necessary.

The version of initramfs-tools in Wheezy does not yet support prepending things to the initrm, so we use a custom compression command which sneakily does it for us. This could be done better from Jessie onwards.

Note that Linux only supports this from v3.8 onwards, so this doesn't work for Wheezy (which uses v3.2). From Jessie onwards we should benefit from microcode updates even for native (build) jobs.

Tested on both Intel and AMD where it appeared to have the desired effect under Xen. Under native I only tried Wheezy which doesn't support early microcode.

Quoted 6 lines

Note that I've not bothered to implement late microcode updates, which would work on v3.2 too, since that would need a different set of files etc and it doesn't seem especially pressing.

Thanks for doing this so quickly, Ian!

Jan

#2 lore raw
Quoted 29 lines · Jan Beulich
Quoted 29 lines
Quoted 1 line
Quoted 1 line · On 11.06.15 at 12:52, <[email protected]> wrote:

Both Xen and Linux support extracting a microcode update from an initramfs early during boot. This requires prepending a suitable uncompressed cpio archive containing the necessary files to the initrd.

Xen also supports loading the microcode cpio from any multiboot module, but for in order to allow the possibility of loading on native boots (e.g. for build jobs) we prefer the prepend method.

This patch provides mg-cpu-microcode-update which creates a suitable microcode cpio in the images directory and arranges for it to be added to the host during installation (so it is done before the kernel is installed and initramfs generated etc, saving faff). It also adds "ucode=scan" to the Xen command line when necessary.

The version of initramfs-tools in Wheezy does not yet support prepending things to the initrm, so we use a custom compression command which sneakily does it for us. This could be done better from Jessie onwards.

Note that Linux only supports this from v3.8 onwards, so this doesn't work for Wheezy (which uses v3.2). From Jessie onwards we should benefit from microcode updates even for native (build) jobs.

Tested on both Intel and AMD where it appeared to have the desired effect under Xen. Under native I only tried Wheezy which doesn't support early microcode.

Quoted 8 lines
Quoted 6 lines

Note that I've not bothered to implement late microcode updates, which would work on v3.2 too, since that would need a different set of files etc and it doesn't seem especially pressing.

Thanks for doing this so quickly, Ian!

No problem, it actually turned out to be easier than I had feared it would be.

Ian.

#3 lore raw
In reply to Ian Campbell

Ian Campbell writes ("[PATCH OSSTEST] Arrange to upgrade microcode on x86 test hosts."):

Quoted 4 lines

Both Xen and Linux support extracting a microcode update from an initramfs early during boot. This requires prepending a suitable uncompressed cpio archive containing the necessary files to the initrd.

Looking good...

Osstest/Debian.pm chroot /target chown -R \$u.\$u \$h/.ssh · line 634

Quoted 6 lines
Osstest/Debian.pm +6 -0
+sub preseed_microcode($$)
+{
+    my ($ho,$sfx) = @_;
+    my $prop = "MicrocodeUpdate".ucfirst($r{arch});
+    logm("ucode=$prop $c{$prop}");
+    return unless $c{$prop};

If $c{prop} is undef, the logm will produce a warning. Maybe you want //'' somewhere.

Osstest/Debian.pm chroot /target chown -R \$u.\$u \$h/.ssh · line 638

Quoted 2 lines
Osstest/Debian.pm +2 -0
+	sub {
+	    my $f = $c{Images}."/".$c{$prop};

This is slighly unidiomatic. I would write "$c{Images}/$c{$prop}". But what you have isn't wrong.

Quoted 6 lines
mg-cpu-microcode-update new file +0 -0
new file mode 100755
index 0000000..dd87838

mg-cpu-microcode-update line 10

Quoted 10 lines
mg-cpu-microcode-update new file +10 -0
@@ -0,0 +1,82 @@
+#!/bin/bash
+
+set -e
+
+. cri-getconfig
+
+images=`getconfig Images`
+date=`date +%Y-%m-%d`
+
+TMP=`mktemp -t -d mg-cpu-microcode-update.XXXXXX`

I think it would be better to use ./tmp (or maybe bits of the destination, the way mg-debian-installer-update does).

mg-cpu-microcode-update line 16

Quoted 5 lines
mg-cpu-microcode-update new file +5 -0
+CPIODIR=cpio.x86
+UCODEPATH=kernel/x86/microcode
+
+INTELBIN=GenuineIntel.bin
+AMDBIN=AuthenticAMD.bin

This use of caps lock for unexported shell variables is rather unidiomatic, don't you think ?

mg-cpu-microcode-update line 18

Quoted 1 line
mg-cpu-microcode-update new file +1 -0
+CPIO=$images/microcode.x86.$date.cpio

Also if you set a variable like CPIO you risk something executing $CPIO rather than /usr/bin/cpio :-).

mg-cpu-microcode-update line 26

Quoted 7 lines
mg-cpu-microcode-update new file +7 -0
+LINUX_FW=https://git.kernel.org/cgit/linux/kernel/git/firmware/linux-firmware.git
+
+if [ -f $CPIO ] ;then
+    echo "error: $CPIO already exists." >&2
+    echo "Refusing to overwrite" >&2
+    exit 1
+fi

mg-debian-installer-update is idempotent. Perhaps this script should be too ? After all you can only overwrite something generated today.

mg-cpu-microcode-update line 50

Quoted 4 lines
mg-cpu-microcode-update new file +4 -0
+tar -C intel-ucode -xaf intel-ucode/microcode.tgz microcode.dat
+
+echo >&2 "Converting Intel ucode"
+/usr/sbin/iucode-tool \

Put sbin on the PATH, rather than hardcoding:

PATH="/usr/local/sbin:$PATH:/sbin:/usr/sbin"

Also we should file a bug complaining that this tool should be in /usr/bin.

mg-cpu-microcode-update line 70

Quoted 3 lines
mg-cpu-microcode-update new file +3 -0
+for BIN in $AMD_BINS ; do
+    curl -s $LINUX_FW/plain/$BIN > $BIN
+done

We're really fetching these via the gitweb/cgit/... ? Isn't that a bit fragile ?

Thanks,
Ian.

#4 lore raw
Quoted 8 lines · Ian Jackson

Ian Campbell writes ("[PATCH OSSTEST] Arrange to upgrade microcode on x86 test hosts."):

Quoted 4 lines

Both Xen and Linux support extracting a microcode update from an initramfs early during boot. This requires prepending a suitable uncompressed cpio archive containing the necessary files to the initrd.

Looking good...

Quoted 9 lines

Osstest/Debian.pm chroot /target chown -R \$u.\$u \$h/.ssh · line 634

Quoted 6 lines
Osstest/Debian.pm +6 -0
+sub preseed_microcode($$)
+{
+    my ($ho,$sfx) = @_;
+    my $prop = "MicrocodeUpdate".ucfirst($r{arch});
+    logm("ucode=$prop $c{$prop}");
+    return unless $c{$prop};

If $c{prop} is undef, the logm will produce a warning. Maybe you want //'' somewhere.

I think that was a stray wip debug thing, I could fix, remove or move it below the check of $c{$prop}.

Quoted 6 lines

Osstest/Debian.pm chroot /target chown -R \$u.\$u \$h/.ssh · line 638

Quoted 3 lines
Osstest/Debian.pm +2 -0
+	sub {
+	    my $f = $c{Images}."/".$c{$prop};

This is slighly unidiomatic. I would write "$c{Images}/$c{$prop}". But what you have isn't wrong.

I had that and then switched, for some reason. I'll go back.

Quoted 11 lines
Quoted 11 lines
mg-cpu-microcode-update new file +5 -0
new file mode 100755
index 0000000..dd87838
@@ -0,0 +1,82 @@
+#!/bin/bash
+
+set -e
+
+. cri-getconfig
Quoted 8 lines

mg-cpu-microcode-update line 10

Quoted 5 lines
mg-cpu-microcode-update new file +5 -0
+
+images=`getconfig Images`
+date=`date +%Y-%m-%d`
+
+TMP=`mktemp -t -d mg-cpu-microcode-update.XXXXXX`

I think it would be better to use ./tmp (or maybe bits of the destination, the way mg-debian-installer-update does).

OK.

Quoted 8 lines

mg-cpu-microcode-update line 16

Quoted 5 lines
mg-cpu-microcode-update new file +5 -0
+CPIODIR=cpio.x86
+UCODEPATH=kernel/x86/microcode
+
+INTELBIN=GenuineIntel.bin
+AMDBIN=AuthenticAMD.bin

This use of caps lock for unexported shell variables is rather unidiomatic, don't you think ?

I'm not really in the habit of caring for short scripts, but I'll down case them.

Quoted 5 lines

mg-cpu-microcode-update line 18

Quoted 2 lines
mg-cpu-microcode-update new file +1 -0
+CPIO=$images/microcode.x86.$date.cpio

Also if you set a variable like CPIO you risk something executing $CPIO rather than /usr/bin/cpio :-).

True. I'll call it UCODECPIO^Wucodecpio or some such.

Quoted 10 lines

mg-cpu-microcode-update line 26

Quoted 7 lines
mg-cpu-microcode-update new file +7 -0
+LINUX_FW=https://git.kernel.org/cgit/linux/kernel/git/firmware/linux-firmware.git
+
+if [ -f $CPIO ] ;then
+    echo "error: $CPIO already exists." >&2
+    echo "Refusing to overwrite" >&2
+    exit 1
+fi

mg-debian-installer-update is idempotent. Perhaps this script should be too ? After all you can only overwrite something generated today.

True. I'll arrange that.

I'll see if I can also make it deterministic like with the installer stuff, which will make spotting differences easier.

Quoted 8 lines

mg-cpu-microcode-update line 50

Quoted 4 lines
mg-cpu-microcode-update new file +4 -0
+tar -C intel-ucode -xaf intel-ucode/microcode.tgz microcode.dat
+
+echo >&2 "Converting Intel ucode"
+/usr/sbin/iucode-tool \

Put sbin on the PATH, rather than hardcoding:

PATH="/usr/local/sbin:$PATH:/sbin:/usr/sbin"

Ack.

Quoted 2 lines

Also we should file a bug complaining that this tool should be in /usr/bin.

I'll do that.

Quoted 6 lines

mg-cpu-microcode-update line 70

Quoted 3 lines
mg-cpu-microcode-update new file +3 -0
+for BIN in $AMD_BINS ; do
+    curl -s $LINUX_FW/plain/$BIN > $BIN
+done

We're really fetching these via the gitweb/cgit/... ? Isn't that a bit fragile ?

A bit, yes. The alternative is to clone the whole of linux-firmware.git. I suppose with --depth=1 that might not be so bad. Would you prefer that?

Ian.

#5 lore raw

Ian Campbell writes ("Re: [PATCH OSSTEST] Arrange to upgrade microcode on x86 test hosts."):

Quoted 6 lines · Ian Jackson
Quoted 2 lines

If $c{prop} is undef, the logm will produce a warning. Maybe you want //'' somewhere.

I think that was a stray wip debug thing, I could fix, remove or move it below the check of $c{$prop}.

Any of those would be fine by me.

Quoted 5 lines
Quoted 2 lines

This use of caps lock for unexported shell variables is rather unidiomatic, don't you think ?

I'm not really in the habit of caring for short scripts, but I'll down case them.

I find it confusing, because I want to know what other process is going to be using them. Thanks.

Quoted 7 lines
Quoted 2 lines

mg-debian-installer-update is idempotent. Perhaps this script should be too ? After all you can only overwrite something generated today.

True. I'll arrange that.

I'll see if I can also make it deterministic like with the installer stuff, which will make spotting differences easier.

Great.

Quoted 10 lines
Quoted 6 lines

mg-cpu-microcode-update line 70

Quoted 3 lines
mg-cpu-microcode-update new file +3 -0
+for BIN in $AMD_BINS ; do
+    curl -s $LINUX_FW/plain/$BIN > $BIN
+done

We're really fetching these via the gitweb/cgit/... ? Isn't that a bit fragile ?

A bit, yes. The alternative is to clone the whole of linux-firmware.git. I suppose with --depth=1 that might not be so bad. Would you prefer that?

Yes.

Will go on to read your v2 just now...

Ian.