iPXE
Data Structures | Functions | Variables
image_cmd.c File Reference

Image management commands. More...

#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <getopt.h>
#include <ipxe/image.h>
#include <ipxe/command.h>
#include <ipxe/parseopt.h>
#include <ipxe/shell.h>
#include <usr/imgmgmt.h>

Go to the source code of this file.

Data Structures

struct  imgsingle_options
 "img{single}" options More...
struct  imgsingle_descriptor
 An "img{single}" family command descriptor. More...
struct  imgmulti_options
 "img{multi}" options More...

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
static int imgsingle_exec (int argc, char **argv, struct imgsingle_descriptor *desc)
 The "img{single}" family of commands.
static int imgfetch_exec (int argc, char **argv)
 The "imgfetch" command.
static int imgselect (struct image *image, struct imgsingle_options *opts __unused)
 "imgselect" command action
static int imgselect_exec (int argc, char **argv)
 The "imgselect" command.
static int imgexec (struct image *image, struct imgsingle_options *opts)
 "imgexec" command action
static int imgexec_exec (int argc, char **argv)
 The "imgexec" command.
static int imgargs_exec (int argc, char **argv)
 The "imgargs" command body.
static int imgmulti_exec (int argc, char **argv, void(*payload)(struct image *image))
 The "img{multi}" family of commands.
static int imgstat_exec (int argc, char **argv)
 The "imgstat" command.
static int imgfree_exec (int argc, char **argv)
 The "imgfree" command.

Variables

union {
   struct option_descriptor   imgexec [4]
   struct option_descriptor   imgsingle [3]
opts
 "img{single}" option list
static struct command_descriptor imgfetch_cmd
 "imgfetch" command descriptor
struct imgsingle_descriptor imgfetch_desc
 "imgfetch" family command descriptor
static struct command_descriptor imgselect_cmd
 "imgselect" command descriptor
struct imgsingle_descriptor imgselect_desc
 "imgselect" family command descriptor
static struct command_descriptor imgexec_cmd
 "imgexec" command descriptor
struct imgsingle_descriptor imgexec_desc
 "imgexec" family command descriptor
static struct command_descriptor imgargs_cmd
 "imgargs" command descriptor
struct imgsingle_descriptor imgargs_desc
 "imgargs" family command descriptor
static struct option_descriptor imgmulti_opts [] = {}
 "img{multi}" option list
static struct command_descriptor imgmulti_cmd
 "img{multi}" command descriptor
struct command image_commands[] __command
 Image management commands.

Detailed Description

Image management commands.

Definition in file image_cmd.c.


Function Documentation

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )
static int imgsingle_exec ( int  argc,
char **  argv,
struct imgsingle_descriptor desc 
) [static]

The "img{single}" family of commands.

Parameters:
argcArgument count
argvArgument list
desc"img{single}" command descriptor
action_nameAction name (for error messages)
actionAction to take upon image
Return values:
rcReturn status code

Definition at line 102 of file image_cmd.c.

References imgsingle_descriptor::acquire, imgsingle_descriptor::action, imgsingle_options::autofree, imgsingle_descriptor::cmd, cmdline, concat_args(), ENOMEM, image::flags, free, IMAGE_AUTO_UNREGISTER, image_find_selected(), image_set_cmdline(), image_set_name(), imgsingle_options::name, NULL, optind, parse_options(), imgsingle_descriptor::preaction, printf(), rc, strerror(), imgsingle_options::timeout, and imgsingle_descriptor::verb.

Referenced by imgargs_exec(), imgexec_exec(), imgfetch_exec(), and imgselect_exec().

                                                                {
        struct imgsingle_options opts;
        char *name_uri = NULL;
        char *cmdline = NULL;
        struct image *image;
        int rc;

        /* Parse options */
        if ( ( rc = parse_options ( argc, argv, desc->cmd, &opts ) ) != 0 )
                goto err_parse_options;

        /* Parse name/URI string and command line, if present */
        if ( optind < argc ) {
                name_uri = argv[optind];
                if ( argv[ optind + 1 ] != NULL ) {
                        cmdline = concat_args ( &argv[ optind + 1 ] );
                        if ( ! cmdline ) {
                                rc = -ENOMEM;
                                goto err_parse_cmdline;
                        }
                }
        }

        /* Acquire the image */
        if ( name_uri ) {
                if ( ( rc = desc->acquire ( name_uri, opts.timeout,
                                            &image ) ) != 0 )
                        goto err_acquire;
        } else {
                image = image_find_selected();
                if ( ! image ) {
                        printf ( "No image selected\n" );
                        goto err_acquire;
                }
        }

        /* Carry out command pre-action, if applicable */
        if ( desc->preaction )
                desc->preaction ( image );

        /* Set the image name, if applicable */
        if ( opts.name ) {
                if ( ( rc = image_set_name ( image, opts.name ) ) != 0 ) {
                        printf ( "Could not name image: %s\n",
                                 strerror ( rc ) );
                        goto err_set_name;
                }
        }

        /* Set the command-line arguments, if applicable */
        if ( cmdline ) {
                if ( ( rc = image_set_cmdline ( image, cmdline ) ) != 0 ) {
                        printf ( "Could not set arguments: %s\n",
                                 strerror ( rc ) );
                        goto err_set_cmdline;
                }
        }

        /* Set the auto-unregister flag, if applicable */
        if ( opts.autofree )
                image->flags |= IMAGE_AUTO_UNREGISTER;

        /* Carry out command action, if applicable */
        if ( desc->action ) {
                if ( ( rc = desc->action ( image, &opts ) ) != 0 ) {
                        printf ( "Could not %s: %s\n",
                                 desc->verb, strerror ( rc ) );
                        goto err_action;
                }
        }

        /* Success */
        rc = 0;

 err_action:
 err_set_cmdline:
 err_set_name:
 err_acquire:
        free ( cmdline );
 err_parse_cmdline:
 err_parse_options:
        return rc;
}
static int imgfetch_exec ( int  argc,
char **  argv 
) [static]

The "imgfetch" command.

Parameters:
argcArgument count
argvArgument list
Return values:
rcReturn status code

Definition at line 205 of file image_cmd.c.

References imgsingle_exec().

                                                   {
        return imgsingle_exec ( argc, argv, &imgfetch_desc );
}
static int imgselect ( struct image image,
struct imgsingle_options *opts  __unused 
) [static]

"imgselect" command action

Parameters:
imageImage
optsOptions
Return values:
rcReturn status code

Definition at line 216 of file image_cmd.c.

References image_select().

                                                                 {
        return image_select ( image );
}
static int imgselect_exec ( int  argc,
char **  argv 
) [static]

The "imgselect" command.

Parameters:
argcArgument count
argvArgument list
Return values:
rcReturn status code

Definition at line 241 of file image_cmd.c.

References imgsingle_exec().

                                                    {
        return imgsingle_exec ( argc, argv, &imgselect_desc );
}
static int imgexec ( struct image image,
struct imgsingle_options opts 
) [static]

"imgexec" command action

Parameters:
imageImage
optsOptions
Return values:
rcReturn status code

Definition at line 257 of file image_cmd.c.

References image_exec(), image_replace(), rc, imgsingle_options::replace, shell_stop(), and SHELL_STOP_COMMAND_SEQUENCE.

                                                                           {
        int rc;

        /* Perform replacement or execution as applicable */
        if ( opts->replace ) {

                /* Try to replace image */
                if ( ( rc = image_replace ( image ) ) != 0 )
                        return rc;

                /* Stop script and tail-recurse into replacement image */
                shell_stop ( SHELL_STOP_COMMAND_SEQUENCE );

        } else {

                /* Try to execute image */
                if ( ( rc = image_exec ( image ) ) != 0 )
                        return rc;
        }

        return 0;
}
static int imgexec_exec ( int  argc,
char **  argv 
) [static]

The "imgexec" command.

Parameters:
argcArgument count
argvArgument list
Return values:
rcReturn status code

Definition at line 295 of file image_cmd.c.

References imgsingle_exec().

                                                 {
        return imgsingle_exec ( argc, argv, &imgexec_desc );
}
static int imgargs_exec ( int  argc,
char **  argv 
) [static]

The "imgargs" command body.

Parameters:
argcArgument count
argvArgument list
Return values:
rcReturn status code

Definition at line 318 of file image_cmd.c.

References imgsingle_exec().

                                                  {
        return imgsingle_exec ( argc, argv, &imgargs_desc );
}
static int imgmulti_exec ( int  argc,
char **  argv,
void(*)(struct image *image payload 
) [static]

The "img{multi}" family of commands.

Parameters:
argcArgument count
argvArgument list
payloadFunction to execute on each image
Return values:
rcReturn status code

Definition at line 341 of file image_cmd.c.

References ENOENT, find_image(), for_each_image_safe, optind, parse_options(), printf(), and rc.

Referenced by imgfree_exec(), and imgstat_exec().

                                                                        {
        struct imgmulti_options opts;
        struct image *image;
        struct image *tmp;
        int i;
        int rc;

        /* Parse options */
        if ( ( rc = parse_options ( argc, argv, &imgmulti_cmd, &opts ) ) != 0 )
                return rc;

        /* If no images are explicitly specified, process all images */
        if ( optind == argc ) {
                for_each_image_safe ( image, tmp )
                        payload ( image );
                return 0;
        }

        /* Otherwise, process specified images */
        for ( i = optind ; i < argc ; i++ ) {
                image = find_image ( argv[i] );
                if ( ! image ) {
                        printf ( "\"%s\": no such image\n", argv[i] );
                        return -ENOENT;
                }
                payload ( image );
        }

        return 0;
}
static int imgstat_exec ( int  argc,
char **  argv 
) [static]

The "imgstat" command.

Parameters:
argcArgument count
argvArgument list
Return values:
rcReturn status code

Definition at line 380 of file image_cmd.c.

References imgmulti_exec(), and imgstat().

                                                  {
        return imgmulti_exec ( argc, argv, imgstat );
}
static int imgfree_exec ( int  argc,
char **  argv 
) [static]

The "imgfree" command.

Parameters:
argcArgument count
argvArgument list
Return values:
rcReturn status code

Definition at line 391 of file image_cmd.c.

References imgmulti_exec(), and unregister_image().

                                                  {
        return imgmulti_exec ( argc, argv, unregister_image );
}

Variable Documentation

Definition at line 58 of file image_cmd.c.

Definition at line 62 of file image_cmd.c.

union { ... } opts [static]

"img{single}" option list

Initial value:
        COMMAND_DESC ( struct imgsingle_options, opts.imgsingle,
                       1, MAX_ARGUMENTS, "<uri> [<arguments>...]" )

"imgfetch" command descriptor

Definition at line 188 of file image_cmd.c.

Initial value:
 {
        .cmd = &imgfetch_cmd,
        .acquire = imgdownload_string,
}

"imgfetch" family command descriptor

Definition at line 193 of file image_cmd.c.

Initial value:
        COMMAND_DESC ( struct imgsingle_options, opts.imgsingle,
                       1, MAX_ARGUMENTS, "<uri|image> [<arguments>...]" )

"imgselect" command descriptor

Definition at line 222 of file image_cmd.c.

Initial value:
 {
        .cmd = &imgselect_cmd,
        .acquire = imgacquire,
        .action = imgselect,
        .verb = "select",
}

"imgselect" family command descriptor

Definition at line 227 of file image_cmd.c.

Initial value:
        COMMAND_DESC ( struct imgsingle_options, opts.imgexec,
                       0, MAX_ARGUMENTS, "[<uri|image> [<arguments>...]]" )

"imgexec" command descriptor

Definition at line 246 of file image_cmd.c.

Initial value:
 {
        .cmd = &imgexec_cmd,
        .acquire = imgacquire,
        .action = imgexec,
        .verb = "boot",
}

"imgexec" family command descriptor

Definition at line 281 of file image_cmd.c.

Initial value:
        COMMAND_DESC ( struct imgsingle_options, opts.imgsingle,
                       1, MAX_ARGUMENTS, "<uri|image> [<arguments>...]" )

"imgargs" command descriptor

Definition at line 300 of file image_cmd.c.

Initial value:
 {
        .cmd = &imgargs_cmd,
        .acquire = imgacquire,
        .preaction = image_clear_cmdline,
}

"imgargs" family command descriptor

Definition at line 305 of file image_cmd.c.

struct option_descriptor imgmulti_opts[] = {} [static]

"img{multi}" option list

Definition at line 326 of file image_cmd.c.

Initial value:
        COMMAND_DESC ( struct imgmulti_options, imgmulti_opts, 0, MAX_ARGUMENTS,
                       "[<image>...]" )

"img{multi}" command descriptor

Definition at line 329 of file image_cmd.c.

struct command image_commands [] __command

Image management commands.

Definition at line 396 of file image_cmd.c.