NVIDIA-PRIME: Cant switch to intel

Whenever I switch from my gpu (gtx 1060) to my igpu (Intel 530), I cant boot. The loading screen is stuck with the following message:

[Failed] Failed to start NVIDIA Persistence Daemon. See 'systemctl status nvidia-persistenced.service' for details.

However, I can boot when I switch back to the Nvidia gpu and switching to my igpu without rebooting works temporarily. I'm using Kubuntu 16.10 and only my internal laptop display. That's the output when I switch to my igpu:

sudo prime-select intel
Info: the current GL alternatives in use are: ['nvidia-378', 'nvidia-378']
Info: the current EGL alternatives in use are: ['nvidia-378', 'nvidia-378']
Info: selecting nvidia-378-prime for the intel profile
update-alternatives: using /usr/lib/nvidia-378-prime/ld.so.conf to provide /etc/ld.so.conf.d/x86_64-linux-gnu_GL.conf (x86_64-linux-gnu_gl_conf) in manual mode
update-alternatives: using /usr/lib/nvidia-378-prime/ld.so.conf to provide /etc/ld.so.conf.d/x86_64-linux-gnu_EGL.conf (x86_64-linux-gnu_egl_conf) in manual mode
update-alternatives: using /usr/lib/nvidia-378-prime/alt_ld.so.conf to provide /etc/ld.so.conf.d/i386-linux-gnu_GL.conf (i386-linux-gnu_gl_conf) in manual mode
update-alternatives: using /usr/lib/nvidia-378-prime/alt_ld.so.conf to provide /etc/ld.so.conf.d/i386-linux-gnu_EGL.conf (i386-linux-gnu_egl_conf) in manual mode

sudo prime-select nvidia
Info: the current GL alternatives in use are: ['nvidia-378-prime', 'nvidia-378-prime']
Info: the current EGL alternatives in use are: ['nvidia-378-prime', 'nvidia-378-prime']
Info: selecting nvidia-378 for the nvidia profile
update-alternatives: using /usr/lib/nvidia-378/ld.so.conf to provide /etc/ld.so.conf.d/x86_64-linux-gnu_GL.conf (x86_64-linux-gnu_gl_conf) in manual mode
update-alternatives: using /usr/lib/nvidia-378/ld.so.conf to provide /etc/ld.so.conf.d/x86_64-linux-gnu_EGL.conf (x86_64-linux-gnu_egl_conf) in manual mode
update-alternatives: using /usr/lib/nvidia-378/alt_ld.so.conf to provide /etc/ld.so.conf.d/i386-linux-gnu_GL.conf (i386-linux-gnu_gl_conf) in manual mode
update-alternatives: using /usr/lib/nvidia-378/alt_ld.so.conf to provide /etc/ld.so.conf.d/i386-linux-gnu_EGL.conf (i386-linux-gnu_egl_conf) in manual mode

Result of lspci -k | grep -EA2 'VGA|3D'

00:02.0 VGA compatible controller: Intel Corporation HD Graphics 530 (rev 06)
        DeviceName:  Onboard IGD
        Subsystem: CLEVO/KAPOK Computer HD Graphics 530
--
01:00.0 VGA compatible controller: NVIDIA Corporation GP106M [GeForce GTX 1060] (rev a1)
        Subsystem: CLEVO/KAPOK Computer Device 6a03
        Kernel driver in use: nvidia

I tried to solve this issue on my own for over a week and have not a clue what to do. Everything I tried failed.

Could you please help me?

UPDATE 5.2.2017:

I tested it again today and realized that switching to the intel gpu does not work at all with 375 and 378. Logging out freezes the screen. So why did it work once? If I install all the drivers again after I deleted the old ones and afterwards use prime-select intel (without reboot!), prime-select does not update the prime profiles -> It selects one of the old non-nvidia profiles (mesa profiles instead of nvidia) and it works. But as soon as I switch to an nvidia profile or reboot, the prime profiles are updated and it doesn't work anymore. So I guess there's something wrong with the EGl alternatives for the intel gpu? Sorry for any inconvenience.

UPDATE 12.2.2017:

Yesterday, I modified the nvidia-prime software and fixed the bug. I will soon publish (probably next week) my modified version and paste a link here, but I still encounter an issue: when I shutdown my PC after I executed sudo prime-select intel:

ksmserver crashes (I'm using Kubuntu, probably not a problem with Ubuntu)

-> the PC can't shutdown completely and the old (wrong) update-alternatives for the Intel GPU are restored

-> I can't boot.

However, if I run sudo prime-select intel after shutdown in recovery mode everything works alright and I can use the Intel GPU.

UPDATE 17.2.2017:

I posted my workaround and am still trying to get the Bug fixed in the official Repositories. However, this is probably a Bug in update-alternatives, so at next I have to post a bug-report there.

In the meantime, enjoy my workaround! I hope it works for you too. If there are any questions please leave a comment.

UPDATE 26.2.2017

As @whizzzkid pointed out, the nvidia graphics card still remains active after applying my patch. However, today I was able to resolve this issue. I will probably update my answer to include my 2nd patch next week. Here are some tests I ran:

Nvidia: 43-45W

Intel (nvidia on): 29-31W

Intel (nvidia off): 15-17W


Solution 1:

Here's my fix/workaround for the bug:

If you want to use bumblebee instead of nvidia-prime, have a look at the answer provided by @whizzzkid below.

Tested with Kubuntu 16.10 64-bit using KDE Plasma Version.7.5 and KDE Frameworks Version 5.26.0. The following steps work for me. I hope they will work for you too, but I can't guarantee that and take no responsibility if they don't. But I'm glad to help you if you leave a comment.

In my case, the problem was that the update-alternative /usr/lib/nvidia-XYZ-prime/ didn't work at all. It's supposed to redirect calls to the intel-mesa driver, but for some reason it didn't. So it's probably after all a bug in update-alternatives? I still haven't found a place to report a bug for update-alternatives. Please comment if you can help me.

I basically modified nvidia-prime a bit to change the update-alternatives directly to the intel-mesa driver instead of /usr/lib/nvidia-XYZ-prime/.

Important:

Before you apply any of these steps make sure to know how to access recovery mode on your PC. If you can't boot anymore due to this guide you can still undo the changes in recovery mode with the commands in the last section of this guide. See this website about how to access recovery mode for more information.

If you use a 32-bit operating system, you have to leave out all commands containing x86_64 in Step 4 and in the section How to undo all these modifications.


Step 1:

Install my patch using an easy method provided by @whizzzkid in his answer below:

cd /usr/bin
sudo mv prime-select prime-select.bkup
sudo wget https://raw.githubusercontent.com/C11235/nvidia-prime-bugfix/master/prime-select
sudo chmod 755 prime-select

Step 2:

Apply my second patch to reduce the power consumption in intel mode:

Check which nvidia-driver version you have. For example with the following command:

$ cat /proc/driver/nvidia/version
NVRM version: NVIDIA UNIX x86_64 Kernel Module  378.13  Tue Feb  7 20:10:06 PST 2017
GCC version:  gcc version 6.2.0 20161005 (Ubuntu 6.2.0-5ubuntu12)

In this case nvidia-XYZ is nvidia-378. See this Stackoverflow thread.

Create a bash file containing with the following contents named for example poweroff.sh

#!/bin/bash
line=$(</etc/prime-discrete)
if [ "$line" == "off" ] 
   then
   rmmod nvidia-drm
   rmmod nvidia-modeset
   rmmod nvidia
   bash -c 'echo OFF > /proc/acpi/bbswitch'
fi 

Create a second bash file containing with the following contents named for example poweron.sh and replace XYZ with your nvidia driver version number.

#!/bin/bash
modprobe nvidia-XYZ
modprobe nvidia-XYZ-modeset
modprobe nvidia-XYZ-drm
modprobe nvidia-XYZ-uvm
echo ON > /proc/acpi/bbswitch

In Intel mode, you have to run the first script after login with sudo and the second one before logout with sudo (otherwise your laptop will probably crash, as in my case). I just execute them with sudo bash FILENAME.

So far I wasn't able to automatically autostart these scripts as root on login and before logout. However, it might be possible with upstart, as @Fiximan pointed out. I might update this answer again sometime in the future if I manage to do so.


(probably) KDE Plasma specific errors:

There is still a bug I wasn't able to fix in KDE Plasma: Whenever I switch from Nvidia to Intel after I started the PC in nvidia-mode, ksmserver crashes and the PC is rendered unusable, I have to turn it off by pressing the power button for about 5-10 seconds. This sometimes results in the update-alternatives being changed to nvidia-prime instead of intel-mesa which leads to a black-screen upon startup.

If you happen to be affected by the ksmserver bug, please contribute to the official bug-report on bugs.kde.org.

However, the problem that the PC won't shutdown can be avoided by always selecting intel-mode before shutdown, even when you use nvidia-mode. If you do this, everything works as it should in the first place and you don't have to power off your PC by long-pressing the power-button except after you apply this guide in nvidia-mode.

A simple workaround for the black-screen-upon-startup-bug is to just remove the not working update-alternatives completely:

Step 1:

You can access your PC again after you enter recovery mode, select root command shell and execute the following 2 commands:

mount -o remount,rw /
prime-select nvidia

Step 2:

Execute the following commands:

sudo update-alternatives --remove i386-linux-gnu_gl_conf /usr/lib/nvidia-XYZ-prime/alt_ld.so.conf
sudo update-alternatives --remove i386-linux-gnu_egl_conf /usr/lib/nvidia-XYZ-prime/alt_ld.so.conf
sudo update-alternatives --remove x86_64-linux-gnu_gl_conf /usr/lib/nvidia-XYZ-prime/ld.so.conf
sudo update-alternatives --remove x86_64-linux-gnu_egl_conf /usr/lib/nvidia-XYZ-prime/ld.so.conf

How to undo all these modifications

Just execute the following commands:

sudo rm /usr/bin/prime-select
sudo mv /home/YOUR-USERNAME/prime-select.backup /usr/bin/prime-select
sudo update-alternatives --install /etc/ld.so.conf.d/i386-linux-gnu_GL.conf i386-linux-gnu_gl_conf /usr/lib/nvidia-XYZ-prime/alt_ld.so.conf 8603
sudo update-alternatives --install /etc/ld.so.conf.d/i386-linux-gnu_EGL.conf i386-linux-gnu_egl_conf /usr/lib/nvidia-XYZ-prime/alt_ld.so.conf 8603
sudo update-alternatives --install /etc/ld.so.conf.d/x86_64-linux-gnu_GL.conf x86_64-linux-gnu_gl_conf /usr/lib/nvidia-XYZ-prime/ld.so.conf 8603
sudo update-alternatives --install /etc/ld.so.conf.d/x86_64-linux-gnu_EGL.conf x86_64-linux-gnu_egl_conf /usr/lib/nvidia-XYZ-prime/ld.so.conf 8603

Solution 2:

the patch by @C11235 works, now i can switch between nvidia and intel, but this does not reduce the power consumption. :-/ which means both are still running.

BTW this is an easier way to do this.

cd /usr/bin
sudo mv prime-select prime-select.bkup
sudo wget https://raw.githubusercontent.com/C11235/nvidia-prime-bugfix/master/prime-select
sudo chmod 755 prime-select

Solution 3:

Solution for not booting after switching to intel videocard :
Add kernel boot parameters :

acpi_osi=! acpi_osi="Windows 2009"

The reason of this problem: bug #156341 in kernel
Solution source: link

some more detailed, may be it will be more easy to understand :

sudo echo -e "GRUB_CMDLINE_LINUX_DEFAULT='acpi_osi=! acpi_osi='Windows 2009''"  | sudo tee -a /etc/default/grub
sudo update-grub

Solution 4:

Possible issue with the 2nd Fix: Last command of the "poweroff"-script prompts

    bash: /proc/acpi/bbswitch: No such file or directory

Apparently, when I reboot in Intel mode (Changing the mode doesn't go into effect 'til I rebboot, don't know whether this is normal), the bbswitch-Module doesn't even get loaded. So there is no bbswitch-file and no way to turn off the nvidia GPU.

I'm using:

  • MSI-Laptop (GL72 7RDX)
  • Integrated GPU: Intel® HD Graphics 630 (Kaby Lake GT2)
  • separate GPU: Geforce GTX 1050
  • OS: Ubuntu 16.04 lts

Solution for this problem:

Edit the file /etc/modules (with root privileges) and add the line

    bbswitch

Now bbswitch Is operational even when I boot in intel mode, and C11235's second fix works just fine.