1: #!/bin/bash
2: # Program:
3: # For Proxmox VE (1~3). Convert OpenVZ Container to Template
4: # 在Proxmox VE (1到3版都可以使用)中,將OpenVZ的虛擬機器(Container)轉換成虛擬應用樣板(Template)
5: # History:
6: # 2013/07/13 Pulipuli Chen First release
7:
8: # 歡迎訊息
9: echo "========================================="
10: echo " OpenVZ Container to Template Packager"
11: echo "========================================="
12:
13: # 宣告目錄參數
14: container_dir=/var/lib/vz/private/
15: template_dir=/var/lib/vz/template/cache/
16: if [ ! -d $container_dir ] || [ ! -d $template_dir ]; then
17: echo "This script only for Proxmox VE 1~3"
18: echo "http://www.proxmox.com/downloads/category/iso-images-pve"
19: echo "Abort"
20: exit 0
21: fi
22:
23: # 請輸入要轉換的VMID
24: read -p "Please enter OpenVZ container's VMID: [100] " vmid
25: if [ -z $vmid ]; then
26: vmid=100
27: fi
28:
29: # 回傳訊息,告知使用者要轉換的VMID
30: echo "You want to package container VMID $vmid to template"
31:
32: # 宣告虛擬機器的目錄
33: ct_dir=$container_dir/$vmid
34:
35: # 檢查該VMID的虛擬機器是否存在
36: if [ -d $ct_dir ]; then
37:
38: # 如果存在的話
39:
40: # 詢問作業系統與其版本
41: read -p "Please enter template's OS and version (ex: centos-5, debain-6.0, ubuntu-10.04): [centos-5] " template_os
42: if [ -z $template_os ]; then
43: template_os=centos-5
44: fi
45: until [[ "$template_os" == *"-"* ]]; do
46: echo "Template's OS and version should include '-', ex: centos-5, debain-6.0, ubuntu-10.04. "
47: read -p "Please enter template's OS and version again : [centos-5]" template_os
48: if [ -z $template_os ]; then
49: template_os=centos-5
50: fi
51: done
52:
53: # 詢問應用系統的名字
54: read -p "Please enter template's application name (ex: standard, moodle, dspace-dlll): [custom] " name
55: if [ -z $name ]; then
56: name=custom
57: fi
58: until [[ "$name" != *"_"* ]]; do
59: echo "Template's application name should NOT include '_', ex: standard, moodle, dspace-dlll."
60: read -p "Please enter template's application name again : [custom] " name
61: if [ -z $name ]; then
62: name=custom
63: fi
64: done
65:
66: # 詢問應用系統的版本號
67: read -p "Please enter template's application version (ex: 5.6-1, 10.04-4): [1.0-0] " version
68: if [ -z $version ]; then
69: version=1.0-0
70: fi
71: until [[ "$version" == *"."*"-"* ]]; do
72: echo "Template's application version should include '.' and '-', ex: 5.6-1, 10.04-4."
73: read -p "Please enter template's application version again : [1.0-0] " version
74: if [ -z $version ]; then
75: version=1.0-0
76: fi
77: done
78:
79: # 詢問樣板的架構
80: read -p "Please enter template's architecture (arch.) (ex: i386): [i386] " template_arch
81: if [ -z $template_arch ]; then
82: template_arch=i386
83: fi
84:
85: template_tar=${template_os}-${name}_${version}_${template_arch}.tar.gz
86:
87: # 確認轉換項目
88: read -p "Do you wish to package container VMID $vmid to template $template_tar? [Y/n] " RESP
89: if [ "$RESP" = "n" ]; then
90: echo "Abort"
91: exit 0
92: fi
93:
94: # 檢查是否有同樣名稱的樣板
95: template_path=${template_dir}${template_tar}
96: if [ -d $template_path ]; then
97: read -p "${template_tar} existed. Do you wish to overwrite it? [Y/n] " overwrite
98: if [ "$overwrite" = "n" ]; then
99: echo "Abort"
100: exit 0
101: fi
102: fi
103:
104: # 停止運作中的虛擬機器
105: echo "Stop VMID $vmid..."
106: vzctl stop $vmid
107:
108: # 開始進行轉換
109: echo "Start to package VMID $vmid to template $template_tar ..."
110: cd $ct_dir
111: tar -czvf --overwrite ${template_path} ./
112:
113: # 完成訊息
114: echo "Package complete!"
115: echo "You can use template $template_tar in your Proxmox VE now."
116: echo "Template location path is $template_path"
117: else
118:
119: # 如果沒有該虛擬機器,則停止腳本檔
120: echo "VMID $vmid not exist"
121: echo "Abort"
122: fi
Comments