読者です 読者をやめる 読者になる 読者になる

labunix's blog

labunixのラボUnix

vmplayer上のsqueezeにopenvzを導入。

■vmplayer上のsqueezeにopenvzを導入。
 Wheezy上で2.6.32-042stab059.7カーネルはちょっと古すぎ。。。
 ちなみにx86系よりamd64系の方がハードルが上がります。

$ echo "wheezy" > /dev/null;uname -r
3.2.0-4-amd64

$ echo "squeeze" > /dev/null;uname -r
2.6.32-5-amd64

$ grep flags /proc/cpuinfo | sed s/" "/"\n"/g | grep vmx
vmx
vmx

■OpenVZの導入

$ OPT=$(echo `uname -r` | sed s/"-amd64"/"-openvz&"/); \
  sudo apt-get install -y linux-image-$OPT linux-headers-$OPT linux-headers-2.6.32-5-common-openvz

$ sudo apt-get install -y vzctl vzdump vzquota

$ sudo apt-get install -y debootstrap
$ sudo apt-get install -y libvirt-bin uuid-runtime

■grubの確認
 OpenVZカーネルで起動するようになっているので、
 そのまま再起動

$ grep "menuentry" /boot/grub/grub.cfg | nl -w2 -v0 | sed s/"--"/"\n\t&"/g
 0	menuentry 'Debian GNU/Linux, with Linux 2.6.32-5-openvz-amd64' 
	--class debian 
	--class gnu-linux 
	--class gnu 
	--class os {
 1	menuentry 'Debian GNU/Linux, with Linux 2.6.32-5-openvz-amd64 (recovery mode)' 
	--class debian 
	--class gnu-linux 
	--class gnu 
	--class os {
 2	menuentry 'Debian GNU/Linux, with Linux 2.6.32-5-amd64' 
	--class debian 
	--class gnu-linux 
	--class gnu 
	--class os {
 3	menuentry 'Debian GNU/Linux, with Linux 2.6.32-5-amd64 (recovery mode)' 
	--class debian 
	--class gnu-linux 
	--class gnu 
	--class os {

$ sudo shutdown -r now && exit

■OpenVZのサービス起動を確認

$ uname -r
2.6.32-5-openvz-amd64

$ grep -i usage /etc/init.d/vz
	echo "Usage: $0 {start|stop|status|restart|force-reload}"

$ sudo /etc/init.d/vz status
OpenVZ is running...

$ lsmod | grep ^vz
vzethdev                7301  0 
vznetdev               17967  0 
vzrst                 110574  0 
vzcpt                  97250  0 
vzdquota               35190  0 [permanent]
vzmon                  16333  4 vzethdev,vznetdev,vzrst,vzcpt
vzdev                   1824  4 vzethdev,vznetdev,vzdquota,vzmon
vzevent                 1723  1 

■以下の手順を参考に行う。

$ lv -s /usr/share/doc/vzctl/README.Debian.gz | wc -l
230

■verbose mode

$ grep "Usage\|default" /etc/init.d/vzeventd 
	[ -r /etc/default/vzeventd ] && . /etc/default/vzeventd
	echo $"Usage: $0 {start|stop|status|restart|try-restart|force-reload}"

$ test -f /etc/default/vzeventd || echo 'OPTIONS=-v' | \
  sudo tee /etc/default/vzeventd && sudo /etc/init.d/vzeventd restart
OPTIONS=-v
Shutting down vzeventd: Stopped

Checking vzevent kernel module .....done
Starting vzeventd: Started

$ sudo tail -1 /var/log/vzctl.log 
2013-07-19T03:16:15+0900 vzeventd : Started

■リンク

$ sudo ln -s /var/lib/vz /vz

■コンテナNo.1の作成

$ man vzsplit | grep -A 1 '  *\-n '
       -n numve
              Specify the number of containers.

$ /usr/sbin/vzsplit -n 1 | \
  sudo tee /etc/vz/conf/ve-big.conf-sample > /dev/null
WARNING: Recommended minimal size of partition holding /var/lib/vz/private/ is 20Gb!

■ VEID (100)でコンテナの元を作成

$ grep "^deb .* squeeze main" /etc/apt/sources.list | awk '{print $2}'
http://ftp.jp.debian.org/debian/

$ sudo /usr/sbin/debootstrap --arch i386 squeeze /var/lib/vz/private/100 \
  `grep "^deb .* squeeze main" /etc/apt/sources.list | awk '{print $2}'`
I: Retrieving Release
I: Retrieving Packages
I: Validating Packages
...
I: Base system installed successfully.

$ ls /var/lib/vz/private/100/
bin   dev  home  media  opt   root  selinux  sys  usr
boot  etc  lib   mnt    proc  sbin  srv      tmp  var

■ブリッジの設定

$ /sbin/ifconfig venet
venet0    Link encap:不明なネット  ハードウェアアドレス 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00 
          UP BROADCAST POINTOPOINT RUNNING NOARP  MTU:1500  メトリック:1
          RXパケット:0 エラー:0 損失:0 オーバラン:0 フレーム:0
          TXパケット:0 エラー:0 損失:0 オーバラン:0 キャリア:0
      衝突(Collisions):0 TXキュー長:0 
          RXバイト:0 (0.0 B)  TXバイト:0 (0.0 B)

$ sudo /usr/sbin/vzctl set 100 --netif_add eth0,,,,vzbr0 --save
WARNING: /etc/vz/conf/100.conf not found: No such file or directory
Saved parameters for CT 100
$ cat /etc/vz/conf/100.conf | sed s/"[0-9A-F]*\:"/"XX:"/g
NETIF="ifname=eth0,bridge=vzbr0,mac=XX:XX:XX:XX:XX:6F,host_ifname=veth100.0,host_mac=XX:XX:XX:XX:XX:45"


$ sudo /usr/sbin/vzctl set 100 --hostname vzsqueeze --save
Warning: distribution not specified in CT config, using defaults from /etc/vz/dists/default
Saved parameters for CT 100

$ echo "vzsqueeze" | sudo tee /vz/private/100/etc/hostname

$ echo "vzsqueeze.test.local" | sudo tee /vz/private/100/etc/mailname
vzsqueeze.test.local

$ sudo sed -i s%"^[0-9].*/getty"%"# &"%g /vz/private/100/etc/inittab
$ sudo ln -sf /proc/mounts /var/lib/vz/private/100/etc/mtab

$ sudo /usr/sbin/vzctl set 100 --ipadd 192.168.152.10 --save
Warning: distribution not specified in CT config, using defaults from /etc/vz/dists/default
Saved parameters for CT 100

$ sudo /usr/sbin/vzctl set 100 --nameserver 192.168.152.2 --save
Warning: distribution not specified in CT config, using defaults from /etc/vz/dists/default
Saved parameters for CT 100

$ sudo /usr/sbin/vzctl set 100 --applyconfig basic --save
Saved parameters for CT 100
$ echo "OSTEMPLATE=debian" | sudo tee -a /etc/vz/conf/100.confOSTEMPLATE=debian

■プロキシの設定を追加。

$ sudo cp /etc/apt/apt.conf /vz/private/100/etc/apt/
$ test -f /etc/profile.d/proxy.sh && \
  sudo cp /etc/profile.d/proxy.sh /vz/private/100/etc/profile.d/

■これをしないとブリッジの意味が無い。

$ sudo sed -i s/"^#\(net.ipv4.ip_forward=1\)"/"\1"/ /etc/sysctl.conf

■ゲストOSの起動

$ sudo vzctl start 100
Warning: distribution not specified in CT config, using defaults from /etc/vz/dists/default
Starting container ...
Initializing quota ...
Container is mounted
Adding IP address(es): 192.168.152.10
Setting CPU units: 1000
Set hostname: vzsqueeze
File resolv.conf was modified
Configure veth devices: veth100.0 
Container start in progress...

$ sudo virsh list
 Id Name                 State
----------------------------------
100 100                  running

$ sudo vzctl enter 100
entered into CT 100
root@vzsqueeze:/#

■sshサーバの導入

# apt-get update
# apt-get install -y openssh-server

■ユーザの設定

# useradd labunix
# passwd labunix
Enter new UNIX password: 
Retype new UNIX password: 
passwd: password updated successfully
# su labunix
$ ssh-keygen -t rsa
$ exit
# echo "UseDNS no" | tee -a /etc/ssh/sshd_config 
UseDNS no
# /etc/init.d/ssh restart
Restarting OpenBSD Secure Shell server: sshd.
# apt-get install -y vim chkconfig nmap sudo
# usermod -a -G sudo labunix
# exit
$ ssh 192.168.152.10
$ sudo nmap  192.168.152.10 | grep ^[0-9]
22/tcp open  ssh
$ exit

■wheezyテンプレート

$ URL="http://download.openvz.org/template/precreated/contrib"; \
  TEMPLATE="debian-7.0-x86_64.tar.gz"; \
  wget $URL/$TEMPLATE && \
  sudo mv debian-7.0-x86_64.tar.gz /vz/template/cache/

$ sudo vzctl create 101 --ostemplate debian-7.0-x86_64 --config basic
Creating container private area (debian-7.0-x86_64)
Performing postcreate actions
Container private area was created

$ ls /vz/private/101/
bin   dev  home  lib64  mnt  proc  run   selinux  sys  usr
boot  etc  lib   media  opt  root  sbin  srv      tmp  var

■その他のテンプレート

$ w3m -dump "$URL" | grep "tar.gz " | awk '{printf "%6s %-50s\n",$5,$3}'
   24M altlinux-2.4.tar.gz                               
   20M altlinux-3.0-etcnet07.tar.gz                      
   20M altlinux-3.0.tar.gz                               
   22M altlinux-4.0-i586.tar.gz                          
   34M altlinux-5.1-i586.tar.gz                          
   35M altlinux-5.1-x86_64.tar.gz                        
   21M altlinux-20060914-x86_64.tar.gz                   
   20M altlinux-20060914.tar.gz                          
   21M altlinux-20060928-x86_64.tar.gz                   
   30M altlinux-p6-20120321-i586.tar.gz                  
   31M altlinux-p6-20120321-x86_64.tar.gz                
   88M arch-0.8-i686-minimal.tar.gz                      
   93M arch-0.8-x86_64-minimal.tar.gz                    
  209M arch-2012-11-19-x86_64-minimal-caner-version.tar.gz
  220M cctel-6.2.18-x86_64-default.tar.gz                
  124M centos-5-i386-default.tar.gz                      
   46M centos-5-i386-minimal.tar.gz                      
  143M centos-5-x86_64-default.tar.gz                    
  491M centos-5-x86_64-goautodial-ce-2.1-openvz.tar.gz   
   46M centos-5-x86_64-minimal.tar.gz                    
  123M cern-4-i386-default.tar.gz                        
   46M cern-4-i386-minimal.tar.gz                        
  134M cern-4-x86_64-default.tar.gz                      
   51M cern-4-x86_64-minimal.tar.gz                      
   46M debian-4.0-amd64-minimal.tar.gz                   
   47M debian-4.0-i386-minimal.tar.gz                    
  117M debian-4.0-proxmox-mailgateway-2.0.tar.gz         
   59M debian-5.0-amd64-minimal.tar.gz                   
   59M debian-5.0-i386-minimal.tar.gz                    
   74M debian-6.0-amd64-minimal.tar.gz                   
   74M debian-6.0-i386-minimal.tar.gz                    
   74M debian-7.0-amd64-minimal.tar.gz                   
   75M debian-7.0-i386-minimal.tar.gz                    
  116M debian-7.0-x86_64.tar.gz                          
  133M fedora-8-i386-default-EOL.tar.gz                  
  140M fedora-8-x86_64-default-EOL.tar.gz                
  139M fedora-9-i386-default-EOL.tar.gz                  
  152M fedora-9-i386-default.tar.gz                      
   60M fedora-9-i386-minimal.tar.gz                      
  145M fedora-9-x86_64-default-EOL.tar.gz                
  173M fedora-9-x86_64-default.tar.gz                    
   65M fedora-9-x86_64-minimal.tar.gz                    
  150M fedora-10-i386-default-EOL.tar.gz                 
  156M fedora-10-x86_64-default-EOL.tar.gz               
  165M fedora-11-i386-default-EOL.tar.gz                 
  171M fedora-11-x86_64-default-EOL.tar.gz               
  165M fedora-12-i386-default-EOL.tar.gz                 
  171M fedora-12-x86_64-default-EOL.tar.gz               
  109M fedora-core-7-i386-default.tar.gz                 
   61M fedora-core-7-i386-minimal.tar.gz                 
  139M gentoo-amd64-20071018.tar.gz                      
  139M gentoo-openvz-amd64-2008.11.27.tar.gz             
  228M gentoo-openvz-stage3-amd64-20110520.tar.gz        
  124M gentoo-openvz-x86-2008.11.30.tar.gz               
  148M opensuse-10.1-i386.tar.gz                         
  191M opensuse-11.0-i586-20081217.tar.gz                
  110M owl-3.0-release-i686.tar.gz                       
  114M owl-3.0-release-x86_64.tar.gz                     
   26M slackware-10.2-i386-minimal.tar.gz                
   28M slackware-11.0-i386-minimal.tar.gz                
   29M slackware-12.0-i386-minimal.tar.gz                
   39M slackware-13.1-i386-minimal.tar.gz                
   33M slackware-13.37-i486-minimal.tar.gz               
   36M slackware-13.37-x86_64-minimal.tar.gz             
   73M sles-10.2-i586-minimal.tar.gz                     
  155M suse-9.3-x86_64.tar.gz                            
   60M ubuntu-6.06-i386-minimal.tar.gz                   
   45M ubuntu-7.10-i386-minimal.tar.gz                   
   50M ubuntu-8.04.2-i386-minimal.tar.gz                 
   59M ubuntu-9.04-i386-minimal.tar.gz                   
  124M ubuntu-10.04-lamp_10.04_amd64.tar.gz              
  121M ubuntu-10.04-lamp_10.04_i386.tar.gz               
   84M ubuntu-10.04-minimal_10.04_amd64.tar.gz           
   83M ubuntu-10.04-minimal_10.04_i386.tar.gz