Read disk sectors linux

How can I read a known sector of a disk from Linux kernel module

In linux kernel if we have a pointer to «struct gendisk» of a disk, then how can we read a known sector number from the disk.

1 Answer 1

These functions helped me. I used submit_bio and submitted a bio to the disk. The major and minor shall be assumed as input to read_sector_thread_func

static struct bio * my_mpage_alloc(struct block_device *bdev, sector_t first_sector, int nr_vecs, gfp_t gfp_flags) < struct bio *bio; bio = my_bio_alloc(gfp_flags, nr_vecs); if (bio == NULL && (current->flags & PF_MEMALLOC)) < while (!bio && (nr_vecs /= 2)) bio = my_bio_alloc(gfp_flags, nr_vecs); >if (bio) < //bio->bi_bdev = bdev; bio->bi_iter.bi_sector = first_sector; > return bio; > static void src_endio(struct bio *bio) < if (bio->bi_private) < complete(bio->bi_private); > return; > static int read_sector_thread_func(void *data) < struct block_device *r_bdev = NULL; struct bio *r_bio = NULL; struct page *page = NULL; int part = NULL; int len = 0; struct gendisk * disk = NULL; unsigned char * addr; u64 first_sector_num = 0; disk = get_gendisk(MKDEV(global_dev_major, global_dev_minor), &part); DECLARE_COMPLETION_ONSTACK(r_wait); page = alloc_pages(GFP_ATOMIC, 0); printk("%d : %s ", __LINE__, __func__); r_bio = my_mpage_alloc(r_bdev, first_sector_num, 1, GFP_ATOMIC); if(r_bio==NULL) < printk("bio is NULL"); return; >bio_add_page(r_bio, page, 512, 0); r_bio->bi_private = &r_wait; r_bio->bi_end_io = src_endio; r_bio->bi_disk = disk; bio_associate_blkg(r_bio); bio_get(r_bio); bio_set_op_attrs(r_bio, REQ_OP_READ, 0); submit_bio(r_bio); wait_for_completion_io(&r_wait); bio_put(r_bio); return 0; > 

Источник

Reading Hard Disk Sectors in C++ on Linux

The linux drivers for NTFS are robust, but not as efficient as linux drivers for linux file systems or Windows drivers for Windows file systems. The Windows installer overwrites the entire partition that it is installed on, and will overwrite GRUB on MBR systems preventing Linux operating systems from booting.

Reading Hard Disk Sectors in C++ on Linux

The hard disk is just another file (not a «regular file» but a «device file», but still, a file). Open it the normal way.

int fdes = open("/dev/sda1", O_RDONLY); if (fdes < 0) err(1, "/dev/sda1"); . do more . 

You will get permission errors unless you have the right permissions. Note that "/dev/sda1" is just an example, it is the first partition on disk sda , the exact path will depend on your system. You can list mount points with the mount command, and you can access entire disks (instead of just partitions) using /dev/sda , /dev/sdb , etc.

You could also open it as a C++ fstream or C FILE , but I do not recommend this. You will have a better time finding example code and getting help on forums if you use open instead.

As others have correctly pointed out, disk access on Linux (and other Unix-like operating systems) is via a device special file. On my Ubuntu laptop, my hard drive is named "/dev/sda".

Since you specifically ask how to do it in C++ (not merely how to do it in Linux), here is how to read one sector using std::ifstream .

#include #include #include #include #include int main() < // Which disk? char diskName[] = "/dev/sda"; std::string diskError = std::string() + diskName + ": "; // Open device file std::ifstream disk(diskName, std::ios_base::binary); if(!disk) throw(std::runtime_error(diskError + std::strerror(errno))); // Seek to 54321'th sector disk.seekg(512 * 54321); if(!disk) throw(std::runtime_error(diskError + std::strerror(errno))); // Read in one sector std::vectorbuffer(512); disk.read(&buffer[0], 512); if(!disk) throw(std::runtime_error(diskError + std::strerror(errno))); > 

you can dump disk sectors to a file with the dd command and read the file generated

Читайте также:  Gcc linux как работает

How do I view all available HDD's/partitions?, The command-line solution: to check which drives your system can see: sudo fdisk -l. If your drive is in the list, you'll be able to see what partitions are on the drive, like this: Disk /dev/sda: 160.0 GB, 160041885696 bytes

Mount directory from another drive to Desktop/Videos

As ravery answered you can mount another drive as ~/Videos

You can also mount the drive to an unused path in /mnt . Then bind mount directories from the second drive onto directories in ~/

# Custom bind mounts uuid= /mnt/UUID ext4 defaults 0 2 /mnt/UUID/Videos /home/$USER/Videos none bind 0 0 /mnt/UUID/Music /home/$USER/Music none bind 0 0 

Automount is done via the file /ect/fstab. The entry format is:

Device designations (/sdX) maybe used, but since they may change it is best to use UUID.

First copy your videos to the partition you want to use. Verify the copy was successful before deleting from your home directory. Next open gparted, select the drive and partition you want to use. Click on "info" in the partition menu at the top. Record the UUID of the partition you want to use.

Last, you will need root permissions to edit /etc/fstab. Add the following lines to the end of the file.

#Video partition mount uuid= /home//Videos ext4 defaults 0 2 

Replace < UUID>with the UUID recorded earlier, and < user>with your user name. If the partition is on an external drive add nofail to the options so that boot doesn't crash if the drive isn't present. IF the partition isn't formatted ext4 correct that as needed.

Linux C programming: How to get a device's partition, IMHO a more safe way is to: 1) Create a couple of pipes 2) fork a process an redirect the pipes to child's stdin and sdtout (fd 0 and 1) 3) execl "fdisk /dev/sdXXX" 4) send a "p\n" command to the child process 5) read the lines containg the complete partition information. I hope that this can help you. Share. …

Any downside of installing Ubuntu in C drive over full format install?

Starting in 17.04 new installations of Ubuntu use a swap file instead of a swap partition, so everything is installed on a single ext4 partition by default including the /home directory. This makes the most efficient use of valuable disk storage space on SSDs in situations in which Ubuntu is installed on an SSD. In situations where there are multiple hard disks or partitions there are ways listed below of making efficient use of the storage space on the other partition(s) too.

Suppose Ubuntu ran into some problem, would I be able to retain data in /home directory if I want to install Windows/Ubuntu again?

You can reinstall Ubuntu without losing data in /home even without a separate /home partition. See the following Ubuntu documentation links.

  • Ubuntu documentation Home Folder wiki
  • Ubuntu documentation Ubuntu Reinstallation wiki

Will there be any performance difference if I format all drives and make remaining space as /home over keeping previous Windows drives?

NTFS is not suitable for a home partition, because there are certain type of file system objects (character devices, named pipes, etc.) which are required for certain services but are not supported on NTFS. Quoted from: Using a folder on an ntfs partition as /home

You can use custom folders for folders in /home in order to span your home directory across multiple hard disks.

Читайте также:  Fio linux как пользоваться

Example ( xdg-user-dirs-update - Update XDG user dir configuration):

 xdg-user-dirs-update --set DOWNLOAD /media/Redman/2nd-HDD/Downloads/ 

would switch from /home/$USER/Downloads/ to /mediaRedman/2nd-HDD/Downloads/ and downloaded files would then download to the 2nd HDD. The same applies for all the other directories.

Both the local ~/.config/user-dirs.dirs and global /etc/xdg/user-dirs.defaults configuration files use the following environmental variable format to point to user directories: XDG_DIRNAME_DIR="$HOME/directory_name" An example configuration file looks like this (these are all the template directories):

Results of cat ~/.config/user-dirs.dirs :

XDG_DESKTOP_DIR="$HOME/Desktop" XDG_DOCUMENTS_DIR="$HOME/Documents" XDG_DOWNLOAD_DIR="$HOME/Downloads" XDG_MUSIC_DIR="$HOME/Music" XDG_PICTURES_DIR="$HOME/Pictures" XDG_PUBLICSHARE_DIR="$HOME/Public" XDG_TEMPLATES_DIR="$HOME/Templates" XDG_VIDEOS_DIR="$HOME/Videos" 

As xdg-user-dirs will source the local configuration file to point to the appropriate user directories, it is therefore possible to specify custom folders. For example, if a custom folder for the XDG_DOWNLOAD_DIR variable has been named $HOME/Internet in ~/.config/user-dirs.dirs any application that uses this variable will use this directory.

What about in case I want to go back to Windows, can I retain /home data?

The Windows installer overwrites the entire partition that it is installed on, and will overwrite GRUB on MBR systems preventing Linux operating systems from booting. For this reason it is recommended to install Windows before installing Ubuntu in a dual boot.

If you keep partitions with the NTFS file system, you can mount them from linux with read and write access, but there are downsides.

  • The linux drivers for NTFS are robust, but not as efficient as linux drivers for linux file systems or Windows drivers for Windows file systems. So things will be slower than if you create ext4 file systems and copy your data files from the NTFS file systems (maybe via a backup drive).
  • The ownership and permissions are not flexible, when managed by the linux drivers from NTFS (and FAT32 and exFAT). They are set during mounting, to be the same for all files and directories, and cannot be modified unless you remount the file system.
  • Some special features of linux cannot be managed by NTFS. You should have linux file systems for all partitions belonging to the Ubuntu system: root ( / ), and if you have a separate home partition ( /home ) etc.
  • There are no good maintenance tools for NTFS in linux, so you cannot repair it or keep it fresh, except with Windows.

But in a dual boot scenario, it can be a good idea to have a separate data partition with NTFS. It can be accessed from Ubuntu as well as from Windows.

Preliminary remark: You can preserve your data only if they currently are on separate partitions than the one known as the C drive.

You can indeed overwrite your partition, known to Windows as the c-drive, with the Ubuntu GNU/Linux operating system, and leave the other partitions in place. There are, however, issues in maintaining ntfs partitions on a computer where MS Windows is not installed.

Windows has its own file system, ntfs. All your current partitions, therefore, are most likely formatted in the ntfs file system format. Windows also supports vfat, so it is not excluded that some of your partitions are formatted in vfat. However, vfat is an older, less robust and error-resistant file system, and has limitations, amongst others with respect to the maximum file size it can store. Ubuntu, on the other hand, has different file systems. The ext4 system is the most used and is the file system created by a default installation.

Читайте также:  Linux mint бэкап системы

Linux can read and write ntfs partitions flawlessly, unless other operating systems (I am looking at you, Apple). Thus, from this point of view, what you want can be achieved.

There is a major caveat, however. ntfs is a proprietary file system format. The ins and outs of the file system therefore are known only by the company that created the file system. While Ubuntu and GNU/Linux operating systems in general, can work with it, and even have basic tools to check the consistency of the file system, only the Windows file system checking tools (chkdsk at the command line) are fully capable of deeply investigating and repairing the file system. For this reason, any critical ntfs partition you have should be accessible by a Windows system .

You can't easily take out an internal disk to temporarily mount it to a Windows system to check. Thus, it is strongly discouraged to keep ntfs partitions around on internal drives where no Windows system is installed.

The advice, if you move away from Windows to Ubuntu, is therefore to reformat all existing partitions to a linux filesystem, typically ext4. This will erase all data, so you should move the data to an external disk, and make sure your backup is up to date, so data can be restored after the installation of the new operating system.

How to set up multiple hard drives as one volume?, You can see all the drives Ubuntu sees and how they are listed by running: sudo fdisk -l. The first line in each section should give you enough information to identify your drive. It will look like: Disk /dev/sda: 500.1 GB, 500107862016 bytes. The part that matters is /dev/sda. Now run: sudo fdisk /dev/sda.

Источник

Read a single sector from a disk

I am trying to read a single specific sector from the disk directly. I've currently run out of ideas and any suggestions how to go about it would be great!

5 Answers 5

Try something like this to do it from the CLI:

# df -h . Filesystem Size Used Avail Use% Mounted on /dev/sda2 27G 24G 1.6G 94% / # dd bs=512 if=/dev/sda2 of=/tmp/sector200 skip=200 count=1 1+0 records in 1+0 records out 
FILES /dev/sd[a-h]: the whole device /dev/sd[a-h]2: individual block partitions 

And if you want to do this from within a program, just use a combination of system calls from man 2 . like open, lseek, , and read , with the parameters from the dd example.

Given the sizes of disks these days, either use lseek64() , or #define _FILE_OFFSET_BITS 64 to ensure that off_t is a 64 bit type.

could you please give some c programming language specific functions to read and write to disk sectors?

I'm not sure what the best programmatic approach is, but from the Linux command-line you could use the dd command in combination with the raw device for your disk to directly read from the disk.

You need to sudo this command to get access to the raw disk device (e.g. /dev/rdisk0).

For example, the following will read a single 512-byte block from an offset of 900 blocks from the top of disk0 and output it to stdout.

sudo dd if=/dev/rdisk0 bs=512 skip=900 count=1 

See the dd man page to get additional information on the parameters to dd.

Источник

Оцените статью
Adblock
detector