From 69bc0ffe447a812e42d2116a704e83e885695d3d Mon Sep 17 00:00:00 2001 From: BinbinLee Date: Sat, 6 May 2023 13:44:52 +0800 Subject: [PATCH] add wic ln command to create a symbolic link to a file or directory in ext* partition Signed-off-by: BinbinLee --- scripts/lib/wic/engine.py | 24 +++++++++++++++++++++ scripts/lib/wic/help.py | 44 +++++++++++++++++++++++++++++++++++++++ scripts/wic | 26 +++++++++++++++++++++++ 3 files changed, 94 insertions(+) diff --git a/scripts/lib/wic/engine.py b/scripts/lib/wic/engine.py index 674ccfc2441..db566bd9841 100644 --- a/scripts/lib/wic/engine.py +++ b/scripts/lib/wic/engine.py @@ -354,6 +354,23 @@ def copy(self, src, dest): exec_cmd(cmd, as_shell=True) self._put_part_image(pnum) + def link(self, src, dest): + """Create a symbolic link to a file in wic image.""" + pnum = dest.part + + if pnum not in self.partitions: + raise WicError("Partition %s is not in the image" % pnum) + + if self.partitions[pnum].fstype.startswith('ext'): + cmd = "printf 'cd {}\nsymlink {} {}\n' | {} -w {}".\ + format(os.path.dirname(dest.path), os.path.basename(src), + src, self.debugfs, self._get_part_image(pnum)) + else: # fat + print("Not support the fat file system!") + + exec_cmd(cmd, as_shell=True) + self._put_part_image(pnum) + def remove_ext(self, pnum, path, recursive): """ Remove files/dirs and their contents from the partition. @@ -579,6 +596,13 @@ def wic_cp(args, native_sysroot): disk = Disk(args.dest.image, native_sysroot) disk.copy(args.src, args.dest) +def wic_ln(args, native_sysroot): + """ + Create file's Symbolic link in ext partition of + partitioned image. + """ + disk = Disk(args.dest.image, native_sysroot) + disk.link(args.src, args.dest) def wic_rm(args, native_sysroot): """ diff --git a/scripts/lib/wic/help.py b/scripts/lib/wic/help.py index 73e3380cde1..e50c163e53f 100644 --- a/scripts/lib/wic/help.py +++ b/scripts/lib/wic/help.py @@ -274,6 +274,49 @@ def invoke_subcommand(args, parser, main_command_usage, subcommands): details. """ +wic_ln_usage = """ + + Create a symboilic link to a file or dir in partitioned image + + usage: wic ln : + + This command a symbolic link to a file or dir in ext* partitions. + + See 'wic help ls' for more detailed instructions. + +""" + +wic_ln_help = """ + +NAME + wic ln - Create a symboilic link to a file or dir in partitioned image + +SYNOPSIS + wic ln : + wic ln : --native-sysroot + +DESCRIPTION + This command a symbolic link to a file or dir in ext* partitions. + + For example: + $ wic ls test.rootfs.wic:2/bin/ | grep sdbus + + $ wic ls test.rootfs.wic:2/usr/bin/ | grep sdbus + 6224 100775 (1) 0 0 61536 30-Mar-2023 14:45 sdbus-service + 6255 100775 (1) 0 0 42352 29-Mar-2023 13:42 sdbus-client + 6269 100775 (1) 0 0 19848 29-Mar-2023 10:52 sdbus-demo + + To create a symbolic link + $ wic ln /usr/bin/sdbus-service test.rootfs.wic:2/bin/ + + $ wic ls test.rootfs.wic:2/bin/ | grep sdbus + 6254 120777 (7) 0 0 22 6-May-2023 10:44 sdbus-service + + The -n option is used to specify the path to the native sysroot + containing the tools(parted and mtools) to use. + +""" + wic_ls_usage = """ List content of a partitioned image @@ -1108,6 +1151,7 @@ class (see the SourcePlugin source for details): list - List available canned images and source plugins ls - List contents of partitioned image or partition + ln - Create a symbolic link to a file in ext* partitions rm - Remove files or directories from the vfat or ext* partitions help - Show help for a wic COMMAND or TOPIC write - Write an image to a device diff --git a/scripts/wic b/scripts/wic index 06e0b48db07..c626ab747be 100755 --- a/scripts/wic +++ b/scripts/wic @@ -231,6 +231,12 @@ def wic_list_subcommand(args, usage_str): if not engine.wic_list(args, scripts_path): raise WicError("Bad list arguments, exiting") +def wic_ln_subcommand(args, usage_str): + """ + Command-line handling for creating a symbilic link to a file/dir in images. + The real work is done by engine.wic_ln() + """ + engine.wic_ln(args, args.native_sysroot) def wic_ls_subcommand(args, usage_str): """ @@ -292,6 +298,9 @@ helptopics = { "create": [wic_help_topic_subcommand, wic_help_topic_usage, hlp.wic_create_help], + "ln": [wic_help_topic_subcommand, + wic_help_topic_usage, + hlp.wic_ln_help], "ls": [wic_help_topic_subcommand, wic_help_topic_usage, hlp.wic_ls_help], @@ -391,6 +400,14 @@ def wic_init_parser_ls(subparser): subparser.add_argument("-n", "--native-sysroot", help="path to the native sysroot containing the tools") +def wic_init_parser_ln(subparser): + subparser.add_argument("src", + help="") + subparser.add_argument("dest", + help="image spec: :[]") + subparser.add_argument("-n", "--native-sysroot", + help="path to the native sysroot containing the tools") + def imgpathtype(arg): img = imgtype(arg) if img.part is None: @@ -471,6 +488,10 @@ subcommands = { hlp.wic_list_usage, hlp.wic_list_help, wic_init_parser_list], + "ln": [wic_ln_subcommand, + hlp.wic_ln_usage, + hlp.wic_ln_help, + wic_init_parser_ln], "ls": [wic_ls_subcommand, hlp.wic_ls_usage, hlp.wic_ls_help, @@ -538,6 +559,11 @@ def main(argv): args.src = imgtype(args.src) else: raise argparse.ArgumentTypeError("no image or partition number specified.") + elif args.command == "ln": + if ":" in args.dest: + args.dest = imgtype(args.dest) + else: + raise argparse.ArgumentTypeError("no image or partition number specified.") return hlp.invoke_subcommand(args, parser, hlp.wic_help_usage, subcommands)