Prevent Windows 10 from formatting ext4 partition on pendrive

I used to create two partitions on pendrives that would be used either on Windows or GNU/Linux.

The first partition I'd format with ntfs, the second one with ext4. This way Windows would not see the second partition and would not ask to format when plugged in.

Since one of the last updates to Windows 10, this trick does not work anymore.

These pendrives I'm talking about are given to teachers at schools that use random GNU/Linux distributions but at home use Windows 10. When they get back to the school from home they realize that the ext4 partition is gone, as they just clicked 'yes' when Windows asked to format the partition.

I have tried to hide the second partition with parted, but I don't find the right switch.

What should I do in order to hide/protect the ext4 partition on the pendrive from Windows 10?


Solution 1:

1. UDF can replace ext4 to protect a partition with linux style

  • The problem is that Windows 10 has changed from ignoring a second partition with the linux ext4 file system to prompting the user the format it.
  • Windows 10 does not prompt the user to format UDF, the Universal Disk Format.

UDF is described in the following links,

  • en.wikipedia.org/wiki/Universal_Disk_Format
  • tanguy.ortolo.eu/blog/article93/usb-udf
  • Full compatibility with Linux, Windows and MacOS, scroll down to 'UDF'

UDF provides a workaround that helps protect a partition with linux style

  • links and
  • individual permissions for files and directories.

It is even possible to use UDF in the casper-rw partition of a persistent live drive, illustrated by the following screenshot of Lubuntu 16.04.1 LTS,

enter image description here

Commands

  • Install udftools if necessary

    sudo apt-get install udftools
    
  • Create a partition table with gparted or gnome-disks

  • Erase confusing data with dd (risky!)

    sudo dd if=/dev/zero of=/dev/sdxn bs=1M count=1  # wipe first MiB
    

    where x is the drive letter and n is the partition number. Check and double check that everything is correct before you press the Enter key to run the dd command line!

  • Create UDF file system (risky!)

    sudo mkudffs -b 512 --media-type=hd --lvid=my-label /dev/sdxn
    

    where x is the drive letter and n is the partition number. Check and double check that everything is correct before you press the Enter key!

Backup

But it might be difficult to find good tools to repair UDF. Maybe Windows can fix some errors. So it is important to backup the content regularly, so that the content is not lost, if the file system gets damaged.


2. ext4in a file and loop mount to use it

If you must have ext4, you can put it in a file and loop mount to use it from linux. I don't think that Windows will bother to look in the file.

This method is the same as for a casper-rw file for persistence.

Example:

  • Create mount points (only once)

    sudo mkdir -p /mnt/lp1
    sudo mkdir -p /mnt/sd1
    
  • Mount the partition, where you intend to create the file.

    sudo mount /dev/sdxn /mnt/sd1
    

    where x is the drive letter and n is the partition number.

  • Create a file. In the FAT32 file system the maximum file size 4 GB, but in NTFS and UDF, the size is limited by the size of the partition.

    The following command line will make an empty file with the size (bs * count, in this case 1MiB*8KiB) = 8GiB.

    sudo dd if=/dev/zero of=/mnt/sd1/linux-fs bs=1M count=8K
    
  • Create an ext4 file system in the file

    sudo mkfs.ext4 /mnt/sd1/linux-fs
    
  • In order to use the file in linux, loop mount it (you can provide a script or desktop file for the end users),

    sudo mount -o loop /mnt/sd1/linux-fs /mnt/lp1
    

    or maybe you would prefer something like this,

    sudo mount -o defaults,users,loop /mnt/sd1/linux-fs /mnt/lp1
    
  • I assume that you already create a structure of directories, ownerships and permissions in the ext4 partition, and the same structure should work in this ext4 file too.

The content will be available via the mount point /mnt/lp1, which means that you and an end user can read and write files and directories, just like an ext4 file system in a partition. But Windows does not prompt the user the format it.

Solution 2:

Solution

As @sudodus has stated correctly:

The problem is that Windows 10 has changed from ignoring a second partition with the linux ext4 file system to prompting the user the format it.

At the same time, it seems like your standard Windows computer ships with hidden partitions (efi, recovery, ...) by default, so there is definitely some way to mark a partition such that Windows ignores it. Here is what worked for me:

  1. Create a GPT partition table.
  2. Set the partition type of your Linux partitions to "Microsoft basic data".
  3. Add the "hidden" attribute to that partition (as suggested here).

You can do steps two and three in any order, but step two seems to be necessary. From the looks of it, Microsoft chose to ignore attributes on partitions with non-Microsoft partition types.

Commands (using gdisk)

This is how you would partition an empty disk at /dev/sdb with two partitions. Partition one will be visible to Windows, partition two will be hidden. Note that these are instructions to format the disk. You will loose all data you had previously stored on this particular device.

$ sudo gdisk /dev/sdb                     # You might want to use a different disk
GPT fdisk (gdisk) version 1.0.8

Partition table scan:
  MBR: protective
  BSD: not present
  APM: not present
  GPT: present

Found valid GPT with protective MBR; using GPT.

Command (? for help): o
This option deletes all partitions and creates a new protective MBR.
Proceed? (Y/N): y

Command (? for help): n
Partition number (1-128, default 1):
First sector (34-123404254, default = 2048) or {+-}size{KMGTP}:
Last sector (2048-123404254, default = 123404254) or {+-}size{KMGTP}: +30G
Current type is 8300 (Linux filesystem)
Hex code or GUID (L to show codes, Enter = 8300): 0700
Changed type of partition to 'Microsoft basic data'

Command (? for help): n
Partition number (2-128, default 2):
First sector (34-123404254, default = 62916608) or {+-}size{KMGTP}:
Last sector (62916608-123404254, default = 123404254) or {+-}size{KMGTP}:
Current type is 8300 (Linux filesystem)
Hex code or GUID (L to show codes, Enter = 8300): 0700
Changed type of partition to 'Microsoft basic data'

Command (? for help): x

Expert command (? for help): a
Partition number (1-2): 2
Known attributes are:
0: system partition
1: hide from EFI
2: legacy BIOS bootable
60: read-only
62: hidden
63: do not automount

Attribute value is 0000000000000000. Set fields are:
  No fields set

Toggle which attribute field (0-63, 64 or <Enter> to exit): 62
Have enabled the 'hidden' attribute.
Attribute value is 4000000000000000. Set fields are:
62 (hidden)

Toggle which attribute field (0-63, 64 or <Enter> to exit):

Expert command (? for help): w

Final checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING
PARTITIONS!!

Do you want to proceed? (Y/N): y
OK; writing new GUID partition table (GPT) to /dev/sdb.
Warning: The kernel is still using the old partition table.
The new table will be used at the next reboot or after you
run partprobe(8) or kpartx(8)
The operation has completed successfully.