[BUG] Preinstall script won't run before INSTALLING RAW IMAGE

1,791 views
Skip to first unread message

kelvi...@gmail.com

unread,
Jun 7, 2018, 2:06:28 PM6/7/18
to swupdate
Tested with a raw image with "installed-directly", and discovered that either "<script> preinst" or "preinstall" script won't run until after END INSTALLING STREAMING:

Partial log:
...
[INFO ] : SWUPDATE running : [download_info] : Received : 263634944 / 268440064
[INFO ] : SWUPDATE running : [download_info] : Received : 268353536 / 268440064
[TRACE] : SWUPDATE running : [extract_files] : END INSTALLING STREAMING
[TRACE] : SWUPDATE running : [network_initializer] : Valid image found: copying to FLASH
[INFO ] : SWUPDATE running : Installation in progress
...
[TRACE] : SWUPDATE running : [execute_shell_script] : Calling shell script /tmp/scripts/prewrite.sh : return with 0
...
[TRACE] : SWUPDATE running : [execute_shell_script] : Calling shell script /tmp/scripts/postwrite.sh : return with 0
[INFO ] : SWUPDATE successful ! SWUPDATE successful !
...

On sw-description:

scripts: (
{
filename = "prewrite.sh";
type = "preinstall";
},
{
filename = "postwrite.sh";
type = "postinstall";
}
);
images: (
{
filename = "rootfs.ext4"
device = "/dev/update";
installed-directly = true;
type = "raw";
}

Stefano Babic

unread,
Jun 7, 2018, 4:30:46 PM6/7/18
to kelvi...@gmail.com, swupdate
Hi Kelvin,

On 07/06/2018 20:06, kelvi...@gmail.com wrote:
> Tested with a raw image with "installed-directly", and discovered that either "<script> preinst" or "preinstall" script won't run until after END INSTALLING STREAMING:

Well, it works exactly as it was thought.

SWUpdate does not force any order how the images / files / scripts are
packed into the SWU. But if you use "installed-directly", the image is
streamed and installed at once - that forces an order because scripts
should packed before the rest.

Instead of doing this, the solution is to use the embedded LUA script -
see documentation. The script is embedded into sw-description and you
can set hooks to decide when it runs. It is *always* run before any
image is installed.
Best regards,
Stefano Babic

--
=====================================================================
DENX Software Engineering GmbH, Managing Director: Wolfgang Denk
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: +49-8142-66989-53 Fax: +49-8142-66989-80 Email: sba...@denx.de
=====================================================================

kelvi...@gmail.com

unread,
Jun 21, 2018, 1:41:02 PM6/21/18
to swupdate
On Thursday, June 7, 2018 at 4:30:46 PM UTC-4, Stefano Babic wrote:
>
> SWUpdate does not force any order how the images / files / scripts are
> packed into the SWU. But if you use "installed-directly", the image is
> streamed and installed at once - that forces an order because scripts
> should packed before the rest.
>
> Instead of doing this, the solution is to use the embedded LUA script -
> see documentation. The script is embedded into sw-description and you
> can set hooks to decide when it runs. It is *always* run before any
> image is installed.
>

I have trouble embedded lua script. The documentation says the syntax

embedded-scripts = "<lua code>"

So I assume the embedded-scripts node is to be inside the "images: (...)" node. I put the lua functions inside the double quotes as instructed by the documentation.

While the parser doesn't complain about the embedded lua code, however I cannot seem to be able to run the function inside. I modified the emmcpart.lua code from the official SWUpdate source code, and put the os.capture() and preinst() functions in there, replacing double-quotes with single quote.

My question:
(1) I want to run preinst() before the image is raw-streamed to the mmc, can you provide a pseudo code on how I should do that?
(2) Can I include the emmcpart.lua file in the "file: ()" node, and expect to run a function from emmcpart.lua through a hook? If so, please provide a pseudo code as example.

Thanks,
Kelvin

kelvi...@gmail.com

unread,
Jun 21, 2018, 2:11:20 PM6/21/18
to swupdate
> My question:
> (1) I want to run preinst() before the image is raw-streamed to the mmc, can you provide a pseudo code on how I should do that?
> (2) Can I include the emmcpart.lua file in the "file: ()" node, and expect to run a function from emmcpart.lua through a hook? If so, please provide a pseudo code as example.
>

Looks like I have figured out (1). embedded-scripts= "<lua code>" section is placed under software = {} root section. I also had to escape "\" character with another "\" to pacify the parser. I added a hook="<function>" to the images attribute

software =
{
version = "0.0.2";

embedded-script = "
function os.capture(cmd, raw)
local f = assert(io.popen(cmd, 'r'))
local s = assert(f:read('*a'))
f:close()
if (raw) then return s end
s = string.gsub(s, '^%s+', '')
s = string.gsub(s, '%s+$', '')
s = string.gsub(s, '[\\n\\r]+', ' ')
return s
end

function preinst()
local cmdline = os.capture('cat /proc/cmdline', 1)
local bootmmcpart = string.sub(string.match(cmdline, 'mmcblk0p%d'), 9)
print('bootmmcpart = ' .. bootmmcpart)
local upgrademmcpart = bootmmcpart
if bootmmcpart == '1' then
upgrademmcpart = '2'
else
upgrademmcpart = '1'
end
print('upgrademmcpart = ' .. upgrademmcpart)
cmd = 'ln -s /dev/mmcblk0p' .. upgrademmcpart .. ' /dev/mmcpart_4_upgrade'
print('Setting symbolic link: ' .. cmd)
os.capture(cmd, 1)
cmd = 'fw_setenv mmcpart ' .. upgrademmcpart
print('Setting u-boot environment variable: ' .. cmd)
os.capture(cmd, 1)

return true, bootmmcpart
end
";

Controller = {

hardware-compatibility: [ "0.1" ];
images: (
{
filename = "boot.vfat";
device = "/dev/mmcpart_4_upgrade";


installed-directly = true;
type = "raw";

hook = "preinst"
sha256 = "f008f5a78f4d372f9a2812b0c4328237b5a9b483a6b403cac5c712c44fe2a0db";
}
);

};
}

Stefano Babic

unread,
Jun 22, 2018, 2:02:22 AM6/22/18
to kelvi...@gmail.com, swupdate

kelvi...@gmail.com

unread,
Jun 26, 2018, 10:26:56 AM6/26/18
to swupdate
Hi Stefano,

Is there a way to embed a postinst() LUA function, so SWUpdate will call it automatically at post-installation of images and files?

I have only been able to call an embedded LUA function as a hook to an image or file.

Kelvin

Stefano Babic

unread,
Jun 26, 2018, 1:01:11 PM6/26/18
to kelvi...@gmail.com, swupdate
On 26/06/2018 16:26, kelvi...@gmail.com wrote:
> Hi Stefano,
>
> Is there a way to embed a postinst() LUA function, so SWUpdate will call it automatically at post-installation of images and files?

No

The post- scripts are global, that means they are called after
installing all files and images.

>
> I have only been able to call an embedded LUA function as a hook to an image or file.
>

Kelvin Kwong

unread,
Jun 26, 2018, 2:43:51 PM6/26/18
to Stefano Babic, swupdate
This is not what I discovered. In fact I was hoping that the embedded LUA functions were actually global so preinst() and postinst() are run before installation and after installation.

Based on my findings for raw-streaming,

1. the only way to run preinst() is through hooks, and
2. postinst() can only be automatically run as an external lua script.

Kelvin

Stefano Babic

unread,
Jun 27, 2018, 4:24:22 AM6/27/18
to Kelvin Kwong, Stefano Babic, swupdate
Hi Kevin,

On 26/06/2018 20:43, Kelvin Kwong wrote:
> This is not what I discovered. In fact I was hoping that the embedded
> LUA functions were actually global so preinst() and postinst() are run
> before installation and after installation.
>
> Based on my findings for raw-streaming,
>

This is exactly how it can work. If you are using streaming, the
pre-install scripts must be available before installing an image, and
this means that an order (scripts first, then images) is mandatory. The
only rule in SWUpdate is sw-description first, and this explain why the
only way to have pre- install scripts in streaming mode is to embed them
in sw-description.

post- install scripts run then at the end of the install.

> 1. the only way to run preinst() is through hooks, and
> 2. postinst() can only be automatically run as an external lua script.
>

Best regards,
Stefano Babic
>
> On Tue, Jun 26, 2018 at 1:01 PM Stefano Babic <sba...@denx.de
> <mailto:sba...@denx.de>> wrote:
>
> On 26/06/2018 16:26, kelvi...@gmail.com
> <mailto:kelvi...@gmail.com> wrote:
> > Hi Stefano,
> >
> > Is there a way to embed a postinst() LUA function, so SWUpdate
> will call it automatically at post-installation of images and files?
>
> No
>
> The post- scripts are global, that means they are called after
> installing all files and images.
>
> >
> > I have only been able to call an embedded LUA function as a hook
> to an image or file.
> >
>
> Best regards,
> Stefano Babic
>
>
> --
> =====================================================================
> DENX Software Engineering GmbH,      Managing Director: Wolfgang Denk
> HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
> Phone: +49-8142-66989-53 <tel:+49%208142%206698953> Fax:
> +49-8142-66989-80 <tel:+49%208142%206698980> Email: sba...@denx.de
> <mailto:sba...@denx.de>
> =====================================================================
>
> --
> You received this message because you are subscribed to the Google
> Groups "swupdate" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to swupdate+u...@googlegroups.com
> <mailto:swupdate+u...@googlegroups.com>.
> To post to this group, send email to swup...@googlegroups.com
> <mailto:swup...@googlegroups.com>.
> For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages