Compare commits
	
		
			51 Commits
		
	
	
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| f33b3cfec4 | |||
| 1006207fc2 | |||
| 6acea0d25b | |||
| fb4010378f | |||
| d0646c51b3 | |||
| c6e0a93ed8 | |||
| 1e2f937cb5 | |||
| b72c432a47 | |||
| ca1c7bea5f | |||
| fb5c4690bf | |||
| 5d4c54f5fa | |||
| c79ac4ac30 | |||
| 76c720354c | |||
| f3f348164a | |||
| dcec35459c | |||
| e4d699e228 | |||
| 48a0357a3f | |||
| 5b2f3a2f0a | |||
| 2df22b10b1 | |||
| c19e40b139 | |||
| 259c3ee1e0 | |||
| 8cabae7134 | |||
| 9fcc7a6572 | |||
| 5586b164c6 | |||
| 4cc1c476aa | |||
| ce79783540 | |||
| 18d35b9224 | |||
| f41c93368d | |||
| 452b0e4b4e | |||
| dbe9622a01 | |||
| 6ead474a78 | |||
| 4edd0b7414 | |||
| b54ebfd915 | |||
| 69665fdcef | |||
| 7e1c213ff4 | |||
| 26ceb1630a | |||
| 87e9ae894a | |||
| ed5f8f0b1c | |||
| c55262690a | |||
| c5aa547e5d | |||
| cdcb466417 | |||
| bb3136c9ef | |||
| 9208102c84 | |||
| fce64b2939 | |||
| 44b877eae1 | |||
| c2277ce10c | |||
| 549d517cf9 | |||
| 14984f417c | |||
| 346fd52a8e | |||
| 684d97cc21 | |||
| a3dd0a7aa2 | 
							
								
								
									
										112
									
								
								README.md
									
									
									
									
									
								
							
							
						
						
									
										112
									
								
								README.md
									
									
									
									
									
								
							| @ -8,31 +8,30 @@ Gocage can handle multiple datastores, so you can have jails on HDD storage and | |||||||
|  |  | ||||||
| From v0.33b, due to multi ZFS pool support, gocage is no longer 100% compatible with iocage.   | From v0.33b, due to multi ZFS pool support, gocage is no longer 100% compatible with iocage.   | ||||||
| Zfs datasets now should be specified with the ZFS pool. e.g. :   | Zfs datasets now should be specified with the ZFS pool. e.g. :   | ||||||
| <code> | <pre><code>Config.Jail_zfs = 1 | ||||||
| Config.Jail_zfs = 1 |  | ||||||
| Config.Jail_zfs_dataset = myzfspool/poudriere | Config.Jail_zfs_dataset = myzfspool/poudriere | ||||||
| Config.Jail_zfs_mountpoint = none | Config.Jail_zfs_mountpoint = none | ||||||
| </code> | </code></pre> | ||||||
|  |  | ||||||
| Create jails | Create jails | ||||||
| ------------ | ------------ | ||||||
| For now, we can't pass config at creation time. We have to define config after creation:   | You need to specify release, and optional configuration:   | ||||||
| <pre><code> | <pre><code>gocage create jail1 -r 13.2-RELEASE -p "Config.Ip4_addr='vnet0|192.168.1.91/24',Config.Ip6=none,Config.Boot=1" | ||||||
| gocage create jail1 -r 13.2-RELEASE | </code></pre> | ||||||
| gocage set Config.Ip4_addr="vnet0|192.168.1.91/24" Config.Vnet=1 jail1 |  | ||||||
|  | Create basejail. A basejail is a jail based on a release: system will be nullfs read-only mounted from the release directory. Main advantage is that release updates will immediately apply to jails based on this release. Another advantage is that jail system is mounted read-only, a plus from a security perspective:   | ||||||
|  | <pre><code>gocage create -b -r 14.0-RELEASE basejail1 | ||||||
| </code></pre> | </code></pre> | ||||||
|  |  | ||||||
|  |  | ||||||
| List jails | List jails | ||||||
| ---------- | ---------- | ||||||
| Nothing fancy, just use   | <pre><code>gocage list</code></pre> | ||||||
| `gocage list` |  | ||||||
|  |  | ||||||
| ### Specify fields to display | ### Specify fields to display | ||||||
|  |  | ||||||
| Use -o to specify which fields you want to display:   | Use -o to specify which fields you want to display:   | ||||||
| <pre><code> | <pre><code>gocage list -o JID,Name,Running,Config.Boot,Config.Comment   | ||||||
| gocage list -o JID,Name,Running,Config.Boot,Config.Comment   |  | ||||||
| +=====+==========+=========+=============+================+   | +=====+==========+=========+=============+================+   | ||||||
| | JID | Name     | Running | Config.Boot | Config.Comment |   | | JID | Name     | Running | Config.Boot | Config.Comment |   | ||||||
| +=====+==========+=========+=============+================+   | +=====+==========+=========+=============+================+   | ||||||
| @ -46,7 +45,7 @@ gocage list -o JID,Name,Running,Config.Boot,Config.Comment | |||||||
| +-----+----------+---------+-------------+----------------+   | +-----+----------+---------+-------------+----------------+   | ||||||
| </code></pre> | </code></pre> | ||||||
|  |  | ||||||
| See [cmd/struct.go](https://git.nosd.in/yo/gocage/src/branch/master/cmd/struct.go) for field names. | Use `gocage properties`to list available fields. | ||||||
|  |  | ||||||
|  |  | ||||||
| Filter jails   | Filter jails   | ||||||
| @ -54,8 +53,7 @@ Filter jails | |||||||
|  |  | ||||||
| ### By name | ### By name | ||||||
| Just add name on gocage list command :   | Just add name on gocage list command :   | ||||||
| <pre><code> | <pre><code>gocage list srv-bdd srv-web | ||||||
| gocage list srv-bdd srv-web |  | ||||||
| +=====+=========+=================+=======================+=========+ | +=====+=========+=================+=======================+=========+ | ||||||
| | JID | Name    | Config.Release  | Config.Ip4_addr       | Running | | | JID | Name    | Config.Release  | Config.Ip4_addr       | Running | | ||||||
| +=====+=========+=================+=======================+=========+ | +=====+=========+=================+=======================+=========+ | ||||||
| @ -63,12 +61,11 @@ gocage list srv-bdd srv-web | |||||||
| +-----+---------+-----------------+-----------------------+---------+ | +-----+---------+-----------------+-----------------------+---------+ | ||||||
| | 41  | srv-web | 13.0-RELEASE-p4 | vnet0|192.168.1.26/24 | true    | | | 41  | srv-web | 13.0-RELEASE-p4 | vnet0|192.168.1.26/24 | true    | | ||||||
| +-----+---------+-----------------+-----------------------+---------+ | +-----+---------+-----------------+-----------------------+---------+ | ||||||
| </pre></code> | </code></pre> | ||||||
|  |  | ||||||
| ### By field value | ### By field value | ||||||
| You can filter jails with -f option, followed by key=value. Suppose you want to see only active at boot jails:   | You can filter jails with -f option, followed by key=value. Suppose you want to see only active at boot jails:   | ||||||
| <pre><code> | <pre><code>gocage list -f Config.Boot=1 -o JID,Name,Running,Config.Boot,Config.Comment | ||||||
| gocage list -f Config.Boot=1 -o JID,Name,Running,Config.Boot,Config.Comment |  | ||||||
| +=====+==========+=========+=============+================+ | +=====+==========+=========+=============+================+ | ||||||
| | JID | Name     | Running | Config.Boot | Config.Comment | | | JID | Name     | Running | Config.Boot | Config.Comment | | ||||||
| +=====+==========+=========+=============+================+ | +=====+==========+=========+=============+================+ | ||||||
| @ -80,11 +77,10 @@ gocage list -f Config.Boot=1 -o JID,Name,Running,Config.Boot,Config.Comment | |||||||
| +-----+----------+---------+-------------+----------------+ | +-----+----------+---------+-------------+----------------+ | ||||||
| | 22  | srv-dns1 | true    | 1           |                | | | 22  | srv-dns1 | true    | 1           |                | | ||||||
| +-----+----------+---------+-------------+----------------+ | +-----+----------+---------+-------------+----------------+ | ||||||
| </pre></code> | </code></pre> | ||||||
|  |  | ||||||
| Now, only active at boot and running :   | Now, only active at boot and running :   | ||||||
| <pre><code> | <pre><code>gocage list -f Config.Boot=1,Running=true -o JID,Name,Running,Config.Boot | ||||||
| gocage list -f Config.Boot=1,Running=true -o JID,Name,Running,Config.Boot |  | ||||||
| +=====+==========+=========+=============+ | +=====+==========+=========+=============+ | ||||||
| | JID | Name     | Running | Config.Boot | | | JID | Name     | Running | Config.Boot | | ||||||
| +=====+==========+=========+=============+ | +=====+==========+=========+=============+ | ||||||
| @ -94,13 +90,12 @@ gocage list -f Config.Boot=1,Running=true -o JID,Name,Running,Config.Boot | |||||||
| +-----+----------+---------+-------------+ | +-----+----------+---------+-------------+ | ||||||
| | 22  | srv-dns1 | true    | 1           | | | 22  | srv-dns1 | true    | 1           | | ||||||
| +-----+----------+---------+-------------+ | +-----+----------+---------+-------------+ | ||||||
| </pre></code> | </code></pre> | ||||||
|  |  | ||||||
| Sort jails   | Sort jails   | ||||||
| ---------- | ---------- | ||||||
| Use -s switch followed by sort criteria. Criteria is a field name, prefixed with + or - for sort order (increase/decrease):   | Use -s switch followed by sort criteria. Criteria is a field name, prefixed with + or - for sort order (increase/decrease):   | ||||||
| <pre><code> | <pre><code>gocage list -f Config.Boot=1,Running=true -o JID,Name,Running,Config.Boot -s +JID | ||||||
| gocage list -f Config.Boot=1,Running=true -o JID,Name,Running,Config.Boot -s +JID |  | ||||||
| +=====+==========+=========+=============+ | +=====+==========+=========+=============+ | ||||||
| | JID | Name     | Running | Config.Boot | | | JID | Name     | Running | Config.Boot | | ||||||
| +=====+==========+=========+=============+ | +=====+==========+=========+=============+ | ||||||
| @ -110,12 +105,11 @@ gocage list -f Config.Boot=1,Running=true -o JID,Name,Running,Config.Boot -s +JI | |||||||
| +-----+----------+---------+-------------+ | +-----+----------+---------+-------------+ | ||||||
| | 183 | test     | true    | 1           | | | 183 | test     | true    | 1           | | ||||||
| +-----+----------+---------+-------------+ | +-----+----------+---------+-------------+ | ||||||
| </pre></code> | </code></pre> | ||||||
|  |  | ||||||
| You can use up to 3 criteria, delimited with comma.   | You can use up to 3 criteria, delimited with comma.   | ||||||
| As an example, you want to list boot priorities of automatically starting jails:   | As an example, you want to list boot priorities of automatically starting jails:   | ||||||
| <pre><code> | <pre><code>gocage list -o JID,Name,Config.Ip4_addr,Config.Priority,Config.Boot,Running -s -Config.Priority,-Config.Boot -f Running=true | ||||||
| gocage list -o JID,Name,Config.Ip4_addr,Config.Priority,Config.Boot,Running -s -Config.Priority,-Config.Boot -f Running=true |  | ||||||
| +=====+==============+=======================+=================+=============+=========+ | +=====+==============+=======================+=================+=============+=========+ | ||||||
| | JID | Name         | Config.Ip4_addr       | Config.Priority | Config.Boot | Running | | | JID | Name         | Config.Ip4_addr       | Config.Priority | Config.Boot | Running | | ||||||
| +=====+==============+=======================+=================+=============+=========+ | +=====+==============+=======================+=================+=============+=========+ | ||||||
| @ -127,47 +121,62 @@ gocage list -o JID,Name,Config.Ip4_addr,Config.Priority,Config.Boot,Running -s - | |||||||
| +-----+--------------+-----------------------+-----------------+-------------+---------+ | +-----+--------------+-----------------------+-----------------+-------------+---------+ | ||||||
| | 4   | coincoin     | vnet0|192.168.1.9/24  | 20              | 0           | true    | | | 4   | coincoin     | vnet0|192.168.1.9/24  | 20              | 0           | true    | | ||||||
| +-----+--------------+-----------------------+-----------------+-------------+---------+ | +-----+--------------+-----------------------+-----------------+-------------+---------+ | ||||||
| </pre></code> | </code></pre> | ||||||
|  |  | ||||||
|  |  | ||||||
| Stop jails   | Stop jails   | ||||||
| ---------- | ---------- | ||||||
| `gocage stop test` | <pre><code>gocage stop test</code></pre> | ||||||
|  |  | ||||||
|  |  | ||||||
| Update jails   | Update jails   | ||||||
| ---------- | ---------- | ||||||
| To update jail patch version, use gocage update :   | To update jail patch version, use gocage update :   | ||||||
| `gocage update test` | <pre><code>gocage update test</code></pre> | ||||||
|  |  | ||||||
|  | Update basejails/releases   | ||||||
|  | ---------- | ||||||
|  | To update basejails, you need to update the release they are based on. Specify release with -r, and the datastore storing concerned release with -d :   | ||||||
|  | <pre><code>gocage update -d fastgocage -r 14.1-RELEASE</code></pre> | ||||||
|  |  | ||||||
| Upgrade jails   | Upgrade jails   | ||||||
| ---------- | ---------- | ||||||
| To upgrade jail to newer release, use gocage upgrade :   | To upgrade jail to newer release, use gocage upgrade :   | ||||||
| `gocage upgrade -r 13.2-RELEASE test` | <pre><code>gocage upgrade -r 13.2-RELEASE test</code></pre> | ||||||
|    |    | ||||||
| A pre-upgrade snapshot wil be made so we can rollback if needed.   | A pre-upgrade snapshot wil be made so you can rollback if needed.   | ||||||
|  |  | ||||||
|  | Upgrading basejail/release | ||||||
|  | ---------- | ||||||
|  | Upgrading basejails currently needs to be done manually, for each jail.   | ||||||
|  | The idea is to stop the jail, change the content of its fstab file to point to the new release, then start jail.   | ||||||
|  | If one change the fstab while the jail is running, its system directories won't be unmounted at stop time and this will provoke stop errors.   | ||||||
|  | To minimize downtime, the change could be scripted:   | ||||||
|  | <pre><code>gocage stop jail1 | ||||||
|  | sed -i .bak 's/14.0-RELEASE/14.1-RELEASE/' /iocage/jails/jail1/fstab | ||||||
|  | # Avoid race-condition by waiting for the update in fstab | ||||||
|  | until grep -q 14.1-RELEASE /iocage/jails/jail1/fstab; do sleep 0.2; done | ||||||
|  | gocage start jail1 | ||||||
|  | </code></pre> | ||||||
|  |  | ||||||
|  | You can now update ports. | ||||||
|  |  | ||||||
| Delete jails   | Delete jails   | ||||||
| ---------- | ---------- | ||||||
| `gocage destroy test` | <pre><code>gocage destroy test</code></pre> | ||||||
|  |  | ||||||
|  |  | ||||||
| Multi datastore   | Multi datastore   | ||||||
| ---------- | ---------- | ||||||
| A datastore is a ZFS dataset mounted. It should be declared in gocage.conf.yml, specifying its ZFS mountpoint : | A datastore is a ZFS dataset mounted. It should be declared in gocage.conf.yml, specifying its ZFS mountpoint : | ||||||
| <pre><code> | <pre><code>datastore: | ||||||
| datastore: |  | ||||||
|   - /iocage |   - /iocage | ||||||
|   - /fastiocage |   - /fastiocage | ||||||
| </pre></code> | </code></pre> | ||||||
|  |  | ||||||
| In gocage commands, datastore name is the mountpoint without its "/" prefix.   | In gocage commands, datastore name is the mountpoint without its "/" prefix.   | ||||||
|  |  | ||||||
| ### List datastores | ### List datastores | ||||||
| <pre><code> | <pre><code>gocage datastore list  | ||||||
| gocage datastore list  |  | ||||||
| +============+=============+============+===========+==========+============+ | +============+=============+============+===========+==========+============+ | ||||||
| | Name       | Mountpoint  | ZFSDataset | Available | Used     | Referenced | | | Name       | Mountpoint  | ZFSDataset | Available | Used     | Referenced | | ||||||
| +============+=============+============+===========+==========+============+ | +============+=============+============+===========+==========+============+ | ||||||
| @ -175,23 +184,21 @@ gocage datastore list | |||||||
| +------------+-------------+------------+-----------+----------+------------+ | +------------+-------------+------------+-----------+----------+------------+ | ||||||
| | fastiocage | /fastiocage | ssd/iocage | 1.5 TB    | 65.3 KB  | 34.6 KB    | | | fastiocage | /fastiocage | ssd/iocage | 1.5 TB    | 65.3 KB  | 34.6 KB    | | ||||||
| +------------+-------------+------------+-----------+----------+------------+ | +------------+-------------+------------+-----------+----------+------------+ | ||||||
| </pre></code> | </code></pre> | ||||||
|  |  | ||||||
| ### Filter datastores   | ### Filter datastores   | ||||||
| As with jails and snapshots, you can filter by name:   | As with jails and snapshots, you can filter by name:   | ||||||
| <pre><code> | <pre><code>gocage datastore list iocage | ||||||
| gocage datastore list iocage |  | ||||||
| +============+=============+============+===========+==========+============+ | +============+=============+============+===========+==========+============+ | ||||||
| | Name       | Mountpoint  | ZFSDataset | Available | Used     | Referenced | | | Name       | Mountpoint  | ZFSDataset | Available | Used     | Referenced | | ||||||
| +============+=============+============+===========+==========+============+ | +============+=============+============+===========+==========+============+ | ||||||
| | iocage     | /iocage     | hdd/iocage | 1.6 TB    | 414.9 GB | 27.5 KB    | | | iocage     | /iocage     | hdd/iocage | 1.6 TB    | 414.9 GB | 27.5 KB    | | ||||||
| +------------+-------------+------------+-----------+----------+------------+ | +------------+-------------+------------+-----------+----------+------------+ | ||||||
| </pre></code> | </code></pre> | ||||||
|  |  | ||||||
| ### Sort datastores   | ### Sort datastores   | ||||||
| You can sort datastores:   | You can sort datastores:   | ||||||
| <pre><code> | <pre><code>gocage datastore list -s -Available | ||||||
| gocage datastore list -s -Available |  | ||||||
| +============+=============+============+===========+==========+============+ | +============+=============+============+===========+==========+============+ | ||||||
| | Name       | Mountpoint  | ZFSDataset | Available | Used     | Referenced | | | Name       | Mountpoint  | ZFSDataset | Available | Used     | Referenced | | ||||||
| +============+=============+============+===========+==========+============+ | +============+=============+============+===========+==========+============+ | ||||||
| @ -199,9 +206,9 @@ gocage datastore list -s -Available | |||||||
| +------------+-------------+------------+-----------+----------+------------+ | +------------+-------------+------------+-----------+----------+------------+ | ||||||
| | fastiocage | /fastiocage | ssd/iocage | 1.5 TB    | 65.3 KB  | 34.6 KB    | | | fastiocage | /fastiocage | ssd/iocage | 1.5 TB    | 65.3 KB  | 34.6 KB    | | ||||||
| +------------+-------------+------------+-----------+----------+------------+ | +------------+-------------+------------+-----------+----------+------------+ | ||||||
| </pre></code> | </code></pre> | ||||||
|  |  | ||||||
| See [cmd/struct.go](https://git.nosd.in/yo/gocage/src/branch/master/cmd/struct.go) for field names. | Use `gocage properties`to list available fields. | ||||||
|  |  | ||||||
| Migrating jails   | Migrating jails   | ||||||
| ---------- | ---------- | ||||||
| @ -214,26 +221,23 @@ Be aware the moment you migrate a jail to another datastore than /iocage default | |||||||
| Then you need to disable iocage service, and enable gocage so the jails will start automatically at boot.   | Then you need to disable iocage service, and enable gocage so the jails will start automatically at boot.   | ||||||
| Also make sure, if you don't destroy source jail, that it won't have the "boot" property set or you will have the 2 jails up at boot.   | Also make sure, if you don't destroy source jail, that it won't have the "boot" property set or you will have the 2 jails up at boot.   | ||||||
|  |  | ||||||
| <pre><code> | <pre><code>gocage migrate -d fastiocage srv-random | ||||||
| gocage migrate -d fastiocage srv-random |  | ||||||
| Snapshot data/iocage/jails/srv-random: Done | Snapshot data/iocage/jails/srv-random: Done | ||||||
| Snapshot data/iocage/jails/srv-random/root: Done | Snapshot data/iocage/jails/srv-random/root: Done | ||||||
| Migrate jail config dataset to fastdata/iocage/jails/srv-random: Done | Migrate jail config dataset to fastdata/iocage/jails/srv-random: Done | ||||||
| Migrate jail filesystem dataset to fastdata/iocage/jails/srv-random/root: Done | Migrate jail filesystem dataset to fastdata/iocage/jails/srv-random/root: Done | ||||||
| </pre></code> | </code></pre> | ||||||
|  |  | ||||||
|  |  | ||||||
| Fetch   | Fetch   | ||||||
| ---------- | ---------- | ||||||
| Files can be fetched from custom repository, or from local directory with "from" option.   | Files can be fetched from custom repository, or from local directory with "from" option.   | ||||||
| For example if you destroyed releases/12.3-RELEASE and still have the downloaded files in /iocage/download/12.3-RELEASE:   | For example if you destroyed releases/12.3-RELEASE and still have the downloaded files in /iocage/download/12.3-RELEASE:   | ||||||
| <pre><code> | <pre><code>gocage fetch -r 12.3 -d iocage -f file:/iocage/download | ||||||
| gocage fetch -r 12.3 -o iocage --from file:/iocage/download | </code></pre> | ||||||
| </pre></code> |  | ||||||
|  |  | ||||||
|  |  | ||||||
| TODO | TODO | ||||||
| ---------- | ---------- | ||||||
| gocage create from templates   | gocage create from templates   | ||||||
| gocage init   |  | ||||||
|   create default pool with defaults.json   |  | ||||||
|  | |||||||
							
								
								
									
										11
									
								
								TODO.md
									
									
									
									
									
								
							
							
						
						
									
										11
									
								
								TODO.md
									
									
									
									
									
								
							| @ -1,8 +1,19 @@ | |||||||
| Replicating jails between two servers (use zrepl) | Replicating jails between two servers (use zrepl) | ||||||
|  | Manage remote jails : | ||||||
|  |   - Make gocage a service | ||||||
|  |   - All commands should become API endpoint | ||||||
|  |   - How to handle authentication ? | ||||||
|  |  | ||||||
| DEBUG:  | DEBUG:  | ||||||
| - cmd/list.go:275: | - cmd/list.go:275: | ||||||
|     // FIXME ??? Shouldn't be ioc-$Name ? |     // FIXME ??? Shouldn't be ioc-$Name ? | ||||||
|     j.InternalName = rj.Name |     j.InternalName = rj.Name | ||||||
| - WriteConfigToDisk don't write neither "release" in cmd stop neither "last_started" in cmd start | - WriteConfigToDisk don't write neither "release" in cmd stop neither "last_started" in cmd start | ||||||
|  |   26/08/2023 : Last_started is updated | ||||||
|  |  | ||||||
|  | BUGS: | ||||||
|  | - unable to set values containing equal sign : | ||||||
|  |   # gocage set Config.Exec_poststart="jail -m allow.mount.linprocfs=1 name=ioc-poudriere-noo" poudriere-noo | ||||||
|  |   Error parsing args: Config.Exec_poststart=jail -m allow.mount.linprocfs=1 name=ioc-poudriere-noo | ||||||
|  | - Fix fstab when migrating jail   | ||||||
|  |  | ||||||
|  | |||||||
| @ -12,7 +12,7 @@ import ( | |||||||
| func ShellJail(args []string) error { | func ShellJail(args []string) error { | ||||||
| 	// We cant shell more than one jail bc we replace gocage execution with jexec, so there wont be no return to gocage | 	// We cant shell more than one jail bc we replace gocage execution with jexec, so there wont be no return to gocage | ||||||
| 	if len(args) > 0 { | 	if len(args) > 0 { | ||||||
| 		cj, err := getJailFromArray(args[0], []string{"jail"}, gJails) | 		cj, err := getJailFromArray(args[0], []string{"basejail", "jail"}, gJails) | ||||||
| 		if err != nil { | 		if err != nil { | ||||||
| 			fmt.Printf("Error getting jail %s: %v\n", args[0], err) | 			fmt.Printf("Error getting jail %s: %v\n", args[0], err) | ||||||
| 			return err | 			return err | ||||||
|  | |||||||
							
								
								
									
										180
									
								
								cmd/create.go
									
									
									
									
									
								
							
							
						
						
									
										180
									
								
								cmd/create.go
									
									
									
									
									
								
							| @ -5,7 +5,9 @@ import ( | |||||||
| 	"fmt" | 	"fmt" | ||||||
| 	//"log" | 	//"log" | ||||||
| 	"time" | 	"time" | ||||||
|  | 	"errors" | ||||||
| 	"strings" | 	"strings" | ||||||
|  | 	cp "github.com/otiai10/copy" | ||||||
| 	log "github.com/sirupsen/logrus" | 	log "github.com/sirupsen/logrus" | ||||||
| ) | ) | ||||||
|  |  | ||||||
| @ -14,6 +16,11 @@ func CreateJail(args []string) { | |||||||
| 	var err error | 	var err error | ||||||
| 	var jtype []string | 	var jtype []string | ||||||
|  |  | ||||||
|  | 	if gCreateArgs.BaseJail && gCreateArgs.Release == "" { | ||||||
|  | 		fmt.Println("Release should be set when creating basejail") | ||||||
|  | 		os.Exit(1) | ||||||
|  | 	} | ||||||
|  |  | ||||||
| 	if len(gCreateArgs.JailType) > 0 { | 	if len(gCreateArgs.JailType) > 0 { | ||||||
| 		jtype = []string{gCreateArgs.JailType} | 		jtype = []string{gCreateArgs.JailType} | ||||||
| 	} | 	} | ||||||
| @ -37,7 +44,7 @@ func CreateJail(args []string) { | |||||||
|  |  | ||||||
| 		var ds *Datastore | 		var ds *Datastore | ||||||
| 		if len(gCreateArgs.Datastore) > 0 { | 		if len(gCreateArgs.Datastore) > 0 { | ||||||
| 			fmt.Printf("DEBUG: Use %s datastore\n", gCreateArgs.Datastore) | 			log.Debugf("Use %s datastore\n", gCreateArgs.Datastore) | ||||||
| 			ds, err = getDatastoreFromArray(gCreateArgs.Datastore, gDatastores) | 			ds, err = getDatastoreFromArray(gCreateArgs.Datastore, gDatastores) | ||||||
| 			if err != nil { | 			if err != nil { | ||||||
| 				fmt.Printf("ERROR Getting datastore: %s\n", gCreateArgs.Datastore, err.Error()) | 				fmt.Printf("ERROR Getting datastore: %s\n", gCreateArgs.Datastore, err.Error()) | ||||||
| @ -47,37 +54,130 @@ func CreateJail(args []string) { | |||||||
| 			ds = &gDatastores[0] | 			ds = &gDatastores[0] | ||||||
| 		} | 		} | ||||||
|  |  | ||||||
| 		/* |  | ||||||
| 		// Create and populate datasets |  | ||||||
| 		err = zfsCreateDataset(fmt.Sprintf("%s/jails/%s", ds.ZFSDataset, jname), fmt.Sprintf("%s/jails/%s", ds.Mountpoint, jname), "lz4") |  | ||||||
| 		if err != nil { |  | ||||||
| 			fmt.Printf("ERROR: %s\n", err.Error()) |  | ||||||
| 			return |  | ||||||
| 		} |  | ||||||
| 		err = zfsCreateDataset(fmt.Sprintf("%s/jails/%s/root", ds.ZFSDataset, jname), fmt.Sprintf("%s/jails/%s/root", ds.Mountpoint, jname), "lz4") |  | ||||||
| 		if err != nil { |  | ||||||
| 			fmt.Printf("ERROR: %s\n", err.Error()) |  | ||||||
| 			return |  | ||||||
| 		} |  | ||||||
| 		*/ |  | ||||||
|  |  | ||||||
| 		// Get base template if specified | 		// Get base template if specified | ||||||
| 		if len(gCreateArgs.BaseTemplate) > 0 { | 		if gCreateArgs.BaseJail { | ||||||
| 			log.Debugf("Jail will be created from a base template\n") | 			/************************************************************************** | ||||||
| 			/*bj, err := getJailFromArray(jname, []string{"template"}, gJails) | 			*  Create based jail from a template | ||||||
| 			if err != nil { | 			*/ | ||||||
| 				if strings.EqualFold(err.Error(), "Jail not found") { | 			log.Debugf("Jail will be created read-only from release %s\n", gCreateArgs.Release) | ||||||
|  |  | ||||||
| 				} else { | 			// First check if we got release on the same datastore | ||||||
| 					fmt.Printf("ERROR: %s\n", err.Error()) | 			releasePath := fmt.Sprintf("%s/releases/%s/root", ds.Mountpoint, gCreateArgs.Release) | ||||||
|  | 			_, err := os.Stat(releasePath) | ||||||
|  | 			if os.IsNotExist(err) { | ||||||
|  | 				fmt.Printf("ERROR: Release locally not available. Run \"gocage fetch\"\n") | ||||||
| 				return | 				return | ||||||
| 			} | 			} | ||||||
|  |  | ||||||
|  | 			// Create jail datasets | ||||||
|  | 			dstDset := fmt.Sprintf("%s/jails/%s", ds.ZFSDataset, jname) | ||||||
|  | 			fmt.Printf("    > Initialize dataset %s\n", dstDset) | ||||||
|  | 			err = zfsCreateDataset(dstDset, "", "") | ||||||
|  | 			if err != nil { | ||||||
|  | 				fmt.Printf("ERROR creating dataset %s: %s\n", dstDset, err.Error()) | ||||||
|  | 				return | ||||||
|  | 			} | ||||||
|  | 			// Create jail root datasets | ||||||
|  | 			dstRootDset := fmt.Sprintf("%s/jails/%s/root", ds.ZFSDataset, jname) | ||||||
|  | 			fmt.Printf("    > Initialize dataset %s\n", dstRootDset) | ||||||
|  | 			err = zfsCreateDataset(dstRootDset, "", "") | ||||||
|  | 			if err != nil { | ||||||
|  | 				fmt.Printf("ERROR creating dataset %s: %s\n", dstRootDset, err.Error()) | ||||||
|  | 				return | ||||||
|  | 			} | ||||||
|  |  | ||||||
|  | 			// Create needed directories with basejail permissions | ||||||
|  | 			fmt.Printf("    > Create base read-only directories\n") | ||||||
|  | 			dstRootDir := fmt.Sprintf("%s/jails/%s/root", ds.Mountpoint, jname) | ||||||
|  | 			for _, d := range append(gBaseDirs, gEmptyDirs...) { | ||||||
|  | 				dstPath := dstRootDir | ||||||
|  | 				srcPath := releasePath | ||||||
|  | 				for _, cd := range strings.Split(d, "/") { | ||||||
|  | 					srcPath = fmt.Sprintf("%s/%s", srcPath, cd) | ||||||
|  | 					dstPath = fmt.Sprintf("%s/%s", dstPath, cd) | ||||||
|  | 					_, err := os.Stat(dstPath) | ||||||
|  | 					if errors.Is(err, os.ErrNotExist) { | ||||||
|  | 						srcPerm, err := getPermissions(srcPath) | ||||||
|  | 						if err != nil { | ||||||
|  | 							fmt.Printf("ERROR getting permissions of %s: %s\n", srcPath, err.Error()) | ||||||
|  | 							return | ||||||
|  | 						} | ||||||
|  | 						err = os.Mkdir(dstPath, srcPerm.Mode().Perm()) | ||||||
|  | 						if err != nil { | ||||||
|  | 							fmt.Printf("ERROR creating directory %s: %s\n", dstPath, err.Error()) | ||||||
|  | 							return | ||||||
|  | 						} | ||||||
|  | 					} | ||||||
|  | 				} | ||||||
|  | 			} | ||||||
|  |  | ||||||
|  | 			// Copy these from basejail | ||||||
|  | 			fmt.Printf("    > Create base writable directories\n") | ||||||
|  | 			for _, d := range gCopyDirs { | ||||||
|  | 				err := cp.Copy(fmt.Sprintf("%s/%s", releasePath, d), fmt.Sprintf("%s/%s", dstRootDir, d)) | ||||||
|  | 				if err != nil { | ||||||
|  | 					fmt.Printf("ERROR copying %s to %s: %s\n", fmt.Sprintf("%s/%s", releasePath, d), | ||||||
|  | 						   fmt.Sprintf("%s/%s", dstRootDir, d), err.Error()) | ||||||
|  | 					return | ||||||
|  | 				} | ||||||
|  | 			} | ||||||
|  |  | ||||||
|  | 			/////////////////////////////////////////////////////////////////////// | ||||||
|  | 			// Copy defaults.json... | ||||||
|  | 			jailConfPath := fmt.Sprintf("%s/jails/%s/config.json", ds.Mountpoint, jname) | ||||||
|  | 			err = copyFile(fmt.Sprintf("%s/defaults.json", ds.Mountpoint), | ||||||
|  | 				       jailConfPath) | ||||||
|  | 			if err != nil { | ||||||
|  | 				fmt.Printf("ERROR creating config.json: %s\n", err.Error()) | ||||||
|  | 				return | ||||||
|  | 			} | ||||||
|  | 			/////////////////////////////////////////////////////////////////////// | ||||||
|  | 			// ... and update it | ||||||
|  | 			jailConf, err := getJailConfig(jailConfPath) | ||||||
|  | 			if err != nil { | ||||||
|  | 				log.Println("ERROR reading jail config from %s", jailConfPath) | ||||||
|  | 			} | ||||||
|  |  | ||||||
|  | 			// Build jail object from config | ||||||
|  | 			jailRootPath := fmt.Sprintf("%s/jails/%s/%s", ds.Mountpoint, jname, "root") | ||||||
|  | 			j := Jail{ | ||||||
|  | 				Name:       jailConf.Host_hostuuid, | ||||||
|  | 				Config:     jailConf, | ||||||
|  | 				ConfigPath: jailConfPath, | ||||||
|  | 				Datastore:  ds.Name, | ||||||
|  | 				RootPath:   jailRootPath, | ||||||
|  | 				Running:    false, | ||||||
|  | 			} | ||||||
|  |  | ||||||
|  | 			// We need to store the basejail template. We could : | ||||||
|  | 			// 1. Use "origin" ? | ||||||
|  | 			// 2. Add a json item to config ("basejail_template" p.e.), but iocage would delete it once jail is started from iocage | ||||||
|  | 			// 3. Add a gocage specific config ("config.gocage.json" p.e.) | ||||||
|  | 			j.Config.Jailtype = "basejail" | ||||||
|  | 			j.Config.Origin = gCreateArgs.Release | ||||||
|  | 			j.Config.Host_hostname = jname | ||||||
|  | 			j.Config.Host_hostuuid = jname | ||||||
|  |  | ||||||
|  | 			j.WriteConfigToDisk(false) | ||||||
|  |  | ||||||
|  | 			/////////////////////////////////////////////////////////////////////// | ||||||
|  | 			// Create fstab | ||||||
|  | 			fstabHandle, err := os.Create(fmt.Sprintf("%s/jails/%s/fstab", ds.Mountpoint, jname)) | ||||||
|  | 			if err != nil { | ||||||
|  | 				fmt.Printf("ERROR creating fstab: %s", err.Error()) | ||||||
|  | 				return | ||||||
|  | 			} | ||||||
|  | 			defer fstabHandle.Close() | ||||||
|  |  | ||||||
|  | 			for _, d := range gBaseDirs { | ||||||
|  | 				fmt.Fprintf(fstabHandle, "%s\t%s\tnullfs\tro\t0\t0\n", fmt.Sprintf("%s/%s", releasePath, d), fmt.Sprintf("%s/%s", dstRootDir, d)) | ||||||
|  | 			} | ||||||
|  |  | ||||||
|  | 			fmt.Printf("  > Jail created!\n") | ||||||
| 		} else { | 		} else { | ||||||
| 				fmt.Printf("Jail exist: %s\n", jname) | 			/************************************************************************** | ||||||
| 				continue | 			 *  Create normal jail with its own freebsd base | ||||||
| 			}*/ | 			 */ | ||||||
| 		} else { |  | ||||||
| 			// Normal jail with its own freebsd base |  | ||||||
| 			log.Debugf("Creating jail with its own freebsd base\n") | 			log.Debugf("Creating jail with its own freebsd base\n") | ||||||
|  |  | ||||||
| 			// First check if we got release on the same datastore | 			// First check if we got release on the same datastore | ||||||
| @ -88,8 +188,7 @@ func CreateJail(args []string) { | |||||||
| 			} | 			} | ||||||
|  |  | ||||||
| 			/////////////////////////////////////////////////////////////////////// | 			/////////////////////////////////////////////////////////////////////// | ||||||
| 			// | 			// Create and populate jail filesystem from release | ||||||
| 			// Create and populate jail filesystem |  | ||||||
| 			dstDset := fmt.Sprintf("%s/jails/%s", ds.ZFSDataset, jname) | 			dstDset := fmt.Sprintf("%s/jails/%s", ds.ZFSDataset, jname) | ||||||
| 			fmt.Printf("    > Initialize dataset %s\n", dstDset) | 			fmt.Printf("    > Initialize dataset %s\n", dstDset) | ||||||
| 			sNow := time.Now().Format("20060102150405") | 			sNow := time.Now().Format("20060102150405") | ||||||
| @ -142,10 +241,7 @@ func CreateJail(args []string) { | |||||||
| 				return | 				return | ||||||
| 			} | 			} | ||||||
|  |  | ||||||
| 			fmt.Printf("Jail filesystem successfuly initalized\n") |  | ||||||
| 			 |  | ||||||
| 			/////////////////////////////////////////////////////////////////////// | 			/////////////////////////////////////////////////////////////////////// | ||||||
| 			// |  | ||||||
| 			// Copy defaults.json... | 			// Copy defaults.json... | ||||||
| 			jailConfPath := fmt.Sprintf("%s/jails/%s/config.json", ds.Mountpoint, jname) | 			jailConfPath := fmt.Sprintf("%s/jails/%s/config.json", ds.Mountpoint, jname) | ||||||
| 			err = copyFile(fmt.Sprintf("%s/defaults.json", ds.Mountpoint),  | 			err = copyFile(fmt.Sprintf("%s/defaults.json", ds.Mountpoint),  | ||||||
| @ -155,15 +251,13 @@ func CreateJail(args []string) { | |||||||
| 				return | 				return | ||||||
| 			} | 			} | ||||||
| 			/////////////////////////////////////////////////////////////////////// | 			/////////////////////////////////////////////////////////////////////// | ||||||
| 			// |  | ||||||
| 			// ... and update it | 			// ... and update it | ||||||
| 			// Get conf from config.json |  | ||||||
| 			jailConf, err := getJailConfig(jailConfPath) | 			jailConf, err := getJailConfig(jailConfPath) | ||||||
| 			if err != nil { | 			if err != nil { | ||||||
| 				log.Println("ERROR reading jail config from %s", jailConfPath) | 				log.Println("ERROR reading jail config from %s", jailConfPath) | ||||||
| 			} | 			} | ||||||
|  |  | ||||||
| 			// 2. Build jail object from config | 			// Build jail object from config | ||||||
| 			jailRootPath := fmt.Sprintf("%s/jails/%s/%s", ds.Mountpoint, jname, "root") | 			jailRootPath := fmt.Sprintf("%s/jails/%s/%s", ds.Mountpoint, jname, "root") | ||||||
| 			j := Jail{ | 			j := Jail{ | ||||||
| 				Name:       jailConf.Host_hostuuid, | 				Name:       jailConf.Host_hostuuid, | ||||||
| @ -177,11 +271,10 @@ func CreateJail(args []string) { | |||||||
| 			j.Config.Release = gCreateArgs.Release | 			j.Config.Release = gCreateArgs.Release | ||||||
| 			j.Config.Host_hostname = jname | 			j.Config.Host_hostname = jname | ||||||
| 			j.Config.Host_hostuuid = jname | 			j.Config.Host_hostuuid = jname | ||||||
| 						 | 			j.Config.Jailtype = "jail" | ||||||
| 			j.WriteConfigToDisk(false) | 			j.WriteConfigToDisk(false) | ||||||
|  |  | ||||||
| 			/////////////////////////////////////////////////////////////////////// | 			/////////////////////////////////////////////////////////////////////// | ||||||
| 			// |  | ||||||
| 			// Create fstab | 			// Create fstab | ||||||
| 			fstabHandle, err := os.Create(fmt.Sprintf("%s/jails/%s/fstab", ds.Mountpoint, jname)) | 			fstabHandle, err := os.Create(fmt.Sprintf("%s/jails/%s/fstab", ds.Mountpoint, jname)) | ||||||
| 			if err != nil { | 			if err != nil { | ||||||
| @ -189,8 +282,19 @@ func CreateJail(args []string) { | |||||||
| 				return | 				return | ||||||
| 			} | 			} | ||||||
| 			defer fstabHandle.Close() | 			defer fstabHandle.Close() | ||||||
|  | 			fmt.Printf("  > Jail created!\n") | ||||||
| 		} | 		} | ||||||
|  |  | ||||||
| 		// TODO : Set JailType | 		var cmdline []string | ||||||
|  | 		for _, props := range strings.Split(gCreateArgs.Properties, ",") { | ||||||
|  | 			cmdline = append(cmdline, props) | ||||||
|  | 		} | ||||||
|  |  | ||||||
|  | 		// Reload jail list so SetJailProperties will see it | ||||||
|  | 		ListJails(nil, false) | ||||||
|  |  | ||||||
|  | 		cmdline = append(cmdline, jname) | ||||||
|  | 		log.Debugf("cmdline: \"%v\"", cmdline) | ||||||
|  | 		SetJailProperties(cmdline) | ||||||
| 	} | 	} | ||||||
| } | } | ||||||
|  | |||||||
| @ -1,85 +0,0 @@ | |||||||
| package cmd |  | ||||||
|  |  | ||||||
| const ( |  | ||||||
| 	fbsdUpdateConfig = ` |  | ||||||
| 	# $FreeBSD$ |  | ||||||
| 	 |  | ||||||
| 	# Trusted keyprint.  Changing this is a Bad Idea unless you've received |  | ||||||
| 	# a PGP-signed email from <security-officer@FreeBSD.org> telling you to |  | ||||||
| 	# change it and explaining why. |  | ||||||
| 	KeyPrint 800651ef4b4c71c27e60786d7b487188970f4b4169cc055784e21eb71d410cc5 |  | ||||||
| 	 |  | ||||||
| 	# Server or server pool from which to fetch updates.  You can change |  | ||||||
| 	# this to point at a specific server if you want, but in most cases |  | ||||||
| 	# using a "nearby" server won't provide a measurable improvement in |  | ||||||
| 	# performance. |  | ||||||
| 	ServerName update.FreeBSD.org |  | ||||||
| 	 |  | ||||||
| 	# Components of the base system which should be kept updated. |  | ||||||
| 	Components world |  | ||||||
| 	 |  | ||||||
| 	# Example for updating the userland and the kernel source code only: |  | ||||||
| 	# Components src/base src/sys world |  | ||||||
| 	 |  | ||||||
| 	# Paths which start with anything matching an entry in an IgnorePaths |  | ||||||
| 	# statement will be ignored. |  | ||||||
| 	IgnorePaths |  | ||||||
| 	 |  | ||||||
| 	# Paths which start with anything matching an entry in an IDSIgnorePaths |  | ||||||
| 	# statement will be ignored by "freebsd-update IDS". |  | ||||||
| 	IDSIgnorePaths /usr/share/man/cat |  | ||||||
| 	IDSIgnorePaths /usr/share/man/whatis |  | ||||||
| 	IDSIgnorePaths /var/db/locate.database |  | ||||||
| 	IDSIgnorePaths /var/log |  | ||||||
| 	 |  | ||||||
| 	# Paths which start with anything matching an entry in an UpdateIfUnmodified |  | ||||||
| 	# statement will only be updated if the contents of the file have not been |  | ||||||
| 	# modified by the user (unless changes are merged; see below). |  | ||||||
| 	UpdateIfUnmodified /etc/ /var/ /root/ /.cshrc /.profile |  | ||||||
| 	 |  | ||||||
| 	# When upgrading to a new FreeBSD release, files which match MergeChanges |  | ||||||
| 	# will have any local changes merged into the version from the new release. |  | ||||||
| 	MergeChanges /etc/ |  | ||||||
| 	 |  | ||||||
| 	### Default configuration options: |  | ||||||
| 	 |  | ||||||
| 	# Directory in which to store downloaded updates and temporary |  | ||||||
| 	# files used by FreeBSD Update. |  | ||||||
| 	WorkDir /iocage/freebsd-update |  | ||||||
| 	 |  | ||||||
| 	# Destination to send output of "freebsd-update cron" if an error |  | ||||||
| 	# occurs or updates have been downloaded. |  | ||||||
| 	# MailTo root |  | ||||||
| 	 |  | ||||||
| 	# Is FreeBSD Update allowed to create new files? |  | ||||||
| 	# AllowAdd yes |  | ||||||
| 	 |  | ||||||
| 	# Is FreeBSD Update allowed to delete files? |  | ||||||
| 	# AllowDelete yes |  | ||||||
| 	 |  | ||||||
| 	# If the user has modified file ownership, permissions, or flags, should |  | ||||||
| 	# FreeBSD Update retain this modified metadata when installing a new version |  | ||||||
| 	# of that file? |  | ||||||
| 	# KeepModifiedMetadata yes |  | ||||||
| 	 |  | ||||||
| 	# When upgrading between releases, should the list of Components be |  | ||||||
| 	# read strictly (StrictComponents yes) or merely as a list of components |  | ||||||
| 	# which *might* be installed of which FreeBSD Update should figure out |  | ||||||
| 	# which actually are installed and upgrade those (StrictComponents no)? |  | ||||||
| 	StrictComponents yes |  | ||||||
| 	 |  | ||||||
| 	# When installing a new kernel perform a backup of the old one first |  | ||||||
| 	# so it is possible to boot the old kernel in case of problems. |  | ||||||
| 	BackupKernel no |  | ||||||
| 	 |  | ||||||
| 	# If BackupKernel is enabled, the backup kernel is saved to this |  | ||||||
| 	# directory. |  | ||||||
| 	# BackupKernelDir /boot/kernel.old |  | ||||||
| 	 |  | ||||||
| 	# When backing up a kernel also back up debug symbol files? |  | ||||||
| 	BackupKernelSymbolFiles no |  | ||||||
| 	 |  | ||||||
| 	# Create a new boot environment when installing patches |  | ||||||
| 	CreateBootEnv no |  | ||||||
| 	` |  | ||||||
| )  |  | ||||||
							
								
								
									
										153
									
								
								cmd/init.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										153
									
								
								cmd/init.go
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,153 @@ | |||||||
|  | package cmd | ||||||
|  |  | ||||||
|  | import ( | ||||||
|  | 	"os" | ||||||
|  | 	"fmt" | ||||||
|  | 	"strings" | ||||||
|  |  | ||||||
|  | 	"github.com/spf13/viper" | ||||||
|  | 	log "github.com/sirupsen/logrus" | ||||||
|  | ) | ||||||
|  |  | ||||||
|  | /******************************************************************************** | ||||||
|  |  * Initialize datastore(s) /iocage, /iocage/jails | ||||||
|  |  * Put defaults.json, | ||||||
|  |  * Update it with hostid, interfaces, and maybe other necessary fields | ||||||
|  |  * Initialize bridge | ||||||
|  |  *******************************************************************************/ | ||||||
|  | func InitGoCage(args []string) { | ||||||
|  | 	// Create datastores | ||||||
|  | 	for _, dstore := range viper.GetStringSlice("datastore") { | ||||||
|  | 		log.Debugf("Ranging over %v\n", dstore) | ||||||
|  | 		dset, err := zfsGetDatasetByMountpoint(dstore) | ||||||
|  | 		if err != nil && strings.HasSuffix(err.Error(), "No such file or directory\"") { | ||||||
|  | 			if len(gZPool) == 0 { | ||||||
|  | 				log.Errorf("Datastore mountpoint \"%s\" does not exist. Specify a pool if you want to create it.", dstore) | ||||||
|  | 				return | ||||||
|  | 			} | ||||||
|  | 			// Create dataset /iocage | ||||||
|  | 			rootDSName := fmt.Sprintf("%s%s", gZPool, dstore) | ||||||
|  | 			log.Debugf("Creating dataset %s mounted on %s\n", rootDSName, dstore) | ||||||
|  | 			if err = zfsCreateDataset(rootDSName, dstore, ""); err != nil { | ||||||
|  | 				log.Errorf("Error creating dataset %s: %v\n", rootDSName, err) | ||||||
|  | 				return | ||||||
|  | 			} | ||||||
|  | 			// Create /iocage/jail, releases, templates | ||||||
|  | 			for _, l := range []string{"jails","releases","templates"} { | ||||||
|  | 				cds := fmt.Sprintf("%s/%s", rootDSName, l) | ||||||
|  | 				cmp := fmt.Sprintf("%s/%s", dstore, l) | ||||||
|  | 				log.Debugf("Creating dataset %s mounted on %s\n", cds, cmp) | ||||||
|  | 				if err = zfsCreateDataset(cds, cmp, ""); err != nil { | ||||||
|  | 					log.Errorf("Error creating dataset %s: %v\n", cds, err) | ||||||
|  | 					return | ||||||
|  | 				} | ||||||
|  | 			} | ||||||
|  | 			 | ||||||
|  | 			// Create /iocage/defaults.json | ||||||
|  | 			exists, err := doFileExist(fmt.Sprintf("%s/defaults.json", dstore)) | ||||||
|  | 			if err != nil { | ||||||
|  | 				log.Errorf("Error checking defaults.json: %v\n", err) | ||||||
|  | 				return | ||||||
|  | 			} | ||||||
|  | 			if !exists { | ||||||
|  | 				if err = createDefaultsJson(dstore, gBridge); err != nil { | ||||||
|  | 					log.Errorf("%v\n", err) | ||||||
|  | 				} | ||||||
|  | 			} | ||||||
|  | 		} else if err != nil { | ||||||
|  | 			log.Errorf("Error checking datastore existence: %v\n", err) | ||||||
|  | 			return | ||||||
|  | 		} else { | ||||||
|  | 			log.Debugf("Datastore dataset exist: %s\n", dset) | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
|  | 	// Check and create bridge | ||||||
|  | 	// FIXME: What if bridge name is invalid, as we already wrote it in defaults.json in dstore loop? | ||||||
|  | 	if len(gBridge) > 0 && len(gInterface) > 0 { | ||||||
|  | 		if err := initBridge(); err != nil { | ||||||
|  | 			log.Errorf("%v\n", err) | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func createDefaultsJson(rootDirectory string, bridge string) error { | ||||||
|  | 	hostid, err := os.ReadFile("/etc/hostid") | ||||||
|  | 	if err != nil { | ||||||
|  | 		log.Fatalf("Unable to read /etc/hostid: %v\n", err) | ||||||
|  | 	} | ||||||
|  | 	json := strings.Replace(gDefaultsJson, "TO-BE-REPLACED-WITH-HOSTID", strings.Trim(string(hostid), "\n"), 1) | ||||||
|  | 	json = strings.Replace(json, "TO-BE-REPLACED-WITH-BRIDGE", bridge, 1) | ||||||
|  | 	if err := os.WriteFile(fmt.Sprintf("%s/defaults.json", rootDirectory), []byte(json), 0640); err != nil { | ||||||
|  | 		log.Fatal(err) | ||||||
|  | 	} | ||||||
|  | 	 | ||||||
|  | 	return nil | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func createInterface(iface string) error { | ||||||
|  | 	log.Debugf("creating interface \"%s\"\n", iface) | ||||||
|  | 	cmd := fmt.Sprintf("/sbin/ifconfig %s create", iface) | ||||||
|  | 	_, err := executeCommand(cmd) | ||||||
|  | 	if err != nil { | ||||||
|  | 		return err | ||||||
|  | 	} | ||||||
|  | 	return nil | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func bringUpInterface(iface string) error { | ||||||
|  | 	log.Debugf("bringing up interface \"%s\"\n", iface) | ||||||
|  | 	cmd := fmt.Sprintf("/sbin/ifconfig %s up", iface) | ||||||
|  | 	_, err := executeCommand(cmd) | ||||||
|  | 	if err != nil { | ||||||
|  | 		return err | ||||||
|  | 	} | ||||||
|  | 	return nil | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func addMemberToBridge(bridge string, iface string) error { | ||||||
|  | 	log.Debugf("adding member interface \"%s\" to bridge \"%s\"\n", iface, bridge) | ||||||
|  | 	cmd := fmt.Sprintf("/sbin/ifconfig %s addm %s", bridge, iface) | ||||||
|  | 	_, err := executeCommand(cmd) | ||||||
|  | 	if err != nil { | ||||||
|  | 		return err | ||||||
|  | 	} | ||||||
|  | 	return nil | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func initBridge() error { | ||||||
|  | 	hostInt, err := gJailHost.GetInterfaces() | ||||||
|  | 	if err != nil { | ||||||
|  | 		return fmt.Errorf("Error listing interfaces: %v\n", err) | ||||||
|  | 	} | ||||||
|  | 	if !isStringInArray(hostInt, gInterface) { | ||||||
|  | 		return fmt.Errorf("Interface not found: %s\n", gInterface) | ||||||
|  | 	} | ||||||
|  | 	if !isStringInArray(hostInt, gBridge) { | ||||||
|  | 		if err := createInterface(gBridge); err != nil { | ||||||
|  | 			return fmt.Errorf("Error creating bridge: %v\n", err) | ||||||
|  | 		} | ||||||
|  | 		if err := bringUpInterface(gBridge); err != nil { | ||||||
|  | 			return fmt.Errorf("Error bringing up bridge: %v\n", err) | ||||||
|  | 		} | ||||||
|  | 		log.Infof("bridge was created, but it won't persist reboot. Configure rc.conf to persist. See https://docs.freebsd.org/en/books/handbook/advanced-networking/#network-bridging\n") | ||||||
|  | 		log.Infof("It is strongly suggested you move interface %s IP to bridge %s\n", gInterface, gBridge) | ||||||
|  | 	} | ||||||
|  | 	// FIXME: Need to check if not already member | ||||||
|  | 	members, err := getBridgeMembers(gBridge) | ||||||
|  | 	if err != nil { | ||||||
|  | 		return fmt.Errorf("Error getting bridge members: %v\n", err) | ||||||
|  | 	} | ||||||
|  | 	 | ||||||
|  | 	// Return if interface already member of the bridge | ||||||
|  | 	for _, m := range members { | ||||||
|  | 		log.Debugf("Bridge member: %s\n", m) | ||||||
|  | 		if strings.EqualFold(m, gInterface) { | ||||||
|  | 			return nil | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
|  | 	if err := addMemberToBridge(gBridge, gInterface); err != nil { | ||||||
|  | 		return fmt.Errorf("Error adding interface to bridge: %v\n", err) | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	return nil | ||||||
|  | } | ||||||
| @ -4,7 +4,6 @@ import ( | |||||||
| 	"fmt" | 	"fmt" | ||||||
| 	"net" | 	"net" | ||||||
| 	"regexp" | 	"regexp" | ||||||
| 	"strconv" |  | ||||||
| 	"strings" | 	"strings" | ||||||
| 	"io/ioutil" | 	"io/ioutil" | ||||||
| 	"golang.org/x/net/route" | 	"golang.org/x/net/route" | ||||||
| @ -190,34 +189,17 @@ func getArch() (string, error) { | |||||||
|  |  | ||||||
| func getFreeBSDVersion() (FreeBSDVersion, error) { | func getFreeBSDVersion() (FreeBSDVersion, error) { | ||||||
| 	var version FreeBSDVersion | 	var version FreeBSDVersion | ||||||
| 	regex := `([0-9]{1,2})(\.)?([0-9]{1,2})?\-([^\-]*)(\-)?(p[0-9]{1,2})?` |  | ||||||
| 	 |  | ||||||
| 	re := regexp.MustCompile(regex) |  | ||||||
| 	 | 	 | ||||||
| 	out, err := executeCommand("/bin/freebsd-version") | 	out, err := executeCommand("/bin/freebsd-version") | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return version, fmt.Errorf("Error executing \"/bin/freebsd-version\": %v", err) | 		return version, fmt.Errorf("Error executing \"/bin/freebsd-version\": %v", err) | ||||||
| 	} | 	} | ||||||
| 	 | 	 | ||||||
| 	if re.MatchString(out) { | 	version, err = freebsdVersionToStruct(out) | ||||||
| 		version.major, err = strconv.Atoi(re.FindStringSubmatch(out)[1]) |  | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return version, err | 		return version, err | ||||||
| 	} | 	} | ||||||
| 		version.minor, err = strconv.Atoi(re.FindStringSubmatch(out)[3]) |  | ||||||
| 		if err != nil { |  | ||||||
| 			return version, err |  | ||||||
| 		} |  | ||||||
| 		version.flavor = strings.Trim(re.FindStringSubmatch(out)[4], "\n") |  | ||||||
|  |  | ||||||
| 		// Skip the 'p' starting patch level |  | ||||||
| 		if len(re.FindStringSubmatch(out)[6]) > 0 { |  | ||||||
| 			version.patchLevel, err = strconv.Atoi(re.FindStringSubmatch(out)[6][1:]) |  | ||||||
| 			if err != nil { |  | ||||||
| 				return version, err |  | ||||||
| 			} |  | ||||||
| 		} |  | ||||||
| 	} |  | ||||||
| 	return version, nil | 	return version, nil | ||||||
| } | } | ||||||
|  |  | ||||||
|  | |||||||
| @ -275,6 +275,13 @@ func listJailsFromDirectory(dir string, dsname string) ([]Jail, error) { | |||||||
| 					// FIXME ??? Shouldn't be ioc-$Name ? | 					// FIXME ??? Shouldn't be ioc-$Name ? | ||||||
| 					j.InternalName = rj.Name | 					j.InternalName = rj.Name | ||||||
| 					j.Devfs_ruleset = rj.Devfs_ruleset | 					j.Devfs_ruleset = rj.Devfs_ruleset | ||||||
|  | 					// Update release | ||||||
|  | 					r, err := getVersion(&j) | ||||||
|  | 					if err != nil { | ||||||
|  | 						fmt.Printf("ERROR getting jail %s version: %s\n", j.Name, err.Error()) | ||||||
|  | 					} else { | ||||||
|  | 						j.Config.Release = r | ||||||
|  | 					} | ||||||
| 					break | 					break | ||||||
| 				} | 				} | ||||||
| 			} | 			} | ||||||
|  | |||||||
| @ -1,8 +1,9 @@ | |||||||
| package cmd | package cmd | ||||||
|  |  | ||||||
| import ( | import ( | ||||||
| 	"errors" | 	"os" | ||||||
| 	"fmt" | 	"fmt" | ||||||
|  | 	"errors" | ||||||
| 	"reflect" | 	"reflect" | ||||||
| 	"strconv" | 	"strconv" | ||||||
| 	"strings" | 	"strings" | ||||||
| @ -20,7 +21,7 @@ func GetJailProperties(args []string) { | |||||||
| 				jail, err = getJailFromArray(a, []string{""}, gJails) | 				jail, err = getJailFromArray(a, []string{""}, gJails) | ||||||
| 				if err != nil { | 				if err != nil { | ||||||
| 					fmt.Printf("Error: %s\n", err.Error()) | 					fmt.Printf("Error: %s\n", err.Error()) | ||||||
| 					return | 					os.Exit(1) | ||||||
| 				} | 				} | ||||||
| 			} else { | 			} else { | ||||||
| 				props = append(props, a) | 				props = append(props, a) | ||||||
| @ -105,20 +106,20 @@ func SetJailProperties(args []string) { | |||||||
| 		return | 		return | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	// Get jail by index to modify it | 	cj, err := getJailFromArray(jail.Name, []string{""}, gJails) | ||||||
| 	for i, _ := range gJails { | 	if err != nil { | ||||||
| 		if gJails[i].Name == jail.Name { | 		fmt.Printf("Error getting jail %s: %v\n", jail.Name, err) | ||||||
|  | 		return | ||||||
|  | 	} | ||||||
|  |  | ||||||
| 	for _, p := range props { | 	for _, p := range props { | ||||||
| 				err := setStructFieldValue(&gJails[i], p.name, p.value) | 		err := setStructFieldValue(cj, p.name, p.value) | ||||||
| 		if err != nil { | 		if err != nil { | ||||||
| 			fmt.Printf("Error: %s\n", err.Error()) | 			fmt.Printf("Error: %s\n", err.Error()) | ||||||
| 			return | 			return | ||||||
| 		} else { | 		} else { | ||||||
| 					fmt.Printf("%s: %s set to %s\n", gJails[i].Name, p.name, p.value) | 			fmt.Printf("%s: %s set to %s\n", cj.Name, p.name, p.value) | ||||||
| 					gJails[i].ConfigUpdated = true |  | ||||||
| 				} |  | ||||||
| 			} |  | ||||||
| 			writeConfigToDisk(&gJails[i], false) |  | ||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
|  | 	cj.WriteConfigToDisk(false) | ||||||
| } | } | ||||||
|  | |||||||
							
								
								
									
										80
									
								
								cmd/root.go
									
									
									
									
									
								
							
							
						
						
									
										80
									
								
								cmd/root.go
									
									
									
									
									
								
							| @ -14,7 +14,7 @@ import ( | |||||||
| ) | ) | ||||||
|  |  | ||||||
| const ( | const ( | ||||||
| 	gVersion = "0.37" | 	gVersion = "0.42h" | ||||||
| 	 | 	 | ||||||
| 	// TODO : Get from $jail_zpool/defaults.json | 	// TODO : Get from $jail_zpool/defaults.json | ||||||
| 	MIN_DYN_DEVFS_RULESET = 1000 | 	MIN_DYN_DEVFS_RULESET = 1000 | ||||||
| @ -22,9 +22,10 @@ const ( | |||||||
|  |  | ||||||
| type createArgs struct { | type createArgs struct { | ||||||
| 	Release      string | 	Release      string | ||||||
| 	BaseTemplate string | 	BaseJail     bool | ||||||
| 	Datastore    string | 	Datastore    string | ||||||
| 	JailType     string | 	JailType     string | ||||||
|  | 	Properties   string | ||||||
| } | } | ||||||
|  |  | ||||||
| var ( | var ( | ||||||
| @ -51,11 +52,15 @@ var ( | |||||||
| 	gNoJailLineSep   bool | 	gNoJailLineSep   bool | ||||||
| 	gNoSnapLineSep   bool | 	gNoSnapLineSep   bool | ||||||
| 	gNoDSLineSep     bool | 	gNoDSLineSep     bool | ||||||
|  | 	gBridgeStaticMac bool | ||||||
|  |  | ||||||
| 	gHostVersion float64 | 	gHostVersion float64 | ||||||
|  |  | ||||||
| 	gTimeZone     string | 	gTimeZone     string | ||||||
| 	gSnapshotName string | 	gSnapshotName string | ||||||
|  | 	gZPool        string | ||||||
|  | 	gBridge       string | ||||||
|  | 	gInterface    string | ||||||
|  |  | ||||||
| 	gMigrateDestDatastore string | 	gMigrateDestDatastore string | ||||||
| 	gYesToAll             bool | 	gYesToAll             bool | ||||||
| @ -64,6 +69,16 @@ var ( | |||||||
| 	gFetchIntoDS     string | 	gFetchIntoDS     string | ||||||
| 	gFetchFrom       string | 	gFetchFrom       string | ||||||
| 	gUpgradeRelease  string | 	gUpgradeRelease  string | ||||||
|  | 	gUpdateRelease   string | ||||||
|  | 	gUpdateReleaseDS string | ||||||
|  |  | ||||||
|  | 	// For a based jail, these are directories binded to basejail | ||||||
|  | 	gBaseDirs = []string{"bin", "boot", "lib", "libexec", "rescue", "sbin", "usr/bin", "usr/include", | ||||||
|  | 		"usr/lib", "usr/lib32", "usr/libdata", "usr/libexec", "usr/sbin", "usr/share"} | ||||||
|  | 	// These directories are to be created empty | ||||||
|  | 	gEmptyDirs = []string{"dev", "media", "mnt", "net", "proc"} | ||||||
|  | 	// Copy these from base template | ||||||
|  | 	gCopyDirs = []string{"etc", "root", "tmp", "var"} | ||||||
|  |  | ||||||
| 	gMdevfs          sync.Mutex | 	gMdevfs          sync.Mutex | ||||||
|  |  | ||||||
| @ -85,23 +100,21 @@ It support iocage jails and can coexist with iocage.`, | |||||||
| 		Long:  `Let this show you how much fail I had to get this *cough* perfect`, | 		Long:  `Let this show you how much fail I had to get this *cough* perfect`, | ||||||
| 		Run: func(cmd *cobra.Command, args []string) { | 		Run: func(cmd *cobra.Command, args []string) { | ||||||
| 			fv, _ := getFreeBSDVersion() | 			fv, _ := getFreeBSDVersion() | ||||||
|  | 			if fv.patchLevel > 0 { | ||||||
|  | 				fmt.Printf("GoCage v.%s on FreeBSD %d.%d-%s-p%d\n", gVersion, fv.major, fv.minor, fv.flavor, fv.patchLevel) | ||||||
|  | 			} else { | ||||||
| 				fmt.Printf("GoCage v.%s on FreeBSD %d.%d-%s\n", gVersion, fv.major, fv.minor, fv.flavor) | 				fmt.Printf("GoCage v.%s on FreeBSD %d.%d-%s\n", gVersion, fv.major, fv.minor, fv.flavor) | ||||||
|  | 			} | ||||||
| 		}, | 		}, | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	/* TODO |  | ||||||
| 	Initialize datastore(s) /iocage, /iocage/jails |  | ||||||
| 	Put defaults.json, update it with hostid,interfaces, and maybe other necessary fields |  | ||||||
| 	Initialize bridge |  | ||||||
| 	initCmd = &cobra.Command{ | 	initCmd = &cobra.Command{ | ||||||
| 		Use:   "init", | 		Use:   "init", | ||||||
| 		Short: "Initialize GoCage", | 		Short: "Initialize GoCage", | ||||||
| 		//Long:  `Let this show you how much fail I had to get this *cough* perfect`, |  | ||||||
| 		Run: func(cmd *cobra.Command, args []string) { | 		Run: func(cmd *cobra.Command, args []string) { | ||||||
| 			fv, _ := getFreeBSDVersion() | 			InitGoCage(args) | ||||||
| 			fmt.Printf("GoCage v.%s on FreeBSD %d.%d-%s\n", gVersion, fv.major, fv.minor, fv.flavor) |  | ||||||
| 		}, | 		}, | ||||||
| 	}*/ | 	} | ||||||
|  |  | ||||||
| 	listCmd = &cobra.Command{ | 	listCmd = &cobra.Command{ | ||||||
| 		Use:   "list", | 		Use:   "list", | ||||||
| @ -371,6 +384,11 @@ func init() { | |||||||
| 	rootCmd.PersistentFlags().BoolVar(&gDebug, "debug", false, "Debug mode") | 	rootCmd.PersistentFlags().BoolVar(&gDebug, "debug", false, "Debug mode") | ||||||
|  |  | ||||||
| 	// Command dependant switches | 	// Command dependant switches | ||||||
|  | 	initCmd.Flags().StringVarP(&gZPool, "pool", "p", "", "ZFS pool to create datastore on") | ||||||
|  | 	initCmd.Flags().StringVarP(&gBridge, "bridge", "b", "", "bridge to create for jails networking") | ||||||
|  | 	initCmd.Flags().StringVarP(&gInterface, "interface", "i", "", "interface to add as bridge member. This should be your main interface") | ||||||
|  | 	initCmd.MarkFlagRequired("bridge") | ||||||
|  | 	initCmd.MarkFlagsRequiredTogether("bridge", "interface") | ||||||
|  |  | ||||||
| 	// We reuse these flags in "gocage snapshot list myjail" and 'gocage datastore list" commands | 	// We reuse these flags in "gocage snapshot list myjail" and 'gocage datastore list" commands | ||||||
| 	listCmd.Flags().StringVarP(&gDisplayJColumns, "outcol", "o", "JID,Name,Config.Release,Config.Ip4_addr,Running", "Show these columns in output") | 	listCmd.Flags().StringVarP(&gDisplayJColumns, "outcol", "o", "JID,Name,Config.Release,Config.Ip4_addr,Running", "Show these columns in output") | ||||||
| @ -403,22 +421,28 @@ func init() { | |||||||
| 	migrateCmd.MarkFlagRequired("datastore") | 	migrateCmd.MarkFlagRequired("datastore") | ||||||
|  |  | ||||||
| 	fetchCmd.Flags().StringVarP(&gFetchRelease, "release", "r", "", "Release to fetch (e.g.: \"13.1-RELEASE\"") | 	fetchCmd.Flags().StringVarP(&gFetchRelease, "release", "r", "", "Release to fetch (e.g.: \"13.1-RELEASE\"") | ||||||
| 	fetchCmd.Flags().StringVarP(&gFetchIntoDS, "datastore", "o", "", "Datastore release will be saved to") | 	fetchCmd.Flags().StringVarP(&gFetchIntoDS, "datastore", "d", "", "Datastore release will be saved to") | ||||||
| 	fetchCmd.Flags().StringVarP(&gFetchFrom, "from", "d", "", "Repository to download from. Should contain XY.Z-RELEASE. File protocol supported") | 	fetchCmd.Flags().StringVarP(&gFetchFrom, "from", "f", "", "Repository to download from. Should contain XY.Z-RELEASE. File protocol supported") | ||||||
| 	fetchCmd.MarkFlagRequired("release") | 	fetchCmd.MarkFlagRequired("release") | ||||||
| 	fetchCmd.MarkFlagRequired("datastore") | 	fetchCmd.MarkFlagRequired("datastore") | ||||||
|  |  | ||||||
| 	upgradeCmd.Flags().StringVarP(&gUpgradeRelease, "release", "r", "", "Release to upgrade to (e.g.: \"13.1-RELEASE\"") | 	upgradeCmd.Flags().StringVarP(&gUpgradeRelease, "release", "r", "", "Release to upgrade to (e.g.: \"13.1-RELEASE\"") | ||||||
| 	upgradeCmd.MarkFlagRequired("release") | 	upgradeCmd.MarkFlagRequired("release") | ||||||
|  |  | ||||||
|  | 	updateCmd.Flags().StringVarP(&gUpdateRelease, "release", "r", "", "Release to update (e.g.: \"13.1-RELEASE\"") | ||||||
|  | 	updateCmd.Flags().StringVarP(&gUpdateReleaseDS, "datastore", "d", "", "Datastore release is stored on") | ||||||
|  | 	updateCmd.MarkFlagsRequiredTogether("release", "datastore") | ||||||
|  |  | ||||||
| 	createCmd.Flags().StringVarP(&gCreateArgs.Release, "release", "r", "", "Release for the jail (e.g.: \"13.1-RELEASE\"") | 	createCmd.Flags().StringVarP(&gCreateArgs.Release, "release", "r", "", "Release for the jail (e.g.: \"13.1-RELEASE\"") | ||||||
| 	createCmd.Flags().StringVarP(&gCreateArgs.BaseTemplate, "basetpl", "b", "", "Base template. This will create a jail based on basetpl, so every up(date|grade) made to basetpl will immediately propagate to new jail\n") | 	createCmd.Flags().BoolVarP(&gCreateArgs.BaseJail, "basejail", "b", false, "Basejail. This will create a jail mounted read only from a release, so every up(date|grade) made to this release will immediately propagate to new jail.\n") | ||||||
| 	createCmd.Flags().StringVarP(&gCreateArgs.Datastore, "datastore", "d", "", "Datastore to create the jail on. Defaults to first declared in config.") | 	createCmd.Flags().StringVarP(&gCreateArgs.Datastore, "datastore", "d", "", "Datastore to create the jail on. Defaults to first declared in config.") | ||||||
|  | 	createCmd.Flags().StringVarP(&gCreateArgs.Properties, "configuration", "p", "", "Configuration properties with format k1=v1,k2=v2 (Ex: \"Config.Ip4_addr=vnet0|192.168.1.2,Config.Ip6=none\")") | ||||||
|  |  | ||||||
| 	// Now declare commands | 	// Now declare commands | ||||||
|  | 	rootCmd.AddCommand(initCmd) | ||||||
| 	rootCmd.AddCommand(versionCmd) | 	rootCmd.AddCommand(versionCmd) | ||||||
| 	rootCmd.AddCommand(listCmd) | 	rootCmd.AddCommand(listCmd) | ||||||
| 	listCmd.AddCommand(listPropsCmd) | 	rootCmd.AddCommand(listPropsCmd) | ||||||
| 	rootCmd.AddCommand(stopCmd) | 	rootCmd.AddCommand(stopCmd) | ||||||
| 	rootCmd.AddCommand(startCmd) | 	rootCmd.AddCommand(startCmd) | ||||||
| 	rootCmd.AddCommand(restartCmd) | 	rootCmd.AddCommand(restartCmd) | ||||||
| @ -433,7 +457,6 @@ func init() { | |||||||
| 	rootCmd.AddCommand(updateCmd) | 	rootCmd.AddCommand(updateCmd) | ||||||
| 	rootCmd.AddCommand(upgradeCmd) | 	rootCmd.AddCommand(upgradeCmd) | ||||||
| 	rootCmd.AddCommand(createCmd) | 	rootCmd.AddCommand(createCmd) | ||||||
| 	 |  | ||||||
| 	rootCmd.AddCommand(testCmd) | 	rootCmd.AddCommand(testCmd) | ||||||
| 	 | 	 | ||||||
| 	snapshotCmd.AddCommand(snapshotListCmd) | 	snapshotCmd.AddCommand(snapshotListCmd) | ||||||
| @ -464,17 +487,6 @@ func initConfig() { | |||||||
| 		os.Exit(1) | 		os.Exit(1) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	// Load default configs from datastores |  | ||||||
| 	err := ListDatastores(viper.GetStringSlice("datastore"), false) |  | ||||||
| 	if err != nil { |  | ||||||
| 		fmt.Printf("ERROR: error checking datastores: %v\n", err) |  | ||||||
| 		os.Exit(1) |  | ||||||
| 	} |  | ||||||
|  |  | ||||||
| 	//	fmt.Println("Using config file:", viper.ConfigFileUsed()) |  | ||||||
| 	//	fmt.Printf("datastore in config : %s\n", viper.GetStringSlice("datastore")) |  | ||||||
| 	//	fmt.Printf("datastore.0 in config : %s\n", viper.GetStringSlice("datastore.0")) |  | ||||||
|  |  | ||||||
| 	// Command line flags have priority on config file | 	// Command line flags have priority on config file | ||||||
| 	if rootCmd.Flags().Lookup("sudo") != nil && false == rootCmd.Flags().Lookup("sudo").Changed { | 	if rootCmd.Flags().Lookup("sudo") != nil && false == rootCmd.Flags().Lookup("sudo").Changed { | ||||||
| 		gUseSudo = viper.GetBool("sudo") | 		gUseSudo = viper.GetBool("sudo") | ||||||
| @ -508,11 +520,27 @@ func initConfig() { | |||||||
| 		os.Exit(1) | 		os.Exit(1) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
|  | 	gBridgeStaticMac = viper.GetBool("static-macs") | ||||||
|  |  | ||||||
| 	if gDebug { | 	if gDebug { | ||||||
| 		log.SetLevel(log.DebugLevel) | 		log.SetLevel(log.DebugLevel) | ||||||
| 		log.Debugf("Debug mode enabled\n") | 		log.Debugf("Debug mode enabled\n") | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
|  | 	// no need to check prerequesites if we are initializing gocage | ||||||
|  | 	for _, rc := range rootCmd.Commands() { | ||||||
|  | 		//fmt.Printf("DEBUG: rootCmd subcommand: %v. Was it called? %s\n", rc.Use, rootCmd.Commands()[i].CalledAs()) | ||||||
|  | 		if len(rc.CalledAs()) > 0 && strings.EqualFold("init", rc.CalledAs()) { | ||||||
|  | 			return | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	// Load default configs from datastores | ||||||
|  | 	err := ListDatastores(viper.GetStringSlice("datastore"), false) | ||||||
|  | 	if err != nil { | ||||||
|  | 		fmt.Printf("ERROR: error checking datastores: %v\n", err) | ||||||
|  | 		os.Exit(1) | ||||||
|  | 	} | ||||||
| } | } | ||||||
|  |  | ||||||
|  |  | ||||||
|  | |||||||
							
								
								
									
										75
									
								
								cmd/start.go
									
									
									
									
									
								
							
							
						
						
									
										75
									
								
								cmd/start.go
									
									
									
									
									
								
							| @ -340,7 +340,7 @@ func configureDhcpOrAcceptRtadv(jail *Jail, ipproto int, enable bool) error { | |||||||
|  |  | ||||||
| 		if ipproto == IPv6 { | 		if ipproto == IPv6 { | ||||||
| 			key = fmt.Sprintf("%s_ipv6", key) | 			key = fmt.Sprintf("%s_ipv6", key) | ||||||
| 			value = "inet6 auto_linklocal accept_rtadv autoconf" | 			value = "\"inet6 auto_linklocal accept_rtadv autoconf\"" | ||||||
| 		} | 		} | ||||||
|  |  | ||||||
| 		if enable == true { | 		if enable == true { | ||||||
| @ -363,7 +363,7 @@ func checkRtsold(jail *Jail) error { | |||||||
| 	if strings.Contains(jail.Config.Ip6_addr, "accept_rtadv") == false { | 	if strings.Contains(jail.Config.Ip6_addr, "accept_rtadv") == false { | ||||||
| 		return fmt.Errorf("Must set at least one ip6_addr to accept_rtadv!\n") | 		return fmt.Errorf("Must set at least one ip6_addr to accept_rtadv!\n") | ||||||
| 	} | 	} | ||||||
| 	err := enableRcKeyValue(jail.ConfigPath, "rtsold_enable", "yes") | 	err := enableRcKeyValue(fmt.Sprintf("%s/etc/rc.conf", jail.RootPath), "rtsold_enable", "yes") | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return fmt.Errorf("ERROR setting rtsold_enable=YES with sysrc for jail %s: %s\n", jail.Name, err) | 		return fmt.Errorf("ERROR setting rtsold_enable=YES with sysrc for jail %s: %s\n", jail.Name, err) | ||||||
| 	} | 	} | ||||||
| @ -810,7 +810,9 @@ func generateMAC(jail *Jail, nic string) ([]byte, []byte, error) { | |||||||
| 	} | 	} | ||||||
| 	 | 	 | ||||||
| 	hsmac := append(prefix, suffix...) | 	hsmac := append(prefix, suffix...) | ||||||
| 	jsmac := append(hsmac[:5], hsmac[5]+1) | 	jsmac := make([]byte, 6) | ||||||
|  | 	copy(jsmac, hsmac) | ||||||
|  | 	jsmac[5] = jsmac[5] + 1 | ||||||
| 	 | 	 | ||||||
| 	// Save MACs to config | 	// Save MACs to config | ||||||
| 	pname := fmt.Sprintf("Config.%s_mac", strings.Title(nic)) | 	pname := fmt.Sprintf("Config.%s_mac", strings.Title(nic)) | ||||||
| @ -840,7 +842,7 @@ func setupVnetInterfaceHostSide(jail *Jail) ([]string, error) { | |||||||
| 		bridge := v[1] | 		bridge := v[1] | ||||||
| 		 | 		 | ||||||
| 		// Get host side MAC | 		// Get host side MAC | ||||||
| 		pname := fmt.Sprintf("Config.%s_mac", nic) | 		pname := fmt.Sprintf("Config.%s_mac", strings.Title(nic)) | ||||||
| 		var val *reflect.Value | 		var val *reflect.Value | ||||||
| 		val, pname, err = getStructFieldValue(jail, pname) | 		val, pname, err = getStructFieldValue(jail, pname) | ||||||
| 		if err != nil { | 		if err != nil { | ||||||
| @ -853,13 +855,22 @@ func setupVnetInterfaceHostSide(jail *Jail) ([]string, error) { | |||||||
| 				return []string{}, err | 				return []string{}, err | ||||||
| 			} | 			} | ||||||
| 		} else { | 		} else { | ||||||
| 			hsmac = val.Bytes() | 			if strings.EqualFold(val.String(), "none") { | ||||||
|  | 				hsmac, _, err = generateMAC(jail, nic) | ||||||
|  | 				if err != nil { | ||||||
|  | 					return []string{}, err | ||||||
|  | 				} | ||||||
|  | 			} | ||||||
|  | 			hsmac, err = hex.DecodeString(strings.Split(val.String(), " ")[0]) | ||||||
|  | 			if err != nil { | ||||||
|  | 				return []string{}, fmt.Errorf("Error converting %s to hex\n", strings.Split(val.String(), " ")[0]) | ||||||
|  | 			} | ||||||
| 		} | 		} | ||||||
| 		 | 		 | ||||||
| 		// Get bridge MTU | 		// Get bridge MTU | ||||||
| 		mtu, err := gJailHost.GetBridgeMTU(bridge) | 		mtu, err := gJailHost.GetBridgeMTU(bridge) | ||||||
| 		if err != nil { | 		if err != nil { | ||||||
| 			return []string{}, fmt.Errorf("Error getting bridge mtu: %v\n", err) | 			return []string{}, fmt.Errorf("Error getting bridge \"%s\" mtu: %v\n", bridge, err) | ||||||
| 		} | 		} | ||||||
| 		 | 		 | ||||||
| 		// Create epair interface | 		// Create epair interface | ||||||
| @ -899,7 +910,7 @@ func setupVnetInterfaceHostSide(jail *Jail) ([]string, error) { | |||||||
| 	return epairs, nil | 	return epairs, nil | ||||||
| } | } | ||||||
|  |  | ||||||
| func setupVnetInterfaceJailSide(jail *Jail) error { | func setupVnetInterfaceJailSide(jail *Jail, hostepairs []string) error { | ||||||
| 	var jsmac []byte | 	var jsmac []byte | ||||||
| 	var err error | 	var err error | ||||||
|  |  | ||||||
| @ -909,8 +920,10 @@ func setupVnetInterfaceJailSide(jail *Jail) error { | |||||||
| 	 | 	 | ||||||
| 	for _, i := range strings.Split(jail.Config.Ip4_addr, ",") { | 	for _, i := range strings.Split(jail.Config.Ip4_addr, ",") { | ||||||
| 		v := strings.Split(i, "|") | 		v := strings.Split(i, "|") | ||||||
|  | 		if len(v) > 1 { | ||||||
| 			ip4s[v[0]] = v[1] | 			ip4s[v[0]] = v[1] | ||||||
| 		} | 		} | ||||||
|  | 	} | ||||||
| 	for _, i := range strings.Split(jail.Config.Ip6_addr, ",") { | 	for _, i := range strings.Split(jail.Config.Ip6_addr, ",") { | ||||||
| 		v := strings.Split(i, "|") | 		v := strings.Split(i, "|") | ||||||
| 		if len(v) > 1 { | 		if len(v) > 1 { | ||||||
| @ -919,7 +932,7 @@ func setupVnetInterfaceJailSide(jail *Jail) error { | |||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	// Loop through configured interfaces | 	// Loop through configured interfaces | ||||||
| 	for _, nicCnf := range strings.Split(jail.Config.Interfaces, ",") { | 	for i, nicCnf := range strings.Split(jail.Config.Interfaces, ",") { | ||||||
| 		v := strings.Split(nicCnf, ":") | 		v := strings.Split(nicCnf, ":") | ||||||
| 		if len(v) != 2 { | 		if len(v) != 2 { | ||||||
| 			return fmt.Errorf("Invalid value for Interfaces: %s\n", nicCnf) | 			return fmt.Errorf("Invalid value for Interfaces: %s\n", nicCnf) | ||||||
| @ -930,9 +943,11 @@ func setupVnetInterfaceJailSide(jail *Jail) error { | |||||||
| 		// inside jail final nic name | 		// inside jail final nic name | ||||||
| 		jnic := strings.Replace(v[0], "vnet", "epair", 1) | 		jnic := strings.Replace(v[0], "vnet", "epair", 1) | ||||||
| 		jnic = jnic + "b" | 		jnic = jnic + "b" | ||||||
|  | 		// host side associated jail nic name | ||||||
|  | 		jsepair := fmt.Sprintf("%sb", strings.TrimSuffix(hostepairs[i], "a")) | ||||||
|  |  | ||||||
| 		// Get jail side MAC | 		// Get jail side MAC | ||||||
| 		pname := fmt.Sprintf("Config.%s_mac", nic) | 		pname := fmt.Sprintf("Config.%s_mac", strings.Title(nic)) | ||||||
| 		var val *reflect.Value | 		var val *reflect.Value | ||||||
| 		val, pname, err = getStructFieldValue(jail, pname) | 		val, pname, err = getStructFieldValue(jail, pname) | ||||||
| 		if err != nil { | 		if err != nil { | ||||||
| @ -945,10 +960,14 @@ func setupVnetInterfaceJailSide(jail *Jail) error { | |||||||
| 				return err | 				return err | ||||||
| 			} | 			} | ||||||
| 		} else { | 		} else { | ||||||
| 			jsmac = val.Bytes() | 			jsmac, err = hex.DecodeString(strings.Split(val.String(), " ")[1]) | ||||||
|  |                         if err != nil { | ||||||
|  |                                 return fmt.Errorf("Error converting %s to hex\n", strings.Split(val.String(), " ")[1]) | ||||||
|                         } |                         } | ||||||
|  |  | ||||||
| 		cmd := fmt.Sprintf("/sbin/ifconfig %s vnet %s", jnic, jail.InternalName) | 		} | ||||||
|  |  | ||||||
|  | 		cmd := fmt.Sprintf("/sbin/ifconfig %s vnet %s", jsepair, jail.InternalName) | ||||||
| 		_, err := executeCommand(cmd) | 		_, err := executeCommand(cmd) | ||||||
| 		if err != nil { | 		if err != nil { | ||||||
| 			return fmt.Errorf("Error linking interface to jail: %v\n", err) | 			return fmt.Errorf("Error linking interface to jail: %v\n", err) | ||||||
| @ -957,17 +976,17 @@ func setupVnetInterfaceJailSide(jail *Jail) error { | |||||||
| 		// Get bridge MTU | 		// Get bridge MTU | ||||||
| 		mtu, err := gJailHost.GetBridgeMTU(bridge) | 		mtu, err := gJailHost.GetBridgeMTU(bridge) | ||||||
| 		if err != nil { | 		if err != nil { | ||||||
| 			return fmt.Errorf("Error getting bridge %s mtu: %v\n", bridge, err) | 			return fmt.Errorf("Error getting bridge \"%s\" mtu: %v\n", bridge, err) | ||||||
| 		} | 		} | ||||||
|  |  | ||||||
| 		cmd = fmt.Sprintf("/usr/sbin/jexec %d ifconfig %s mtu %d", jail.JID, jnic, mtu) | 		cmd = fmt.Sprintf("/usr/sbin/jexec %d ifconfig %s mtu %d", jail.JID, jsepair, mtu) | ||||||
| 		_, err = executeCommand(cmd) | 		_, err = executeCommand(cmd) | ||||||
| 		if err != nil { | 		if err != nil { | ||||||
| 			return fmt.Errorf("Error setting mtu: %v\n", err) | 			return fmt.Errorf("Error setting mtu: %v\n", err) | ||||||
| 		} | 		} | ||||||
|  |  | ||||||
| 		// rename epairXXb to epair0b (or opair1b, ...) | 		// rename epairXXb to epair0b (or opair1b, ...) | ||||||
| 		cmd = fmt.Sprintf("/usr/sbin/setfib %s jexec %d ifconfig %s name %s", jail.Config.Exec_fib, jail.JID, jnic, jnic) | 		cmd = fmt.Sprintf("/usr/sbin/setfib %s jexec %d ifconfig %s name %s", jail.Config.Exec_fib, jail.JID, jsepair, jnic) | ||||||
| 		_, err = executeCommand(cmd) | 		_, err = executeCommand(cmd) | ||||||
| 		if err != nil { | 		if err != nil { | ||||||
| 			return fmt.Errorf("Error linking interface to jail: %v\n", err) | 			return fmt.Errorf("Error linking interface to jail: %v\n", err) | ||||||
| @ -988,6 +1007,19 @@ func setupVnetInterfaceJailSide(jail *Jail) error { | |||||||
| 			if err != nil { | 			if err != nil { | ||||||
| 				return fmt.Errorf("Error adding member %s to %s: %v: %s\n", nic, bridge, err, out) | 				return fmt.Errorf("Error adding member %s to %s: %v: %s\n", nic, bridge, err, out) | ||||||
| 			} | 			} | ||||||
|  | 			// Fix mac flapping and instabilities with bridged vnet network | ||||||
|  | 			if gBridgeStaticMac { | ||||||
|  | 				printablemac := hex.EncodeToString(jsmac) | ||||||
|  | 				for i := 2 ; i < len(printablemac) ; i += 3 { | ||||||
|  | 					printablemac = printablemac[:i] + ":" + printablemac[i:] | ||||||
|  | 				} | ||||||
|  | 				log.Debugf("Set %s.%d as static to %s in %s address cache\n", nic, jail.JID, printablemac, bridge) | ||||||
|  | 				cmd = fmt.Sprintf("/sbin/ifconfig %s static %s.%d %s", bridge, nic, jail.JID, printablemac) | ||||||
|  | 				out, err := executeCommand(cmd) | ||||||
|  | 				if err != nil { | ||||||
|  | 					return fmt.Errorf("Error setting %s.%d static with %s on %s: %v: %s\n", nic, jail.JID, printablemac, bridge, err, out) | ||||||
|  | 				} | ||||||
|  | 			} | ||||||
| 		} | 		} | ||||||
|  |  | ||||||
| 		// Check we have an IP for the nic, and set it into jail | 		// Check we have an IP for the nic, and set it into jail | ||||||
| @ -1070,9 +1102,9 @@ func StartJailsAtBoot() { | |||||||
| 	var curThNb int | 	var curThNb int | ||||||
| 	var curPri int | 	var curPri int | ||||||
|  |  | ||||||
| 	// Get boot enabled jails | 	// Get boot enabled non-template jails | ||||||
| 	for _, j := range gJails { | 	for _, j := range gJails { | ||||||
| 		if j.Config.Boot > 0 { | 		if j.Config.Boot > 0 && !strings.EqualFold(j.Config.Jailtype, "template") { | ||||||
| 			startList = append(startList, j) | 			startList = append(startList, j) | ||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
| @ -1166,7 +1198,7 @@ func StartJail(args []string) { | |||||||
|  |  | ||||||
| 	for _, a := range args { | 	for _, a := range args { | ||||||
| 		// Check if jail exist and is distinctly named | 		// Check if jail exist and is distinctly named | ||||||
| 		cj, err = getJailFromArray(a, []string{"jail"}, gJails) | 		cj, err = getJailFromArray(a, []string{"basejail", "jail"}, gJails) | ||||||
| 		if err != nil { | 		if err != nil { | ||||||
| 			fmt.Printf("Error getting jail: %s\n", err) | 			fmt.Printf("Error getting jail: %s\n", err) | ||||||
| 			continue | 			continue | ||||||
| @ -1366,7 +1398,7 @@ func StartJail(args []string) { | |||||||
| 		} | 		} | ||||||
|  |  | ||||||
| 		// Synchronize jail config to disk | 		// Synchronize jail config to disk | ||||||
| 		writeConfigToDisk(cj, false) | 		cj.WriteConfigToDisk(false) | ||||||
|  |  | ||||||
| 		start_cmd := fmt.Sprintf("/usr/sbin/jail -f /var/run/jail.%s.conf -c", cj.InternalName) | 		start_cmd := fmt.Sprintf("/usr/sbin/jail -f /var/run/jail.%s.conf -c", cj.InternalName) | ||||||
|  |  | ||||||
| @ -1416,20 +1448,20 @@ func StartJail(args []string) { | |||||||
| 		} | 		} | ||||||
|  |  | ||||||
| 		fmt.Printf("  > Setup VNet network:\n") | 		fmt.Printf("  > Setup VNet network:\n") | ||||||
| 		_, err = setupVnetInterfaceHostSide(cj);  | 		hsepairs, err := setupVnetInterfaceHostSide(cj); | ||||||
| 		if err != nil { | 		if err != nil { | ||||||
| 			fmt.Printf("Error setting VNet interface host side: %v\n", err) | 			fmt.Printf("Error setting VNet interface host side: %v\n", err) | ||||||
| 			return | 			return | ||||||
| 		} | 		} | ||||||
|  |  | ||||||
| 		if err = setupVnetInterfaceJailSide(cj); err != nil { | 		if err = setupVnetInterfaceJailSide(cj, hsepairs); err != nil { | ||||||
| 			fmt.Printf("Error setting VNet interface jail side: %v\n", err) | 			fmt.Printf("Error setting VNet interface jail side: %v\n", err) | ||||||
| 			return | 			return | ||||||
| 		} | 		} | ||||||
| 		fmt.Printf("  > Setup VNet network: OK\n") | 		fmt.Printf("  > Setup VNet network: OK\n") | ||||||
| 		 | 		 | ||||||
| 		// Set default route, unless main network is dhcp | 		// Set default route, unless main network is dhcp | ||||||
| 		if ! cj.isFirstNetDhcp() { | 		if ! cj.isFirstNetDhcp() && !strings.EqualFold(cj.Config.Ip4_addr, "none") { | ||||||
| 			fmt.Printf("  > Setup default ipv4 gateway:\n") | 			fmt.Printf("  > Setup default ipv4 gateway:\n") | ||||||
| 			cmd := fmt.Sprintf("/usr/sbin/setfib %s /usr/sbin/jexec %d route add default %s", cj.Config.Exec_fib, cj.JID, cj.Config.Defaultrouter) | 			cmd := fmt.Sprintf("/usr/sbin/setfib %s /usr/sbin/jexec %d route add default %s", cj.Config.Exec_fib, cj.JID, cj.Config.Defaultrouter) | ||||||
| 			out, err := executeCommand(cmd) | 			out, err := executeCommand(cmd) | ||||||
| @ -1519,7 +1551,6 @@ func StartJail(args []string) { | |||||||
| 		cj.Config.Last_started = curDate | 		cj.Config.Last_started = curDate | ||||||
| 		writeConfigToDisk(cj, false) | 		writeConfigToDisk(cj, false) | ||||||
|  |  | ||||||
| 		 |  | ||||||
| 		/* | 		/* | ||||||
| 			out, err := executeCommand(fmt.Sprintf("rctl jail:%s", cj.InternalName)) | 			out, err := executeCommand(fmt.Sprintf("rctl jail:%s", cj.InternalName)) | ||||||
| 			if err == nil && len(out) > 0 { | 			if err == nil && len(out) > 0 { | ||||||
|  | |||||||
							
								
								
									
										32
									
								
								cmd/stop.go
									
									
									
									
									
								
							
							
						
						
									
										32
									
								
								cmd/stop.go
									
									
									
									
									
								
							| @ -83,6 +83,7 @@ func umountAndUnjailZFS(jail *Jail) error { | |||||||
| } | } | ||||||
|  |  | ||||||
| func destroyVNetInterfaces(jail *Jail) error { | func destroyVNetInterfaces(jail *Jail) error { | ||||||
|  | 	if !strings.EqualFold(jail.Config.Ip4_addr, "none") { | ||||||
| 		for _, i := range strings.Split(jail.Config.Ip4_addr, ",") { | 		for _, i := range strings.Split(jail.Config.Ip4_addr, ",") { | ||||||
| 			iname := fmt.Sprintf("%s.%d", strings.Split(i, "|")[0], jail.JID) | 			iname := fmt.Sprintf("%s.%d", strings.Split(i, "|")[0], jail.JID) | ||||||
| 			fmt.Printf("%s: ", iname) | 			fmt.Printf("%s: ", iname) | ||||||
| @ -94,6 +95,20 @@ func destroyVNetInterfaces(jail *Jail) error { | |||||||
| 				fmt.Printf("OK\n") | 				fmt.Printf("OK\n") | ||||||
| 			} | 			} | ||||||
| 		} | 		} | ||||||
|  | 	} | ||||||
|  | 	if !strings.EqualFold(jail.Config.Ip6_addr, "none") { | ||||||
|  | 		for _, i := range strings.Split(jail.Config.Ip6_addr, ",") { | ||||||
|  | 			iname := fmt.Sprintf("%s.%d", strings.Split(i, "|")[0], jail.JID) | ||||||
|  | 			fmt.Printf("%s: ", iname) | ||||||
|  | 			_, err := executeCommand(fmt.Sprintf("ifconfig %s destroy", iname)) | ||||||
|  | 			//_, err := executeScript(fmt.Sprintf("ifconfig %s destroy >/dev/null 2>&1", iname)) | ||||||
|  | 			if err != nil { | ||||||
|  | 				return err | ||||||
|  | 			} else { | ||||||
|  | 				fmt.Printf("OK\n") | ||||||
|  | 			} | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
|  |  | ||||||
| 	return nil | 	return nil | ||||||
| } | } | ||||||
| @ -123,7 +138,7 @@ func deleteDevfsRuleset(ruleset int) error { | |||||||
| 	return nil | 	return nil | ||||||
| } | } | ||||||
|  |  | ||||||
| func umountJailFsFromHost(jail *Jail, mountpoint string) error { | func umountFsFromHost(mountpoint string) error { | ||||||
| 	cmd := "mount -p" | 	cmd := "mount -p" | ||||||
| 	out, err := executeCommand(cmd) | 	out, err := executeCommand(cmd) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| @ -134,11 +149,11 @@ func umountJailFsFromHost(jail *Jail, mountpoint string) error { | |||||||
| 	for _, l := range strings.Split(out, "\n") { | 	for _, l := range strings.Split(out, "\n") { | ||||||
| 		f := strings.Split(remSpPtrn.ReplaceAllString(l, " "), " ") | 		f := strings.Split(remSpPtrn.ReplaceAllString(l, " "), " ") | ||||||
| 		if len(f) > 2 { | 		if len(f) > 2 { | ||||||
| 			if strings.EqualFold(f[1], fmt.Sprintf("%s%s", jail.RootPath, mountpoint)) { | 			if strings.EqualFold(f[1], mountpoint) { | ||||||
| 				cmd = fmt.Sprintf("umount %s%s", jail.RootPath, mountpoint) | 				cmd = fmt.Sprintf("umount %s", mountpoint) | ||||||
| 				_, err := executeCommand(cmd) | 				_, err := executeCommand(cmd) | ||||||
| 				if err != nil { | 				if err != nil { | ||||||
| 					return errors.New(fmt.Sprintf("Error umounting %s%s: %s", jail.RootPath, mountpoint, err.Error())) | 					return errors.New(fmt.Sprintf("Error umounting %s: %s", mountpoint, err.Error())) | ||||||
| 				} | 				} | ||||||
| 				return nil | 				return nil | ||||||
| 			} | 			} | ||||||
| @ -148,6 +163,10 @@ func umountJailFsFromHost(jail *Jail, mountpoint string) error { | |||||||
| 	return nil | 	return nil | ||||||
| } | } | ||||||
|  |  | ||||||
|  | func umountJailFsFromHost(jail *Jail, mountpoint string) error { | ||||||
|  | 	return umountFsFromHost(fmt.Sprintf("%s%s", jail.RootPath, mountpoint)) | ||||||
|  | } | ||||||
|  |  | ||||||
| // Internal usage only | // Internal usage only | ||||||
| func stopJail(jail *Jail) error { | func stopJail(jail *Jail) error { | ||||||
| 	cmd := "jail -q" | 	cmd := "jail -q" | ||||||
| @ -270,7 +289,7 @@ func StopJail(args []string) { | |||||||
|  |  | ||||||
| 	for _, a := range args { | 	for _, a := range args { | ||||||
| 		// Check if jail exist and is distinctly named | 		// Check if jail exist and is distinctly named | ||||||
| 		cj, err = getJailFromArray(a, []string{"jail"}, gJails) | 		cj, err = getJailFromArray(a, []string{"basejail", "jail"}, gJails) | ||||||
| 		if err != nil { | 		if err != nil { | ||||||
| 			fmt.Printf("Error getting jail: %s\n", err) | 			fmt.Printf("Error getting jail: %s\n", err) | ||||||
| 			continue | 			continue | ||||||
| @ -404,7 +423,8 @@ func StopJail(args []string) { | |||||||
| 			fmt.Printf("  > Umount mountpoints from %s\n", fstab) | 			fmt.Printf("  > Umount mountpoints from %s\n", fstab) | ||||||
| 			errs := 0 | 			errs := 0 | ||||||
| 			for _, m := range mounts { | 			for _, m := range mounts { | ||||||
| 				err = umountJailFsFromHost(cj, m.Mountpoint) | 				log.Debugf("Umounting %s\n", m.Mountpoint) | ||||||
|  | 				err = umountFsFromHost(m.Mountpoint) | ||||||
| 				if err != nil { | 				if err != nil { | ||||||
| 					fmt.Printf("ERROR: %s\n", err.Error()) | 					fmt.Printf("ERROR: %s\n", err.Error()) | ||||||
| 					errs += 1 | 					errs += 1 | ||||||
|  | |||||||
| @ -5,43 +5,53 @@ import ( | |||||||
| 	"fmt" | 	"fmt" | ||||||
| 	//"log" | 	//"log" | ||||||
| 	"time" | 	"time" | ||||||
|  | 	"strings" | ||||||
|  | 	"github.com/spf13/viper" | ||||||
| ) | ) | ||||||
|  |  | ||||||
|  |  | ||||||
| // Internal usage only | // Internal usage only | ||||||
| func updateJail(jail *Jail) error { | func updateJail(jail *Jail, doUpdateVersion bool) error { | ||||||
| 	// Create default config as temporary file | 	// Create default config as temporary file | ||||||
| 	cfgFile, err := os.CreateTemp("", "gocage-jail-update-") | 	cfgFile, err := os.CreateTemp("", "gocage-jail-update-") | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return err | 		return err | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	cfgFile.Write([]byte(fbsdUpdateConfig)) | 	// Folder containing update/upgrade temporary files. Mutualized so we save bandwith when upgrading multiple jails | ||||||
|  | 	uwd := viper.GetString("updateWorkDir") | ||||||
| 	defer cfgFile.Close() | 	if len(uwd) == 0 { | ||||||
| 	defer os.Remove(cfgFile.Name()) | 		return fmt.Errorf("updateWorkDir not set in configuration") | ||||||
|  | 	} | ||||||
| 	// Folder containing update/uipgrade temporary files. Common so we save bandwith when upgrading multiple jails | 	_, err = os.Stat(uwd) | ||||||
| 	// TODO: Variabilize /iocage/freebsd-update |  | ||||||
| 	_, err = os.Stat("/iocage/freebsd-update") |  | ||||||
| 	if os.IsNotExist(err) { | 	if os.IsNotExist(err) { | ||||||
| 		if err := os.Mkdir("/iocage/freebsd-update", 0755); err != nil { | 		if err := os.Mkdir(uwd, 0755); err != nil { | ||||||
| 			return err | 			return err | ||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
|  | 	cfgFile.Write([]byte(strings.Replace(fbsdUpdateConfig, "TO-BE-REPLACED-WITH-UPDATEWORKDIR", uwd, 1))) | ||||||
|  | 	defer cfgFile.Close() | ||||||
|  | 	defer os.Remove(cfgFile.Name()) | ||||||
|  |  | ||||||
| 	cmd := fmt.Sprintf("/usr/sbin/freebsd-update --not-running-from-cron -f %s -b %s --currently-running %s fetch install",  | 	cmd := fmt.Sprintf("/usr/sbin/freebsd-update --not-running-from-cron -f %s -b %s --currently-running %s fetch", | ||||||
| 					   cfgFile.Name(), jail.RootPath, jail.Config.Release) | 					   cfgFile.Name(), jail.RootPath, jail.Config.Release) | ||||||
|  |  | ||||||
| 	//fmt.Printf("DEBUG: Prepare to execute \"%s\"\n", cmd) |  | ||||||
|  |  | ||||||
| 	err = executeCommandWithOutputToStdout(cmd) | 	err = executeCommandWithOutputToStdout(cmd) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return err | 		return err | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	// Get and write new release into config.json | 	cmd = fmt.Sprintf("/usr/sbin/freebsd-update --not-running-from-cron -f %s -b %s --currently-running %s install", | ||||||
|  | 			   cfgFile.Name(), jail.RootPath, jail.Config.Release) | ||||||
|  | 	err = executeCommandWithOutputToStdout(cmd) | ||||||
|  | 	if err != nil { | ||||||
|  | 		return err | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	// Get and write new release into config.json. Don't do that for fake jail (aka release updating) | ||||||
|  | 	if doUpdateVersion { | ||||||
| 		updateVersion(jail) | 		updateVersion(jail) | ||||||
|  | 	} | ||||||
|  |  | ||||||
| 	return nil | 	return nil | ||||||
| } | } | ||||||
| @ -51,6 +61,52 @@ func UpdateJail(args []string) { | |||||||
| 	var cj *Jail | 	var cj *Jail | ||||||
| 	var err error | 	var err error | ||||||
|  |  | ||||||
|  | 	// User is updateing a release, fake a jail | ||||||
|  | 	if  len(gUpdateRelease) > 0 { | ||||||
|  | 		// get datastore mountpoing from datastore name | ||||||
|  | 		ds, err := getDatastoreFromArray(gUpdateReleaseDS, gDatastores) | ||||||
|  | 		if err != nil { | ||||||
|  | 			fmt.Printf("Error getting datastore %s: %v\n", gUpdateReleaseDS, err) | ||||||
|  | 			return | ||||||
|  | 		} | ||||||
|  | 		rp := fmt.Sprintf("%s/releases/%s/root", ds.Mountpoint, gUpdateRelease) | ||||||
|  | 		fakeJail := Jail{RootPath: rp} | ||||||
|  | 		v, err := getVersion(&fakeJail) | ||||||
|  | 		if err != nil { | ||||||
|  | 			fmt.Printf("Error getting version of release %s: %v\n", gUpdateRelease, err) | ||||||
|  | 			return | ||||||
|  | 		} | ||||||
|  | 		fakeJail.Config.Release = v | ||||||
|  |  | ||||||
|  | 		// Remove patch level from Release | ||||||
|  | 		fv, err := freebsdVersionToStruct(fakeJail.Config.Release) | ||||||
|  | 		if err != nil { | ||||||
|  | 			fmt.Printf("Error converting release %s: %v\n", fakeJail.Config.Release, err) | ||||||
|  | 			return | ||||||
|  | 		} | ||||||
|  | 		release := fmt.Sprintf("%d.%d-%s", fv.major, fv.minor, fv.flavor) | ||||||
|  | 		// Snapshot before updating | ||||||
|  | 		dt := time.Now() | ||||||
|  | 		curDate := fmt.Sprintf("%s", dt.Format("2006-01-02_15-04-05")) | ||||||
|  | 		snapshotName := fmt.Sprintf("gocage_update_%s_%s", v, curDate) | ||||||
|  | 		err = zfsSnapshot(fmt.Sprintf("%s/releases/%s", ds.ZFSDataset, release), snapshotName) | ||||||
|  | 		if err != nil { | ||||||
|  | 			fmt.Printf("Error snapshoting release %s: %v\n", gUpdateRelease, err) | ||||||
|  | 			return | ||||||
|  | 		} | ||||||
|  | 		err = zfsSnapshot(fmt.Sprintf("%s/releases/%s/root", ds.ZFSDataset, release), snapshotName) | ||||||
|  | 		if err != nil { | ||||||
|  | 			fmt.Printf("Error snapshoting release %s: %v\n", gUpdateRelease, err) | ||||||
|  | 		} else { | ||||||
|  | 			fmt.Printf("Release %s was snapshoted with success: %s\n", gUpdateRelease, snapshotName) | ||||||
|  | 		} | ||||||
|  |  | ||||||
|  | 		if err = updateJail(&fakeJail, false); err != nil { | ||||||
|  | 			fmt.Printf("Error updating release %s: %v\n", gUpdateRelease, err) | ||||||
|  | 		} | ||||||
|  | 		return | ||||||
|  | 	} | ||||||
|  |  | ||||||
| 	for _, a := range args { | 	for _, a := range args { | ||||||
| 		// Check if jail exist and is distinctly named | 		// Check if jail exist and is distinctly named | ||||||
| 		cj, err = getJailFromArray(a, []string{""}, gJails) | 		cj, err = getJailFromArray(a, []string{""}, gJails) | ||||||
| @ -59,11 +115,17 @@ func UpdateJail(args []string) { | |||||||
| 			continue | 			continue | ||||||
| 		} | 		} | ||||||
|  |  | ||||||
|  | 		// We cant update basejail as system is readonly | ||||||
|  | 		if strings.EqualFold(cj.Config.Jailtype, "basejail") { | ||||||
|  | 			fmt.Printf("%s is a basejail using %s system files. Please update %s!\n", cj.Name, cj.Config.Origin, cj.Config.Origin) | ||||||
|  | 			continue | ||||||
|  | 		} | ||||||
|  |  | ||||||
| 		fmt.Printf("  > Snapshot jail %s\n", cj.Name) | 		fmt.Printf("  > Snapshot jail %s\n", cj.Name) | ||||||
| 		// Set snapshot name | 		// Set snapshot name | ||||||
| 		dt := time.Now() | 		dt := time.Now() | ||||||
| 		curDate := fmt.Sprintf("%s", dt.Format("2006-01-02_15-04-05")) | 		curDate := fmt.Sprintf("%s", dt.Format("2006-01-02_15-04-05")) | ||||||
| 		gSnapshotName = fmt.Sprintf("goc_update_%s_%s", cj.Config.Release, curDate) | 		gSnapshotName = fmt.Sprintf("gocage_update_%s_%s", cj.Config.Release, curDate) | ||||||
| 		err := createJailSnapshot(*cj) | 		err := createJailSnapshot(*cj) | ||||||
| 		if err != nil { | 		if err != nil { | ||||||
| 			fmt.Printf("  > Snapshot jail %s: ERROR: %s\n", cj.Name, err.Error()) | 			fmt.Printf("  > Snapshot jail %s: ERROR: %s\n", cj.Name, err.Error()) | ||||||
| @ -72,7 +134,7 @@ func UpdateJail(args []string) { | |||||||
| 		fmt.Printf("  > Snapshot jail %s: OK\n", cj.Name) | 		fmt.Printf("  > Snapshot jail %s: OK\n", cj.Name) | ||||||
| 		 | 		 | ||||||
| 		fmt.Printf("  > Update jail %s\n", cj.Name) | 		fmt.Printf("  > Update jail %s\n", cj.Name) | ||||||
| 		err = updateJail(cj) | 		err = updateJail(cj, true) | ||||||
| 		if err != nil { | 		if err != nil { | ||||||
| 			fmt.Printf("ERROR: %s\n", err.Error()) | 			fmt.Printf("ERROR: %s\n", err.Error()) | ||||||
| 		} else { | 		} else { | ||||||
|  | |||||||
| @ -8,6 +8,7 @@ import ( | |||||||
| 	//"log" | 	//"log" | ||||||
| 	"time" | 	"time" | ||||||
| 	"strings" | 	"strings" | ||||||
|  | 	"github.com/spf13/viper" | ||||||
| ) | ) | ||||||
|  |  | ||||||
| // Internal usage only | // Internal usage only | ||||||
| @ -18,19 +19,20 @@ func upgradeJail(jail *Jail, version string) error { | |||||||
| 		return err | 		return err | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	cfgFile.Write([]byte(fbsdUpdateConfig)) | 	// Folder containing update/upgrade temporary files. Mutualized so we save bandwith when upgrading multiple jails | ||||||
|  | 	uwd := viper.GetString("updateWorkDir") | ||||||
| 	defer cfgFile.Close() | 	if len(uwd) == 0 { | ||||||
| 	defer os.Remove(cfgFile.Name()) | 		return fmt.Errorf("updateWorkDir not set in configuration") | ||||||
|  | 	} | ||||||
| 	// Folder containing update/uipgrade temporary files. Common so we save bandwith when upgrading multiple jails | 	_, err = os.Stat(uwd) | ||||||
| 	// TODO: Variabilize /iocage/freebsd-update |  | ||||||
| 	_, err = os.Stat("/iocage/freebsd-update") |  | ||||||
| 	if os.IsNotExist(err) { | 	if os.IsNotExist(err) { | ||||||
| 		if err := os.Mkdir("/iocage/freebsd-update", 0755); err != nil { | 		if err := os.Mkdir(uwd, 0755); err != nil { | ||||||
| 			return err | 			return err | ||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
|  | 	cfgFile.Write([]byte(strings.Replace(fbsdUpdateConfig, "TO-BE-REPLACED-WITH-UPDATEWORKDIR", uwd, 1))) | ||||||
|  | 	defer cfgFile.Close() | ||||||
|  | 	defer os.Remove(cfgFile.Name()) | ||||||
|  |  | ||||||
| 	// Get current version. Won't work on stopped jail. | 	// Get current version. Won't work on stopped jail. | ||||||
| 	fbsdvers, err := executeCommandInJail(jail, "/bin/freebsd-version") | 	fbsdvers, err := executeCommandInJail(jail, "/bin/freebsd-version") | ||||||
| @ -39,7 +41,6 @@ func upgradeJail(jail *Jail, version string) error { | |||||||
| 		return err | 		return err | ||||||
| 	} | 	} | ||||||
| 	fbsdvers = strings.TrimRight(fbsdvers, "\n") | 	fbsdvers = strings.TrimRight(fbsdvers, "\n") | ||||||
| 	//fbsdvers := jail.Config.Release |  | ||||||
|  |  | ||||||
| 	cmd := fmt.Sprintf("/usr/sbin/freebsd-update -f %s -b %s --currently-running %s -r %s upgrade",  | 	cmd := fmt.Sprintf("/usr/sbin/freebsd-update -f %s -b %s --currently-running %s -r %s upgrade",  | ||||||
| 			   cfgFile.Name(), jail.RootPath, fbsdvers, version) | 			   cfgFile.Name(), jail.RootPath, fbsdvers, version) | ||||||
|  | |||||||
							
								
								
									
										251
									
								
								cmd/utils.go
									
									
									
									
									
								
							
							
						
						
									
										251
									
								
								cmd/utils.go
									
									
									
									
									
								
							| @ -8,6 +8,7 @@ import ( | |||||||
| 	"sort" | 	"sort" | ||||||
| 	"bufio" | 	"bufio" | ||||||
| 	"errors" | 	"errors" | ||||||
|  | 	"regexp" | ||||||
| 	"os/exec" | 	"os/exec" | ||||||
| 	"reflect" | 	"reflect" | ||||||
| 	"strconv" | 	"strconv" | ||||||
| @ -25,7 +26,88 @@ const ( | |||||||
| 	// Maximum thread qty for start/stop | 	// Maximum thread qty for start/stop | ||||||
| 	gMaxThreads    = 4 | 	gMaxThreads    = 4 | ||||||
|  |  | ||||||
| 	gDefaultsJson = ` { | 	fbsdUpdateConfig = `# $FreeBSD$ | ||||||
|  |  | ||||||
|  | # Trusted keyprint.  Changing this is a Bad Idea unless you've received | ||||||
|  | # a PGP-signed email from <security-officer@FreeBSD.org> telling you to | ||||||
|  | # change it and explaining why. | ||||||
|  | KeyPrint 800651ef4b4c71c27e60786d7b487188970f4b4169cc055784e21eb71d410cc5 | ||||||
|  |  | ||||||
|  | # Server or server pool from which to fetch updates.  You can change | ||||||
|  | # this to point at a specific server if you want, but in most cases | ||||||
|  | # using a "nearby" server won't provide a measurable improvement in | ||||||
|  | # performance. | ||||||
|  | ServerName update.FreeBSD.org | ||||||
|  |  | ||||||
|  | # Components of the base system which should be kept updated. | ||||||
|  | Components world | ||||||
|  |  | ||||||
|  | # Example for updating the userland and the kernel source code only: | ||||||
|  | # Components src/base src/sys world | ||||||
|  |  | ||||||
|  | # Paths which start with anything matching an entry in an IgnorePaths | ||||||
|  | # statement will be ignored. | ||||||
|  | IgnorePaths | ||||||
|  |  | ||||||
|  | # Paths which start with anything matching an entry in an IDSIgnorePaths | ||||||
|  | # statement will be ignored by "freebsd-update IDS". | ||||||
|  | IDSIgnorePaths /usr/share/man/cat | ||||||
|  | IDSIgnorePaths /usr/share/man/whatis | ||||||
|  | IDSIgnorePaths /var/db/locate.database | ||||||
|  | IDSIgnorePaths /var/log | ||||||
|  |  | ||||||
|  | # Paths which start with anything matching an entry in an UpdateIfUnmodified | ||||||
|  | # statement will only be updated if the contents of the file have not been | ||||||
|  | # modified by the user (unless changes are merged; see below). | ||||||
|  | UpdateIfUnmodified /etc/ /var/ /root/ /.cshrc /.profile | ||||||
|  |  | ||||||
|  | # When upgrading to a new FreeBSD release, files which match MergeChanges | ||||||
|  | # will have any local changes merged into the version from the new release. | ||||||
|  | MergeChanges /etc/ | ||||||
|  |  | ||||||
|  | ### Default configuration options: | ||||||
|  |  | ||||||
|  | # Directory in which to store downloaded updates and temporary | ||||||
|  | # files used by FreeBSD Update. | ||||||
|  | WorkDir TO-BE-REPLACED-WITH-UPDATEWORKDIR | ||||||
|  |  | ||||||
|  | # Destination to send output of "freebsd-update cron" if an error | ||||||
|  | # occurs or updates have been downloaded. | ||||||
|  | # MailTo root | ||||||
|  |  | ||||||
|  | # Is FreeBSD Update allowed to create new files? | ||||||
|  | # AllowAdd yes | ||||||
|  |  | ||||||
|  | # Is FreeBSD Update allowed to delete files? | ||||||
|  | # AllowDelete yes | ||||||
|  |  | ||||||
|  | # If the user has modified file ownership, permissions, or flags, should | ||||||
|  | # FreeBSD Update retain this modified metadata when installing a new version | ||||||
|  | # of that file? | ||||||
|  | # KeepModifiedMetadata yes | ||||||
|  |  | ||||||
|  | # When upgrading between releases, should the list of Components be | ||||||
|  | # read strictly (StrictComponents yes) or merely as a list of components | ||||||
|  | # which *might* be installed of which FreeBSD Update should figure out | ||||||
|  | # which actually are installed and upgrade those (StrictComponents no)? | ||||||
|  | StrictComponents yes | ||||||
|  |  | ||||||
|  | # When installing a new kernel perform a backup of the old one first | ||||||
|  | # so it is possible to boot the old kernel in case of problems. | ||||||
|  | BackupKernel no | ||||||
|  |  | ||||||
|  | # If BackupKernel is enabled, the backup kernel is saved to this | ||||||
|  | # directory. | ||||||
|  | # BackupKernelDir /boot/kernel.old | ||||||
|  |  | ||||||
|  | # When backing up a kernel also back up debug symbol files? | ||||||
|  | BackupKernelSymbolFiles no | ||||||
|  |  | ||||||
|  | # Create a new boot environment when installing patches | ||||||
|  | CreateBootEnv no | ||||||
|  | ` | ||||||
|  |  | ||||||
|  | 	gDefaultsJson = `{ | ||||||
|     "CONFIG_VERSION": "27", |     "CONFIG_VERSION": "27", | ||||||
|     "allow_chflags": 0, |     "allow_chflags": 0, | ||||||
|     "allow_mlock": 0, |     "allow_mlock": 0, | ||||||
| @ -79,9 +161,9 @@ const ( | |||||||
|     "exec_timeout": "60", |     "exec_timeout": "60", | ||||||
|     "host_domainname": "none", |     "host_domainname": "none", | ||||||
|     "host_time": 1, |     "host_time": 1, | ||||||
|        "hostid": "36353536-3135-5a43-4a34-313130315a56", |     "hostid": "TO-BE-REPLACED-WITH-HOSTID", | ||||||
|     "hostid_strict_check": 0, |     "hostid_strict_check": 0, | ||||||
|        "interfaces": "vnet0:bridge0", |     "interfaces": "vnet0:TO-BE-REPLACED-WITH-BRIDGE", | ||||||
|     "ip4": "new", |     "ip4": "new", | ||||||
|     "ip4_addr": "none", |     "ip4_addr": "none", | ||||||
|     "ip4_saddrsel": 1, |     "ip4_saddrsel": 1, | ||||||
| @ -147,7 +229,7 @@ const ( | |||||||
|     "type": "jail", |     "type": "jail", | ||||||
|     "used": "readonly", |     "used": "readonly", | ||||||
|     "vmemoryuse": "off", |     "vmemoryuse": "off", | ||||||
|        "vnet": 0, |     "vnet": 1, | ||||||
|     "vnet0_mac": "none", |     "vnet0_mac": "none", | ||||||
|     "vnet1_mac": "none", |     "vnet1_mac": "none", | ||||||
|     "vnet2_mac": "none", |     "vnet2_mac": "none", | ||||||
| @ -157,7 +239,7 @@ const ( | |||||||
|     "wallclock": "off", |     "wallclock": "off", | ||||||
|     "writebps": "off", |     "writebps": "off", | ||||||
|     "writeiops": "off" |     "writeiops": "off" | ||||||
|        } | } | ||||||
| ` | ` | ||||||
| ) | ) | ||||||
|  |  | ||||||
| @ -362,7 +444,7 @@ func executeCommand(cmdline string) (string, error) { | |||||||
| 		out, err = exec.Command(cmd[0]).CombinedOutput() | 		out, err = exec.Command(cmd[0]).CombinedOutput() | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	return string(out), err | 	return strings.TrimSuffix(string(out), "\n"), err | ||||||
| } | } | ||||||
|  |  | ||||||
| /* From iocage:  | /* From iocage:  | ||||||
| @ -474,6 +556,8 @@ func executeCommandWithOutputToStdout(cmdline string) (error) { | |||||||
| 		word = word + string(c) | 		word = word + string(c) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
|  | 	log.Debugf("executeCommandWithOutputToStdout: will execute \"%s\"\n", strings.Join(cmd, " ")) | ||||||
|  |  | ||||||
| 	var execHandle *exec.Cmd | 	var execHandle *exec.Cmd | ||||||
| 	if len(cmd) > 1 { | 	if len(cmd) > 1 { | ||||||
| 		execHandle = exec.Command(cmd[0], cmd[1:]...) | 		execHandle = exec.Command(cmd[0], cmd[1:]...) | ||||||
| @ -668,6 +752,30 @@ func executeScript(script string) (string, error) { | |||||||
| 	return string(out), err | 	return string(out), err | ||||||
| } | } | ||||||
|  |  | ||||||
|  | /***************************************************************************** | ||||||
|  |  * | ||||||
|  |  * Network related operations | ||||||
|  |  * | ||||||
|  |  *****************************************************************************/ | ||||||
|  | func getBridgeMembers(bridge string) ([]string, error) { | ||||||
|  | 	var members []string | ||||||
|  | 	cmd := fmt.Sprintf("/sbin/ifconfig %s", bridge) | ||||||
|  | 	out, err := executeCommand(cmd) | ||||||
|  | 	if err != nil { | ||||||
|  | 		return members, errors.New(fmt.Sprintf("%v; command returned \"%s\"", err, out)) | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	for _, line := range strings.Split(out, "\n") { | ||||||
|  | 		if strings.HasPrefix(strings.TrimLeft(line, " \t"), "member:") { | ||||||
|  | 			m := strings.Split(strings.TrimLeft(strings.Split(line, ":")[1], " "), " ")[0] | ||||||
|  | 			log.Debugf("%s is member of %s\n", m, bridge) | ||||||
|  | 			members = append(members, m) | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
|  | 	return members, nil | ||||||
|  | } | ||||||
|  |  | ||||||
|  |  | ||||||
| /***************************************************************************** | /***************************************************************************** | ||||||
| * | * | ||||||
| * ZFS datasets/pools operations | * ZFS datasets/pools operations | ||||||
| @ -790,9 +898,20 @@ func doZfsDatasetExist(dataset string) (bool, error) { | |||||||
| 	return true, nil | 	return true, nil | ||||||
| } | } | ||||||
|  |  | ||||||
| // Create ZFS dataset. mountpoint can be "none", then the dataset won't be mounted | /* Create ZFS dataset | ||||||
|  |  * mountpoint can be "none", then the dataset won't be mounted | ||||||
|  |  * mountpoint can be "", then it will be inherited | ||||||
|  |  * compression can be "", then it wil be inherited | ||||||
|  |  */ | ||||||
| func zfsCreateDataset(dataset, mountpoint, compression string) error { | func zfsCreateDataset(dataset, mountpoint, compression string) error { | ||||||
| 	cmd := fmt.Sprintf("zfs create -o mountpoint=%s -o compression=%s %s", mountpoint, compression, dataset) | 	cmd := "zfs create" | ||||||
|  | 	if len(mountpoint) > 0 { | ||||||
|  | 		cmd = fmt.Sprintf("%s -o mountpoint=%s", cmd, mountpoint) | ||||||
|  | 	} | ||||||
|  | 	if len(compression) > 0 { | ||||||
|  | 		cmd = fmt.Sprintf("%s -o compression=%s", cmd, compression) | ||||||
|  | 	} | ||||||
|  | 	cmd = fmt.Sprintf("%s %s", cmd, dataset) | ||||||
| 	out, err := executeCommand(cmd) | 	out, err := executeCommand(cmd) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return errors.New(fmt.Sprintf("%v; command returned \"%s\"", err, out)) | 		return errors.New(fmt.Sprintf("%v; command returned \"%s\"", err, out)) | ||||||
| @ -801,7 +920,9 @@ func zfsCreateDataset(dataset, mountpoint, compression string) error { | |||||||
| } | } | ||||||
| // Return dataset name for a given mountpoint | // Return dataset name for a given mountpoint | ||||||
| func zfsGetDatasetByMountpoint(mountpoint string) (string, error) { | func zfsGetDatasetByMountpoint(mountpoint string) (string, error) { | ||||||
| 	cmd := fmt.Sprintf("zfs list -p -r -H -o name %s", mountpoint) | 	// We dont want no recursivity | ||||||
|  | 	//cmd := fmt.Sprintf("zfs list -p -r -H -o name %s", mountpoint) | ||||||
|  | 	cmd := fmt.Sprintf("zfs list -p -H -o name %s", mountpoint) | ||||||
| 	out, err := executeCommand(cmd) | 	out, err := executeCommand(cmd) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return "", errors.New(fmt.Sprintf("%v; command returned \"%s\"", err, out)) | 		return "", errors.New(fmt.Sprintf("%v; command returned \"%s\"", err, out)) | ||||||
| @ -821,6 +942,11 @@ func zfsDestroy(dataset string) error { | |||||||
| 	return nil | 	return nil | ||||||
| } | } | ||||||
|  |  | ||||||
|  | /***************************************************************************** | ||||||
|  |  * | ||||||
|  |  * Filesystem operations | ||||||
|  |  * | ||||||
|  |  *****************************************************************************/ | ||||||
| /* Copy file */ | /* Copy file */ | ||||||
| func copyFile(src, dst string) error { | func copyFile(src, dst string) error { | ||||||
| 	srcfinfo, err := os.Stat(src) | 	srcfinfo, err := os.Stat(src) | ||||||
| @ -845,6 +971,22 @@ func copyFile(src, dst string) error { | |||||||
| 	return err | 	return err | ||||||
| } | } | ||||||
|  |  | ||||||
|  | // Get permissions of file or folder | ||||||
|  | func getPermissions(path string) (os.FileInfo, error) { | ||||||
|  | 	return os.Stat(path) | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func doFileExist(filePath string) (bool, error) { | ||||||
|  | 	if _, err := os.Stat(filePath); err != nil { | ||||||
|  | 		if errors.Is(err, os.ErrNotExist) { | ||||||
|  | 			return false, nil | ||||||
|  | 		} else { | ||||||
|  | 			return false, err | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
|  | 	return true, nil | ||||||
|  | } | ||||||
|  |  | ||||||
| /***************************************************************************** | /***************************************************************************** | ||||||
|  * |  * | ||||||
|  * rc.conf management |  * rc.conf management | ||||||
| @ -879,6 +1021,44 @@ func disableRcKey(rcconfpath string, key string) error { | |||||||
| 	return nil | 	return nil | ||||||
| } | } | ||||||
|  |  | ||||||
|  | // returns no error if rc key does not exist | ||||||
|  | func getCurrentRcKeyValue(rcconfpath string, key string) (string, error) { | ||||||
|  | 	cmd := "/usr/sbin/sysrc -a" | ||||||
|  | 	kvs, err := executeCommand(cmd) | ||||||
|  | 	if err != nil { | ||||||
|  | 		return "", err | ||||||
|  | 	} | ||||||
|  | 	for _, kv := range strings.Split(string(kvs), "\n") { | ||||||
|  | 		fmt.Printf("%s\n", kv) | ||||||
|  | 		if strings.HasPrefix(kv, fmt.Sprintf("%s:", key)) { | ||||||
|  | 			return strings.TrimPrefix(strings.Join(strings.Split(kv, ":")[1:], ":"), " "), nil | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	return "", nil | ||||||
|  | } | ||||||
|  |  | ||||||
|  | // Add a value to current existing key value | ||||||
|  | func addRcKeyValue(rcconfpath string, key string, value string) error { | ||||||
|  | 	var nv string | ||||||
|  | 	cv, err := getCurrentRcKeyValue(rcconfpath, key) | ||||||
|  | 	if err != nil { | ||||||
|  | 		return err | ||||||
|  | 	} | ||||||
|  | 	if len(cv) > 0 { | ||||||
|  | 		log.Debugf("Current value of %s: %s\n", key, cv) | ||||||
|  | 		nv = fmt.Sprintf("\"%s %s\"", cv, value) | ||||||
|  | 	} else { | ||||||
|  | 		nv = fmt.Sprintf("\"%s\"", value) | ||||||
|  | 	} | ||||||
|  | 	cmd := fmt.Sprintf("/usr/sbin/sysrc -f %s %s=%s", rcconfpath, key, nv) | ||||||
|  | 	_, err = executeCommand(cmd) | ||||||
|  | 	if err != nil { | ||||||
|  | 		return err | ||||||
|  | 	} | ||||||
|  | 	return nil | ||||||
|  | } | ||||||
|  |  | ||||||
| /***************************************************************************** | /***************************************************************************** | ||||||
| * Parse an fstab file, returning an array of Mount | * Parse an fstab file, returning an array of Mount | ||||||
| *****************************************************************************/ | *****************************************************************************/ | ||||||
| @ -894,7 +1074,8 @@ func getFstab(path string) ([]Mount, error) { | |||||||
| 	scan := bufio.NewScanner(f) | 	scan := bufio.NewScanner(f) | ||||||
| 	for scan.Scan() { | 	for scan.Scan() { | ||||||
| 		res := strings.Fields(scan.Text()) | 		res := strings.Fields(scan.Text()) | ||||||
| 		if len(res) != 6 { | 		// iocage create lines like that : "/iocage/releases/13.2-RELEASE/root/bin   /iocage/jails/smtp-router-02/root/bin   nullfs  ro      0       0 # Added by iocage on 2023-10-10 17:20:51" | ||||||
|  | 		if (len(res) > 6 && !strings.EqualFold(res[6], "#")) || len(res) < 6 { | ||||||
| 			return mounts, fmt.Errorf("Incorrect format for fstab line %s", scan.Text()) | 			return mounts, fmt.Errorf("Incorrect format for fstab line %s", scan.Text()) | ||||||
| 		} | 		} | ||||||
| 		freq, err := strconv.Atoi(res[4]) | 		freq, err := strconv.Atoi(res[4]) | ||||||
| @ -930,13 +1111,10 @@ func getDevfsRuleset(ruleset int) []string { | |||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return []string{} | 		return []string{} | ||||||
| 	} | 	} | ||||||
| 	// Get rid of the last "\n" | 	return strings.Split(out, "\n")[:len(strings.Split(out, "\n"))] | ||||||
| 	return strings.Split(out, "\n")[:len(strings.Split(out, "\n"))-1] |  | ||||||
| } | } | ||||||
|  |  | ||||||
| func copyDevfsRuleset(ruleset int, srcrs int) error { | func copyDevfsRuleset(ruleset int, srcrs int) error { | ||||||
| 	// Resulting ruleset as an array of line |  | ||||||
| 	//var result []string |  | ||||||
| 	out := getDevfsRuleset(srcrs) | 	out := getDevfsRuleset(srcrs) | ||||||
| 	for _, line := range out { | 	for _, line := range out { | ||||||
| 		//fields := strings.Fields(line) | 		//fields := strings.Fields(line) | ||||||
| @ -1041,7 +1219,7 @@ func getJailFromArray(name string, jailtypes []string, jarray []Jail) (*Jail, er | |||||||
| 	var jails []Jail | 	var jails []Jail | ||||||
|  |  | ||||||
| 	if (len(jailtypes) == 1 && len(jailtypes[0]) == 0) || len(jailtypes) == 0 { | 	if (len(jailtypes) == 1 && len(jailtypes[0]) == 0) || len(jailtypes) == 0 { | ||||||
| 		jailtypes = []string{"jail", "basetpl"} | 		jailtypes = []string{"basejail", "jail", "template"} | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	if strings.Contains(name, "/") { | 	if strings.Contains(name, "/") { | ||||||
| @ -1107,15 +1285,54 @@ func setJailConfigUpdated(jail *Jail) error { | |||||||
| 	return nil | 	return nil | ||||||
| } | } | ||||||
|  |  | ||||||
|  | func freebsdVersionToStruct(rawVersion string) (FreeBSDVersion, error) { | ||||||
|  | 	var version FreeBSDVersion | ||||||
|  | 	var err error | ||||||
|  |  | ||||||
|  | 	regex := `([0-9]{1,2})(\.)?([0-9]{1,2})?\-([^\-]*)(\-)?(p[0-9]{1,2})?` | ||||||
|  |  | ||||||
|  | 	re := regexp.MustCompile(regex) | ||||||
|  |  | ||||||
|  | 	if re.MatchString(rawVersion) { | ||||||
|  | 		version.major, err = strconv.Atoi(re.FindStringSubmatch(rawVersion)[1]) | ||||||
|  | 		if err != nil { | ||||||
|  | 			return version, err | ||||||
|  | 		} | ||||||
|  | 		version.minor, err = strconv.Atoi(re.FindStringSubmatch(rawVersion)[3]) | ||||||
|  | 		if err != nil { | ||||||
|  | 			return version, err | ||||||
|  | 		} | ||||||
|  | 		version.flavor = strings.Trim(re.FindStringSubmatch(rawVersion)[4], "\n") | ||||||
|  |  | ||||||
|  | 		// Skip the 'p' starting patch level | ||||||
|  | 		if len(re.FindStringSubmatch(rawVersion)[6]) > 0 { | ||||||
|  | 			version.patchLevel, err = strconv.Atoi(re.FindStringSubmatch(rawVersion)[6][1:]) | ||||||
|  | 			if err != nil { | ||||||
|  | 				return version, err | ||||||
|  | 			} | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
|  | 	return version, nil | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func getVersion(jail *Jail) (string, error) { | ||||||
|  | 	cvers, err := executeCommand(fmt.Sprintf("%s/bin/freebsd-version", jail.RootPath)) | ||||||
|  | 	if err != nil { | ||||||
|  | 		fmt.Printf("ERROR: %s\n", err.Error()) | ||||||
|  | 		return "", err | ||||||
|  | 	} | ||||||
|  | 	return strings.TrimRight(cvers, "\n"), nil | ||||||
|  | } | ||||||
|  |  | ||||||
| func updateVersion(jail *Jail) error { | func updateVersion(jail *Jail) error { | ||||||
| 	cvers, err := executeCommandInJail(jail, "/bin/freebsd-version") | 	cvers, err := executeCommand(fmt.Sprintf("%s/bin/freebsd-version", jail.RootPath)) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		fmt.Printf("ERROR: %s\n", err.Error()) | 		fmt.Printf("ERROR: %s\n", err.Error()) | ||||||
| 		return err | 		return err | ||||||
| 	} | 	} | ||||||
| 	cvers = strings.TrimRight(cvers, "\n") | 	cvers = strings.TrimRight(cvers, "\n") | ||||||
| 	jail.Config.Release = cvers | 	jail.Config.Release = cvers | ||||||
| 	writeConfigToDisk(jail, false) | 	jail.WriteConfigToDisk(false) | ||||||
|  |  | ||||||
| 	return nil | 	return nil | ||||||
| } | } | ||||||
|  | |||||||
							
								
								
									
										59
									
								
								go.mod
									
									
									
									
									
								
							
							
						
						
									
										59
									
								
								go.mod
									
									
									
									
									
								
							| @ -6,26 +6,53 @@ require ( | |||||||
| 	github.com/c-robinson/iplib v1.0.3 | 	github.com/c-robinson/iplib v1.0.3 | ||||||
| 	github.com/c2h5oh/datasize v0.0.0-20220606134207-859f65c6625b | 	github.com/c2h5oh/datasize v0.0.0-20220606134207-859f65c6625b | ||||||
| 	github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 | 	github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 | ||||||
|  | 	github.com/otiai10/copy v1.12.0 | ||||||
| 	github.com/sirupsen/logrus v1.8.1 | 	github.com/sirupsen/logrus v1.8.1 | ||||||
| 	github.com/spf13/cobra v1.2.1 | 	github.com/spf13/cobra v1.8.1 | ||||||
| 	github.com/spf13/viper v1.9.0 | 	github.com/spf13/viper v1.19.0 | ||||||
| 	golang.org/x/net v0.0.0-20210503060351-7fd8e65b6420 | 	golang.org/x/net v0.25.0 | ||||||
| ) | ) | ||||||
|  |  | ||||||
| require ( | require ( | ||||||
| 	github.com/fsnotify/fsnotify v1.5.1 // indirect | 	github.com/bytedance/sonic v1.11.6 // indirect | ||||||
|  | 	github.com/bytedance/sonic/loader v0.1.1 // indirect | ||||||
|  | 	github.com/cloudwego/base64x v0.1.4 // indirect | ||||||
|  | 	github.com/cloudwego/iasm v0.2.0 // indirect | ||||||
|  | 	github.com/fsnotify/fsnotify v1.7.0 // indirect | ||||||
|  | 	github.com/gabriel-vasile/mimetype v1.4.3 // indirect | ||||||
|  | 	github.com/gin-contrib/sse v0.1.0 // indirect | ||||||
|  | 	github.com/go-playground/locales v0.14.1 // indirect | ||||||
|  | 	github.com/go-playground/universal-translator v0.18.1 // indirect | ||||||
|  | 	github.com/go-playground/validator/v10 v10.20.0 // indirect | ||||||
|  | 	github.com/goccy/go-json v0.10.2 // indirect | ||||||
| 	github.com/hashicorp/hcl v1.0.0 // indirect | 	github.com/hashicorp/hcl v1.0.0 // indirect | ||||||
| 	github.com/inconshreveable/mousetrap v1.0.0 // indirect | 	github.com/inconshreveable/mousetrap v1.1.0 // indirect | ||||||
| 	github.com/magiconair/properties v1.8.5 // indirect | 	github.com/json-iterator/go v1.1.12 // indirect | ||||||
| 	github.com/mitchellh/mapstructure v1.4.2 // indirect | 	github.com/klauspost/cpuid/v2 v2.2.7 // indirect | ||||||
| 	github.com/pelletier/go-toml v1.9.4 // indirect | 	github.com/leodido/go-urn v1.4.0 // indirect | ||||||
| 	github.com/spf13/afero v1.6.0 // indirect | 	github.com/magiconair/properties v1.8.7 // indirect | ||||||
| 	github.com/spf13/cast v1.4.1 // indirect | 	github.com/mattn/go-isatty v0.0.20 // indirect | ||||||
| 	github.com/spf13/jwalterweatherman v1.1.0 // indirect | 	github.com/mitchellh/mapstructure v1.5.0 // indirect | ||||||
|  | 	github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect | ||||||
|  | 	github.com/modern-go/reflect2 v1.0.2 // indirect | ||||||
|  | 	github.com/pelletier/go-toml/v2 v2.2.2 // indirect | ||||||
|  | 	github.com/sagikazarmark/locafero v0.4.0 // indirect | ||||||
|  | 	github.com/sagikazarmark/slog-shim v0.1.0 // indirect | ||||||
|  | 	github.com/sourcegraph/conc v0.3.0 // indirect | ||||||
|  | 	github.com/spf13/afero v1.11.0 // indirect | ||||||
|  | 	github.com/spf13/cast v1.6.0 // indirect | ||||||
| 	github.com/spf13/pflag v1.0.5 // indirect | 	github.com/spf13/pflag v1.0.5 // indirect | ||||||
| 	github.com/subosito/gotenv v1.2.0 // indirect | 	github.com/subosito/gotenv v1.6.0 // indirect | ||||||
| 	golang.org/x/sys v0.0.0-20210823070655-63515b42dcdf // indirect | 	github.com/twitchyliquid64/golang-asm v0.15.1 // indirect | ||||||
| 	golang.org/x/text v0.3.6 // indirect | 	github.com/ugorji/go/codec v1.2.12 // indirect | ||||||
| 	gopkg.in/ini.v1 v1.63.2 // indirect | 	go.uber.org/atomic v1.9.0 // indirect | ||||||
| 	gopkg.in/yaml.v2 v2.4.0 // indirect | 	go.uber.org/multierr v1.9.0 // indirect | ||||||
|  | 	golang.org/x/arch v0.8.0 // indirect | ||||||
|  | 	golang.org/x/crypto v0.23.0 // indirect | ||||||
|  | 	golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect | ||||||
|  | 	golang.org/x/sys v0.20.0 // indirect | ||||||
|  | 	golang.org/x/text v0.15.0 // indirect | ||||||
|  | 	google.golang.org/protobuf v1.34.1 // indirect | ||||||
|  | 	gopkg.in/ini.v1 v1.67.0 // indirect | ||||||
|  | 	gopkg.in/yaml.v3 v3.0.1 // indirect | ||||||
| ) | ) | ||||||
|  | |||||||
| @ -5,6 +5,9 @@ datastore: | |||||||
| # Prefix all commands with sudo | # Prefix all commands with sudo | ||||||
| sudo: false | sudo: false | ||||||
|  |  | ||||||
|  | # Directory used to store update temporary files. Mutualized so we save bandwith | ||||||
|  | updateWorkDir: /iocage/freebsd-updates | ||||||
|  |  | ||||||
| # Columns to display when "gocage list". Column names are struct fields, see cmd/struct.go | # Columns to display when "gocage list". Column names are struct fields, see cmd/struct.go | ||||||
| outcol: 'JID,Name,Config.Release,Config.Ip4_addr,Running' | outcol: 'JID,Name,Config.Release,Config.Ip4_addr,Running' | ||||||
|  |  | ||||||
|  | |||||||
		Reference in New Issue
	
	Block a user