qemu安装gentoo

nat参考官方
http://wiki.qemu.org/Documentation/Networking/NAT#Overview



vnc如果闪退
VNC是个不错的工具,今天掏出VNC Viewer来连接一台N久没登陆的服务器看看,输入密码连上后直接闪退,可能是ColourLevel的问题。

依次点Option-->Advanced-->Expert找到ColourLevel,默认值是pal8,修改为rgb222或full。




centos7带qemu
centos6.5没有
需要
yum install autoconf automake libtool glib2-devel bison bison-devel flex flex-devel
yum install bridge-utils iptables dnsmasq
yum install vnc tigervnc-server
gentoo的下载地址
wget http://mirrors.163.com/gentoo/releases/amd64/20140826/livedvd-amd64-multilib-20140826.iso


gentoo的安装能学到很多东西,
原理是,先启动iso启动盘,
把stage3 作为文件系统,port包解压到/usr
然后chroot,重新配置网络等,编译内核,重启用非iso模式启动

qemu可以使用-kernel和-initrd及-append方式,需要把kerneldisk和romdisk考出来
如果正确安装了grup,也可以不指定这两个参数直接指定img文件
具体如下:
vmware参考

http://blog.csdn.net/mikayong/article/details/6446244

qemu安装如下:
创建磁盘

  
qemu-img create -f qcow2 gentoo.img 6G 
启动qemu ,以cdrom的方式启动,用tap的模式启动网络

view plaincopy to clipboardprint?
  
[root@mytest bin]# cat /etc/qemu-ifup 
#!/bin/sh 
/sbin/ifconfig $1 192.168.0.11 
 
[root@mytest bin]# ifconfig  
tap0      Link encap:Ethernet  HWaddr CA:D1:45:E0:92:7F   
          inet addr:192.168.0.11  Bcast:192.168.0.255  Mask:255.255.255.0  
          inet6 addr: fe80::c8d1:45ff:fee0:927f/64 Scope:Link  
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1  
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0  
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0  
          collisions:0 txqueuelen:500  
          RX bytes:0 (0.0 b)  TX bytes:0 (0.0 b)  

这样主机这样设置后才可以上网

  
echo 1 > /proc/sys/net/ipv4/ip_forward  
/etc/sysconfig/network脚本中添加 FORWARD_IPV4="YES"   
iptables -A FORWARD -s 192.168.0.110/24 -j ACCEPT     
iptables -A FORWARD -m state --state ESTABLISHED,RELATED -j ACCEPT       
iptables -t nat -A POSTROUTING -s 192.168.0.110/24 -j SNAT --to 10.217.12.244  
cat /etc/resolv.conf  

启动qemu

chmod 744 /etc/qemu-ifup
yum install bridge-utils
qemu-system-i386 -hda gentoo.img -boot order=d -cdrom install-x86-minimal-20140415.iso -m 512 -net nic,model=virtio,macaddr=00:16:3e:22:22:22 -net tap -vnc 10.217.12.200:1 &  
vncviewer

10.217.12.200:1

快速进入后在boot的时候回车哦,要不启动不起来
进入虚拟机设置ip
ifconfig 不管是

view plaincopy to clipboardprint?
   
ifconfig eth0 192.168.0.110 netmask 255.255.255.0    
route add default gw 192.168.0.11   
cat /etc/resolv.conf
nameserver 8.8.8.8

ping www.163.com  

 
 
设置密码,启动ssh 
passwd root 
/etc/init.d/sshd start 
 
现在可以通过ssh登录了 
ssh [email protected] 
进入虚拟机后,磁盘操作

view plaincopy to clipboardprint?
  
livecd ~ # fdisk -l  
Disk /dev/sda: 6442 MB, 6442450944 bytes, 12582912 sectors  
Units = sectors of 1 * 512 = 512 bytes  
Sector size (logical/physical): 512 bytes / 512 bytes  
I/O size (minimum/optimal): 512 bytes / 512 by  
  
fdisk /dev/sda  
Command (m for help): n  
Partition type:  
   p   primary (0 primary, 0 extended, 4 free)  
   e   extended  
Select (default p): p  
Partition number (1-4, default 1): 1  
First sector (2048-12582911, default 2048):  
Using default value 2048  
Last sector, +sectors or +size{K,M,G} (2048-12582911, default 12582911):  
Using default value 12582911  
Partition 1 of type Linux and of size 6 GiB is set  
  
Command (m for help): w  
The partition table has been altered!  
  
Calling ioctl() to re-read partition table.  
Syncing disks.  

再fdisk -l 出现了 /dev/sda1

  
mke2fs -j /dev/sda1  
mount /dev/sda1 /mnt/gentoo  
df -lhT  
cd /mnt/gentoo

切换回主机:

scp stage3-i686-20140415.tar.bz2 portage-20140509.tar.bz2 [email protected]:/mnt/gentoo

从官网下载的两个文件系统和包scp到gentoo系统
切换回gentoo:

view plaincopy to clipboardprint?
  
tar xvf stage3-i686-20140415.tar.bz2  
tar xvf  portage-20140509.tar.bz2 -C /mnt/gentoo/usr  
  
rm stage3-i686-20140415.tar.bz2  portage-20140509.tar.bz2  
  
vi /mnt/gentoo/etc/portage/make.conf  
CFLAGS="-O2 -march=i686 -pipe"                                             
CXXFLAGS="${CFLAGS}"                                                     
CHOST="i686-pc-linux-gnu"                                                
GENTOO_MIRRORS="http://mirrors.163.com/gentoo/"  
SYNC="rsync://mirrors.163.com/gentoo-portage"    
MAKEOPT="-j2"                                    
USE="bindist"                                                                    
PORTDIR="/usr/portage"                                                           
DISTDIR="${PORTDIR}/distfiles"                   
PKGDIR="${PORTDIR}/packages"                                                     
~                             
  
cat /etc/conf.d/net  
config_eth0=( "dhcp" )  
 
 

163的有的时候可能不好使
livecd gentoo # cat /mnt/gentoo/etc/portage/make.conf     
# These settings were set by the catalyst build script that automatically
# built this stage.
# Please consult /usr/share/portage/config/make.conf.example for a more
# detailed example.
CFLAGS="-O2 -march=i686 -pipe"
CXXFLAGS="${CFLAGS}"
# WARNING: Changing your CHOST is not something that should be done lightly.
# Please consult http://www.gentoo.org/doc/en/change-chost.xml before changing.
CHOST="i686-pc-linux-gnu"
#GENTOO_MIRRORS="http://mirrors.163.com/gentoo/"    
#SYNC="rsync://mirrors.163.com/gentoo-portage"      
GENTOO_MIRRORS="ftp://linux.ntcu.net/dists/gentoo/ http://www.zentek-international.com/mirrors/gentoo/ http://gentoo.ccccom.com http://mirror.tucdemonic.org/gentoo/ http://gentoo.math.bme.hu http://gentoo.inode.at/ http://gentoo.mirrors.pair.com/" 
SYNC="rsync://rsync.asia.gentoo.org/gentoo-portage" 
#SYNC="rsync://rsync.gentoo.org/gentoo-portage"
#SYNC="rsync://rsync.gentoo.org/gentoo-portage"
MAKEOPT="-j2"
# These are the USE flags that were used in addition to what is provided by the
# profile used for building.
USE="bindist"
PORTDIR="/usr/portage"
DISTDIR="${PORTDIR}/distfiles"
PKGDIR="${PORTDIR}/packages"
livecd gentoo # 


chroot:
网络的dns 
比如 
vi /mnt/gentoo/etc/resolv.conf
nameserver 8.8.8.8 
挂载进程系统
mount -t proc none /mnt/gentoo/proc
mount --rbind /dev /mnt/gentoo/dev
chroot /mnt/gentoo /bin/bash
env-update
source /etc/profile
export PS1="(chroot)$PS1"
passwd
passwd root
emerge --sync --quiet
eselect news read all
eselect profile list


如果需要修改,默认不需要修改
eselect profile set 2
emerge vim
vim /etc/locale.gen
把en_US.UTF-8打开
locale-gen
设置时区
cp /mnt/gentoo/usr/share/zoneinfo/Asia/Shanghai /mnt/gentoo/etc/localtime

编译内核

  
emerge gentoo-sources 
emerge genkernel 
genkernel all 
编译grub

  
emerge grub 
grub2-install --no-floppy /dev/sda 
grub2-mkconfig -o /boot/grub/grub.cfg 
网络
重启用非iso模式进入后

  
modprobe pcnet32  
touch  /etc/udev/rules.d/80-net-name-slot.rules  
ln -s /etc/init.d/net.lo /etc/init.d/net.eth0  
/etc/init.d/net.eth0 restart  
  
/etc/init.d/sshd start  

/etc/resolv.conf  如果用dhcp就不用配置 
网络配置参考

http://blog.csdn.net/leeagle/article/details/6875825

http://hi.baidu.com/ygmpkk2/item/8cbaf9112c7d490db88a1a59

http://tieba.baidu.com/p/2477683069

网络相关的补充

genkernel all
之后在/boot下面多了三个文件,两个文件最重要
kernel-genkernel-x86-3.12.13-gentoo

initramfs-genkernel-x86-3.12.13-gentoo
可以把这两个文件考出来,用qemu -kernel -initrd的方式启动
例如

kenerl方式和硬盘方式

view plaincopy to clipboardprint?
  
qemu-system-i386 -kernel kernel-genkernel-x86-3.12.13-gentoo -initrd initramfs-genkernel-x86-3.12.13-gentoo -hda gentoo.img -boot order=c -m 512 -append "root=/dev/sda1 panic=1 net.ifnames=0" -net nic,model=virtio,macaddr=00:16:3e:22:22:22 -net tap -vnc 106.187.44.20:2 &  

-append是kernel的选项,配合-kernel使用 ,具体参考grub的配置思考
root=/dev/sda1是系统的目录,
比较livecd和硬盘版,
livecd的boot是启动分区,前面有*,硬盘版没这么配置,所以需要加这个参数,否则不好使
看http://wenku.baidu.com/link?url=lWJ-4Oan9LhNLI3YFw14aA42CQG2HrDdRMkU6v57xrURk3lYjKhyDfle92zhcX6lahhTc6omhY2ojIOMDg6-MT4YA16k6yxWlZRsJ19ukIC
这个受到的启发

或者设置完grub之后

  
qemu-system-i386 -hda gentoo.img -boot order=c -m 512 -net nic,model=pcnet,macaddr=00:16:3e:22:22:22 -net tap -vnc 106.187.44.20:2 &  
注意-net nic,modle= 后面有几种方式有的gentoo的硬盘版可能没装全,这里是virtio和pcnet


网络排查网卡驱动问题:

在livecd下启动
网络连接,
查看网卡驱动

lspci
看到
用的网卡virtio
lsmod|grep virtio
有三个virtio_net virtio_pci virtio_ring等
但是在gentoo安装完的就没有,
eselect news read all
后说config_net没有配置神马的

在服务器上

  
qemu-system-i386 -net nic,model=? 
查看支持的网卡类型
看到pcnet

启动的时候改成

  
qemu-system-i386 -hda gentoo.img -boot order=c -m 512 -net nic,model=pcnet,macaddr=00:16:3e:22:22:22 -net tap -vnc 106.187.44.20:2 &
 
所以进入硬盘版本 网络用pcnet32 模块
进入server

  
lsmod|grep pcnet 
如果没有就 
modeprobe pcnet32 
 
ln -s /etc/init.d/net.lo /etc/init.d/net.enp0s3 
./net.enp0s3 start 
ifconfig等参考上面 
有时候连接成net.eth0不好使
即使touch /lib/udev/...80什么什么的也不好使,就用这个吧

设置开机启动后就设置好ip和ssh
在这里添加脚本

ip_ssh.sh

view plaincopy to clipboardprint?
cat /etc/local.d<br>  
#!/bin/sh<br>  
/bin/ifconfig 192.168.0.110 netmask 255.255.255.0<br>  
/bin/route add default gw 192.168.0.11<br>  
/usr/sbin/sshd start<p></p>  
<pre name="code" class="c++"><p>附录:<br>  
关于linux的引导,参考</p>  
<p>http://www.ibm.com/developerworks/cn/linux/l-linuxboot/</p>  
<p>http://www.geekfan.net/8663/</p>  
<p><img src="http://www.tuidaoba.com/wp-content/uploads/2014/05/boot.gif" alt="boot"></p>  
          
        </pre>  

你可能感兴趣的:(虚拟机,linux,qemu)