Hi,
I want to create a virtual partition inside a hard disk. This partition should be usable for booting purpose. Please help me out in this regard by providing me source code in C/c++.
Bye...
Hi,
I want to create a virtual partition inside a hard disk. This partition should be usable for booting purpose. Please help me out in this regard by providing me source code in C/c++.
Bye...
Short answer: you cannot make a truly “runtime-only” virtual partition that the firmware will boot from at power‑on. BIOS/UEFI load boot code from the MBR or the EFI System Partition before any OS-level software runs, so a partition that only exists while a userland program is running cannot be used as the initial boot target. Practical paths are: make a small real boot partition with a bootloader that then mounts/opens a virtual image, or use platform features that support file-backed booting (Windows native VHD/VHDX boot, GRUB loopback for ISOs/images). (en.wikipedia.org)
If you want to change the partition table programmatically, use the OS APIs rather than hand-editing sectors: on Windows use DeviceIoControl with the disk IOCTLs (for example IOCTL_DISK_CREATE_DISK / IOCTL_DISK_SET_DRIVE_LAYOUT_EX), and on Linux use libparted (or call out to parted) so the kernel and userspace stay consistent. Marking a partition “active” is just toggling the MBR boot flag; that does not create a transient in-memory partition that firmware will see. was correct to question the boot-time visibility. (learn.microsoft.com)
Real, practical examples you can study or prototype from C/C++:
Windows: deploy a VHD and add a boot entry with BCDEdit so the Windows Boot Manager can boot a VHD natively (this is how “boot from VHD” works). Example BCDEdit pattern used in docs and MS blogs:
bcdedit /copy {default} /d "VHD boot"
bcdedit /set {GUID} device vhd=[locate]\Windows.vhd
bcdedit /set {GUID} osdevice vhd=[locate]\Windows.vhd See Microsoft guidance for details and limits. (devblogs.microsoft.com)
Linux: present a file as a block device and expose its partitions with losetup + kpartx, or let GRUB loopback an ISO/disk image and load a kernel/initrd that mounts the image as root. Typical commands:
losetup --find --show disk.img
kpartx -av /dev/loopX
# in grub.cfg: loopback loop /path/to/image.iso ; linux (loop)/vmlinuz ... ; initrd (loop)/initrd.img Study the losetup and kpartx man pages and the GRUB loopback docs. Always test in a VM and back up the MBR/partition table (for example with dd or imaging tools) before writing code that touches real disks. (unix.stackexchange.com)
If the goal is source code to learn from, look at production projects instead of closed commercial tools: examine the libparted sources, GRUB source (loopback code), and open utilities like Rufus or GParted to see how they call kernel/ioctl APIs safely. Build and test in VMs or on spare hardware; modifying MBR/partition tables on your main disk will leave the machine unbootable if done incorrectly. , , and all hit pieces of this same reality in the thread — practical solutions exist, but they require either a supported pre-boot mechanism (VHD/GRUB) or a small real boot partition plus a loader that can attach the virtual image. (gnu.org)
Jump to Post— Ancient Dragon 5,243I doubt you will get free source code. But you can buy .
Jump to Post— Salem 6,009> This partition should be usable for booting purpose.
If it's virtual, then it will only exist while part of your s/w is running.At bootup, NONE of your code will be running, so how could you ever boot from it ?
Jump to Post— Salem 6,009So there's nothing virtual about it, it's just hidden ?
I doubt you will get free source code. But you can buy .
But i need a source code for this any how where i can create a virtual partition.
> This partition should be usable for booting purpose.
If it's virtual, then it will only exist while part of your s/w is running.
At bootup, NONE of your code will be running, so how could you ever boot from it ?
By modifying MBR stuff, we can do it. We have to make this virtual partition as the active partition and reboot it...
So there's nothing virtual about it, it's just hidden ?
Er, yeah. It's one of those naming things.
A virtual partition is so called because it doesn't necessarily correspond to physical structures. It's not hidden though. [EDIT] but I think it can be... [/EDIT]
If anybody know how to do virtual partition programmatically, let me know.
Before this, i even want to know how to do partition programatically.
So goto http://sourceforge.net and type "partition" into the search box.
Plenty of open source examples to get your head around.
You'll need a machine you won't mind trashing several times, because in your stumbling around in the dark, you will completely trash your HD and make it unbootable and unrecoverable.
If you are talking about a virtual partition that is created like ghost does to restart your computer in DOS mode, then I would like to see the source code on how that is done as well.
Let me know if you find out how its done.
c program language code to display partition of hard disk
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.