xen-drbd

annotate xen-drbd-install.py @ 5:1c7442c793a1

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