├── COPYING ├── README.md ├── go.mod ├── go.sum ├── mapviz.go ├── sample0 └── samplebad /COPYING: -------------------------------------------------------------------------------- 1 | 2 | Apache License 3 | Version 2.0, January 2004 4 | http://www.apache.org/licenses/ 5 | 6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 7 | 8 | 1. Definitions. 9 | 10 | "License" shall mean the terms and conditions for use, reproduction, 11 | and distribution as defined by Sections 1 through 9 of this document. 12 | 13 | "Licensor" shall mean the copyright owner or entity authorized by 14 | the copyright owner that is granting the License. 15 | 16 | "Legal Entity" shall mean the union of the acting entity and all 17 | other entities that control, are controlled by, or are under common 18 | control with that entity. For the purposes of this definition, 19 | "control" means (i) the power, direct or indirect, to cause the 20 | direction or management of such entity, whether by contract or 21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 22 | outstanding shares, or (iii) beneficial ownership of such entity. 23 | 24 | "You" (or "Your") shall mean an individual or Legal Entity 25 | exercising permissions granted by this License. 26 | 27 | "Source" form shall mean the preferred form for making modifications, 28 | including but not limited to software source code, documentation 29 | source, and configuration files. 30 | 31 | "Object" form shall mean any form resulting from mechanical 32 | transformation or translation of a Source form, including but 33 | not limited to compiled object code, generated documentation, 34 | and conversions to other media types. 35 | 36 | "Work" shall mean the work of authorship, whether in Source or 37 | Object form, made available under the License, as indicated by a 38 | copyright notice that is included in or attached to the work 39 | (an example is provided in the Appendix below). 40 | 41 | "Derivative Works" shall mean any work, whether in Source or Object 42 | form, that is based on (or derived from) the Work and for which the 43 | editorial revisions, annotations, elaborations, or other modifications 44 | represent, as a whole, an original work of authorship. For the purposes 45 | of this License, Derivative Works shall not include works that remain 46 | separable from, or merely link (or bind by name) to the interfaces of, 47 | the Work and Derivative Works thereof. 48 | 49 | "Contribution" shall mean any work of authorship, including 50 | the original version of the Work and any modifications or additions 51 | to that Work or Derivative Works thereof, that is intentionally 52 | submitted to Licensor for inclusion in the Work by the copyright owner 53 | or by an individual or Legal Entity authorized to submit on behalf of 54 | the copyright owner. For the purposes of this definition, "submitted" 55 | means any form of electronic, verbal, or written communication sent 56 | to the Licensor or its representatives, including but not limited to 57 | communication on electronic mailing lists, source code control systems, 58 | and issue tracking systems that are managed by, or on behalf of, the 59 | Licensor for the purpose of discussing and improving the Work, but 60 | excluding communication that is conspicuously marked or otherwise 61 | designated in writing by the copyright owner as "Not a Contribution." 62 | 63 | "Contributor" shall mean Licensor and any individual or Legal Entity 64 | on behalf of whom a Contribution has been received by Licensor and 65 | subsequently incorporated within the Work. 66 | 67 | 2. Grant of Copyright License. Subject to the terms and conditions of 68 | this License, each Contributor hereby grants to You a perpetual, 69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 70 | copyright license to reproduce, prepare Derivative Works of, 71 | publicly display, publicly perform, sublicense, and distribute the 72 | Work and such Derivative Works in Source or Object form. 73 | 74 | 3. Grant of Patent License. Subject to the terms and conditions of 75 | this License, each Contributor hereby grants to You a perpetual, 76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 77 | (except as stated in this section) patent license to make, have made, 78 | use, offer to sell, sell, import, and otherwise transfer the Work, 79 | where such license applies only to those patent claims licensable 80 | by such Contributor that are necessarily infringed by their 81 | Contribution(s) alone or by combination of their Contribution(s) 82 | with the Work to which such Contribution(s) was submitted. If You 83 | institute patent litigation against any entity (including a 84 | cross-claim or counterclaim in a lawsuit) alleging that the Work 85 | or a Contribution incorporated within the Work constitutes direct 86 | or contributory patent infringement, then any patent licenses 87 | granted to You under this License for that Work shall terminate 88 | as of the date such litigation is filed. 89 | 90 | 4. Redistribution. You may reproduce and distribute copies of the 91 | Work or Derivative Works thereof in any medium, with or without 92 | modifications, and in Source or Object form, provided that You 93 | meet the following conditions: 94 | 95 | (a) You must give any other recipients of the Work or 96 | Derivative Works a copy of this License; and 97 | 98 | (b) You must cause any modified files to carry prominent notices 99 | stating that You changed the files; and 100 | 101 | (c) You must retain, in the Source form of any Derivative Works 102 | that You distribute, all copyright, patent, trademark, and 103 | attribution notices from the Source form of the Work, 104 | excluding those notices that do not pertain to any part of 105 | the Derivative Works; and 106 | 107 | (d) If the Work includes a "NOTICE" text file as part of its 108 | distribution, then any Derivative Works that You distribute must 109 | include a readable copy of the attribution notices contained 110 | within such NOTICE file, excluding those notices that do not 111 | pertain to any part of the Derivative Works, in at least one 112 | of the following places: within a NOTICE text file distributed 113 | as part of the Derivative Works; within the Source form or 114 | documentation, if provided along with the Derivative Works; or, 115 | within a display generated by the Derivative Works, if and 116 | wherever such third-party notices normally appear. The contents 117 | of the NOTICE file are for informational purposes only and 118 | do not modify the License. You may add Your own attribution 119 | notices within Derivative Works that You distribute, alongside 120 | or as an addendum to the NOTICE text from the Work, provided 121 | that such additional attribution notices cannot be construed 122 | as modifying the License. 123 | 124 | You may add Your own copyright statement to Your modifications and 125 | may provide additional or different license terms and conditions 126 | for use, reproduction, or distribution of Your modifications, or 127 | for any such Derivative Works as a whole, provided Your use, 128 | reproduction, and distribution of the Work otherwise complies with 129 | the conditions stated in this License. 130 | 131 | 5. Submission of Contributions. Unless You explicitly state otherwise, 132 | any Contribution intentionally submitted for inclusion in the Work 133 | by You to the Licensor shall be under the terms and conditions of 134 | this License, without any additional terms or conditions. 135 | Notwithstanding the above, nothing herein shall supersede or modify 136 | the terms of any separate license agreement you may have executed 137 | with Licensor regarding such Contributions. 138 | 139 | 6. Trademarks. This License does not grant permission to use the trade 140 | names, trademarks, service marks, or product names of the Licensor, 141 | except as required for reasonable and customary use in describing the 142 | origin of the Work and reproducing the content of the NOTICE file. 143 | 144 | 7. Disclaimer of Warranty. Unless required by applicable law or 145 | agreed to in writing, Licensor provides the Work (and each 146 | Contributor provides its Contributions) on an "AS IS" BASIS, 147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 148 | implied, including, without limitation, any warranties or conditions 149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 150 | PARTICULAR PURPOSE. You are solely responsible for determining the 151 | appropriateness of using or redistributing the Work and assume any 152 | risks associated with Your exercise of permissions under this License. 153 | 154 | 8. Limitation of Liability. In no event and under no legal theory, 155 | whether in tort (including negligence), contract, or otherwise, 156 | unless required by applicable law (such as deliberate and grossly 157 | negligent acts) or agreed to in writing, shall any Contributor be 158 | liable to You for damages, including any direct, indirect, special, 159 | incidental, or consequential damages of any character arising as a 160 | result of this License or out of the use or inability to use the 161 | Work (including but not limited to damages for loss of goodwill, 162 | work stoppage, computer failure or malfunction, or any and all 163 | other commercial damages or losses), even if such Contributor 164 | has been advised of the possibility of such damages. 165 | 166 | 9. Accepting Warranty or Additional Liability. While redistributing 167 | the Work or Derivative Works thereof, You may choose to offer, 168 | and charge a fee for, acceptance of support, warranty, indemnity, 169 | or other liability obligations and/or rights consistent with this 170 | License. However, in accepting such obligations, You may act only 171 | on Your own behalf and on Your sole responsibility, not on behalf 172 | of any other Contributor, and only if You agree to indemnify, 173 | defend, and hold each Contributor harmless for any liability 174 | incurred by, or claims asserted against, such Contributor by reason 175 | of your accepting any such warranty or additional liability. 176 | 177 | END OF TERMS AND CONDITIONS 178 | 179 | APPENDIX: How to apply the Apache License to your work. 180 | 181 | To apply the Apache License to your work, attach the following 182 | boilerplate notice, with the fields enclosed by brackets "[]" 183 | replaced with your own identifying information. (Don't include 184 | the brackets!) The text should be enclosed in the appropriate 185 | comment syntax for the file format. We also recommend that a 186 | file or class name and description of purpose be included on the 187 | same "printed page" as the copyright notice for easier 188 | identification within third-party archives. 189 | 190 | Copyright [yyyy] [name of copyright owner] 191 | 192 | Licensed under the Apache License, Version 2.0 (the "License"); 193 | you may not use this file except in compliance with the License. 194 | You may obtain a copy of the License at 195 | 196 | http://www.apache.org/licenses/LICENSE-2.0 197 | 198 | Unless required by applicable law or agreed to in writing, software 199 | distributed under the License is distributed on an "AS IS" BASIS, 200 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 201 | See the License for the specific language governing permissions and 202 | limitations under the License. 203 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # uidmapviz 2 | 3 | A little program to visualize uid mappings 4 | 5 | ## Building 6 | 7 | Build in the usual new-style golang way: 8 | 9 | ``` 10 | git clone https://github.com/hallyn/uidmapviz 11 | cd uidmapviz 12 | go get ./... 13 | go build 14 | ``` 15 | 16 | ## Running 17 | 18 | You can run uidmap with no arguments to show the default 19 | uid mapping (i.e. your first full map entry). 20 | 21 | If you provide a filename as an argument, it should represent a 22 | set of uid mappings for containers. For instance 23 | 24 | c1 0:100000:200000 25 | c1/c2 0:100000:65536 26 | 27 | represents two containers, one nested inside the other. Running 28 | uidmapviz agains tthis gives you 29 | 30 | ``` 31 | +-----------+--------------+------------+-----------------+---------------+------------+----------+ 32 | | CONTAINER | PARENT START | PARENT END | CONTAINER START | CONTAINER END | HOST START | HOST END | 33 | +-----------+--------------+------------+-----------------+---------------+------------+----------+ 34 | | c1/c2 | 100000 | 165536 | 0 | 65536 | 200000 | 265536 | 35 | | c1 | 100000 | 300000 | 0 | 200000 | 100000 | 300000 | 36 | +-----------+--------------+------------+-----------------+---------------+------------+----------+ 37 | ``` 38 | 39 | A child container must come after the parent definition. 40 | If a child container's mapping is not contained within the 41 | parent's available range, then uidmapviz will warn you. 42 | 43 | ## More details 44 | 45 | ``` 46 | ubuntu@lxd1:~$ uidmapviz 47 | Your current default allocation is: 48 | host uid 100000 - 165535 mapping to 0 - 65535 in container 49 | host gid 100000 - 165535 mapping to 0 - 65535 in container 50 | ``` 51 | 52 | We'll create a little file showing the uid mappings we'd like to use: 53 | 54 | ``` 55 | cat > containers << EOF 56 | c1 0:100000:65536 57 | c1/c2 0:100000:65536 58 | c1/c2/c3 0:200000:65536 59 | EOF 60 | ``` 61 | 62 | Here the first field is a container name. If the name contains slashes, 63 | then it is read as grandparent/parent/child. The second field is the 64 | range, in the same format as /etc/subuid: the first id in the container, 65 | the first id in the parent container, and the range of the mapping. For 66 | each container we are mapping 65536 uids, starting at 0 in the container, 67 | to the range starting at 100000 in the parent. 68 | 69 | Let's see what uidmapviz says about this: 70 | 71 | ubuntu@lxd1:~$ uidmapviz containers 72 | Error opening file containers: "Mapping for c1/c2 exceeds its parent's, parentids should be between 0 - 65535" 73 | 74 | c1's uid range is too narrow, as we should have known. When we provide 75 | a container with a mapping for uids 0-20, then only those uids exist in 76 | the container. It cannot delegate uids which do not exist. If we want 77 | to hand 20 uids to a child container while reserving 20 for ourselves, 78 | then we'll need 40 uids in total. 79 | 80 | So let's give our first container 3*65k, or 196608 uids, and hand 65536-196604 81 | to the second container. Then we'll hand the top half of c2's uids to c3. 82 | 83 | ``` 84 | cat > containers2 << EOF 85 | c1 0:100000:196605 86 | c1/c2 0:65536:131072 87 | c1/c2/c3 0:65536:65536 88 | EOF 89 | 90 | ubuntu@lxd1:~$ uidmapviz containers2 91 | Looking at c1 92 | Looking at c1/c2 93 | Looking at c1/c2/c3 94 | +-----------+--------------+------------+-----------------+---------------+------------+----------+ 95 | | CONTAINER | PARENT START | PARENT END | CONTAINER START | CONTAINER END | HOST START | HOST END | 96 | +-----------+--------------+------------+-----------------+---------------+------------+----------+ 97 | | c1 | 100000 | 296605 | 0 | 196605 | 100000 | 296605 | 98 | | c1/c2 | 65536 | 196608 | 0 | 131072 | 165536 | 296608 | 99 | | c1/c2/c3 | 65536 | 131072 | 0 | 65536 | 231072 | 296608 | 100 | +-----------+--------------+------------+-----------------+---------------+------------+----------+ 101 | ``` 102 | 103 | (Usually I use wider and easier to read allocations, i.e. 104 | b:100000:200000) 105 | Perfect. Now we need to make sure that the root user on the host is allowed to 106 | delegate the the range required by c1. We do this using /etc/subuid and 107 | /etc/subgid: 108 | 109 | ``` 110 | root:100000:196605 111 | ``` 112 | 113 | Restart lxd so the deamon will re-read its idmap. 114 | 115 | We'll now create container c1. In order for it to be able to run 116 | nested containers, we'll set the 'security.nesting' flag to true: 117 | 118 | lxc launch wily c1 --config security.nesting=true 119 | 120 | Per-tenant idmaps are a future todo, for now the container will receive 121 | the full default id mapping allocated to the root user. So now we can 122 | log into the c1 container 123 | 124 | lxc exec c1 bash 125 | 126 | and install and setup lxd. This time we'll allocate the root user the 127 | mapping we wanted for c2: 128 | 129 | root:65536:196605 130 | 131 | That should be it. now you can 132 | 133 | lxc launch wily c2 134 | 135 | for an unprivileged container inside an unprivileged container. Configuring 136 | c3 is left as an exercise for the reader. 137 | -------------------------------------------------------------------------------- /go.mod: -------------------------------------------------------------------------------- 1 | module github.com/hallyn/uidmapviz 2 | 3 | go 1.20 4 | 5 | require ( 6 | github.com/lxc/lxd v0.0.0-20230329071945-5109852c8404 7 | github.com/olekukonko/tablewriter v0.0.5 8 | ) 9 | 10 | require ( 11 | github.com/flosch/pongo2 v0.0.0-20200913210552-0d938eb266f3 // indirect 12 | github.com/google/uuid v1.3.0 // indirect 13 | github.com/gorilla/websocket v1.5.0 // indirect 14 | github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 // indirect 15 | github.com/mattn/go-runewidth v0.0.14 // indirect 16 | github.com/pborman/uuid v1.2.1 // indirect 17 | github.com/pkg/xattr v0.4.9 // indirect 18 | github.com/rivo/uniseg v0.4.4 // indirect 19 | github.com/robfig/cron/v3 v3.0.1 // indirect 20 | github.com/sirupsen/logrus v1.9.0 // indirect 21 | golang.org/x/crypto v0.7.0 // indirect 22 | golang.org/x/sys v0.6.0 // indirect 23 | golang.org/x/term v0.6.0 // indirect 24 | gopkg.in/yaml.v2 v2.4.0 // indirect 25 | ) 26 | -------------------------------------------------------------------------------- /go.sum: -------------------------------------------------------------------------------- 1 | github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 2 | github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= 3 | github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 4 | github.com/flosch/pongo2 v0.0.0-20200913210552-0d938eb266f3 h1:fmFk0Wt3bBxxwZnu48jqMdaOR/IZ4vdtJFuaFV8MpIE= 5 | github.com/flosch/pongo2 v0.0.0-20200913210552-0d938eb266f3/go.mod h1:bJWSKrZyQvfTnb2OudyUjurSG4/edverV7n82+K3JiM= 6 | github.com/google/uuid v1.0.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= 7 | github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I= 8 | github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= 9 | github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc= 10 | github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= 11 | github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 h1:Z9n2FFNUXsshfwJMBgNA0RU6/i7WVaAegv3PtuIHPMs= 12 | github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51/go.mod h1:CzGEWj7cYgsdH8dAjBGEr58BoE7ScuLd+fwFZ44+/x8= 13 | github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= 14 | github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= 15 | github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= 16 | github.com/lxc/lxd v0.0.0-20230329071945-5109852c8404 h1:SXgU9bzqxlR0JVxSyIeutv5bjJId2BLcHs6kd1CNVSM= 17 | github.com/lxc/lxd v0.0.0-20230329071945-5109852c8404/go.mod h1:JJ1ShHzaOzMzU0B5TNcdI9+vq8Y45ijVeNYxE1wJ8zM= 18 | github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI= 19 | github.com/mattn/go-runewidth v0.0.14 h1:+xnbZSEeDbOIg5/mE6JF0w6n9duR1l3/WmbinWVwUuU= 20 | github.com/mattn/go-runewidth v0.0.14/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= 21 | github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e h1:fD57ERR4JtEqsWbfPhv4DMiApHyliiK5xCTNVSPiaAs= 22 | github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno= 23 | github.com/olekukonko/tablewriter v0.0.5 h1:P2Ga83D34wi1o9J6Wh1mRuqd4mF/x/lgBS7N7AbDhec= 24 | github.com/olekukonko/tablewriter v0.0.5/go.mod h1:hPp6KlRPjbx+hW8ykQs1w3UBbZlj6HuIJcUGPhkA7kY= 25 | github.com/pborman/uuid v1.2.1 h1:+ZZIw58t/ozdjRaXh/3awHfmWRbzYxJoAdNJxe/3pvw= 26 | github.com/pborman/uuid v1.2.1/go.mod h1:X/NO0urCmaxf9VXbdlT7C2Yzkj2IKimNn4k+gtPdI/k= 27 | github.com/pkg/xattr v0.4.9 h1:5883YPCtkSd8LFbs13nXplj9g9tlrwoJRjgpgMu1/fE= 28 | github.com/pkg/xattr v0.4.9/go.mod h1:di8WF84zAKk8jzR1UBTEWh9AUlIZZ7M/JNt8e9B6ktU= 29 | github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= 30 | github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U= 31 | github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= 32 | github.com/rivo/uniseg v0.4.4 h1:8TfxU8dW6PdqD27gjM8MVNuicgxIjxpm4K7x4jp8sis= 33 | github.com/rivo/uniseg v0.4.4/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88= 34 | github.com/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs= 35 | github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro= 36 | github.com/sirupsen/logrus v1.9.0 h1:trlNQbNUG3OdDrDil03MCb1H2o9nJ1x4/5LYw7byDE0= 37 | github.com/sirupsen/logrus v1.9.0/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ= 38 | github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= 39 | github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= 40 | github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8= 41 | golang.org/x/crypto v0.7.0 h1:AvwMYaRytfdeVt3u6mLaxYtErKYjxA2OXjJ1HHq6t3A= 42 | golang.org/x/crypto v0.7.0/go.mod h1:pYwdfH91IfpZVANVyUOhSIPZaFoJGxTFbZhFTx+dXZU= 43 | golang.org/x/sys v0.0.0-20220408201424-a24fb2fb8a0f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= 44 | golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= 45 | golang.org/x/sys v0.6.0 h1:MVltZSvRTcU2ljQOhs94SXPftV6DCNnZViHeQps87pQ= 46 | golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= 47 | golang.org/x/term v0.6.0 h1:clScbb1cHjoCkyRbWwBEUZ5H/tIFu5TAXIqaZD0Gcjw= 48 | golang.org/x/term v0.6.0/go.mod h1:m6U89DPEgQRMq3DNkDClhWw02AUbt2daBVO4cn4Hv9U= 49 | gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= 50 | gopkg.in/check.v1 v1.0.0-20200902074654-038fdea0a05b h1:QRR6H1YWRnHb4Y/HeNFCTJLFVxaq6wH4YuVdsUOr75U= 51 | gopkg.in/check.v1 v1.0.0-20200902074654-038fdea0a05b/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= 52 | gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= 53 | gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= 54 | gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= 55 | gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= 56 | -------------------------------------------------------------------------------- /mapviz.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "bufio" 5 | "fmt" 6 | "os" 7 | "os/user" 8 | "path" 9 | "strings" 10 | 11 | "github.com/lxc/lxd/shared/idmap" 12 | "github.com/olekukonko/tablewriter" 13 | ) 14 | 15 | func help(status int) { 16 | me := path.Base(os.Args[0]) 17 | fmt.Printf("Usage:\n%s\n", me) 18 | fmt.Printf(" show default id mapping\n") 19 | fmt.Printf("%s cfile\n", me) 20 | fmt.Printf(" show id mappings for containers in cfile\n") 21 | os.Exit(status) 22 | } 23 | 24 | func isHelp(s string) bool { 25 | switch s { 26 | case "-h": 27 | return true 28 | case "--help": 29 | return true 30 | case "help": 31 | return true 32 | default: 33 | return false 34 | } 35 | } 36 | 37 | func showDefaultMap() { 38 | u, err := user.Current() 39 | if err != nil { 40 | fmt.Printf("Error resolving current user") 41 | help(1) 42 | } 43 | set, err := idmap.DefaultIdmapSet("", u.Username) 44 | if err != nil { 45 | fmt.Printf("Error reading default mapset: %q\n", err) 46 | help(1) 47 | } 48 | fmt.Printf("Your current default allocation is:\n\n") 49 | for _, m := range set.Idmap { 50 | t := "uid" 51 | if !m.Isuid { 52 | t = "gid" 53 | } 54 | hmin := m.Hostid 55 | hmax := m.Hostid + m.Maprange - 1 56 | cmin := m.Nsid 57 | cmax := m.Nsid + m.Maprange - 1 58 | fmt.Printf("host %s %d - %d mapping to %d - %d in container\n", 59 | t, hmin, hmax, cmin, cmax) 60 | } 61 | } 62 | 63 | func main() { 64 | if len(os.Args) > 2 { 65 | fmt.Printf("Too many arguments") 66 | help(1) 67 | } 68 | if len(os.Args) == 2 && isHelp(os.Args[1]) { 69 | help(0) 70 | } 71 | 72 | if len(os.Args) == 1 { 73 | showDefaultMap() 74 | return 75 | } 76 | 77 | // to do - parse file. Let's just use bogus input for now 78 | containers, err := ParseFile(os.Args[1]) 79 | if err != nil { 80 | fmt.Printf("Error opening file %s: %q\n", os.Args[1], err) 81 | os.Exit(1) 82 | } 83 | 84 | data, err := Process(containers) 85 | if err != nil { 86 | fmt.Printf("Error processing file %s: %q\n", os.Args[1], err) 87 | os.Exit(1) 88 | } 89 | 90 | table := tablewriter.NewWriter(os.Stdout) 91 | table.SetHeader([]string{ 92 | "Container", 93 | "parent start", 94 | "parent end", 95 | "container start", 96 | "container end", 97 | "host start", 98 | "host end"}) 99 | table.AppendBulk(data) 100 | table.Render() 101 | 102 | return 103 | } 104 | 105 | type container struct { 106 | idmap *idmap.IdmapSet 107 | // for nested containers, the true host min/max 108 | hostmin int64 109 | hostmax int64 110 | } 111 | 112 | type containers map[string]container 113 | 114 | func ParseFile(fName string) (containers, error) { 115 | set := containers{} 116 | file, err := os.Open(fName) 117 | if err != nil { 118 | return set, err 119 | } 120 | defer file.Close() 121 | scanner := bufio.NewScanner(file) 122 | for scanner.Scan() { 123 | // c1/c2 0:100000:65536 124 | s := strings.Fields(scanner.Text()) 125 | if len(s) > 2 { 126 | return set, fmt.Errorf("Too many fields") 127 | } 128 | mapstr := fmt.Sprintf("b:%s", s[1]) 129 | m, err := idmap.IdmapSet{}.Append(mapstr) 130 | if err != nil { 131 | return set, err 132 | } 133 | name := s[0] 134 | hostmin, hostmax, err := verifyRange(name, m.Idmap[0], set) 135 | if err != nil { 136 | return set, err 137 | } 138 | c := container{idmap: &m, hostmin: hostmin, hostmax: hostmax} 139 | set[name] = c 140 | } 141 | 142 | return set, nil 143 | } 144 | 145 | func Process(containers containers) ([][]string, error) { 146 | result := [][]string{} 147 | 148 | for name, c := range containers { 149 | // note - we only do cases where uid+gid are the same, so just 150 | // take the first idmap 151 | fmt.Printf("Looking at %s\n", name) 152 | idMap := c.idmap 153 | r := idMap.Idmap[0].Maprange 154 | pstart := fmt.Sprintf("%d", idMap.Idmap[0].Hostid) 155 | pend := fmt.Sprintf("%d", idMap.Idmap[0].Hostid+r) 156 | cstart := fmt.Sprintf("%d", idMap.Idmap[0].Nsid) 157 | cend := fmt.Sprintf("%d", idMap.Idmap[0].Nsid+r) 158 | hstart := fmt.Sprintf("%d", c.hostmin) 159 | hend := fmt.Sprintf("%d", c.hostmax) 160 | newstr := []string{name, pstart, pend, cstart, cend, hstart, hend} 161 | result = append(result, newstr) 162 | } 163 | 164 | return result, nil 165 | } 166 | 167 | func verifyRange(name string, idMap idmap.IdmapEntry, c containers) (int64, int64, error) { 168 | lineage := strings.Split(name, "/") 169 | if len(lineage) == 1 { 170 | return idMap.Hostid, idMap.Hostid + idMap.Maprange, nil 171 | } 172 | last := len(lineage) - 1 173 | pname := strings.Join(lineage[0:last], "/") 174 | parent, ok := c[pname] 175 | if !ok || parent.hostmin == -1 { 176 | return 0, 0, fmt.Errorf("Parent for %s (%s) is undefined", name, pname) 177 | } 178 | 179 | pidmap := parent.idmap.Idmap[0] 180 | if idMap.Nsid+idMap.Maprange >= pidmap.Nsid+pidmap.Maprange || idMap.Hostid < pidmap.Nsid { 181 | return 0, 0, fmt.Errorf("Mapping for %s exceeds its parent's, parentids should be between %d - %d", 182 | name, pidmap.Nsid, pidmap.Nsid+pidmap.Maprange-1) 183 | } 184 | 185 | // make an idmap shifting the parent's mapping straight onto the host 186 | absstr := fmt.Sprintf("b:%d:%d:%d", pidmap.Nsid, parent.hostmin, pidmap.Maprange) 187 | m := idmap.IdmapSet{} 188 | m, err := m.Append(absstr) 189 | if err != nil { 190 | return 0, 0, err 191 | } 192 | 193 | // map the desired 'hostid' (which is really the parent-ns-id) onto the host 194 | hoststart, _ := m.ShiftIntoNs(idMap.Hostid, idMap.Hostid) 195 | hostend := hoststart + idMap.Maprange 196 | return hoststart, hostend, nil 197 | } 198 | -------------------------------------------------------------------------------- /sample0: -------------------------------------------------------------------------------- 1 | c1 0:100000:200000 2 | c1/c2 0:100000:65536 3 | c1/c2/c3 0:30000:20000 4 | -------------------------------------------------------------------------------- /samplebad: -------------------------------------------------------------------------------- 1 | c1 0:100000:200000 2 | c1/c2 0:100000:65536 3 | c1/c2/c3 0:100000:65536 4 | --------------------------------------------------------------------------------