Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support static MAC address + fix clone VNET jail #725

Merged
merged 25 commits into from
Dec 27, 2024

Conversation

tschettervictor
Copy link
Collaborator

This PR adds some functionality, and some fixes.

  • support for static MAC VNET jails (bridged and vnet)
  • fix cloning VNET jails
  • simplify some sed commands when cloning jails
  • allow cloning VNET jails and regenerate MAC address for cloned jail

This PR has been tested with bridged VNET, VNET, and loopback jails. Steps below

  1. Create jail
  2. Ensure network works
  3. Clone jail
  4. Ensure network works on cloned jail
  5. Ensure jail.conf is updated properly on cloned jail
  6. Ensure rc.conf is updated properly on cloned jail

This commit will generate a static MAC address for each jail, based on the name of the jail. It will use the first half (xx:xx:xx) of the host MAC to avoid network clashes, and generate a random HEX string from the hashed name of the jail. It will then add that random 5 character HEX string in MAC format, and add an "a" and "b" for the host and jail respectively. 

This way a jail can retain it's MAC ID even if it is deleted and reinstalled, as long as the same name is retained.
Current implementation allows for cloning jails that were created using a static MAC.

Also removed some unnecessary sed strings. These were simplified.
- fixed cloning of VNET bridged jails
- added support for regenerating static MAC on cloned jails
- simplified some sed commands to edit cloned jail.conf file

Tested with bridged VNET, VNET, and loopback jails
- support static MAC on bridged and VNET jails
- remove quotes around vnet.interface
@tschettervictor
Copy link
Collaborator Author

Bumping this to get some conversation going.
For me this is absolutely necessary to have as jails in my networks have MAC filtering applied and need to have a static MAC address.

Thoughts?

This reverts some changes the the "update_jailconf" function.

The reason behind this revert is that if a jail somehow has the same name as a directory, then the previous commit would have changed the directory name as well, which would break stuff.

The current code avoids all that and only replaces the necessary jail name value.
@tschettervictor
Copy link
Collaborator Author

I've reverted some edits to the update_jailconf function after a conversation at #666 regarding jail names. The original commit would have rename even a directory if it had the same name as the jail. The original code, however, handled it perfectly, by only renaming the instances it needed to, so I reverted that part.

The other parts seem fine though, regarding the static MAC and cloning jails, so I'm still pushing for this PR.

@tschettervictor
Copy link
Collaborator Author

I've modified this to include the function "generate_static_mac" in "common.sh" since it is only ever used when creating a jail.

When cloning a jail, the same principle is used, but since the "external_interface" variable is not passed to "clone.sh" it is handled like this: since the jail.conf file is copied over from the old jail, and then edited, we can simply "cat" it to retrieve the MAC prefix. Then, the same hashing method is applied to retrieve the suffix based on the new jail name.

I've tested the clone function quite extensively, even cloning jail into the double digit numbers just to make sure the sed commands properly replace values in "jail.conf" and "rc.conf"

@bmac2
Copy link
Collaborator

bmac2 commented Dec 8, 2024

so this PR ONLY changes mac address for vnet jails and bridged jails, not others. @tschettervictor, correct? It does not make any changes to a shared networking, which uses the mac address of the adapter for the mac address, all the same.

Did you intend it to touch shared networking devices?

@tschettervictor
Copy link
Collaborator Author

That is correct. Only VNET and bridged VNET are affected.

Shared network jails do not need to have a static MAC or any type of MAC really, since they use the host stack.

@bmac2
Copy link
Collaborator

bmac2 commented Dec 8, 2024

when I clone a vnet jail, it gets a new hwaddr. BUT if I reboot a cloned jail the hwaddr changes. It didn't on the original jail I cloned from, it was static. What am I missing?

@tschettervictor
Copy link
Collaborator Author

I'm not 100% sure what the hw address if for to be honest. But the actual MAC is the one found under "ether"

Iocage does it the same way.

@tschettervictor
Copy link
Collaborator Author

image

image

Here are 2 screenshot of an Iocage jail after rebooting it. The ether is the same, but the hw is always different.

@bmac2
Copy link
Collaborator

bmac2 commented Dec 8, 2024

PR tests OK for me. no issues I can find. @yaazkal is going to confirm my testing prior to merging this pr.

@bmac2
Copy link
Collaborator

bmac2 commented Dec 26, 2024

same error. not cloning jail correctly.

root@fbsd:/usr/local/share/bastille # bastille clone jail4 clonedjail4 192.168.1.238/24
Valid: (192.168.1.238/24).
Attempting to clone 'jail4' to clonedjail4...
grep: /usr/local/bastille/jails/clonedjail4/jail.conf: No such file or directory
Cloned 'jail4' to 'clonedjail4' successfully.
root@fbsd:/usr/local/share/bastille # bastille list all
JID State IP Address Published Ports Hostname Release Path
jail1 Up 192.168.1.155 - jail1 14.1-RELEASE /usr/local/bastille/jails/jail1/root
jail4 Up 192.168.1.159 - jail4 14.1-RELEASE /usr/local/bastille/jails/jail4/root
jail5 Up 192.168.1.234 - jail5 14.1-RELEASE /usr/local/bastille/jails/jail5/root
root@fbsd:/usr/local/share/bastille #

@bmac2
Copy link
Collaborator

bmac2 commented Dec 26, 2024

grep: /usr/local/bastille/jails/clonedjail4/jail.conf: No such file or directory

that is the error that is blowing it up. Mine is NOT copying the jail.conf file over, nor is it writing a new one. Looked at the code and did NOT see where it copies over or creates the new jail.conf file prior to checking for it.

also, just checked and it is not even creating a directory for the new cloned jail at all. So no path for it to follow to find a conf file.

@bmac2
Copy link
Collaborator

bmac2 commented Dec 26, 2024

from looking at the code, there is a copy command to copy the jail.conf when you are NOT using zfs, that is line 194. But looking above that in the zfs section I do not see it copying the jail.conf file over and creating the paths. I have zfs enabled on all my stuff. What am I missing in the section from line 167 to 185 where it should create the path and copy the jail.conf file over .

@tschettervictor @yaazkal

@tschettervictor
Copy link
Collaborator Author

ZFS clones the entire jail dataset and sends it to the new location, so the jail.conf file is included.

Can you share the output of "mount | grep yournewjailname?

@tschettervictor
Copy link
Collaborator Author

One thing to not. I had to let bastille create its own "bastille_zfs_prefix" which it then mounts at "bastille_prefix"

If this is not done, then it is not mounted at the right place, and bastille will not use "bastille_prefix" as its mount point.

This results in jails not ending up where bastille expects them to end up.

@bmac2
Copy link
Collaborator

bmac2 commented Dec 26, 2024

root@fbsd:/home/bmccormick # mount | grep jail4
/usr/local/bastille/releases/14.1-RELEASE on /usr/local/bastille/jails/jail4/root/.bastille (nullfs, local, noatime, read-only, nfsv4acls)
devfs on /usr/local/bastille/jails/jail4/root/dev (devfs)
root@fbsd:/home/bmccormick #

@bmac2
Copy link
Collaborator

bmac2 commented Dec 26, 2024

I let bastille create all the mount points. I did exactly ONLY the steps I listed a few comments ago.

@bmac2
Copy link
Collaborator

bmac2 commented Dec 26, 2024

jail4 is the one I am trying to clone. The NEW jail does not show any mount points nor does it create the directory for it at all

root@fbsd:/home/bmccormick # bastille clone jail4 clone4 192.168.1.153/24 Valid: (192.168.1.153/24). Attempting to clone 'jail4' to clone4... grep: /usr/local/bastille/jails/clone4/jail.conf: No such file or directory Cloned 'jail4' to 'clone4' successfully.

@tschettervictor
Copy link
Collaborator Author

tschettervictor commented Dec 26, 2024

What does ZFS list show?

I had one issue where it was trying to take snapshots when they were already created, and it errored out.

Also, maybe add "set -x" to the clone file to see what's going on.

@bmac2
Copy link
Collaborator

bmac2 commented Dec 26, 2024

root@fbsd:/home/bmccormick # zfs list
NAME USED AVAIL REFER MOUNTPOINT
zroot 2.50G 226G 96K /zroot
zroot/ROOT 2.50G 226G 96K none
zroot/ROOT/default 2.50G 226G 2.50G /
zroot/home 236K 226G 96K /home
zroot/home/bmccormick 140K 226G 140K /home/bmccormick
zroot/tmp 96K 226G 96K /tmp
zroot/usr 288K 226G 96K /usr
zroot/usr/ports 96K 226G 96K /usr/ports
zroot/usr/src 96K 226G 96K /usr/src
zroot/var 736K 226G 96K /var
zroot/var/audit 96K 226G 96K /var/audit
zroot/var/crash 96K 226G 96K /var/crash
zroot/var/log 248K 226G 248K /var/log
zroot/var/mail 104K 226G 104K /var/mail
zroot/var/tmp 96K 226G 96K /var/tmp
root@fbsd:/home/bmccormick #

@tschettervictor
Copy link
Collaborator Author

Your bastille mounts aren't even showing...

I'm on IRC now. How can we hook up?

@bmac2
Copy link
Collaborator

bmac2 commented Dec 26, 2024

ZFS options

bastille_zfs_enable="YES" ## default: ""
bastille_zfs_zpool="" ## default: ""
bastille_zfs_prefix="bastille" ## default: "${bastille_zfs_zpool}/bastille"
bastille_zfs_options="-o compress=lz4 -o atime=off" ## default: "-o compress=lz4 -o atime=off"

all default options, no changes from default

@tschettervictor
Copy link
Collaborator Author

You have to add your zpool there.

Here is my zfs list

root@bastille:~ # zfs list | grep bastille
zroot/bastille                        1.01G  33.7G      416K  /usr/local/bastille
zroot/bastille/backups                 384K  33.7G      384K  /usr/local/bastille/backups
zroot/bastille/cache                   210M  33.7G      384K  /usr/local/bastille/cache
zroot/bastille/cache/13.4-RELEASE      209M  33.7G      209M  /usr/local/bastille/cache/13.4-RELEASE
zroot/bastille/jails                  59.8M  33.7G      384K  /usr/local/bastille/jails
zroot/bastille/jails/jail1            19.8M  33.7G      400K  /usr/local/bastille/jails/jail1
zroot/bastille/jails/jail1/root       19.4M  33.7G     19.4M  /usr/local/bastille/jails/jail1/root
zroot/bastille/jails/jail2            19.8M  33.7G      400K  /usr/local/bastille/jails/jail2
zroot/bastille/jails/jail2/root       19.4M  33.7G     19.4M  /usr/local/bastille/jails/jail2/root
zroot/bastille/jails/jailvnet         19.8M  33.7G      400K  /usr/local/bastille/jails/jailvnet
zroot/bastille/jails/jailvnet/root    19.5M  33.7G     19.5M  /usr/local/bastille/jails/jailvnet/root
zroot/bastille/logs                    512K  33.7G      512K  /var/log/bastille
zroot/bastille/releases                765M  33.7G      384K  /usr/local/bastille/releases
zroot/bastille/releases/13.4-RELEASE   764M  33.7G      764M  /usr/local/bastille/releases/13.4-RELEASE
zroot/bastille/templates               384K  33.7G      384K  /usr/local/bastille/templates
root@bastille:~ #

@tschettervictor
Copy link
Collaborator Author

zroot/bastille is mounted at /usr/local/bastille

@bmac2
Copy link
Collaborator

bmac2 commented Dec 26, 2024

what is your bastille.conf ?

@tschettervictor
Copy link
Collaborator Author

ZFS options

bastille_zfs_enable="YES" ## default: ""
bastille_zfs_zpool="zroot" ## default: ""
bastille_zfs_prefix="bastille" ## default: "${bastille_zfs_zpool}/bastille"
bastille_zfs_options="-o compress=lz4 -o atime=off" ## default: "-o compress=lz4 -o atime=off"

Everything else is default.

@bmac2
Copy link
Collaborator

bmac2 commented Dec 26, 2024

ZFS options

bastille_zfs_enable="YES" ## default: ""
bastille_zfs_zpool="zroot" ## default: ""
bastille_zfs_prefix="bastille" ## default: "${bastille_zfs_zpool}/bastille"
bastille_zfs_options="-o compress=lz4 -o atime=off" ## default: "-o compress=lz4 -o atime=off"

mine and it is not creating mount points like yours

@tschettervictor
Copy link
Collaborator Author

You need to let bastille create "zroot/bastille" or it will not mount it correctly.

I recommend trashing it and restarting. Just zfs destroy -r zroot/bastille

Then bootstrap a release and it will do its thing automatically.

@bmac2
Copy link
Collaborator

bmac2 commented Dec 26, 2024

root@fbsd:/usr/local/etc/bastille # bastille list all
JID State IP Address Published Ports Hostname Release Path
jail1 Down 192.168.1.155 - jail1 14.1-RELEASE /usr/local/bastille/jails/jail1/root
jail2 Down 192.168.1.156 - jail2 14.1-RELEASE /usr/local/bastille/jails/jail2/root
jail3 Down 192.168.1.152,10.0.0.152 - jail3 14.1-RELEASE /usr/local/bastille/jails/jail3/root
jail4 Up 192.168.1.159 - jail4 14.1-RELEASE /usr/local/bastille/jails/jail4/root
jail5 Down 192.168.1.160 - jail5 14.1-RELEASE /usr/local/bastille/jails/jail5/root
jail6 Up 192.168.1.162 - jail6 14.1-RELEASE /usr/local/bastille/jails/jail6/root
jail7 Up 192.168.1.163 - jail7 14.1-RELEASE /usr/local/bastille/jails/jail7/root
root@fbsd:/usr/local/etc/bastille #

jail6 and jail7 were created after setting the zfs root setting. Still no love

@tschettervictor
Copy link
Collaborator Author

I had to completely restart after that happened to me. The problem is that if bastille does not create the zroot/bastille dataset, then it never gets mounted at the right place. This then affects everything going forward.

@bmac2
Copy link
Collaborator

bmac2 commented Dec 26, 2024

cannot create 'zroot/bastille/jails/jail7': parent does not exist
cannot create 'zroot/bastille/jails/jail7/root': parent does not exist
that is my error now

@tschettervictor
Copy link
Collaborator Author

Were you able to destroy zroot/bastille?

I had that error too before I completely wiped it.

@bmac2
Copy link
Collaborator

bmac2 commented Dec 26, 2024

the default settings are NOT to create a new zfs mount for each jail. The default action is to just put them in the
/usr/local/bastille/jails/ directory. When did creating a new zfs for each jail become default settings?? Do we want to enforce that?

@yaazkal

@bmac2
Copy link
Collaborator

bmac2 commented Dec 26, 2024

root@fbsd:/home/bmccormick # bastille create -V jail8 14.1-RELEASE 192.168.1.162/24 re0
Valid: (192.168.1.162/24).
Valid: (re0).
cannot create 'zroot/bastille/jails/jail8': parent does not exist
cannot create 'zroot/bastille/jails/jail8/root': parent does not exist

Creating a thinjail...

no IP address found for not set
[jail8]:
e0a_bastille1
e0b_bastille1

@bmac2
Copy link
Collaborator

bmac2 commented Dec 27, 2024

after fixing my zfs issues this PR tests correctly.

things tested.

  1. Cloning jails, both vnet and normal jailes
  2. static ether addresses holding after reboot of jail

Merging PR

@bmac2 bmac2 merged commit 43b839a into BastilleBSD:master Dec 27, 2024
1 check passed
@yaazkal
Copy link
Collaborator

yaazkal commented Dec 27, 2024

thank you @bmac2 unfortunately I had no time to test it.
thank you @tschettervictor for the initiative.

@yaazkal yaazkal mentioned this pull request Jan 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants