xen-drbd

annotate xen-drbd-install.py @ 0:b6dc3480caae

xen-drbd merge
author igor
date Thu Sep 27 18:56:29 2007 +0300 (2007-09-27)
parents
children afe3c0992ec3
rev   line source
igor@0 1 #!/usr/bin/python
igor@0 2
igor@0 3 import sys
igor@0 4
igor@0 5 network=sys.argv[1]
igor@0 6 try:
igor@0 7 exec 'from %s import domains,disk_table,lvm_vg_name,node1,node1_ip,node2,node2_ip, \
igor@0 8 ip_network, domain_name, ip_nameserver, ip_gateway, apt_get_install, \
igor@0 9 debian_release, debian_mirror, \
igor@0 10 bridges, vlans, management_vlan, trunk, management_ip, management_gw ' % (network)
igor@0 11 except:
igor@0 12 print "Can't find or interpret module %s with topology description" %(network)
igor@0 13 sys.exit(1)
igor@0 14
igor@0 15 drbd_base_port=7790
igor@0 16 meta_disk='/dev/%s/meta'%lvm_vg_name
igor@0 17 drbd_devices=[]
igor@0 18
igor@0 19 def make_lvm(do_print=1):
igor@0 20 if do_print:
igor@0 21 print "lvcreate -L %s -n %s /dev/%s" % ("10G", "meta", lvm_vg_name)
igor@0 22 for dom in domains:
igor@0 23 for disk in disk_table[dom]:
igor@0 24 if disk.find(":") == -1:
igor@0 25 if do_print:
igor@0 26 print "lvcreate -L %s -n %s /dev/%s" % (disk, dom, lvm_vg_name)
igor@0 27 drbd_devices.append([dom, "/dev/%s/%s" % (lvm_vg_name, dom)])
igor@0 28 else:
igor@0 29 if do_print:
igor@0 30 print "lvcreate -L %s -n %s /dev/%s" % (disk.split(':')[1], disk.split(':')[0], lvm_vg_name)
igor@0 31 drbd_devices.append([disk.split(':')[0], "/dev/%s/%s" % (lvm_vg_name, disk.split(':')[0])])
igor@0 32
igor@0 33
igor@0 34 def make_drbdconf():
igor@0 35 print "cat <<DRBD > /etc/drbd.conf"
igor@0 36 i=0
igor@0 37 for drbd in drbd_devices:
igor@0 38 resource=drbd[0]
igor@0 39 disk=drbd[1]
igor@0 40 drbd_number=i
igor@0 41 i+=1
igor@0 42 print """
igor@0 43 resource %s {
igor@0 44 protocol C;
igor@0 45 net {
igor@0 46 allow-two-primaries;
igor@0 47 after-sb-0pri discard-least-changes;
igor@0 48 after-sb-1pri call-pri-lost-after-sb;
igor@0 49 after-sb-2pri call-pri-lost-after-sb;
igor@0 50 }
igor@0 51 syncer {
igor@0 52 rate 5M;
igor@0 53 }
igor@0 54 on %s
igor@0 55 {
igor@0 56 device /dev/drbd%s;
igor@0 57 disk %s;
igor@0 58 address %s:%s;
igor@0 59 meta-disk %s[%s];
igor@0 60 }
igor@0 61 on %s
igor@0 62 {
igor@0 63 device /dev/drbd%s;
igor@0 64 disk %s;
igor@0 65 address %s:%s;
igor@0 66 meta-disk %s[%s];
igor@0 67 }
igor@0 68 }
igor@0 69
igor@0 70 """ % (resource, node1, drbd_number, disk, node1_ip, i+drbd_base_port, meta_disk, drbd_number, node2, drbd_number, disk, node2_ip, i+drbd_base_port, meta_disk, drbd_number )
igor@0 71 print "DRBD"
igor@0 72
igor@0 73
igor@0 74 def make_drbd_links():
igor@0 75 make_lvm(0)
igor@0 76 print "mkdir -p /dev/drbd"
igor@0 77 i=0
igor@0 78 for drbd in drbd_devices:
igor@0 79 drbd_number=i
igor@0 80 i+=1
igor@0 81 resource=drbd[0]
igor@0 82 disk=drbd[1]
igor@0 83 print "ln -sf /dev/drbd%s /dev/drbd/%s" %(drbd_number, resource)
igor@0 84
igor@0 85 def make_fs():
igor@0 86 for dom in domains:
igor@0 87 print "mkfs.ext3 /dev/drbd/"+dom
igor@0 88
igor@0 89
igor@0 90 def make_mount():
igor@0 91 print "mkdir -p /domU"
igor@0 92 for dom in domains:
igor@0 93 print "mkdir -p /domU/%s" % (dom)
igor@0 94 print "mount /dev/drbd/%s /domU/%s" % (dom,dom)
igor@0 95
igor@0 96 def make_umount():
igor@0 97 for dom in domains:
igor@0 98 print "umount /domU/%s" % (dom)
igor@0 99
igor@0 100 def make_dns():
igor@0 101 i=1
igor@0 102 for dom in domains:
igor@0 103 print "%s A %s.%s" % (dom, ip_network, i)
igor@0 104 i+=1
igor@0 105
igor@0 106 def make_dns_reverse():
igor@0 107 i=1
igor@0 108 for dom in domains:
igor@0 109 print "%s A %s.%s." % (i, dom, domain_name)
igor@0 110 i+=1
igor@0 111
igor@0 112 def make_install_debian():
igor@0 113 print """
igor@0 114 debootstrap %s /domU/%s %s
igor@0 115 chroot /domU/%s apt-get install -y --force-yes %s
igor@0 116 """ % (debian_release,domains[0],debian_mirror,domains[0],apt_get_install)
igor@0 117
igor@0 118 def make_rsync():
igor@0 119 for dom in domains[1:]:
igor@0 120 print """
igor@0 121 rsync -a /domU/%s/ /domU/%s/
igor@0 122 """ % (domains[0], dom)
igor@0 123
igor@0 124 def make_etcfstab():
igor@0 125 i=0
igor@0 126 for dom in domains:
igor@0 127 i+=1
igor@0 128 print """
igor@0 129 cat <<FSTAB > /domU/%s/etc/fstab
igor@0 130 proc /proc proc defaults 0 0
igor@0 131 /dev/hda1 / ext3 defaults,errors=remount-ro 0 1
igor@0 132 FSTAB
igor@0 133 """ % (dom)
igor@0 134
igor@0 135 def make_etcnetworkinterfaces():
igor@0 136 i=0
igor@0 137 for dom in domains:
igor@0 138 i+=1
igor@0 139 print """
igor@0 140 cat <<INTERFACES > /domU/%s/etc/network/interfaces
igor@0 141 auto lo eth0
igor@0 142 iface lo inet loopback
igor@0 143
igor@0 144 iface eth0 inet static
igor@0 145 address %s.%s
igor@0 146 netmask 255.255.255.0
igor@0 147 network %s.0
igor@0 148 broadcast %s.255
igor@0 149 gateway %s
igor@0 150 dns-nameservers %s
igor@0 151 dns-search %s
igor@0 152 INTERFACES
igor@0 153 """ % (dom,ip_network,i,ip_network, ip_network, ip_gateway, domain_name, ip_nameserver)
igor@0 154
igor@0 155 def make_etcresolvconf():
igor@0 156 for dom in domains:
igor@0 157 print """
igor@0 158 cat <<RESOLVCONF > /domU/%s/etc/resolv.conf
igor@0 159 search %s
igor@0 160 nameserver %s
igor@0 161 RESOLVCONF
igor@0 162 """ % (dom,domain_name,ip_nameserver)
igor@0 163
igor@0 164 def make_etchostname():
igor@0 165 for dom in domains:
igor@0 166 print """
igor@0 167 cat <<HOSTNAME > /domU/%s/etc/hostname
igor@0 168 %s
igor@0 169 HOSTNAME
igor@0 170 """ % (dom,dom)
igor@0 171
igor@0 172
igor@0 173 def make_etchosts():
igor@0 174 i=0
igor@0 175 for dom in domains:
igor@0 176 i+=1
igor@0 177 print """
igor@0 178 cat <<HOSTS > /domU/%s/etc/hosts
igor@0 179 127.0.0.1 localhost
igor@0 180 192.168.1.%s %s.%s %s
igor@0 181
igor@0 182 ::1 ip6-localhost ip6-loopback
igor@0 183 fe00::0 ip6-localnet
igor@0 184 ff00::0 ip6-mcastprefix
igor@0 185 ff02::1 ip6-allnodes
igor@0 186 ff02::2 ip6-allrouters
igor@0 187 ff02::3 ip6-allhosts
igor@0 188 HOSTS
igor@0 189 """ % (dom,i,dom,domain_name,dom)
igor@0 190
igor@0 191 def make_rootssh():
igor@0 192 for dom in domains:
igor@0 193 print """
igor@0 194 mkdir -p /domU/%s/root/.ssh/
igor@0 195 cp ~/.ssh/id_dsa.pub /domU/%s/root/.ssh/authorized_keys
igor@0 196 chmod 600 /domU/%s/root/.ssh/authorized_keys
igor@0 197 """ % (dom,dom,dom)
igor@0 198
igor@0 199 def make_l3rc():
igor@0 200 for dom in domains:
igor@0 201 print """
igor@0 202 cat <<EOF > /domU/%s/root/.l3rc
igor@0 203 l3cd=/users/igor/xen-drbd-dom0-setup/%s.%s/root
igor@0 204 EOF
igor@0 205 """ % (dom,dom,domain_name)
igor@0 206
igor@0 207
igor@0 208 def make_etc():
igor@0 209 make_etcfstab()
igor@0 210 make_etcnetworkinterfaces()
igor@0 211 make_etcresolvconf()
igor@0 212 make_etchosts()
igor@0 213 make_etchostname()
igor@0 214 make_rootssh()
igor@0 215 make_l3rc()
igor@0 216
igor@0 217 def make_all_system():
igor@0 218 make_drbd_links()
igor@0 219 make_fs()
igor@0 220 make_mount()
igor@0 221 make_install_debian()
igor@0 222 make_rsync()
igor@0 223 make_etc()
igor@0 224 make_umount()
igor@0 225
igor@0 226 def get_management_ip():
igor@0 227 return management_ip
igor@0 228
igor@0 229 def get_default_gateway():
igor@0 230 return management_gw
igor@0 231
igor@0 232 def make_bridges():
igor@0 233 ip=get_management_ip()
igor@0 234 gw=get_default_gateway()
igor@0 235 print "ifconfig %s 0.0.0.0" % (trunk)
igor@0 236 for bridge in bridges:
igor@0 237 vlan=vlans[bridges.index(bridge)]
igor@0 238 print "vconfig add %s %s" % (trunk,vlan)
igor@0 239 print "/etc/xen/scripts/network-bridge start vifnum=%s bridge=%s netdev=%s.%s" % (bridges.index(bridge)+2,bridge,trunk,vlan)
igor@0 240 print "ifconfig %s.%s %s" % (trunk,management_vlan,management_ip)
igor@0 241 if management_gw:
igor@0 242 print "route add default gw %s" % (management_gw)
igor@0 243
igor@0 244
igor@0 245 def show_usage():
igor@0 246 print """
igor@0 247 Usage:
igor@0 248 xen-drbd-install <network> <command> (to view)
igor@0 249 xen-drbd-install <network> <command> | sh -s (to run)
igor@0 250
igor@0 251 <network> is a name of the file, which contains network description
igor@0 252
igor@0 253 Commands:
igor@0 254 make-all
igor@0 255 make-bridges
igor@0 256 make-links
igor@0 257 """
igor@0 258
igor@0 259 #make_all_system()
igor@0 260
igor@0 261 if len(sys.argv) > 1:
igor@0 262 command = sys.argv[2]
igor@0 263 if command == 'make-all':
igor@0 264 make_all_system()
igor@0 265 if command == 'make-bridges':
igor@0 266 make_bridges()
igor@0 267 elif command == 'make-links':
igor@0 268 make_drbd_links()
igor@0 269 else:
igor@0 270 show_usage()
igor@0 271 else:
igor@0 272 show_usage()
igor@0 273