Class: BoxGrinder::XenServerPlugin

Inherits:
BasePlugin
  • Object
show all
Defined in:
lib/xenserver-platform-plugin.rb

Instance Method Summary collapse

Instance Method Details

#after_initObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/xenserver-platform-plugin.rb', line 32

def after_init
  if %w{fedora rhel centos}.include? @appliance_config.os.name
    register_deliverable(:disk => "#{@appliance_config.name}.raw")
  end
  register_deliverable(:vhd => "#{@appliance_config.name}.vhd")

  register_supported_os('fedora', ['13', '14', '15', '16'])
  register_supported_os('centos', ['5', '6'])
  register_supported_os('sl', ['5', '6'])
  register_supported_os('rhel', ['5', '6'])
  register_supported_os('ubuntu', ['lucid', 'oneiric', 'precise', 'maveric'])
  if `which VBoxManage`.empty?
    @log.error "VBoxManage binary not found in your path, aborting."
    exit 1
  end
end

#change_configuration(guestfs_helper) ⇒ Object



233
234
235
236
237
238
239
240
241
# File 'lib/xenserver-platform-plugin.rb', line 233

def change_configuration(guestfs_helper)
  guestfs_helper.augeas do
    # disable password authentication
    set("/etc/ssh/sshd_config", "PasswordAuthentication", "no")

    # disable root login
    set("/etc/ssh/sshd_config", "PermitRootLogin", "without-password")
  end
end

#convert_to_vhdObject



61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/xenserver-platform-plugin.rb', line 61

def convert_to_vhd
  @log.info "Converting to VMDK Sparse using qemu-img..."
  if @appliance_config.os.name == 'ubuntu'
    @exec_helper.execute "qemu-img convert -O vmdk '#{@previous_deliverables.disk}' '#{@deliverables.vhd}.vmdk'"
    @log.info "Converting to VHD using VBoxManage..."
    @exec_helper.execute "VBoxManage clonehd --format VHD '#{@deliverables.vhd}.vmdk' '#{@deliverables.vhd}'"
    FileUtils.rm_f "#{@deliverables.vhd}.vmdk"
  else
    @exec_helper.execute "qemu-img convert -O vmdk '#{@deliverables.disk}' '#{@deliverables.disk}.vmdk'"
    @log.info "Converting to VHD using VBoxManage..."
    @exec_helper.execute "VBoxManage clonehd --format VHD '#{@deliverables.disk}.vmdk' '#{@deliverables.vhd}'"
    FileUtils.rm_f "#{@deliverables.disk}.vmdk"
  end
end

#create_devices(guestfs) ⇒ Object



129
130
131
132
133
134
135
136
137
# File 'lib/xenserver-platform-plugin.rb', line 129

def create_devices(guestfs)
  return if guestfs.exists('/sbin/MAKEDEV') == 0

  @log.debug "Creating required devices..."
  guestfs.sh("/sbin/MAKEDEV -d /dev -x console")
  guestfs.sh("/sbin/MAKEDEV -d /dev -x null")
  guestfs.sh("/sbin/MAKEDEV -d /dev -x zero")
  @log.debug "Devices created."
end

#disk_device_prefixObject



139
140
141
142
143
144
# File 'lib/xenserver-platform-plugin.rb', line 139

def disk_device_prefix
  disk = 'xv'
  disk = 's' if (@appliance_config.os.name == 'rhel' or @appliance_config.os.name == 'centos') and @appliance_config.os.version == '5'

  disk
end

#enable_networking(guestfs) ⇒ Object

enable networking on default runlevels



193
194
195
196
197
198
# File 'lib/xenserver-platform-plugin.rb', line 193

def enable_networking(guestfs)
  @log.debug "Enabling networking..."
  guestfs.sh("/sbin/chkconfig network on")
  guestfs.upload("#{File.dirname(__FILE__)}/src/ifcfg-eth0", "/etc/sysconfig/network-scripts/ifcfg-eth0")
  @log.debug "Networking enabled."
end

#enable_nosegneg_flag(guestfs) ⇒ Object

This fixes issues with Fedora 14 on EC2: bugzilla.redhat.com/show_bug.cgi?id=651861#c39



185
186
187
188
189
190
# File 'lib/xenserver-platform-plugin.rb', line 185

def enable_nosegneg_flag(guestfs)
  @log.debug "Enabling nosegneg flag..."
  guestfs.sh("echo \"hwcap 1 nosegneg\" > /etc/ld.so.conf.d/libc6-xen.conf")
  guestfs.sh("/sbin/ldconfig")
  @log.debug "Nosegneg enabled."
end

#executeObject



49
50
51
52
53
54
55
# File 'lib/xenserver-platform-plugin.rb', line 49

def execute
  if %w{fedora rhel centos}.include? @appliance_config.os.name
    execute_rpmdistro
  else
    execute_ubuntu
  end
end

#execute_post(guestfs_helper) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
# File 'lib/xenserver-platform-plugin.rb', line 117

def execute_post(guestfs_helper)
  unless @appliance_config.post['xenserver'].nil?
    @appliance_config.post['xenserver'].each do |cmd|
      @log.debug "Executing xenserver post command #{cmd}"
      guestfs_helper.sh(cmd, :arch => @appliance_config.hardware.arch)
    end
    @log.debug "Post xenserver commands from appliance definition file executed."
  else
    @log.debug "No xenserver commands specified, skipping."
  end
end

#execute_rpmdistroObject



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/xenserver-platform-plugin.rb', line 76

def execute_rpmdistro
  @linux_helper = LinuxHelper.new(:log => @log)

  @log.info "Converting #{@appliance_config.name} appliance image to XenServer format..."

  @image_helper.create_disk(@deliverables.disk, 10) # 10 GB destination disk

  @image_helper.customize([@previous_deliverables.disk, @deliverables.disk], :automount => false) do |guestfs, guestfs_helper|
    @image_helper.sync_filesystem(guestfs, guestfs_helper)
    
    @log.debug "Uploading '/etc/resolv.conf'..."
    guestfs.upload("/etc/resolv.conf", "/etc/resolv.conf")
    @log.debug "'/etc/resolv.conf' uploaded."

    if (@appliance_config.os.name == 'rhel' or @appliance_config.os.name == 'centos') and @appliance_config.os.version == '5'
      # Remove normal kernel
      guestfs.sh("yum -y remove kernel")
      # because we need to install kernel-xen package
      guestfs_helper.sh("yum -y install kernel-xen", :arch => @appliance_config.hardware.arch)
      # and add require modules
      @linux_helper.recreate_kernel_image(guestfs, ['xenblk', 'xennet'])
    end

    create_devices(guestfs)

    upload_fstab(guestfs)
    enable_networking(guestfs)
    upload_rc_local(guestfs)
    #change_configuration(guestfs_helper)
    install_menu_lst(guestfs)

    enable_nosegneg_flag(guestfs) if @appliance_config.os.name == 'fedora'

    execute_post(guestfs_helper)
  end

  convert_to_vhd

  @log.info "Image converted to XenServer format."
end

#execute_ubuntuObject



57
58
59
# File 'lib/xenserver-platform-plugin.rb', line 57

def execute_ubuntu
  convert_to_vhd
end

#install_menu_lst(guestfs) ⇒ Object



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/xenserver-platform-plugin.rb', line 166

def install_menu_lst(guestfs)
  @log.debug "Uploading '/boot/grub/menu.lst' file..."
  menu_lst_data = File.open("#{File.dirname(__FILE__)}/src/menu.lst").read

  menu_lst_data.gsub!(/#TITLE#/, @appliance_config.name)
  menu_lst_data.gsub!(/#KERNEL_VERSION#/, @linux_helper.kernel_version(guestfs))
  menu_lst_data.gsub!(/#KERNEL_IMAGE_NAME#/, @linux_helper.kernel_image_name(guestfs))

  menu_lst = Tempfile.new('menu_lst')
  menu_lst << menu_lst_data
  menu_lst.flush

  guestfs.upload(menu_lst.path, "/boot/grub/menu.lst")

  menu_lst.close
  @log.debug "'/boot/grub/menu.lst' file uploaded."
end

#upload_fstab(guestfs) ⇒ Object



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/xenserver-platform-plugin.rb', line 146

def upload_fstab(guestfs)
  @log.debug "Uploading '/etc/fstab' file..."

  fstab_file = @appliance_config.is64bit? ? "#{File.dirname(__FILE__)}/src/fstab_64bit" : "#{File.dirname(__FILE__)}/src/fstab_32bit"

  fstab_data = File.open(fstab_file).read
  fstab_data.gsub!(/#DISK_DEVICE_PREFIX#/, disk_device_prefix)
  fstab_data.gsub!(/#FILESYSTEM_TYPE#/, @appliance_config.hardware.partitions['/']['type'])

  fstab = Tempfile.new('fstab')
  fstab << fstab_data
  fstab.flush

  guestfs.upload(fstab.path, "/etc/fstab")

  fstab.close

  @log.debug "'/etc/fstab' file uploaded."
end

#upload_rc_local(guestfs) ⇒ Object



200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/xenserver-platform-plugin.rb', line 200

def upload_rc_local(guestfs)
  @log.debug "Uploading '/etc/rc.d/rc.local' file..."
  rc_local = Tempfile.new('rc_local')

  if guestfs.exists("/etc/rc.d/rc.local") == 1
    # We're appending
    rc_local << guestfs.read_file("/etc/rc.d/rc.local")
  else
    # We're creating new file
    rc_local << "#!/bin/bash\n\n"
  end

  rc_local << File.read("#{File.dirname(__FILE__)}/src/rc_local")
  rc_local.flush

  guestfs.upload(rc_local.path, "/etc/rc.d/rc.local")

  rc_local.close

  # Fedora 16 doesn't have /etc/rc.local file and we need to
  # enable rc.local compatibility with systemd
  # We need to make sure that network is available when executing rc.local
  if (@appliance_config.os.name == 'fedora' and @appliance_config.os.version >= '16')
    guestfs.cp("/lib/systemd/system/rc-local.service", "/etc/systemd/system/")
    guestfs.sh("sed -i '/^ConditionFileIsExecutable/a After=network.target' /etc/systemd/system/rc-local.service")
    guestfs.sh("systemctl enable rc-local.service")
    guestfs.ln_sf("/etc/rc.d/rc.local", "/etc/rc.local")
    guestfs.chmod(0755, "/etc/rc.d/rc.local")
  end

  @log.debug "'/etc/rc.d/rc.local' file uploaded."
end