山羊博客 Just go for fun http://wp.fungo.me/ Sun, 18 Feb 2018 15:26:58 +0800 Sun, 18 Feb 2018 15:26:58 +0800 Jekyll v3.7.2 搭建本地 ubuntu 12.04 和 ubuntu-cloud 镜像 <p>我们知道,ubuntu 是通过 apt-get 这套包管理软件来安装软件的,比如你要安装 screen, apt-get install -y screen 就可以了,apt-get 就会从网络上的一个软件源里面把 screen 相关的包下载下来,然后安装。apt-get 是从网络端的软件源来安装的,所以在我们装好操作系统后,经常会有这么一步,选择最近的软件源,来替代 /etc/apt/sources.list 配置的默认的软件源,一般我们会用 ustc, 163, souhu 等国内比较快的源。</p> <p>如果是个人电脑的话,这种方式没有问题,可是如果是公司有很多服务器的情况下,这种方式就不合适了。因为每台服务器升级安装软件都要单独从网络下载一份软件包,一方面由于公司网络带宽的限制,速度不会太快,另一方面,重复下载,浪费带宽。所以考虑在公司内部搭建一个 ubuntu 镜像,作为缓存或者代理,定时和网络上的镜像源同步,把公司里的电脑的源配置成本地源,这样就能既提升速度,又节省带宽。</p> <p>下面是本人在给公司部署 OpenStack 时候搭建镜像源的过程:</p> <p>镜像服务器</p> <p>这里我们用到 apt-mirror</p> <p>[code lang=”bash”]</p> <p>sudo apt-get install apt-mirror</p> <p>[/code]</p> <!--more--> <p>配置文件在 /etc/apt/mirror.list, 默认是下面的内容</p> <p>[code lang=”text”]</p> <p>############# config ##################</p> <p>#</p> <p># set base_path /var/spool/apt-mirror</p> <p>#</p> <p># set mirror_path $base_path/mirror</p> <p># set skel_path $base_path/skel</p> <p># set var_path $base_path/var</p> <p># set cleanscript $var_path/clean.sh</p> <p># set defaultarch <running host="" architecture=""></running></p> <p># set postmirror_script $var_path/postmirror.sh</p> <p># set run_postmirror 0</p> <p>set nthreads 20</p> <p>set _tilde 0</p> <p>#</p> <p>############# end config ##############</p> <p>deb http://archive.ubuntu.com/ubuntu precise main restricted universe multiverse</p> <p>deb http://archive.ubuntu.com/ubuntu precise-security main restricted universe multiverse</p> <p>deb http://archive.ubuntu.com/ubuntu precise-updates main restricted universe multiverse</p> <p>#deb http://archive.ubuntu.com/ubuntu precise-proposed main restricted universe multiverse</p> <p>#deb http://archive.ubuntu.com/ubuntu precise-backports main restricted universe multiverse</p> <p>deb-src http://archive.ubuntu.com/ubuntu precise main restricted universe multiverse</p> <p>deb-src http://archive.ubuntu.com/ubuntu precise-security main restricted universe multiverse</p> <p>deb-src http://archive.ubuntu.com/ubuntu precise-updates main restricted universe multiverse</p> <p>#deb-src http://archive.ubuntu.com/ubuntu precise-proposed main restricted universe multiverse</p> <p>#deb-src http://archive.ubuntu.com/ubuntu precise-backports main restricted universe multiverse</p> <p>clean http://archive.ubuntu.com/ubuntu</p> <p>[/code]</p> <p>改成这样</p> <p>[code lang=”text”]</p> <p>############# config ##################</p> <p>#</p> <p># set base_path /var/spool/apt-mirror</p> <p>#</p> <p># set mirror_path $base_path/mirror</p> <p># set skel_path $base_path/skel</p> <p># set var_path $base_path/var</p> <p># set cleanscript $var_path/clean.sh</p> <p># set defaultarch</p> <p># set postmirror_script $var_path/postmirror.sh</p> <p># set run_postmirror 0</p> <p>set nthreads 20</p> <p>set _tilde 0</p> <p>#</p> <p>############# end config ##############</p> <p># Precise 64Bit Mirror</p> <p>deb-amd64 http://cn.archive.ubuntu.com/ubuntu precise main restricted universe multiverse</p> <p>deb-amd64 http://cn.archive.ubuntu.com/ubuntu precise-security main restricted universe multiverse</p> <p>deb-amd64 http://cn.archive.ubuntu.com/ubuntu precise-updates main restricted universe multiverse</p> <p>deb-amd64 http://cn.archive.ubuntu.com/ubuntu precise-proposed main restricted universe multiverse</p> <p>deb-amd64 http://cn.archive.ubuntu.com/ubuntu precise-backports main restricted universe multiverse</p> <p># Precise 32Bit Mirror</p> <p>deb-i386 http://cn.archive.ubuntu.com/ubuntu precise main restricted universe multiverse</p> <p>deb-i386 http://cn.archive.ubuntu.com/ubuntu precise-security main restricted universe multiverse</p> <p>deb-i386 http://cn.archive.ubuntu.com/ubuntu precise-updates main restricted universe multiverse</p> <p>deb-i386 http://cn.archive.ubuntu.com/ubuntu precise-proposed main restricted universe multiverse</p> <p>deb-i386 http://cn.archive.ubuntu.com/ubuntu precise-backports main restricted universe multiverse</p> <p>deb-src http://cn.archive.ubuntu.com/ubuntu precise main restricted universe multiverse</p> <p>deb-src http://cn.archive.ubuntu.com/ubuntu precise-security main restricted universe multiverse</p> <p>deb-src http://cn.archive.ubuntu.com/ubuntu precise-updates main restricted universe multiverse</p> <p>deb-src http://cn.archive.ubuntu.com/ubuntu precise-proposed main restricted universe multiverse</p> <p>deb-src http://cn.archive.ubuntu.com/ubuntu precise-backports main restricted universe multiverse</p> <p>clean http://cn.archive.ubuntu.com/ubuntu</p> <p>deb-amd64 http://ubuntu-cloud.archive.canonical.com/ubuntu precise-updates/grizzly main</p> <p>deb-amd64 http://ubuntu-cloud.archive.canonical.com/ubuntu precise-proposed/grizzly main</p> <p>deb-i386 http://ubuntu-cloud.archive.canonical.com/ubuntu precise-updates/grizzly main</p> <p>deb-i386 http://ubuntu-cloud.archive.canonical.com/ubuntu precise-proposed/grizzly main</p> <p>clean http://ubuntu-cloud.archive.canonical.com/ubuntu</p> <p>[/code]</p> <p>如果不需要源文件的话,可以把 deb-src 给去掉, deb-amd64 是针对 64 位的包,deb-i386 是针对 32 位的包,根据需要可以删减。</p> <p>[code lang=”text”]</p> <p>deb-amd64 http://ubuntu-cloud.archive.canonical.com/ubuntu precise-updates/grizzly main</p> <p>deb-amd64 http://ubuntu-cloud.archive.canonical.com/ubuntu precise-proposed/grizzly main</p> <p>deb-i386 http://ubuntu-cloud.archive.canonical.com/ubuntu precise-updates/grizzly main</p> <p>deb-i386 http://ubuntu-cloud.archive.canonical.com/ubuntu precise-proposed/grizzly main</p> <p>[/code]</p> <p>这一段是针对 OpenStack G 版的, 可以根据需要换成相应的版本,ubuntu 官方现在提供 folsom, grizzly, havana 三个版本。</p> <p>配置完成后,开一个 screen 窗口</p> <p>[code lang=”bash”]</p> <p>sudo apt-mirror /etc/apt/mirror.list</p> <p>[/code]</p> <p>然后就可以去干别的事了(喝咖啡调代码什么的)</p> <p>按我上面的配置同步完成后,一共有 144G 大小。</p> <p>上面的是手动同步更新,可以设置 cron 任务自动同步更新</p> <p>apt-mirror 在安装完成后会自动创建一个 cron 任务 /etc/cron.d/apt-mirror,</p> <p>[code lang=”text”]</p> <p>#</p> <p># Regular cron jobs for the apt-mirror package</p> <p>#</p> <p>0 4 * * * apt-mirror /usr/bin/apt-mirror &gt; /var/spool/apt-mirror/var/cron.log</p> <p>[/code]</p> <p>把最后一行的注释去掉,表示每天早晨 4 点进行自动同步</p> <p>安装 apache</p> <p>[code lang=”bash”]</p> <p>sudo apt-get install apache2</p> <p>sudo ln -s /var/spool/apt-mirror/mirror/cn.archive.ubuntu.com/ubuntu /var/www/ubuntu</p> <p>sudo ln -s /var/spool/apt-mirror/mirror/ubuntu-cloud.archive.canonical.com/ubuntu /var/www/ubuntu-cloud</p> <p>[/code]</p> <p>其它服务器配置</p> <p>其它 ubuntu 服务器就可以把 /etc/apt/sources.list 配置成这台镜像服务器啦!</p> <p>mirrors.hengtiansoft.com 是我们的刚装好的镜像服务器</p> <p>[code lang=”text”]</p> <p>#</p> <p># deb cdrom:[Ubuntu-Server 12.04.1 LTS _Precise Pangolin_ – Release amd64 (20120817.3)]/ dists/precise/main/binary-i386/</p> <p># deb cdrom:[Ubuntu-Server 12.04.1 LTS _Precise Pangolin_ – Release amd64 (20120817.3)]/ dists/precise/restricted/binary-i386/</p> <p># deb cdrom:[Ubuntu-Server 12.04.1 LTS _Precise Pangolin_ – Release amd64 (20120817.3)]/ precise main restricted</p> <p>#deb cdrom:[Ubuntu-Server 12.04.1 LTS _Precise Pangolin_ – Release amd64 (20120817.3)]/ dists/precise/main/binary-i386/</p> <p>#deb cdrom:[Ubuntu-Server 12.04.1 LTS _Precise Pangolin_ – Release amd64 (20120817.3)]/ dists/precise/restricted/binary-i386/</p> <p>#deb cdrom:[Ubuntu-Server 12.04.1 LTS _Precise Pangolin_ – Release amd64 (20120817.3)]/ precise main restricted</p> <p># See http://help.ubuntu.com/community/UpgradeNotes for how to upgrade to</p> <p># newer versions of the distribution.</p> <p>deb http://mirrors.hengtiansoft.com/ubuntu/ precise main restricted</p> <p>deb-src http://mirrors.hengtiansoft.com/ubuntu/ precise main restricted</p> <p>## Major bug fix updates produced after the final release of the</p> <p>## distribution.</p> <p>deb http://mirrors.hengtiansoft.com/ubuntu/ precise-updates main restricted</p> <p>deb-src http://mirrors.hengtiansoft.com/ubuntu/ precise-updates main restricted</p> <p>## N.B. software from this repository is ENTIRELY UNSUPPORTED by the Ubuntu</p> <p>## team. Also, please note that software in universe WILL NOT receive any</p> <p>## review or updates from the Ubuntu security team.</p> <p>deb http://mirrors.hengtiansoft.com/ubuntu/ precise universe</p> <p>deb-src http://mirrors.hengtiansoft.com/ubuntu/ precise universe</p> <p>deb http://mirrors.hengtiansoft.com/ubuntu/ precise-updates universe</p> <p>deb-src http://mirrors.hengtiansoft.com/ubuntu/ precise-updates universe</p> <p>## N.B. software from this repository is ENTIRELY UNSUPPORTED by the Ubuntu</p> <p>## team, and may not be under a free licence. Please satisfy yourself as to</p> <p>## your rights to use the software. Also, please note that software in</p> <p>## multiverse WILL NOT receive any review or updates from the Ubuntu</p> <p>## security team.</p> <p>deb http://mirrors.hengtiansoft.com/ubuntu/ precise multiverse</p> <p>deb-src http://mirrors.hengtiansoft.com/ubuntu/ precise multiverse</p> <p>deb http://mirrors.hengtiansoft.com/ubuntu/ precise-updates multiverse</p> <p>deb-src http://mirrors.hengtiansoft.com/ubuntu/ precise-updates multiverse</p> <p>## N.B. software from this repository may not have been tested as</p> <p>## extensively as that contained in the main release, although it includes</p> <p>## newer versions of some applications which may provide useful features.</p> <p>## Also, please note that software in backports WILL NOT receive any review</p> <p>## or updates from the Ubuntu security team.</p> <p>deb http://mirrors.hengtiansoft.com/ubuntu/ precise-backports main restricted universe multiverse</p> <p>deb-src http://mirrors.hengtiansoft.com/ubuntu/ precise-backports main restricted universe multiverse</p> <p>deb http://mirrors.hengtiansoft.com/ubuntu precise-security main restricted</p> <p>deb-src http://mirrors.hengtiansoft.com/ubuntu precise-security main restricted</p> <p>deb http://mirrors.hengtiansoft.com/ubuntu precise-security universe</p> <p>deb-src http://mirrors.hengtiansoft.com/ubuntu precise-security universe</p> <p>deb http://mirrors.hengtiansoft.com/ubuntu precise-security multiverse</p> <p>deb-src http://mirrors.hengtiansoft.com/ubuntu precise-security multiverse</p> <p>## Uncomment the following two lines to add software from Canonical’s</p> <p>## ‘partner’ repository.</p> <p>## This software is not part of Ubuntu, but is offered by Canonical and the</p> <p>## respective vendors as a service to Ubuntu users.</p> <p># deb http://archive.canonical.com/ubuntu precise partner</p> <p># deb-src http://archive.canonical.com/ubuntu precise partner</p> <p>## Uncomment the following two lines to add software from Ubuntu’s</p> <p>## ‘extras’ repository.</p> <p>## This software is not part of Ubuntu, but is offered by third-party</p> <p>## developers who want to ship their latest software.</p> <p># deb http://extras.ubuntu.com/ubuntu precise main</p> <p># deb-src http://extras.ubuntu.com/ubuntu precise main</p> <p>deb http://mirrors.hengtiansoft.com/ubuntu-cloud precise-proposed/grizzly main</p> <p>deb http://mirrors.hengtiansoft.com/ubuntu-cloud precise-updates/grizzly main</p> <p>[/code]</p> <p>apt-get update,更新下缓存</p> <p>之后就可以 apt-get install -y xxxxx 啦,体验飞一般的速度~~~</p> <p>参考:</p> <p><a href="http://unixrob.blogspot.com/2012/05/create-apt-mirror-with-ubuntu-1204-lts.html" title="create an apt-mirror with ubuntu 12.04 lts" target="_blank">create an apt-mirror with ubuntu 12.04 lt</a> (要爬墙)</p> <p><a href="http://wiki.woosum.net/apt-mirror" title="apt-mirror" target="_blank">apt-mirror</a></p> Tue, 10 Sep 2013 00:59:38 +0800 http:/wp.fungo.me/linux/ubuntu/setup-ubuntu-12-04-local-mirror.html http:/wp.fungo.me/linux/ubuntu/setup-ubuntu-12-04-local-mirror.html 12.04 apt-mirror ubuntu 本地源 镜像 GNU/Linux Ubuntu VMware ESXi 的故事 <p>VMware ESXi是一个免费的虚拟化产品,它是VMware vSphere 云架构产品线最基础的部分,提供底层的虚拟化技术。任何人都可以去VMware的官网下载,在物理机上安装,然后就可以创建虚拟机了,并且VMware还贴心的提供了免费的管理软件——VMware vSphere Client,在你自己的PC上装上这个软件,然后通过网络连接ESXi服务器,就可以在Client界面上用鼠标点点拖拖创建虚拟机啦,这样一套企业级别虚拟化解决方案就有了,而这些完全free of charge,哦,多么伟大的VMware!如果你真这么想,那么你就too simple了,不要忘记了,VMware是一家商业公司,还有一句话是这么说的“没有免费的午餐”。</p> <p>这种免费解决方案完全是个鸡肋,看下面的图就知道了。。。</p> <div id="attachment_137" style="max-width: 403px" class="wp-caption aligncenter"> <a href="http://blog.hengtiansoft.org/?attachment_id=137" rel="attachment wp-att-137"><img src="http://blog.hengtiansoft.org/wp-content/uploads/2013/04/VMware-ESXi-free-solution.png" alt="VMware ESXi 免费解决方案" width="393" height="365" class="size-full wp-image-137" /></a> <p class="wp-caption-text"> VMware ESXi 免费解决方案 </p> </div> <!--more--> <p> </p> <p>ESXi Server与Client是一对一的,每一个ESXi Server都要一个Client来连接,对一个企业来的Infra说,这是不能容忍的。。。当一个虚拟机出故障,Infra要一个一个client窗口去翻,这是多么boring的事情,看下图就懂了。。</p> <div id="attachment_140" style="max-width: 489px" class="wp-caption aligncenter"> <a href="http://blog.hengtiansoft.org/?attachment_id=140" rel="attachment wp-att-140"><img src="http://blog.hengtiansoft.org/wp-content/uploads/2013/04/Mutiple-vSphere-Client.png" alt="多个 vSphere Client" width="479" height="627" class="size-full wp-image-140" /></a> <p class="wp-caption-text"> 多个 vSphere Client </p> </div> <p> </p> <p>我企业这么多虚拟机跑在ESXi上,我不想让Infra花这么多在人力在虚拟机管理上,有没有一个集中的控制台呢,并且能够在不同ESXi上迁移虚拟机,做到负载均衡这些东西呢,有,VMware早就替你想到了,VMware vCenter,它是VMware的一个私有云解决方案,但是这个东东可是价格不菲呢。。。随着ESXi服务器的增多,企业你要么支付Infra一直增长的管理费用,要么花钱买vCenter,谁让你当初选择了“免费的”ESXi,上了VMware的贼船呢。。VMware在一旁窃笑不已。。</p> <div id="attachment_145" style="max-width: 456px" class="wp-caption aligncenter"> <a href="http://blog.hengtiansoft.org/?attachment_id=145" rel="attachment wp-att-145"><img src="http://blog.hengtiansoft.org/wp-content/uploads/2013/04/VMware-ESXi-privatecloud-solution.png" alt="VMware ESXi 私有云解决方案" width="446" height="321" class="size-full wp-image-145" /></a> <p class="wp-caption-text"> VMware ESXi 私有云解决方案 </p> </div> <p> </p> <p>有没有第三方的统一管理工具,或者我自己开发管理工具来管理ESXi呢。VMware说,有,我们提供web service,你可以编程调用这些web service,我们的vSphere Client就是调用这些web service来做的。。但是,这些web service接口不是免费开放给你的,我自家的vSphere Client可以随便调,并且我们做好给你用,如果你要自己编程写工具的话,就要给我们接口搞用费。。。</p> <p>看看过里的说明吧</p> <p><a href="http://www.vmware.com/download/eula/esx_esxi_eula.html" title="VMWARE END USER LICENSE AGREEMENT" target="_blank">VMWARE END USER LICENSE AGREEMENT</a></p> <p><a href="http://www.virtualizationpractice.com/vmware-forcing-third-parties-to-not-sell-products-for-free-esxi-274/" title="VMware Forcing Third Parties to NOT Sell Products for Free ESXi" target="_blank">VMware Forcing Third Parties to NOT Sell Products for Free ESXi</a></p> <p><a href="http://www.veeam.com/blog/veeam-and-free-esxi.html" title="Veeam and Free ESXi" target="_blank">Veeam and Free ESXi</a></p> <p>在开源的虚拟化技术(KVM,XEN等)这么成熟的今天,还有OpenStack,CloudStack这类开源云平台,我们是不是可以抛弃ESXi了呢。。</p> <p>(VMware已经意识到这种变化,现在已经加入OpenStack了呢。。)</p> <p>本文同步发表在<a href="http://blog.hengtiansoft.org/vmware-esxi-story.html" title="网新恒天私有云团队博客" target="_blank">网新恒天私有云团队博客</a>,谢谢关注,转载请注明出处。</p> Wed, 01 May 2013 02:42:26 +0800 http:/wp.fungo.me/openstack/vmware-esxi-story.html http:/wp.fungo.me/openstack/vmware-esxi-story.html ESXi free openstack VMware OpenStack Openstack 管理 VMware ESX/ESXi (1) 架构 <p>关于OpenStack的研究,大家基本都是集中于KVM,XEN这类开源虚拟化技术,对VMware的研究比较少。我们公司(网新恒天)由于之前采用的虚拟化技术是VMware的,所以在OpenStack研究开始之初就以VMware为虚拟化平台,希望能用OpenStack将VMware管理起来,经过4个月左右的研究开发,已经实现这个目标。现在我们决定将自己的研究成果分享出来,希望能和大家一起交流。</p> <p>本文主要讨论OpenStack管理 VMware ESX/ESXi 时的部署架构。</p> <p>因为VMware vSphere ESX/ESXi 的特殊性,导致Openstack管理ESX/ESXi时的部署架构会与KVM、XEN等不一样,所以刚开始安装的时候会有这样的疑问:nova-compute 装哪里,怎么装呢? 下面就来说明下这个问题。</p> <p>因为VMware ESX/ESXi 是非常封闭的,ssh上去之后,会发现之个系统连一些常用的命令都没有,更不用说python、gcc了,是一个非常精简的系统,所以在ESX/ESXi上直接安装nova-compute基本是不可能的。要单独找一台计算机(物理机/虚拟机),在上面安装ubuntu,然后再在这个上面安装nova-compute,让nova-compute来通过网络的方式来连接ESX/ESXi。ESX/ESXi对外提供web services(SOAP实现)编程接口,nova-compute就是通过调用这些接口,将OpenStack命令转化成web services请求来实现对ESX/ESXi的控制。</p> <p>概念架构图如下所示:</p> <div id="attachment_114" style="max-width: 610px" class="wp-caption alignleft"> <a href="http://blog.hengtiansoft.org/openstack-vmware-esxi-part1-architecture.html/novaesxarchitecture-600" rel="attachment wp-att-114"><img class="size-full wp-image-114" alt="OpenStack ESX/ESXi 架构概念图" src="http://blog.hengtiansoft.org/wp-content/uploads/2013/04/NovaEsxArchitecture-600.png" width="600" height="395" /></a> <p class="wp-caption-text"> OpenStack 管理VMware 架构概念图 </p> </div> <!--more--> <p>在实际部署中,我们这样来做,用虚拟机来做nova-compute结点,并且将这个虚拟机建在它上面的nova-comppute所连接的ESX/ESXi上,因为nova-compute结点只负责和ESX/ESXi通信,虚拟化工作由ESX/ESXi来承担,nova-compute结点的负载非常小,对原有ESX/ESXi影响也非常小,实事证明这种做法是完全可行的。(我们实验用的虚拟机配置1vCPU,2G内存,毫无压力)</p> <div id="attachment_118" style="max-width: 610px" class="wp-caption alignleft"> <a href="http://blog.hengtiansoft.org/openstack-vmware-esxi-part1-architecture.html/openstack-vmware-esxi-deploy" rel="attachment wp-att-118"><img class="size-full wp-image-118" alt="OpenStack VMware ESXi 部署架构" src="http://blog.hengtiansoft.org/wp-content/uploads/2013/04/OpenStack-VMware-ESXi-Deploy.png" width="600" height="405" /></a> <p class="wp-caption-text"> OpenStack VMware ESXi 部署架构 </p> </div> <p>OpenStack官方关于VMware ESX/ESXi Server Support的图如下所示,比较详细。nova-compute通过SOAP使ESX/ESXi执行虚拟机相关操作,如创建、重启、删除,快照等;在创建虚拟的时候,能过glance取镜像传到ESX/ESXi上,在打快照的时候,先在ESX/ESXi执行快照命令,然后将生成的快照传到glance上。可以看到,OpenStack类似一个中转层代理,在两边分别实现OpenStack和ESX/ESXi的接口,然后将两者连接起来。</p> <div style="max-width: 775px" class="wp-caption alignleft"> <a href="http://docs.openstack.org/folsom/openstack-compute/admin/content/figures/vmwareapi_blockdiagram.jpg"><img alt="VMware ESX/ESXi Server Support 官方示意图" src="http://docs.openstack.org/folsom/openstack-compute/admin/content/figures/vmwareapi_blockdiagram.jpg" width="765" height="611" /></a> <p class="wp-caption-text"> VMware ESX/ESXi Server Support 官方示意图 </p> </div> <p>本文同步发表在<a href="http://blog.hengtiansoft.org/openstack-vmware-esxi-part1-architecture.html" title="网新恒天私有云团队博客" target="_blank">网新恒天私有云团队博客</a>,谢谢关注,转载请注明出处。</p> Mon, 29 Apr 2013 22:42:58 +0800 http:/wp.fungo.me/openstack/openstack-%e7%ae%a1%e7%90%86-vmware-esxesxi-1-%e6%9e%b6%e6%9e%84.html http:/wp.fungo.me/openstack/openstack-%e7%ae%a1%e7%90%86-vmware-esxesxi-1-%e6%9e%b6%e6%9e%84.html ESX ESXi nova openstack VMware OpenStack MOD9 去九法 <p>MOD9叫做去九法或者弃九法,是十进制数的一个性质:任何一个整数模9同余于它的各数位上数字之和。经常被用来检验几个数相加、相减、相乘的结果是否正确。</p> <p>原理</p> <div class="highlighter-rouge"><div class="highlight"><pre class="highlight"><code>ABC =100A+10B+1C =(99+1)A+(9+1)B+1C =99A+9B+(A+B+C) =9M+(A+B+C) </code></pre></div></div> <p>因此可得知:</p> <p><code class="highlighter-rouge">ABC≡A+B+C (mod9)</code></p> <p>Refer: <a href="https://zh.wikipedia.org/wiki/%E5%8E%BB%E4%B9%9D%E6%B3%95" title="去九法" target="_blank">去九法</a></p> <p>练手题 <a href="http://acm.hdu.edu.cn/showproblem.php?pid=1013" title="HDOJ 1013 Digital Roots" target="_blank">HDOJ 1013 Digital Roots</a></p> Mon, 29 Apr 2013 06:39:53 +0800 http:/wp.fungo.me/math/mod9-%e5%8e%bb%e4%b9%9d%e6%b3%95.html http:/wp.fungo.me/math/mod9-%e5%8e%bb%e4%b9%9d%e6%b3%95.html Math OpenStack VMware ESXi 相关资料 <p>OpenStack对VMware的支持不是太好,并且由于VMware的商业产品,并不像KVM,XEN那样开放,使用时会涉及License等原因,所以大家在使用OpenStack时对VMware的关注一直很少,这方面的资料也非常少。从12年10月份,我们就一直在研究OpenStack对VMware的支持,中间遇到了不少问题,也获得了不少经验。这篇文章分享下我们在研究OpenStack时有帮助的资料。</p> <p><a href="https://blueprints.launchpad.net/nova/+spec/hypervisor-vmware-vsphere-support" title="VMware vSphere Support bluprit C版本" target="_blank">VMware vSphere Support bluprit</a></p> <p>OpenStack在C版的时候加入对VMware的支持,当时VMware还没有加入OpenStack,这份代码是由<a href="http://blogs.citrix.com/2011/03/25/openstacks-vsphere-support/" title="OpenStack’s vSphere support" target="_blank">Citrix的人花了3个月写的</a>。</p> <p><a href="https://wiki.openstack.org/wiki/VMware-vSphere-support" title="VMware-vSphere-support详细设计说明" target="_blank">VMware vSphere Support full specification</a></p> <p>这篇文档非常有用,包含环境需求、设计scope、测试标准和License问题,刚开始没有太注意其中的<strong>License问题</strong>,后来掉到大坑里了,官方文档要好好读读。</p> <!--more--> <p><a href="http://docs.openstack.org/folsom/openstack-compute/admin/content/vmware.html" title=" VMware ESX/ESXi Server Support" target="_blank">VMware ESX/ESXi Server Support</a></p> <p>官方安装文档里对ESXi的架构说明和安装指导,但这个文档不够详细。</p> <p><a href="https://github.com/openstack/nova/blob/stable/essex/doc/source/vmwareapi_readme.rst" title="VMware ESX/ESXi Server Support for OpenStack Compute" target="_blank">VMware ESX/ESXi Server Support for OpenStack Compute</a></p> <p>这篇文档应该是详细的官方安装指导,可是不知道什么原因在F版的时候就被从代码库里删掉了,在D和E版里都有。非常详细,nova-compute配置,netwrok配置(Flat+VLAN),VNC配置(G版的时候已经不用这个配置,更加方便),IP注入实现。要好好读。</p> <p><a href="http://www.mail-archive.com/openstack@lists.launchpad.net/msg09490.html" title="[Openstack] ESXi documentation.." target="_blank">[Openstack] ESXi documentation..</a></p> <p>ESXi配置问题的邮件列表,里面的配置有问题,但可以参考。</p> <p><a href="https://lists.launchpad.net/openstack/msg10528.html" title="Using VMWare ESXi with openstack" target="_blank">Using VMWare ESXi with openstack</a></p> <p>又一个可供参考的邮件列表。</p> <p><a href="https://answers.launchpad.net/nova/+question/174186" title="VMDK images will not start on ESXi 4.1 Reason: The file specified is not a virtual disk" target="_blank">VMDK images will not start on ESXi 4.1 Reason: The file specified is not a virtual disk</a></p> <p><a href="https://answers.launchpad.net/glance/+question/178944" title="OpenStack Image Registry and Delivery Service (Glance)" target="_blank">OpenStack Image Registry and Delivery Service (Glance)</a></p> <p><a href="http://ashokcloud.blogspot.jp/2011/11/how-to-add-vmware-and-virtualbox-image.html" title="How to add vmware and VirtualBox image in openstack" target="_blank">How to add vmware and VirtualBox image in openstack</a></p> <p>VMDK镜像相关。</p> <p><a href="http://www.thinkvirt.com/?q=node/181" title="Mapping VMware GuestID's to their Associated OS Retail Name" target="_blank">Mapping VMware GuestID’s to their Associated OS Retail Name</a></p> <p>VMware 操作系统类型对应关系。</p> <p><a href="https://bugs.launchpad.net/nova/+bug/796834" title="Incorrect machine id parameters in vmx for guest_tool.py" target="_blank">Incorrect machine id parameters in vmx for guest_tool.py</a></p> <p>VMware IP 注入问题。</p> <p>在12年8月份的时候,宣布加入OpenStack,并<a href="http://blogs.vmware.com/vcloud/2012/10/catch-vmware-next-week-at-openstack-summit.html" title="Catch VMware Next Week at OpenStack Summit" target="_blank">参加G版本的summit</a>,决定对社区贡献代码等等。</p> <p>之后,在G版的blueprint中,提出一个新的设计,加强对VMware的支持,由VMware自己的员工Sean Chen亲自贡献代码。</p> <p><a href="https://blueprints.launchpad.net/nova/+spec/vmware-compute-driver" title="VMware Compute Driver" target="_blank">G版VMware Compute Driver</a></p> <p>加强后的driver支持很多新功能</p> <p>Enhancing VMware Compute Driver</p> <p>• Launch OVF disk image</p> <p>• VNC console</p> <p>• Attach and Detach iSCSI volume</p> <p>• Guest info</p> <p>• Host ops</p> <p>• VLAN</p> <p>• Quantum</p> <p>• Cold migration</p> <p>• Live migration</p> <p>• VirtualCenter support</p> <p><a href="http://www.slideshare.net/opencompute/vmware-nova-compute-driver" title="G版VMware Driver详细设计说明" target="_blank">VMware Nova Compute Driver full specification in Grizzly</a></p> <p>slidshare上的一个slide,讲了vmware driver的历史,和新的功能,需要爬墙。</p> <p><a href="https://review.openstack.org/#/c/16496/" title="F版使用G版中的功能" target="_blank">VMware Compute Driver For Folsom</a></p> <p>虽然新功能是针对G版的,但Sean Chen也对F版做了新功能补丁,非常好。我们现在用的就是基于这部分代码的。</p> <p><a href="http://hi.baidu.com/juacm/item/17cd9224962c18c2a4275af6" title="Openstack 支持ESXI" target="_blank">Openstack 支持ESXI</a></p> <p>这篇应该是国内研究OpenStack支持VMware最早的,有一定参考价值。</p> <p><a href="http://blog.csdn.net/soft_lawrency/article/details/8515081" title="Openstack整合esxi 测试总结" target="_blank">Openstack整合esxi 测试总结</a></p> <p>国内一篇比较新的关于OpenStack使用VMware ESXi作为虚拟化技术的,写得不错。</p> <p>本文同步发表在<a href="http://blog.hengtiansoft.org/openstack-vmware-materials.html" title="网新恒天私有云团队博客" target="_blank">网新恒天私有云团队博客</a>,谢谢关注,转载请注明出处。</p> Sat, 27 Apr 2013 00:15:46 +0800 http:/wp.fungo.me/openstack/openstack-vmware-materials.html http:/wp.fungo.me/openstack/openstack-vmware-materials.html ESX ESXi openstack VMware OpenStack Vagrant:程序员的VirtualBox(一) <h3 id="前言"><strong>前言</strong></h3> <p>VirtualBox相信很多人都用过,它是一个虚拟化软件,用来在我们的电脑上虚拟出一电脑来,和 VMware Workstation, kvm, xen等是一类东西(专业点的说法叫<a href="http://en.wikipedia.org/wiki/Hypervisor" target="_blank">Hypervisor</a>),不过它提供易用的界面(VMware也有),对各平台的支持(Linux, Win, Mac),又非常的轻量,最关键的free呀,所以使用非常广泛。</p> <p>我们平时使用VirtualBox最常见的情况就是,自己电脑是个Win,想跑个Linux,或者电脑是个Linux,要跑win上网银啊,QQ什么的 win only之类的,虽然有wine这样的东东,可以在Linux下跑win下的软件,但有个win系统稳定性不更好么,并且现在的电脑配置,完全不用担心虚拟机消耗内存CPU什么的,你不用那才是浪费呢,何况VirtualBox这么轻量。个人体验,弄个xp虚拟机跑QQ资源消耗比chrome或者ff登webQQ要少,可恶的用了多少js。。。</p> <p>对天天码代码的苦逼程序员来说,VirtualBox就不能是简简单单的挂个Q这么用了-&gt;_-&gt;</p> <p>程序员搞最多的就是码代码了,可能做很多个项目,公司里搞java的,php的,自己回家再玩个python什么的,想体验下新版本的mongodb或者nginx,Emacs或者vim的配置啦,保不准哪个项目前个版本的数据库是mysql,下个版本用了postgresql, 这么多东西全都搞在一起,装在一个电脑上,肯定会被这各种配置环境搞的晕晕的,烦不胜烦。</p> <p>怎么办呢,最好是每个项目都有一个干净的开发环境,只为这个项目,可是我们不可能为每一个项目配一个电脑吧,有了,虚拟机,给每一个项目配一个虚拟机,开发A的时候就启A的虚拟机,这样各个开发环境互相独立,干干净净。还有一个问题,我们的项目有多个开发人员,如何保障大家的开发环境都一样呢,总不能每个人都一个个点击鼠标,填写配置参数,建好后上支一个个软件安装吧,这太麻烦了,太不geek了。我们想要的是,环境只配置一遍,然后可以把这个环境打包deliver给别人,别人拿到后,直接启起来就可以用。那有没有这样的东西呢,肯定是有的,Vagrant,它就是用来干这个的。</p> <!--more--> <p>下面主角登场,来看看Vagrant是多么适合程序员。</p> <h3 id="介绍篇"><strong>介绍篇</strong></h3> <p>“Vagrant uses Oracle’s VirtualBox to build configurable, lightweight, and portable virtual machines dynamically.”</p> <p>Vagrant 是 Mitchell Hashimoto 用 ruby 写的,去年11月份,Mitchell 专门成立了一个公司 HashiCorp 来更好的开发 Vagrant, 并且申明,Vagrant会一直开源。<a href=" http://www.hashicorp.com/blog/announcing-hashicorp.html" title="Announcing HashiCorp" target="_blank">Announcing HashiCorp</a></p> <p>感兴趣的还可以看下作者在就HashiCorp成立在Hacker News上发的一个topic http://news.ycombinator.com/item?id=4843508</p> <h3 id="安装篇"><strong>安装篇</strong></h3> <p>确保安装了VirtualBox,fedora 用户可以参考本人以前写的 <a href="http://fungo.me/linux/fedora/fedora15-install-virtualbox-and-extension-pack.html" title="Fedora 15 安装 virtualbox 及 Extension Pack" target="_blank">Fedora 15 安装 virtualbox 及 Extension Pack</a></p> <p>Vagrant有2种安装方式,一种是通过平台的安装包,一种是通过RubyGems,官方推荐首选安装包的方式安装</p> <p>Vagrant官方发布了各种平台的安装包,可以去<a href="http://downloads.vagrantup.com/" target="_blank">下载</a></p> <p>点开一个版本后会看到有 mac, win,以及各常用的Linux distro(debain, arch, redhat),下载下来安装就好了,就像装一个普通软件一样。</p> <p>安装包里已经打包了ruby,所以不用事先安装ruby环境。</p> <p>如果没有发现自己系统的安装包的话,请采用RubyGems方式安装</p> <p>1) 先安装Ruby和RubyGems,请自行google安装</p> <p>2) 安装vagrant</p> <p>gem install vagrant</p> <p>安装完成之后可以使用vagrant -v命令来查看vagrant的版本号</p> <h3 id="使用篇"><strong>使用篇</strong></h3> <p>安装后好,我们就可以来玩vagrant了 ^_^</p> <p>先建一个文件夹,然后进到文件夹里</p> <p>mkdir vagrant-test &amp;&amp; cd vagrant-test</p> <p>按照官网教程,敲下面命令,第一条要从网上下载大约260M的box文件,所以会等一会,视网速情况了。</p> <p>[bash]</p> <p>$ vagrant box add lucid32 http://files.vagrantup.com/lucid32.box</p> <p>$ vagrant init lucid32</p> <p>$ vagrant up</p> <p>[/bash]</p> <p>这样之后,就启动一个虚拟机了,系统是ubuntu lucid 32位,可以在VirtualBox的控制台界面看到多了一个,如下图</p> <p><a href="http://fungo.me/wp-content/uploads/2013/02/vagrant1.png"><img src="http://fungo.me/wp-content/uploads/2013/02/vagrant1-300x225.png" alt="vagrant vm in VirtualBox" title="vagrant vm in VirtualBox" width="300" height="225" class="aligncenter size-medium wp-image-537" srcset="http://wp.fungo.me/wp-content/uploads/2013/02/vagrant1-300x225.png 300w, http://wp.fungo.me/wp-content/uploads/2013/02/vagrant1.png 770w" sizes="(max-width: 300px) 100vw, 300px" /></a></p> <p>但是不能通过控制台访问,要想进去的话,输入</p> <p>vagrant ssh</p> <p>然后就进虚拟机里面了,可以各种操作了</p> <p>关闭虚拟机</p> <p>vagrant halt</p> <p>删除虚拟机</p> <p>vagrant destroy</p> Fri, 22 Feb 2013 01:02:28 +0800 http:/wp.fungo.me/linux/vagrant-for-programmer-ch1.html http:/wp.fungo.me/linux/vagrant-for-programmer-ch1.html vagrant virtualbox GNU/Linux http代理环境下使用github <p>公司上网一般是通过http代理,只能访问外网的443和80端口,github现在本身就支持https的方式提交代码,这里讲的是如何通过默认方式即git协议提交代码,我们知道git是基于ssh的,端口是22,github这个服务器的ssh端口我们又不能像自己的服务器一样改成443,怎么办呢。在基于http做好的ssh代理之上,再连接github,已经开启的ssh代理是不受端口限制的。</p> <p>首先参考 <a href="http://fungo.me/linux/tunneling-ssh-over-http-proxy.html" title="用ssh突破公司http代理" target="_blank">用ssh突破公司http代理</a>,把ssh代理建立起来。</p> <p>然后按照github的官网<a href="https://help.github.com/articles/generating-ssh-keys" title="Generating SSH Keys" target="_blank">Generating SSH Keys</a>,生成密钥,并相应的配置github的帐户。</p> <p>配置好后在 .ssh/conifg 里添加配置如下:</p> <div class="language-bash highlighter-rouge"><div class="highlight"><pre class="highlight"><code> Host github.com HostName github.com User git PreferredAuthentications publickey IdentityFile ~/.ssh/github.rsa ProxyCommand nc <span class="nt">-x</span> 127.0.0.1:7070 %h %p </code></pre></div></div> <p>然后就可以正常使用git clone/pull/push这些命令啦!</p> <p>这里有个介绍如何直接使用http代理的,没试过,估计是可以的</p> <p><a href="http://www.vpsee.com/2011/07/how-to-use-git-through-a-http-proxy/" title="如何让 Git 使用 HTTP 代理服务器" target="_blank">如何让 Git 使用 HTTP 代理服务器</a></p> Fri, 23 Nov 2012 22:53:16 +0800 http:/wp.fungo.me/linux/github-through-http-proxy.html http:/wp.fungo.me/linux/github-through-http-proxy.html git github http-proxy ssh 内网 GNU/Linux irssi在http代理条件下登录irc聊天室 <p>公司上网一般是通过http代理,然后只能访问外网的443和80端口,而irc服务器的端口是6667,如irc.freenode.net。那么我们该怎么做呢,可以用ssh的本地端口(Local Forwarding)转发来实现。关于ssh的各种端口转发,<a href="http://pentestmonkey.net/cheat-sheet/ssh-cheat-sheet" title="SSH Cheat Sheet" target="_blank">SSH Cheat Sheet</a>有详细的介绍。</p> <p>前提是要能连上ssh服务,如何在http代理环境下连ssh,参考 <a href="http://fungo.me/linux/tunneling-ssh-over-http-proxy.html" title="用ssh突破公司http代理" target="_blank">用ssh突破公司http代理</a>。</p> <!--more--> <p>然后就可以这样来打开本地转发功能了</p> <p>[code lang=”bash”]</p> <p>ssh -L 1234:irc.freenode.net:6667 user@homemachine</p> <p>[/code]</p> <p>也可以参考SSH Cheat Sheet那篇文章,把所有的转发配置都写在 ~/.ssh/config 里面。</p> <p>然后就可以</p> <p>在irssi的控制台这样来配置代理了</p> <p>[code lang=”bash”]</p> <p>/set proxy_address 127.0.0.1</p> <p>/set proxy_port 1234</p> <p>/set use_proxy ON</p> <p>[/code]</p> <p>irssi的配置文件.irssi/config 里面会多了这样几行</p> <p>[code lang=”bash”]</p> <p>proxy_address = “127.0.0.1”;</p> <p>proxy_port = “1234”;</p> <p>use_proxy = “yes”;</p> <p>[/code]</p> <p>irssi是cli下面一个非常好用的irc工具,与screen可以说是天作之合,<a href="http://quadpoint.org/articles/irssi/ " title="A Guide to Efficiently Using Irssi and Screen" target="_blank">A Guide to Efficiently Using Irssi and Screen</a> 介绍两者如何搭配,非常详细,以后有使用心得的话也写一点。</p> <p>参考:</p> <p><a href="http://www.techanswerguy.com/2010/08/irc-chat-using-irssi-via-ssh-port.html" title="irssi behind proxy" target="_blank">irssi behind proxy (via ssh port forwarding)</a></p> Fri, 23 Nov 2012 22:25:36 +0800 http:/wp.fungo.me/linux/tunnel-irc-with-ssh.html http:/wp.fungo.me/linux/tunnel-irc-with-ssh.html http-proxy irssi ssh 内网 通道 GNU/Linux 用ssh突破公司http代理 <p>公司出于安全考虑一般都通过防火墙将内网和外网隔离开来,然后为了控制监控员工的上网行为,架设专门与外网交互的代理,所有的电脑要想上网必须通过这个代理。代理的形式一般是HTTP和HTTPS的,这样做不仅可以限制员工上哪些网站,还能看上网在干啥,http是完全透明的,没有隐私。</p> <p>作为一个技术人员,我们是不能容忍上网被限制和监控的,怎么办,代理!可以代理的前提是,你要能与外界互通,而现在只能通过公司的http代理与外界互通(当然,你可以突破公司的这种代理限制,另寻与网络互联的方法,不过这种从风险和技术角度来说都有点太过了),所以代理必须能基于http才行。常用的代理是ssh的socks,有没有方法可以在http上打通一层socks通道呢,有,Corkscrew就是专门来干这事的,<a href="http://www.agroman.net/corkscrew/" title="Corkscrew" target="_blank">官方主页</a>的介绍 “Corkscrew is a tool for tunneling SSH through HTTP proxies”,这正事我们想要的。</p> <!--more--> <p>背景故事讲完了,下面说说如何用这个螺丝锥(Corkscrew)在http上钻出一条socks通道出来。</p> <p>前提条件</p> <p>1、外网有一台支持sshd服务的服务器,一般的linux vps是肯定有的</p> <p>2、sshd服务监听在443或者80端口(因为公司的http代理一般只允许访问外网的80-http或443-https服务,如果你的vps本身就用到这2个端口的话,考虑<a href="http://blog.creke.net/758.html" title="让一个端口同时做两件事:http/https和ssh" target="_blank">端口复用</a>)</p> <p>3、本地有一台linux(因为我是在linux下面配的,Windows应该也可以,用putty,因为putty配置里有基于其它代理的配置选项)</p> <p>首先从corkscrew下载最新的代码来编译安装喽</p> <div class="language-bash highlighter-rouge"><div class="highlight"><pre class="highlight"><code>wget http://www.agroman.net/corkscrew/corkscrew-2.0.tar.gz <span class="nb">tar </span>zxvf corkscrew-2.0.tar.gz <span class="nb">cd </span>corkscrew-2.0 ./configure make <span class="nb">install</span> </code></pre></div></div> <p>然后配置~/.ssh/config文件了</p> <p>如果要让所有ssh登录都走代理的话(一般也是这种情况)</p> <div class="language-bash highlighter-rouge"><div class="highlight"><pre class="highlight"><code> Host <span class="k">*</span> ProxyCommand corkscrew http-proxy-server-address 80 %h %p </code></pre></div></div> <p>当然,也可以只上部份ssh登录,只须指明host就行了</p> <div class="language-bash highlighter-rouge"><div class="highlight"><pre class="highlight"><code> Host external.com example.com ProxyCommand corkscrew http-proxy-server-address 80 %h %p </code></pre></div></div> <p>host也支持一些正则匹配,这是ssh config 文件的语法,这里就不详细说明了</p> <p>配置改好后就可以登录外网的vps啦</p> <p><code class="highlighter-rouge">ssh -D 7070 -p 443 username@vps-address</code></p> <p>然后就可以在本地用7070这个socks端口,随便访问什么网站了,使用任何端口了,例如使用git。</p> <p>最后一步也可以写到配置文件里</p> <div class="language-bash highlighter-rouge"><div class="highlight"><pre class="highlight"><code> Host vps-proxy HostName vps-address User username PreferredAuthentications publickey IdentityFile ~/.ssh/vps-host.ira DynamicForward 0.0.0.0:7070 Port 443 </code></pre></div></div> <p>不过这个配置要写在 corkscrew 配置的前面,配置文件默认是找到第一个名字匹配的就不往下找了。</p> <p>参考:</p> <p><a href="http://www.mtu.net/~engstrom/ssh-proxy.php" title="Tunneling SSH over an HTTP-Proxy Server" target="_blank">Tunneling SSH over an HTTP-Proxy Server</a></p> <p><a href="http://www.agroman.net/corkscrew/" title="Corkscrew" target="_blank">Corkscrew offical site</a></p> <p><a href="http://stackoverflow.com/questions/10610503/disable-corkscrew-for-local-addresses" title="Disable corkscrew for local addresses" target="_blank">Disable corkscrew for local addresses</a></p> Fri, 23 Nov 2012 20:37:34 +0800 http:/wp.fungo.me/linux/tunneling-ssh-over-http-proxy.html http:/wp.fungo.me/linux/tunneling-ssh-over-http-proxy.html corkscrew http socks ssh 内网 通道 GNU/Linux 从hibernate criteria 或者 HQL 得到 SQL <p>啥也不说,直接无节操上代码</p> <div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code> <span class="kn">import</span> <span class="nn">java.lang.reflect.Field</span><span class="o">;</span> <span class="kn">import</span> <span class="nn">java.util.Collections</span><span class="o">;</span> <span class="kn">import</span> <span class="nn">org.hibernate.Criteria</span><span class="o">;</span> <span class="kn">import</span> <span class="nn">org.hibernate.SessionFactory</span><span class="o">;</span> <span class="kn">import</span> <span class="nn">org.hibernate.engine.SessionFactoryImplementor</span><span class="o">;</span> <span class="kn">import</span> <span class="nn">org.hibernate.hql.QueryTranslator</span><span class="o">;</span> <span class="kn">import</span> <span class="nn">org.hibernate.hql.QueryTranslatorFactory</span><span class="o">;</span> <span class="kn">import</span> <span class="nn">org.hibernate.hql.ast.ASTQueryTranslatorFactory</span><span class="o">;</span> <span class="kn">import</span> <span class="nn">org.hibernate.impl.CriteriaImpl</span><span class="o">;</span> <span class="kn">import</span> <span class="nn">org.hibernate.impl.SessionImpl</span><span class="o">;</span> <span class="kn">import</span> <span class="nn">org.hibernate.loader.OuterJoinLoader</span><span class="o">;</span> <span class="kn">import</span> <span class="nn">org.hibernate.loader.criteria.CriteriaLoader</span><span class="o">;</span> <span class="kn">import</span> <span class="nn">org.hibernate.persister.entity.OuterJoinLoadable</span><span class="o">;</span> <span class="kd">public</span> <span class="kd">class</span> <span class="nc">HibernateHqlAndCriteriaToSqlTranslator</span> <span class="o">{</span> <span class="kd">private</span> <span class="n">SessionFactory</span> <span class="n">sessionFactory</span><span class="o">;</span> <span class="kd">public</span> <span class="kt">void</span> <span class="nf">setSessionFactory</span><span class="o">(</span><span class="n">SessionFactory</span> <span class="n">sessionFactory</span><span class="o">)</span> <span class="o">{</span> <span class="k">this</span><span class="o">.</span><span class="na">sessionFactory</span> <span class="o">=</span> <span class="n">sessionFactory</span><span class="o">;</span> <span class="o">}</span> <span class="kd">public</span> <span class="n">String</span> <span class="nf">toSql</span><span class="o">(</span><span class="n">Criteria</span> <span class="n">criteria</span><span class="o">)</span> <span class="o">{</span> <span class="k">try</span> <span class="o">{</span> <span class="n">CriteriaImpl</span> <span class="n">c</span> <span class="o">=</span> <span class="o">(</span><span class="n">CriteriaImpl</span><span class="o">)</span> <span class="n">criteria</span><span class="o">;</span> <span class="n">SessionImpl</span> <span class="n">s</span> <span class="o">=</span> <span class="o">(</span><span class="n">SessionImpl</span><span class="o">)</span> <span class="n">c</span><span class="o">.</span><span class="na">getSession</span><span class="o">();</span> <span class="n">SessionFactoryImplementor</span> <span class="n">factory</span> <span class="o">=</span> <span class="o">(</span><span class="n">SessionFactoryImplementor</span><span class="o">)</span> <span class="n">s</span><span class="o">.</span><span class="na">getSessionFactory</span><span class="o">();</span> <span class="n">String</span><span class="o">[]</span> <span class="n">implementors</span> <span class="o">=</span> <span class="n">factory</span><span class="o">.</span><span class="na">getImplementors</span><span class="o">(</span><span class="n">c</span><span class="o">.</span><span class="na">getEntityOrClassName</span><span class="o">());</span> <span class="n">CriteriaLoader</span> <span class="n">loader</span> <span class="o">=</span> <span class="k">new</span> <span class="n">CriteriaLoader</span><span class="o">((</span><span class="n">OuterJoinLoadable</span><span class="o">)</span> <span class="n">factory</span><span class="o">.</span><span class="na">getEntityPersister</span><span class="o">(</span><span class="n">implementors</span><span class="o">[</span><span class="mi">0</span><span class="o">]),</span> <span class="n">factory</span><span class="o">,</span> <span class="n">c</span><span class="o">,</span> <span class="n">implementors</span><span class="o">[</span><span class="mi">0</span><span class="o">],</span> <span class="n">s</span><span class="o">.</span><span class="na">getEnabledFilters</span><span class="o">());</span> <span class="n">Field</span> <span class="n">f</span> <span class="o">=</span> <span class="n">OuterJoinLoader</span><span class="o">.</span><span class="na">class</span><span class="o">.</span><span class="na">getDeclaredField</span><span class="o">(</span><span class="s">"sql"</span><span class="o">);</span> <span class="n">f</span><span class="o">.</span><span class="na">setAccessible</span><span class="o">(</span><span class="kc">true</span><span class="o">);</span> <span class="k">return</span> <span class="o">(</span><span class="n">String</span><span class="o">)</span> <span class="n">f</span><span class="o">.</span><span class="na">get</span><span class="o">(</span><span class="n">loader</span><span class="o">);</span> <span class="o">}</span> <span class="k">catch</span> <span class="o">(</span><span class="n">Exception</span> <span class="n">e</span><span class="o">)</span> <span class="o">{</span> <span class="k">throw</span> <span class="k">new</span> <span class="nf">RuntimeException</span><span class="o">(</span><span class="n">e</span><span class="o">);</span> <span class="o">}</span> <span class="o">}</span> <span class="kd">public</span> <span class="n">String</span> <span class="nf">toSql</span><span class="o">(</span><span class="n">String</span> <span class="n">hqlQueryText</span><span class="o">)</span> <span class="o">{</span> <span class="k">if</span> <span class="o">(</span><span class="n">hqlQueryText</span> <span class="o">!=</span> <span class="kc">null</span> <span class="o">&amp;&amp;</span> <span class="n">hqlQueryText</span><span class="o">.</span><span class="na">trim</span><span class="o">().</span><span class="na">length</span><span class="o">()</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="o">)</span> <span class="o">{</span> <span class="kd">final</span> <span class="n">QueryTranslatorFactory</span> <span class="n">translatorFactory</span> <span class="o">=</span> <span class="k">new</span> <span class="n">ASTQueryTranslatorFactory</span><span class="o">();</span> <span class="kd">final</span> <span class="n">SessionFactoryImplementor</span> <span class="n">factory</span> <span class="o">=</span> <span class="o">(</span><span class="n">SessionFactoryImplementor</span><span class="o">)</span> <span class="n">sessionFactory</span><span class="o">;</span> <span class="kd">final</span> <span class="n">QueryTranslator</span> <span class="n">translator</span> <span class="o">=</span> <span class="n">translatorFactory</span><span class="o">.</span><span class="na">createQueryTranslator</span><span class="o">(</span><span class="n">hqlQueryText</span><span class="o">,</span> <span class="n">hqlQueryText</span><span class="o">,</span> <span class="n">Collections</span><span class="o">.</span><span class="na">EMPTY_MAP</span><span class="o">,</span> <span class="n">factory</span><span class="o">);</span> <span class="n">translator</span><span class="o">.</span><span class="na">compile</span><span class="o">(</span><span class="n">Collections</span><span class="o">.</span><span class="na">EMPTY_MAP</span><span class="o">,</span> <span class="kc">false</span><span class="o">);</span> <span class="k">return</span> <span class="n">translator</span><span class="o">.</span><span class="na">getSQLString</span><span class="o">();</span> <span class="o">}</span> <span class="k">return</span> <span class="kc">null</span><span class="o">;</span> <span class="o">}</span> <span class="o">}</span> </code></pre></div></div> <p>参考:</p> <p><a href="http://narcanti.keyboardsamurais.de/hibernate-criteria-to-sql-translation.html" title="hibernate criteria to sql translation" target="_blank">hibernate criteria to sql translation</a></p> Tue, 20 Nov 2012 19:26:56 +0800 http:/wp.fungo.me/java-2/retrieve-sql-query-from-criteria-or-hql.html http:/wp.fungo.me/java-2/retrieve-sql-query-from-criteria-or-hql.html criteria hibernate HQL SQL java