QEMU挂载磁盘

1、 建一個5G的raw格式的虛擬磁盤,大小根據自己的需要設定。

     $ qemu-img create test.img 5G

2、接着运行qemu并加载磁盘:

$ qemu-system-x86_64 -kernel /usr/src/linux-4.6.2-x86_64/arch/x86/boot/bzImage -initrd ~/Downloads/initramfs.img ~/Downloads/disk.img -append "console=ttyS0" -nographic

此时会发现内核已经成功启动,可以直接进入terminal;

3.、在該磁盤上創建分區

首先运行如下命令,查看加载得磁盘的编号

$ blkid

如图:
QEMU挂载磁盘_第1张图片
在我的电脑里,磁盘是/dev/sda;这个磁盘并没有分区,也没有格式化,还不能够使用;

下面对它进行分区和初始化

剛創建的鏡像文件還是一個裸盤,就像你剛賣回來的新硬盤一樣,還需要進行分區和格式化。

a)分区

   查看磁盤類型


    ```
    $ fdisk -l  
    Disk /dev/sda: 5368 MB, 5368709120 bytes
    255 heads, 63 sectors/track, 652 cylinders
    Units = cylinders of 16065 * 512 = 8225280 bytes

      Device Boot      Start         End      Blocks  Id System
    /dev/sda1               1         652     5237158+ 83 Linux 
    Disk identifier: 0x00000000  
    Disk test.img doesn't contain a valid partition table 
    ```

b) 使用fdisk来创建分区,下面的步驟只創建了一個主分區:

$ sudo fdisk /dev/sda  
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel  
Building a new DOS disklabel with disk identifier 0xcd6d4a4c.  
Changes will remain in memory only, until you decide to write them.  
After that, of course, the previous content won't be recoverable.  
Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)  
WARNING: DOS-compatible mode is deprecated. It's strongly recommended to  
         switch off the mode (command 'c') and change display units to  
         sectors (command 'u').  
Command (m for help): n  
Command action  
   e   extended  
   p   primary partition (1-4)  
p  
Partition number (1-4): 1  
First cylinder (1-8, default 1):   
Using default value 1  
Last cylinder, +cylinders or +size{K,M,G} (1-8, default 8):   
Using default value 8  
Command (m for help): p  
Disk /dev/sda: 5368 MB, 5368709120 bytes  
255 heads, 63 sectors/track, 652 cylinders 
Units = cylinders of 16065 * 512 = 8225280 bytes  
Sector size (logical/physical): 512 bytes / 512 bytes  
I/O size (minimum/optimal): 512 bytes / 512 bytes  
Disk identifier: 0xcd6d4a4c  
      Device Boot      Start         End      Blocks  Id System
        /dev/sda1               1         652     5237158+ 83 Linux 

此时,已经完成磁盘的分区,这里我只分了一个区,可以分成多个区。

4、格式化分区,mount分区:

$ sudo mkfs.ext2 /dev/sda  
$ sudo mount /dev/sda /mnt/

5、卸载mount的镜像文件, 磁盘的创建过程就完成了

$ sudo umount /mnt/  
$ sudo losetup -d /dev/sda

参考自:
qemu虚拟磁盘的管理

你可能感兴趣的:(linux内核,内存分配研究)