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

Pull wireless from config, enabling use in image and initramfs #72

Merged
merged 5 commits into from
Dec 16, 2018

Conversation

zmrow
Copy link

@zmrow zmrow commented Nov 9, 2018

Once testing is completed, this should fix #17

After a few iterations and much pondering, I believe this is the safest, simplest, and easiest way to make wireless connectivity possible both in the rasbian image as well as the initramfs.

Basically if the .wpa_supplicant key exists in the config, we decode it and put it in the ${CATTLEPI_DIR}/wpa_supplicant.conf. This makes it available in subsequent boots to both the initramfs and (after injecting it) the image. We also attempt to update the wpa_supplicant.conf every boot.

Documentation for wpa_suppicant is here.

I'm happy to further explain why I went about it this way, but honestly this is pretty straight forward and I couldn't see how my other (more-complex) methods would have been any better.

@zmrow zmrow added this to the v1.3 milestone Nov 9, 2018
@zmrow zmrow self-assigned this Nov 9, 2018
@zmrow zmrow requested a review from t3spe November 9, 2018 05:41
cattlepi_bring_up_wlan() {
interface="${1:-wlan0}"
wpa_supplicant_location="${2:-/etc/wpa_supplicant/wpa_supplicant.conf}"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should you use this var?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops - missed that. Will do!

cmp -s "${CATTLEPI_DIR}/wpa_supplicant.conf" /tmp/wpa_supplicant.conf
if [ $? -ne 0 ]; then
mv /tmp/wpa_supplicant.conf "${CATTLEPI_DIR}/wpa_supplicant.conf"
echo "wpa_supplicant.conf updated"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we force a reboot if the supplicant was written? this way we could use it immediately.
also, one question i had was: if the initrd writes the supplicant, can the actual raspbian os in which we're booting use ti the first time? (or do we need a reboot also?)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm. Yeah, I suppose we could reboot right away?

After wpa_supplicant is written to $CATTLEPI_DIR in this step, later in the boot process the build step runs which should copy the file to the raspbian filesystem in the appropriate place.

@t3spe
Copy link
Collaborator

t3spe commented Nov 9, 2018

also:
WINNING: http://cattlepi-ci-logs.s3-website-us-west-2.amazonaws.com/7477c47d-1e1e-4968-a8f6-7c47f0231da9/build_output.txt
The super CI picked up the PR and built it. It's still a noop when building but we are pretty close to having a functional e2e

@zmrow
Copy link
Author

zmrow commented Nov 10, 2018

That's awesome!! Can' wait to see the e2e

@zmrow
Copy link
Author

zmrow commented Nov 14, 2018

Oh.... Looks like this will require a change to the API as well? Docs say that keys will get ignored unless they are known. I'm guessing the API doesn't know about a key "wpa_supplicant".

@t3spe
Copy link
Collaborator

t3spe commented Nov 14, 2018

Yep.
The api does not actually care about what you put in the configuration json. It’s the different bits and pieces in the image that give meaning to the config.
So, yes you can add stuff in there, and as long as it’s still a valid json we should be good to go.

@zmrow
Copy link
Author

zmrow commented Nov 15, 2018

Update - I finally had a bit of time to dig into why my POSTs weren't working. I was trying to POST to the API (make localapi_run) to update the config with wpa_supplicant, but was getting a 405.

Derpaderp the local API server doesn't seem support that method yet!! I should be able to edit the on-get method and add the wpa_supplicant key tonight and test this before the weekend.

Apologies for the long wait.

@t3spe
Copy link
Collaborator

t3spe commented Nov 15, 2018

Yes. It’s pretty limited and mosty for serving images. We could extend it or at a minimum have it return something more meaningful

@zmrow
Copy link
Author

zmrow commented Nov 19, 2018

Working through a few issues I found, will update this PR once I fix them.

@zmrow
Copy link
Author

zmrow commented Nov 19, 2018

The wpa_supplicant file is correctly created and put in CATTLEPI_DIR as well as /etc/wpa_supplicant.

In netup we check whether the wpa_supplicant file is readable and then use it. However even though the file exists and should be readable, that block of code in netup being skipped. Really not sure why this is happening.
😕 😖

@t3spe
Copy link
Collaborator

t3spe commented Nov 20, 2018

The netup runs before the rootfs is built. So when it’s running there is nothing there. Later in the process it the rootfs is built but by this time it’s too late? (This is just a guess)

@zmrow
Copy link
Author

zmrow commented Nov 20, 2018

Correct, rootfs won't be available. I just realized that /dev/mmcblk0p1 won't be mounted to /boot at the time netup runs. Since we use /boot/cattlepi as persistent storage for things like config and wpa_supplicant, it must be available in order to use the wpa_supplicant for wireless during initramfs runtime. Of course, if nothing is there, we won't use anything.

Is there any reason you can see not to mount /boot during netup? Options are:

  • Mount /boot during netup, unmount it at the end of netup. Keep the sdmount script. This is a little kludgy but keeps a distinct sdcard mounting step in the initramfs scripts.
  • Mount /boot during netup, don't unmount it at the end of netup, and delete the sdmount script.
  • Rearchitect all of this. The reason we use CATTLEPI_DIR as our persistent storage is because you may not include or add files to the initramfs without rebuilding it, which can't be done from within the initramfs itself. It could be done in the booted raspbian image... this seems a bit more complicated than the approach I've taken.

@t3spe
Copy link
Collaborator

t3spe commented Nov 20, 2018

Somewhat agree w/ all of the above. I do NOT want to alter the initramfs as this would take us on an interesting path.

Here is what I would suggest: let’s add a few methods to the helpers that deal with mounting [ro and rw] and unmounting the sdcard. Invoke those where it’s appropriate. Remove the sdmount and sdmountro steps.

I would also strongly recommend a reboot if the supplicant is updated. (It’s okay if the pi reboots once or twice while booting and picking up “persisting” changes - this ensures it’s going to work if it loses power while up and comes back w/ no network). I am already doing this if I need to partition the sd card.

This commit finishes adding the correct modules/drivers to the
initramfs. We also specify the control interface during
wpa_supplicant invocation in the initramfs to avoid any
conflicts with any interface the user has specified in
the wpa_supplicant.conf.
@zmrow zmrow changed the title [WIP] Pull wireless from config, enabling use in image and initramfs Pull wireless from config, enabling use in image and initramfs Dec 10, 2018
@zmrow
Copy link
Author

zmrow commented Dec 10, 2018

This is fully working now! You can have a fully wireless raspbian_cattlepi image after the initial boot and setup. This means only need to boot the Pi hardwired to the network once and after that it can function fully wirelessly. (I have opened up issue #91 to track the documentation of this feature)

You need a few things for this to function correctly.

  1. A valid wpa_supplicant.conf file that is base64 encoded (just as we do with the usercode). An example of said file is below. See the Raspberry Pi docs here for more info.
  2. This base64 encoded file must live in the config under the top-level key wpa_supplicant. (At the same level as usercode, etc.

Even with proper wireless config, a fully booted raspbian_cattlepi machine that has an ethernet cable plugged into it will give precedence to the hardwired network. I personally feel that this is a good default. Feel free to pull the cable, reboot the box (just to be safe) and your machine can function wirelessly from then on forward.

Example wpa_supplicant:

ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
country=US

network={
        ssid="cool_wireless_ssid"
        psk="REDACTED"
        key_mgmt=WPA-PSK
}

Example config:

{
  "usercode": "",
  "wpa_supplicant": "Awholebunchofbase64encodeddata",
  "bootcode": "",
...
}

@t3spe
Copy link
Collaborator

t3spe commented Dec 16, 2018

thank you

@t3spe t3spe merged commit b833fea into cattlepi:master Dec 16, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

configure wifi on boot from config
2 participants