本次制作操作系统 所有的程序编译都运行在 ubuntu 14中,虚拟机也是安装在ubuntu中的,我们把ubuntu作为我们的编译运行环境,程序的编写我们在windows中。
前面在第一节中介绍了怎么搭建samba环境,现在我们介绍写怎么搭建虚拟机环境。
在ubuntu中使用命令 apt-get install kvm qemu libvirt-bin virtinst virt-manager virt-viewer安装好虚拟机运行环境。
使用下面的脚本配置虚拟机:
<domain type='kvm'>
<name>30OS</name>
<memory unit='KiB'>838</memory>
<currentMemory unit='KiB'>838</currentMemory>
<vcpu placement='static'>1</vcpu>
<os>
<type arch='x86_64' machine='pc-i440fx-trusty'>hvm</type>
<boot dev='fd'/>
</os>
<features>
<acpi/>
</features>
<clock offset='utc'/>
<on_poweroff>destroy</on_poweroff>
<on_reboot>restart</on_reboot>
<on_crash>destroy</on_crash>
<devices>
<emulator>/usr/bin/kvm</emulator>
<disk type='file' device='floppy'>
<driver name='qemu' type='raw'/>
<source file='/home/xj/30OPS/Source/system.img'/>
<target dev='fda' bus='fdc'/>
<address type='drive' domain='0x0000' bus='0' unit='0'/>
</disk>
<controller type='ide' index='0'>
<address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x1'/>
</controller>
<controller type='usb' index='0'>
<address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x2'/>
</controller>
<controller type='pci' index='0' model='pci-root'/>
<input type='mouse' bus='ps2'/>
<input type='keyboard' bus='ps2'/>
<graphics type='vnc' port='-1' autoport='yes' listen='0.0.0.0'>
<listen type='address' address='0.0.0.0'/>
</graphics>
<video>
<model type='cirrus' vram='9216' heads='1'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x02' function='0x0'/>
</video>
<memballoon model='virtio'>
<address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x0'/>
</memballoon>
</devices>
</domain>
上面配置最重要的是要设置为从软驱启动系统!
虚拟机命令介绍:
virsh define HelloOS.xml 用来生成虚拟机
virsh unfefine HelloOs 删除虚拟机
virsh start HelloOS 启动虚拟机
virsh destroy HelloOS 关闭虚拟机
我们使用上面的配置文件生成虚拟机:
virsh define HelloOS.xml
接着启动虚拟机:
virsh start HelloOS
因为我们在终端上面操作,看不到虚拟机启动界面,使用VNC工具
连接 10.46.75.99:3 表示这是系统中的第四个虚拟机,可以看到虚拟机显示的画面。
可以看到我们跑在操作系统的第一个程序已经成功!
有的朋友习惯使用vmare,这里也提供vmare验证的方式:
需要把我们生成的img文件设置为上电启动。
启动后的效果如图所示:表明我们的程序有效了!
为了简化我们每次的操作,我把命令嵌入SCRT的快捷按钮中:
nasm A.asm -o A.boot && gcc ReadWrite.c -o ReadWrite &&./ReadWrite\r 一键编译并生成img文件
virsh destroy 30OS && virsh start 30OS\r 一键重启系统(装载我们新生成的img文件)(使用kvm建立虚拟机时适用)
下一步我们将突破512字节的限制,毕竟操作系统需要实现复杂的功能需要的空间远大于512字节!