Showing posts with label Safely Remove. Show all posts
Showing posts with label Safely Remove. Show all posts

Saturday, December 10, 2016

Kernel Patch - USB3 HDD Gets Mounted Again After Safely Remove


I have just found out that the Linux kernel patch for this issue has appeared on Linux Kernel Mailing List. I think it is supposed to be included in Linux kernel 4.10. Here is how I applied the patch on Debian 8 (Jessie).

Requirements

  1. Debian 8 (Jessie)
  2. Linux kernel source code 4.4.x LTS (Vanilla)
  3. Some basic knowledge on compiling Linux kernel source code

Instructions


1. Download Linux kernel source code.

$ wget "https://cdn.kernel.org/pub/linux/kernel/v4.x/linux-4.4.35.tar.xz"

2. Extract kernel source code.

$ tar xvf linux-4.4.35.tar.xz

3. Enter extracted kernel source code.

$ cd linux-4.4.35

4. Download USB3 HDD safely remove patch here and apply it.

$ patch -p1 -i fix-auto-remount-of-safely-removed-or-ejected-usb-3-devices-v3.patch

5. Copy kernel config from default kernel

$ cp /boot/config-3.16.0-4-686-pae .config
$ make olddefconfig

6. Adjust kernel config. Enable CONFIG_IP_NF_NAT and its sub-configs as modules (or else you will lose iptables NAT).

$ make menuconfig

7. Compile kernel.

$ make -j $(nproc) deb-pkg KDEB_PKGVERSION=$(make kernelversion)-1~local1

8. Install kernel. See notes below for any errors encountered.

$ sudo dpkg -i linux-image-4.4.35_4.4.35-1~local1_i386.deb linux-headers-4.4.35_4.4.35-1~local1_i386.deb

Notes


1. The open-vm-tools included in Debian Jessie cannot be compiled with kernel 4.4. It can be safely purged if you don't use it.

$ sudo apt-get purge open-vm-tools-dkms

2. If you use broadcom-sta-dkms, use the one from backports. The one from stable is too old for kernel 4.4.

$ sudo apt-get -t jessie-backports install broadcom-sta-dkms

3. To suppress error about pcspkr already registered on boot, blacklist snd-pcsp.

$ echo "blacklist snd-pcsp" > "/etc/modprobe.d/snd-pcsp-blacklist.conf"

4. If you are on Intel graphics and Xorg occasionally crash e.g. when you are playing video using VAAPI, add "i915.semaphores=1" to GRUB_CMDLINE_LINUX_DEFAULT in "/etc/default/grub" and run "sudo update-grub"

GRUB_CMDLINE_LINUX_DEFAULT="quiet i915.semaphores=1"

References

Sunday, March 15, 2015

USB HDD Gets Mounted Again After Safely Remove + Workaround

OBSOLETE: See updated info here.

This is actually an update for my previous post: Workaround for USB Drive is Mounted Again After "Safely Remove" on Debian Wheezy

As I have written before, I have had this issue since Debian 7 "Wheezy". When I perform safely remove, my USB HDD always gets mounted again. Then, I had to perform safely remove once more in order to remove my USB HDD for good.

At the time, I was using a laptop with an NVIDIA chipset and this issue did not occur on my friend's laptop which has an Intel chipset. However, I have been using this laptop since before Debian 7 "Wheezy" and it did not have this issue before. It turns out that this issue has not been solved until this post is written (Launchpad Bug 1239087 and Launchpad Bug 792085).

At that time, I started to look at the changes which has been made in linux kernel and udisks source code. Then I found the following lines in udisks /src/helpers/job-drive-detach.c:

      /* the remove file is pretty recent (commit as1297, Dec 2009) */
      if (sysfs_exists (udev_device_get_syspath (udevice_usb_device), "remove"))
        {
          g_printerr ("Disabling USB port for device: ");
          if (!sysfs_write (udev_device_get_syspath (udevice_usb_device), "remove", "1"))
            goto out;
          g_printerr ("OK\n");
        }

After some further digging, what I understand was that commit as1297 in linux kernel was intended to add sysfs "remove" attribute to USB devices in order to "emulate" Windows safely remove behavior. If my memory serves my right, this commit did not exist during Debian 6 "Squeeze" days and safely remove issue did not exist back then with my laptop.

With my limited C language understanding, I decided to remove the source code lines above and recompile udisks. After this, the safely remove works without the disk getting mounted again! I keep using this recompiled udisks during Debian 7 "Wheezy" days while waiting for a proper fix from the experts since it involves linux kernel.

Here comes the update.

A few months later, I had to buy a new laptop. This time I got a laptop which has an Intel chipset with a hope that i won't encounter that bug again. The laptop comes with Core i5 Haswell and has USB 3.0 ports. Since Debian 7 "Wheezy" does not support Haswell, I had to use Debian 8 "Jessie" which was still in testing. To my surprise, I encountered a similar bug but with USB 3.0 and it is worse. This time, no matter how many times I perform safely remove, the disk keeps getting mounted again. So, I thought I will remove the "offending" code again and recompile udisks. However, Debian 8 "Jessie" uses udisks2 which is an (incomplete) rewrite of udisks. Genius! I cannot apply my previous trick anymore.

I spend a few weeks to figure out what udisks does but udisks2 does not. I copy pasted source codes from udisks to udisks2 without any proper understanding. Then I modify it a bit so that my franken-udisks2 can be compiled. I can't explain it all here since it is quite long. At the end of it, safely remove works again for me on Debian 8 "Jessie"!

You can get the franken-udisks2 that I compiled for Debian 8 "Jessie" i386 below. For AMD64, you will have to compile it on your own (sorry, I am still maintaining i386 machines). Please note that while it works for mine, it may not work for your hardware. However, you can always try it AT YOUR OWN RISK! I AM NOT RESPONSIBLE FOR ANY DAMAGE CAUSED BY IT.

Franken-udisks2 (Debian 8 "Jessie" i386 build + debian source): Dropbox

This bug has existed since at least 2011. I am writing this post with a hope that it can help to solve it once and for all so that I can retire my franken-udisks2. I am willing to provide any information required.