[Toybox] [New toy] partprobe
Isaac Dunham
ibid.ag at gmail.com
Fri May 23 10:06:25 PDT 2014
On Fri, May 23, 2014 at 01:53:31PM +0200, Bertold Van den Bergh wrote:
> Hello,
>
> I have attached a patch adding a program that allows re-reading the
> partition table. This is often used on embedded systems booting from SD/USB
> devices that need to resize partitions on first boot.
>
> Sincerely,
> Bertold Van den Bergh
See below for comments.
> diff -r b0a92be71368 toys/pending/partprobe.c
> --- /dev/null Thu Jan 01 00:00:00 1970 +0000
> +++ b/toys/pending/partprobe.c Fri May 23 13:43:46 2014 +0200
> @@ -0,0 +1,51 @@
> +/* partprobe.c - Tell the kernel about partition table changes
> + *
> + * Copyright 2014 Bertold Van den Bergh <vandenbergh at bertold.org>
> + *
> + * see http://man7.org/linux/man-pages/man8/partprobe.8.html
> +
> +USE_PARTPROBE(NEWTOY(partprobe, NULL, TOYFLAG_SBIN))
NULL should be "<1", which means "error if less than 1 file passed".
> +
> +config PARTPROBE
> + bool "partprobe"
> + default n
> + help
> + partprobe - Tell the kernel about partition table changes
> +
> + usage: partprobe [devices...]
Minor stylistic detail:
usage: partprobe DEVICE...
is more the typical representation since [] indicates optional elements
of the command line and user input is all-caps.
> +
> + Asks the kernel to re-read the partition table on the specified
> + devices.
> +*/
> +
> +#include "toys.h"
> +
> +void update_device(char* path)
Here you could easily change it to use loopfiles():
void update_device(int sd_fd, char *path)
> +{
> + int sd_fd = open(path, 0);
> +
> + if(sd_fd < 0){
> + perror_msg("Unable to open %s", path);
> + return;
> + }
> +
And delete everything above here.
> + if(ioctl(sd_fd, BLKRRPART, NULL) < 0)
> + perror_msg("ioctl (BLKRRPART) failed, old layout still used");
> +
> + close(sd_fd);
> +}
> +
> +void partprobe_main(void)
> +{
> + char** opt;
> + if(*toys.optargs == NULL){
> + printf("No devices specified.\n");
> + exit(EXIT_FAILURE);
> + }
> +
Unneeded if you change the option string as indicated (NULL -> "<1").
> + for (opt = toys.optargs; *opt; opt++) {
> + update_device(*opt);
> + }
If you use loopfiles, all of the above becomes:
loopfiles(toys.optargs, update_device);
> +
> + exit(EXIT_SUCCESS);
No need for this; toys.exitval is initialized to 0, and toybox
automatically uses the value of toys.exitval as the return value.
By the way, does partprobe still return success if one or all devices
could be opened but ioctl failed?
> +}
HTH,
Isaac Dunham
1400864785.0
More information about the Toybox
mailing list