How to use xmlstarlet sel to list entries in complex order












0















I'm trying to use xmlstarlet sel to list the disk partitions I need to create from an xml file (example : https://github.com/finley/SystemImager/blob/initrd-from-imageserver-and-dont-package-initrd/doc/examples/disk-layout-complex.xml)
Right now I'm doing the following:



local IFS=;
xmlstarlet sel -t -m "config/disk[@dev="${DISK_DEV}"]/part" -v "concat(@num,';',@size,';',@p_type,';',@id,';',@p_name,';',@flags,';',@lvm_group,';',@raid_dev)" -n ${DISKS_LAYOUT_FILE} | sed '/^s*$/d' |
while read P_NUM P_SIZE P_TYPE P_ID P_NAME P_FLAGS P_LVM_GROUP P_RAID_DEV
do # process partitions creation.



The problem is for variable size partition (use 100% ("*" in the file)). If one is listed before some other, there is no room left when it's time to process the last ones. Thus it is not possible to put a primary swap partition at the end of a diwk with a / of variable size.



Is there a clever way to use xmlstarlet sel to list partitions in the following order:



list all primary and extended partition in same order as written in the xml file until a partition with size "*" is seen;



keep this variable size partition in mind;



then list from the end in reverse order the other partitions;



and finally print the variable size partition;



Repeat operation with logical partitions if any.



For all partition, add a field stating if it was listed in order or reverse order so I can know if I have to create partition relative to beginning of free space or relating to end of free space. (variable partition would be tagged as normal order as they would be created starting at beginning of free space)



If more than one "*" sized partition is encountered in a loop; fail with error would be cool.



For the listed example (link above), this would list partitions to create in the following order (and without the header below):



LIST_FLAG P_NUM P_SIZE P_TYPE P_ID P_NAME P_FLAGS P_LVM_GROUP P_RAID_DEV
normal;1;500;primary;;;boot;;
normal;3;4096;primary;;;;;;
reverse;2;1024;primary;;;swap;;
normal;4;*;extended;;;;;;
normal;7;4096;logical;;;;;;
reverse;6;2048;logical;;;;;;
normal;5;*;logical;;;;;;



I'm processing this within a specially crafted initrd, so I only have access to most common utils like sed/grep/bash2/xmlstarlet/awk. no perl, no python, no language that needs libraries in general.



I'm pretty convinced that there is a solution that does most of the job if not all, but I'm far not enough skilled to even evaluate if it can be done this way. I think I can achieve that in pure bash, but this will be far less elegant.










share|improve this question









New contributor




olahaye74 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

























    0















    I'm trying to use xmlstarlet sel to list the disk partitions I need to create from an xml file (example : https://github.com/finley/SystemImager/blob/initrd-from-imageserver-and-dont-package-initrd/doc/examples/disk-layout-complex.xml)
    Right now I'm doing the following:



    local IFS=;
    xmlstarlet sel -t -m "config/disk[@dev="${DISK_DEV}"]/part" -v "concat(@num,';',@size,';',@p_type,';',@id,';',@p_name,';',@flags,';',@lvm_group,';',@raid_dev)" -n ${DISKS_LAYOUT_FILE} | sed '/^s*$/d' |
    while read P_NUM P_SIZE P_TYPE P_ID P_NAME P_FLAGS P_LVM_GROUP P_RAID_DEV
    do # process partitions creation.



    The problem is for variable size partition (use 100% ("*" in the file)). If one is listed before some other, there is no room left when it's time to process the last ones. Thus it is not possible to put a primary swap partition at the end of a diwk with a / of variable size.



    Is there a clever way to use xmlstarlet sel to list partitions in the following order:



    list all primary and extended partition in same order as written in the xml file until a partition with size "*" is seen;



    keep this variable size partition in mind;



    then list from the end in reverse order the other partitions;



    and finally print the variable size partition;



    Repeat operation with logical partitions if any.



    For all partition, add a field stating if it was listed in order or reverse order so I can know if I have to create partition relative to beginning of free space or relating to end of free space. (variable partition would be tagged as normal order as they would be created starting at beginning of free space)



    If more than one "*" sized partition is encountered in a loop; fail with error would be cool.



    For the listed example (link above), this would list partitions to create in the following order (and without the header below):



    LIST_FLAG P_NUM P_SIZE P_TYPE P_ID P_NAME P_FLAGS P_LVM_GROUP P_RAID_DEV
    normal;1;500;primary;;;boot;;
    normal;3;4096;primary;;;;;;
    reverse;2;1024;primary;;;swap;;
    normal;4;*;extended;;;;;;
    normal;7;4096;logical;;;;;;
    reverse;6;2048;logical;;;;;;
    normal;5;*;logical;;;;;;



    I'm processing this within a specially crafted initrd, so I only have access to most common utils like sed/grep/bash2/xmlstarlet/awk. no perl, no python, no language that needs libraries in general.



    I'm pretty convinced that there is a solution that does most of the job if not all, but I'm far not enough skilled to even evaluate if it can be done this way. I think I can achieve that in pure bash, but this will be far less elegant.










    share|improve this question









    New contributor




    olahaye74 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.























      0












      0








      0








      I'm trying to use xmlstarlet sel to list the disk partitions I need to create from an xml file (example : https://github.com/finley/SystemImager/blob/initrd-from-imageserver-and-dont-package-initrd/doc/examples/disk-layout-complex.xml)
      Right now I'm doing the following:



      local IFS=;
      xmlstarlet sel -t -m "config/disk[@dev="${DISK_DEV}"]/part" -v "concat(@num,';',@size,';',@p_type,';',@id,';',@p_name,';',@flags,';',@lvm_group,';',@raid_dev)" -n ${DISKS_LAYOUT_FILE} | sed '/^s*$/d' |
      while read P_NUM P_SIZE P_TYPE P_ID P_NAME P_FLAGS P_LVM_GROUP P_RAID_DEV
      do # process partitions creation.



      The problem is for variable size partition (use 100% ("*" in the file)). If one is listed before some other, there is no room left when it's time to process the last ones. Thus it is not possible to put a primary swap partition at the end of a diwk with a / of variable size.



      Is there a clever way to use xmlstarlet sel to list partitions in the following order:



      list all primary and extended partition in same order as written in the xml file until a partition with size "*" is seen;



      keep this variable size partition in mind;



      then list from the end in reverse order the other partitions;



      and finally print the variable size partition;



      Repeat operation with logical partitions if any.



      For all partition, add a field stating if it was listed in order or reverse order so I can know if I have to create partition relative to beginning of free space or relating to end of free space. (variable partition would be tagged as normal order as they would be created starting at beginning of free space)



      If more than one "*" sized partition is encountered in a loop; fail with error would be cool.



      For the listed example (link above), this would list partitions to create in the following order (and without the header below):



      LIST_FLAG P_NUM P_SIZE P_TYPE P_ID P_NAME P_FLAGS P_LVM_GROUP P_RAID_DEV
      normal;1;500;primary;;;boot;;
      normal;3;4096;primary;;;;;;
      reverse;2;1024;primary;;;swap;;
      normal;4;*;extended;;;;;;
      normal;7;4096;logical;;;;;;
      reverse;6;2048;logical;;;;;;
      normal;5;*;logical;;;;;;



      I'm processing this within a specially crafted initrd, so I only have access to most common utils like sed/grep/bash2/xmlstarlet/awk. no perl, no python, no language that needs libraries in general.



      I'm pretty convinced that there is a solution that does most of the job if not all, but I'm far not enough skilled to even evaluate if it can be done this way. I think I can achieve that in pure bash, but this will be far less elegant.










      share|improve this question









      New contributor




      olahaye74 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.












      I'm trying to use xmlstarlet sel to list the disk partitions I need to create from an xml file (example : https://github.com/finley/SystemImager/blob/initrd-from-imageserver-and-dont-package-initrd/doc/examples/disk-layout-complex.xml)
      Right now I'm doing the following:



      local IFS=;
      xmlstarlet sel -t -m "config/disk[@dev="${DISK_DEV}"]/part" -v "concat(@num,';',@size,';',@p_type,';',@id,';',@p_name,';',@flags,';',@lvm_group,';',@raid_dev)" -n ${DISKS_LAYOUT_FILE} | sed '/^s*$/d' |
      while read P_NUM P_SIZE P_TYPE P_ID P_NAME P_FLAGS P_LVM_GROUP P_RAID_DEV
      do # process partitions creation.



      The problem is for variable size partition (use 100% ("*" in the file)). If one is listed before some other, there is no room left when it's time to process the last ones. Thus it is not possible to put a primary swap partition at the end of a diwk with a / of variable size.



      Is there a clever way to use xmlstarlet sel to list partitions in the following order:



      list all primary and extended partition in same order as written in the xml file until a partition with size "*" is seen;



      keep this variable size partition in mind;



      then list from the end in reverse order the other partitions;



      and finally print the variable size partition;



      Repeat operation with logical partitions if any.



      For all partition, add a field stating if it was listed in order or reverse order so I can know if I have to create partition relative to beginning of free space or relating to end of free space. (variable partition would be tagged as normal order as they would be created starting at beginning of free space)



      If more than one "*" sized partition is encountered in a loop; fail with error would be cool.



      For the listed example (link above), this would list partitions to create in the following order (and without the header below):



      LIST_FLAG P_NUM P_SIZE P_TYPE P_ID P_NAME P_FLAGS P_LVM_GROUP P_RAID_DEV
      normal;1;500;primary;;;boot;;
      normal;3;4096;primary;;;;;;
      reverse;2;1024;primary;;;swap;;
      normal;4;*;extended;;;;;;
      normal;7;4096;logical;;;;;;
      reverse;6;2048;logical;;;;;;
      normal;5;*;logical;;;;;;



      I'm processing this within a specially crafted initrd, so I only have access to most common utils like sed/grep/bash2/xmlstarlet/awk. no perl, no python, no language that needs libraries in general.



      I'm pretty convinced that there is a solution that does most of the job if not all, but I'm far not enough skilled to even evaluate if it can be done this way. I think I can achieve that in pure bash, but this will be far less elegant.







      bash xml






      share|improve this question









      New contributor




      olahaye74 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question









      New contributor




      olahaye74 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question








      edited 9 hours ago







      olahaye74













      New contributor




      olahaye74 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked 9 hours ago









      olahaye74olahaye74

      13




      13




      New contributor




      olahaye74 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      olahaye74 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      olahaye74 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






















          0






          active

          oldest

          votes











          Your Answer








          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "89"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });






          olahaye74 is a new contributor. Be nice, and check out our Code of Conduct.










          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1122005%2fhow-to-use-xmlstarlet-sel-to-list-entries-in-complex-order%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          olahaye74 is a new contributor. Be nice, and check out our Code of Conduct.










          draft saved

          draft discarded


















          olahaye74 is a new contributor. Be nice, and check out our Code of Conduct.













          olahaye74 is a new contributor. Be nice, and check out our Code of Conduct.












          olahaye74 is a new contributor. Be nice, and check out our Code of Conduct.
















          Thanks for contributing an answer to Ask Ubuntu!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1122005%2fhow-to-use-xmlstarlet-sel-to-list-entries-in-complex-order%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Category:香港粉麵

          List *all* the tuples!

          Channel [V]