Giter Site home page Giter Site logo

nickdu088 / sharpext4 Goto Github PK

View Code? Open in Web Editor NEW
36.0 3.0 8.0 366 KB

A .Net library to provide full access (read/write) to Linux ext2/ext3/ext4 filesystem

Home Page: https://www.nickdu.com/?p=890

C++ 8.07% C 91.93%
ext2 ext3 ext4 windows dotnet csharp filesystem-library ext4-disk filesystem linux

sharpext4's Introduction

SharpExt4

Donate

About

The main purpose of this SharpExt4 project is to provide full access to Linux ext2/3/4 filesystem from Windows .Net application.

For a day-to-day Windows user, it is not easy to read/write ext2/3/4 filesystem directly from Windows environment. Especially for a C# .Net programmer, it is hard to find a .Net library, which can provide full access to Linux ext2/3/4 filesystem.

These are the findings so far:

  1. DiscUtils, is a .NET library to read and write ISO files and Virtual Machine disk files (VHD, VDI, XVA, VMDK, etc). DiscUtils also provides limited access to ext2/3/4 filesystem.
  2. Ext2Fsd is another Windows file system driver for the Ext2, Ext3, and Ext4 file systems. It allows Windows to read Linux file systems natively, providing access to the file system via a drive letter that any program can access.
  3. DiskInternals Linux Reader is a freeware application from DiskInternals, developers of data recovery software.
  4. Ext2explore is an open-source application that works similarly to DiskInternals Linux Reader—but only for Ext4, Ext3, and Ext2 partitions.
  5. The lwext4 project is to provide ext2/3/4 filesystem for microcontrollers.

Overview

The lwext4 is a portable C project for microcontrollers and the library has some cool and unique features. Lwext4 is an excellent choice for SD/MMC card, USB flash drive or any other wear leveled memory types. In Windows, the author recommended to use MSYS-2

I port the lwext4 backbone over to MSVC compiler (Visual Studio 2019), and create the lwext4 as a static lib. SharpExt4 is a clr wrapper of lwext4 to provide modem .Net application access. The SharpExt4 borrows the DiscUtils class concept and creates a friendly interface for .Net

Compile

Visual Studio 2019 C/C++ (It can be simply modified to be compiled in Visual Studio 2013)

.Net Framework 4.5

.Net Core 3.1 .Net 5

How to use the Library

How to use SharpExt4 to access Raspberry Pi SD Card Linux partition.

Here's a few simple examples.

How to read a file from ext4 disk image

	...
	//Open a Linux ext4 disk image
	var disk = ExtDisk.Open(@".\ext4.img");
	//Get the Linux partition and open
	var fs = ExtFileSystem.Open(disk.Parititions[0]);
	//Open a file for read
	var file = fs.OpenFile("/etc/shells", FileMode.Open, FileAccess.Read);
	//Check the file length
	var filelen = file.Length;
	var buf = new byte[filelen];
	//Read the file content
	var count = file.Read(buf, 0, (int)filelen);
	file.Close();
	var content = Encoding.Default.GetString(buf);
	Console.WriteLine(content);
	...

How to list all files in a folder from ext4 disk image

	...
	//Open a Linux ext4 disk image
	var disk = ExtDisk.Open(@".\ext4.img");
	//Get the Linux partition, and open
	var fs = ExtFileSystem.Open(disk.Parititions[0]);
	//List all files in /etc folder
	foreach(var file in fs.GetFiles("/etc", "*", SearchOption.AllDirectories))
	{
		Console.WriteLine(file);
	}
	...

How to create a file in ext4 disk image

	...
	//Open a Linux ext4 disk image
	var disk = ExtDisk.Open(@".\org.img");
	//Get the file system
	var fs = ExtFileSystem.Open(disk.Parititions[0]);
	//Open a file for write
	var file = fs.OpenFile("/etc/test", FileMode.Create, FileAccess.Write);
	var hello = "Hello World";
	var buf = Encoding.ASCII.GetBytes(hello);
	//Write to file
	var count = file.Read(buf, 0, buf.Length);
	file.Close();
	...

How to change a file mode in ext4 disk

	...
	//Open a Linux ext4 disk assume your SD card/USB is physical disk 1
	var disk = ExtDisk.Open(1);
	//Get the file system
	var fs = ExtFileSystem.Open(disk.Parititions[0]);
	//Check file exists
	if(fs.FileExists("/etc/hosts"))
	{
		//0x1FF in HEX, 777 in OCT
		fs.SetMode("/etc/hosts", 0x1FF);
	}
	...

Credits

The core library of SharpExt4 was taken from lwext4:

sharpext4's People

Contributors

nickdu088 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

sharpext4's Issues

Open partition from a file system image?

Is there an easy way to currently open a partition from an image of the partition itself vs a full disk image? I don't see any easy way to do that with the sample provided/library code.

So far, I kinda 'hacked' in a partition to fake disk by just populating the partitions array and that worked to read/write from the partition image on disk basically tricking things into thinking it was a disk image, but the resulting image is damaged once a file is written and has to be repaired.

Just wondering if this use case has ever been considered or if there's a known easy way to approach it?

This is what I'd done so far to 'hack' my partition in as the 'disk':

SharpExt4::ExtDisk^ SharpExt4::ExtDisk::Open(String^ imagePath)
{
	if (File::Exists(imagePath))
	{
		auto disk = gcnew ExtDisk(imagePath);
		disk->bdevs = new ext4_mbr_bdevs();
		disk->capacity = 786432000;
		disk->geometry = gcnew SharpExt4::Geometry(0, 0, 0, 0);
		disk->partitions = gcnew List<Partition^>();

		Partition^ partition = gcnew Partition();
		partition->Offset = 0;
		partition->Size = 786432000;
		disk->partitions->Add(partition);

		return disk;

Which worked to list, create, and get contents of files; but again upon saving it seems to damage the partition image maybe because it's trying to update data in relation to a disk not sure, what it does is fixable via e2fsck but I'm wanting to avoid having to use that every time I modify the image.

Unhandled Exception while disposing ExtFileStream

Hi,

I get an unhandled exception during disposing the ExtFileStream.
The target framework of my application is .NET 7.

I do simple copy operations of large files (up to 2 GB) from my NTFS-Disk to a Micro-SD (Ext4).

Source


  ExtDisk disk = ExtDisk.Open(diskNumber);
  ExtFileSystem fileSystem = ExtFileSystem.Open(disk, disk.Partitions[0]);

  byte[] buffer = File.ReadAllBytes(sourcePath);

  using ExtFileStream stream = fileSystem.OpenFile(destinationPath, FileMode.Create, FileAccess.Write);
  stream.Write(buffer, 0, buffer.Length);
  stream.Close();

The operation is successful but after the call of Dispose via the using-operator or calling Dispose I get the following exception:
grafik
grafik
grafik

Is this the default behavior?
Any idea for a workaround?

~Best Regards

Could Not Read Disk MBR

Hey Nick, I was trying to access a 1TB Sandisk sdxc card formatted as EXT4 and I am getting the error :

An unhandled exception of type 'System.IO.IOException' occurred in SharpExt4.dll
Could not read disk MBR.

Basic Code:

 var disk = ExtDisk.Open(1);
  Debug.WriteLine(disk.ToString());

I can confirm I am passing in the correct Disk number (verified via Disk Manager. Here's some additional info that may help.

OS: Windows 11 Home (10.0.22000)
Visual Studio Community 2022
.NET 4.8

Could it be because I'm using EXT4 with casefolding?

Exception when attempting to write text to file: "Could not read disk MBR."

Hi there,

Firstly thanks for open sourcing this library! Awesome stuff.

Re the below, if you have a preferred issue format I'm happy to re-submit this with whatever details you need - I've tried to include everything you might need to know.

I'm hitting a "Could not read disk MBR." exception when attempting to write a file, interesting I can read the file I am writing to, just not write to it.

  • I'm using VS 2019, opened using Run as Administrator.
  • The project is C# Windows Forms.
  • I'm inside a Windows 10 VM on VirtualBox with the USB attached using the VirtualBox USB passthrough feature.
  • The USB has a microSD with Raspbian imaged, with the default /boot FAT32 and /root EXT4 partitions

Here's a quick breakdown of what the code is doing:

  1. User selects USB disk from a dropdown (using ManagementObjectSearcher to find the disks and parse the disk indexes)
  2. Load the contents of /etc/wpa_supplicant/wpa_supplicant.conf and parse out ssid and psk strings, using:
            var disk = ExtDisk.Open(this.index); // Physical disk index found using ManagementObjectSearcher
            var fs = ExtFileSystem.Open(disk.Parititions[1]);
            var file = fs.OpenFile("/etc/wpa_supplicant/wpa_supplicant.conf", FileMode.Open, FileAccess.Read);
            var filelen = file.Length;
            var buf = new byte[filelen];
            file.Read(buf, 0, (int)filelen);
            file.Close();
            fs.Dispose();
            var content = Encoding.Default.GetString(buf);
            // Do stuff with the content to pull out ssid and psk

Note: This step is working.

  1. User then updates the ssid/psk in text fields in the form, hits a save button which calls:
            string template = $@"ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev";
            template += "\nupdate_config=1";
            template += "\ncountry=AU";
            template += "\nnetwork={";
            template += "\n  ssid=" + ssid;
            template += "\n  psk=" + password;
            template += "\n}";

            var disk = ExtDisk.Open(this.index); // Physical disk index found using ManagementObjectSearcher - same as in the read step
            var fs = ExtFileSystem.Open(disk.Parititions[1]);
            var file = fs.OpenFile("/etc/wpa_supplicant/wpa_supplicant.conf", FileMode.OpenOrCreate, FileAccess.ReadWrite);
            var buf = Encoding.UTF8.GetBytes(template);
            //Write to file
            file.Write(buf, 0, buf.Length);
            file.Close();
            fs.Dispose();

Step three throws the error when attempting to Open the disk.

Could not read disk MBR error

Hello
I mount a Linux-based virtual machine (vhdx) with the arsenal mounter. I see this added in Disk management I want to access this disk with Sharpext4 and read and copy it I couldn't find how to do it. I used the code below but I get the Could not read disk MBR error.

private void Mountet_Click(object sender, RoutedEventArgs e)
{
//Open a Linux ext4 disk image
var disk = ExtDisk.Open(1);
//Get the Linux partition, and open
var fs = ExtFileSystem.Open(disk, disk.Partitions[0]);
//List all files in /etc folder
foreach (var file in fs.GetFiles("/etc", "*", SearchOption.AllDirectories))
{
Console.WriteLine(file);
}
} }

Cannot mount ext4 partition

Hi,

i am testing your lib with an external ext4 disk on a usb hard disk. Under Windows 10 I plugged in the usb disk. Then I used diskpart to determine the disk number (in my case 8). I changed the Sample code to:

//Open a Linux ext4 disk image
 //var disk = ExtDisk.Open(@".\ext4.img");
var disk = ExtDisk.Open(8);
foreach(var part in disk.Partitions)
{
    Console.WriteLine("part = " + part.ToString());
}
...

Sample.exe returns an exception:

C:\Users\mic\source\repos\SharpExt4\Sample\bin\Debug>Sample.exe
part = SharpExt4.Partition
part = SharpExt4.Partition
part = SharpExt4.Partition
part = SharpExt4.Partition

Unbehandelte Ausnahme: System.IO.IOException: Could not mount partition.
   bei SharpExt4.ExtFileSystem.Open(Partition partition) in C:\Users\mic\Source\Repos\SharpExt4\SharpExt4\ExtFileSystem.cpp:Zeile 219.
   bei Sample.Program.Main(String[] args) in C:\Users\mic\Source\Repos\SharpExt4\Sample\Program.cs:Zeile 48.

Interesting is, that the disk seems to contain four partitions, but the usb disk just has one partition on it.
So I assume, that the physical Disk number may not be the one which diskPart is showing me. I checked with wmic as well. The disk index für the usb disk is 8.

Do you have an idea what goes wrong?

Best
Mic

Add support for MBR disk type

Recently, I use SharpEXT4 to read and write SD-Card OrangePI. But when executing the command SharpExt4.ExtDisk.Open(1); /* 1 is the drive shown in diskpart */ then ExtDisk.cpp gives the error System.IO.IOException: 'Could not read disk MBR.'.

What should I do in this case? Looking forward to receiving a solution or patch for this error soon!

GPT partition

Hello,

I want to mount a ext4 partition GPT, is it possible ?

Regards.

ext4 for bootmgr.efi (Windows 7, 8, 10, 11)

Hello!
I want install my Windows 11 for test into EXT4 base partition or into vDISK.vhdx, which save into EXT4 base partition.
I do this:

  1. HDD 120GB = 2 GB FAT16 + 109,6 GB EXT4 base partition;
  2. Save vDISK.vhdx 100 GB NTFS to 109,6 GB EXT4 base partition;
  3. Save bootmgr.efi into 2 GB FAT16;
    I need 4) mount EXT4 base partition (driver ext4_x64.efi) over BCD and bootmgr.efi;
  4. Mount vDISK.vhdx 100 GB NTFS over BCD and bootmgr.efi;
  5. Install Windows to vDISK.vhdx.

Now I have work OS Windows 11 on vDISK.vhdx into NTFS base partition. This work. I want use EXT4 base partition.

Can you help me?

Could not load file or assembly 'SharpExt4, Version=1.0.8402.18282

Tried to reference this project to a dotnet core based project (a dotnet core console application), Then I am getting an issue as mentioned below. It will be great if you are able to help me to fix the same.

Could not load file or assembly 'SharpExt4, Version=1.0.8402.18282, Culture=neutral, PublicKeyToken=null'. Format of the executable (.exe) or library (.dll) is invalid.

Typo on ExtDisk "Partitions" getter

Hi,

I've been using this library and It's really helpful, thanks for it!

I just noticed there's a typo on the "Partitions" getter in the "ExtDisk" class.
Instead of saying Partitions it says Parititions.

Files:

Open multiple image

I would need to copy some files from one ext4 disk to another.
The problem is that when I try to open the second image if I haven't dispose of the first one first an exception is thrown.

var d1 = ExtDisk.Open(@"C:\Users\Giacomo\Downloads\raspbian_1.img");
var fs1 = ExtFileSystem.Open(d1.Partitions[1]);
//d1.Dispose(); //with this line code working but i cant copy file
 
var d2 = ExtDisk.Open(@"C:\Users\Giacomo\Downloads\raspbian_2.img");
var fs2 = ExtFileSystem.Open(d2.Partitions[1]);

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.