{"id":107,"date":"2018-02-05T15:15:05","date_gmt":"2018-02-05T23:15:05","guid":{"rendered":"https:\/\/sysprogs.com\/tutorials\/?p=107"},"modified":"2018-02-05T15:25:46","modified_gmt":"2018-02-05T23:25:46","slug":"creating-a-basic-linux-ram-disk-with-visualkernel","status":"publish","type":"post","link":"https:\/\/sysprogs.com\/VisualKernel\/tutorials\/samples\/ramdisk\/","title":{"rendered":"Creating a basic Linux RAM disk with VisualKernel"},"content":{"rendered":"<p>This tutorial shows how to create a basic RAM disk kernel module for Linux 3.x using VisualKernel. Before you begin please follow the\u00a0<a href=\"http:\/\/sysprogs.com\/VisualKernel\/tutorials\/kgdb\/\">basic KGDB tutorial\u00a0<\/a>or one of the other\u00a0<a href=\"http:\/\/sysprogs.com\/VisualKernel\/tutorials\/\">basic tutorials<\/a>\u00a0to ensure that you can create and debug a basic kernel module.<\/p>\n<ol>\n<li>Start Visual Studio. Open the New Project window and select VisualKernel-&gt;Linux Kernel Module Wizard:<img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/sysprogs.com\/VisualKernel\/legacy_tutorials\/samples\/ramdisk\/img\/01-kernelmodule.png\" width=\"700\" height=\"394\" \/><\/li>\n<li>On the first wizard page select &#8220;Create a new kernel module -&gt; Ramdisk&#8221;:<img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/sysprogs.com\/VisualKernel\/legacy_tutorials\/samples\/ramdisk\/img\/02-ramdisk.png\" width=\"696\" height=\"628\" \/><\/li>\n<li>On the next wizard page select the computer you are targeting:<img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/sysprogs.com\/VisualKernel\/legacy_tutorials\/samples\/ramdisk\/img\/03-targetcomp.png\" width=\"696\" height=\"628\" \/><\/li>\n<li>On the third page you can change the directory where the sources will be stored on just press &#8220;Next&#8221; to accept the default directory:<img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/sysprogs.com\/VisualKernel\/legacy_tutorials\/samples\/ramdisk\/img\/04-sourcedir.png\" width=\"696\" height=\"628\" \/><\/li>\n<li>On the last page select a debugging method that corresponds to your setup (e.g.\u00a0<a href=\"http:\/\/sysprogs.com\/VisualKernel\/legacy_tutorials\/kgdb\/\">KGDB<\/a>\u00a0or\u00a0<a href=\"http:\/\/sysprogs.com\/VisualKernel\/legacy_tutorials\/vmware\/\">VMWare<\/a>):<img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/sysprogs.com\/VisualKernel\/legacy_tutorials\/samples\/ramdisk\/img\/05-kgdb.png\" width=\"696\" height=\"628\" \/><\/li>\n<li>Press &#8220;Finish&#8221; to have your project generated. Build it by selecting Build-&gt;Build Solution:<img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/sysprogs.com\/VisualKernel\/legacy_tutorials\/samples\/ramdisk\/img\/06-buildprj.png\" width=\"700\" height=\"626\" \/><\/li>\n<li>Press F5 to start debugging your kernel module. As loading kernel modules requires root privileges, VisualKernel will ask for the password for the current user and attempt using the sudo command:<img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/sysprogs.com\/VisualKernel\/legacy_tutorials\/samples\/ramdisk\/img\/07-sudo.png\" width=\"510\" height=\"190\" \/><\/li>\n<li>In order to test the module we will need to mount the disk by running some commands over SSH. Select\u00a0<strong>Debug-&gt;Windows-&gt;SSH Console<\/strong>:<img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/sysprogs.com\/VisualKernel\/legacy_tutorials\/samples\/ramdisk\/img\/08-running.png\" width=\"700\" height=\"600\" \/><\/li>\n<li>Run the following commands in the SSH console window:\n<div class=\"code\">sudo mkfs \/dev\/&lt;Your module name&gt;Disk1<br \/>\nsudo mkdir \/mnt\/ramdisk<br \/>\nsudo mount \/dev\/&lt;Your module name&gt;Disk1 \/mnt\/ramdisk<br \/>\ndf -h \/mnt\/ramdisk\/<\/div>\n<p>The\u00a0<strong>df\u00a0<\/strong>tool will report that\u00a0<strong>\/mnt\/ramdisk<\/strong>\u00a0is mounted to the ramdisk device and has 16 MB free:<img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/sysprogs.com\/VisualKernel\/legacy_tutorials\/samples\/ramdisk\/img\/09-freespace.png\" width=\"700\" height=\"600\" \/><\/li>\n<li>Now we will set a breakpoint in the function that processes requests coming from the OS to see how Linux handles block requests. Open the\u00a0<strong>BasicRamDisk.c<\/strong>\u00a0file and set a breakpoint in\u00a0<strong>BasicRamDisk_ProcessQueuedRequest()<\/strong>. Then run the following command in the SSH session window:\n<div class=\"code\">cp \/bin\/sh \/mnt\/ramdisk<\/div>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/sysprogs.com\/VisualKernel\/legacy_tutorials\/samples\/ramdisk\/img\/10-breakpoint.png\" width=\"700\" height=\"600\" \/><\/li>\n<li>Once the breakpoint is hit you can use the Call Stack window to navigate down the stack and see that\u00a0<strong>BasicRamDisk_ProcessQueuedRequest()<\/strong>\u00a0was called while handling the\u00a0<strong>stat\u00a0<\/strong>syscall for\u00a0<strong>\/mnt\/ramdisk\/sh<\/strong>\u00a0file:<img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/sysprogs.com\/VisualKernel\/legacy_tutorials\/samples\/ramdisk\/img\/11-stat.png\" width=\"700\" height=\"600\" \/><\/li>\n<li>If you examine the call stack more, you will notice frames from the ext2 module that are shown in gray because the symbols for it are not loaded. Right-click on a frame below\u00a0<strong>mpage_readpage()<\/strong>\u00a0and select &#8220;Load Symbols from Symbol Path&#8221;:<img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/sysprogs.com\/VisualKernel\/legacy_tutorials\/samples\/ramdisk\/img\/12-symbols.png\" width=\"700\" height=\"600\" \/><\/li>\n<li>VisualKernel will automatically use the symbols it cached when creating your project so you will be able to see the source code of the ext2fs driver and view the values of various variables:<img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/sysprogs.com\/VisualKernel\/legacy_tutorials\/samples\/ramdisk\/img\/13-page.png\" width=\"700\" height=\"600\" \/><\/li>\n<li>Remove the breakpoint and press F5 to continue debugging. If you now go to the SSH Session window, it will probably be timed-out because the entire Linux system was stopped in the debugger for too long. Click on the console icon to open another console window and run &#8220;<strong>sudo umount \/mnt\/ramdisk<\/strong>&#8221; there to unmount your disk:<img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/sysprogs.com\/VisualKernel\/legacy_tutorials\/samples\/ramdisk\/img\/14-umount.png\" width=\"611\" height=\"316\" \/><\/li>\n<li>Now open the GDB Session window and unload the driver. Then press Shift-F5 to stop debugging:<img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/sysprogs.com\/VisualKernel\/legacy_tutorials\/samples\/ramdisk\/img\/15-unload.png\" width=\"700\" height=\"600\" \/><\/li>\n<li>We will now configure VisualKernel to load symbols for ext2.ko module each time you debug this project. Right-click on the project in Solution Explorer and select &#8220;VisualKernel Project Properties&#8221;. Then go to the Debug Settings page, select &#8220;Automatically load symbols for selected modules&#8221; and type &#8220;ext2&#8221; in the module list below:<img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/sysprogs.com\/VisualKernel\/legacy_tutorials\/samples\/ramdisk\/img\/16-autoload.png\" width=\"700\" height=\"652\" \/><\/li>\n<li>If you now start debugging your project and open the Modules window, you will notice that ext2.ko symbols are loaded from the very beginning:<img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/sysprogs.com\/VisualKernel\/legacy_tutorials\/samples\/ramdisk\/img\/17-stack.png\" width=\"700\" height=\"600\" \/><\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>This tutorial shows how to create a basic RAM disk kernel module for Linux 3.x using VisualKernel. Before you begin<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[30],"tags":[32,31],"_links":{"self":[{"href":"https:\/\/sysprogs.com\/tutorials\/wp-json\/wp\/v2\/posts\/107"}],"collection":[{"href":"https:\/\/sysprogs.com\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sysprogs.com\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sysprogs.com\/tutorials\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sysprogs.com\/tutorials\/wp-json\/wp\/v2\/comments?post=107"}],"version-history":[{"count":2,"href":"https:\/\/sysprogs.com\/tutorials\/wp-json\/wp\/v2\/posts\/107\/revisions"}],"predecessor-version":[{"id":110,"href":"https:\/\/sysprogs.com\/tutorials\/wp-json\/wp\/v2\/posts\/107\/revisions\/110"}],"wp:attachment":[{"href":"https:\/\/sysprogs.com\/tutorials\/wp-json\/wp\/v2\/media?parent=107"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sysprogs.com\/tutorials\/wp-json\/wp\/v2\/categories?post=107"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sysprogs.com\/tutorials\/wp-json\/wp\/v2\/tags?post=107"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}