Translate

Tuesday, August 19, 2014

Creating a CentOS 7 instance on Google Cloud



This post is to show how to create a CentOS 7 virtual machine on an existing CentOS 7 system and push it to the Google Cloud as an image to be used to create instances.

Note: Kudos to the folks over at DoIt! for insight gathered from their how-to for an ubuntu image (http://doit-intl.com/blog/2014/5/31/how-to-install-ubuntu-server-on-gce)

Assumptions:

  1. You have a Google cloud account and have created a project and a storage bucket
  2. You have an accessible CentOS 7 system that has virtualization-hypervisor capabilities
  3. You have virt-manager installed to manage virtual machines on that box.
Start virt-manager and createa new VM from a centos7 box:



I used the CentOS 7 Everything DVD ISO. You can probably use one of the others as well. I was just being complete:



You want to create a new storage volume
Select to “Customize configuration before install”
Also select the appropriate interface so you will be able to access your update repository. In my case, I am bridging devices through virbr0:



and ensure it is at least 10gb in size.



Select to “Customize configuration before install”
Also select the appropriate interface so you will be able to access your update repository:



Change the VirtIO Disk bus to be SATA:



You also want to ensure the Device model for your NIC is “virtio”:



Begin installation,

Set your timezone to Region: Etc and City: Coordinated Universal Time:



Add “metadata.google.internal” as an NTP server and select it as the only one to use.






For the system installation destination, create a SINGLE partition mounted at /





It will give you a warning about not having swap defined. This is fine.




It is not advised to add users at this time. Remember this is a TEMPLATE image, so if you add users to it, they will be active in ALL SUBSEQUENT IMAGES created from it; Probably not a good idea.

Reboot and log in the console so we can activate the nic.Once you are logged in, enable eth0 to come up on boot and restart the network services:

Once you are logged in, enable eth0 to come up on boot and restart the network services:

sed -i ‘s/ONBOOT=no/ONBOOT=yes/’ /etc/sysconfig/network-scripts/ifcfg-eth0
systemctl restart network.service

You can now ssh to your vm if you prefer.

Now do your updates and install any packages you wish to have on ALL subsequent VMs that will be created from this image:
yum –y install rsync
yum -y update
Now we need to add a few packages from google to give us the tools to build our cloud image:

yum install \
https://github.com/GoogleCloudPlatform/compute-image-packages/releases/download/1.1.6/gcimagebundle-1.1.6-1.noarch.rpm \
https://github.com/GoogleCloudPlatform/compute-image-packages/releases/download/1.1.6/google-compute-daemon-1.1.6-1.noarch.rpm \
https://github.com/GoogleCloudPlatform/compute-image-packages/releases/download/1.1.6/google-startup-scripts-1.1.6-1.noarch.rpm

Our image needs a few tweaks to fit the expectations of the hypervisor:

#Configure to get hostname from DHCP
rm -f rm /etc/hostname
ln -s /usr/share/google/set-hostname /etc/dhcp/dhclient.d/

#Configure serial terminal
echo '# ttyS0 - getty
start on stopped rc or RUNLEVEL=[2345]
stop on runlevel [!2345]
respawn
exec /sbin/getty -L 115200 ttyS0 vt102'>/etc/init/ttyS0.conf

#Setup grub options for Serial TTY and regenerate
sed -i 's/^GRUB_CMDLINE_LINUX/#GRUB_CMDLINE_LINUX/' /etc/default/grub
echo 'GRUB_CMDLINE_LINUX="console=ttyS0,115200n8 ignore_loglevel"
GRUB_SERIAL_COMMAND="serial --speed=115200 --unit=0 --word=8 --parity=no --stop=1"' >> /etc/default/grub
grub2-mkconfig  -o /boot/grub2/grub.cfg

#This is a trigger for GCE
echo "GOOGLE" > /etc/ssh/sshd_not_to_be_run

# remove existing host keys (they will be regenerated in any instance)
rm -f /etc/ssh/ssh_host*

#Add some needed sysctl options
echo '# provides protection from ToCToU races
fs.protected_hardlinks=1
# provides protection from ToCToU races
fs.protected_symlinks=1
# makes locating kernel addresses more difficult
kernel.kptr_restrict=1
# set ptrace protections
kernel.yama.ptrace_scope=1
# set perf only available to root
kernel.perf_event_paranoid=2
# disable ipv6
net.ipv6.conf.all.disable_ipv6=1
net.ipv6.conf.default.disable_ipv6=1
net.ipv6.conf.lo.disable_ipv6=1'>/etc/sysctl.d/12-gce-recommended.conf

We need to get our google SDK implemented so we will be able to connect to our project on the cloud:
curl https://sdk.cloud.google.com | bash

We need to login to our project:
gcloud auth login

Let’s build our image:
gcimagebundle -d /dev/sda -r / -o /tmp --loglevel=DEBUG --log_file=/tmp/image_bundle.log

Now we can copy it to one of our storage buckets (use the name of the file created above and the name of your storage bucket):
gsutil cp /tmp/<image_created_above> gs://<storage_bucket>/mycentos-7.tar.gz

And finally, create an image to use:
gcutil addimage centos7 gs://<storage_bucket>/mycentos-7.tar.gz

That's it!
You should now be able to see a new image under your project's Compute->Images list.
You can use this as an image when you select "New disk from image" as a BOOT SOURCE
In our example, the image is named "centos7".

Happy Imaging! Please leave any comments or questions and I will try to help out.

Brian Andrus

2 comments:

  1. file "/etc/init/ttyS0.conf" does not exist in centos 7 ?

    ReplyDelete
    Replies
    1. You had to create it from one of the steps above.
      The section labeled "#Configure serial terminal"

      Also, there is now a CentOS 7 ISO you can use instead. I wrote this before they had published an ISO so there was no way to put CentOS 7 on gcloud at the time.

      Delete