AWS – Disk / VM Export

Czasami potrzebujemy przenieść maszynę wirtualną znajdującą się w chmurze do innego providera chmury, lub wręcz do on-prem. Chociażby po-to, aby mieć backup. O ile w Microsoft Azure sprawa jest banalnie proste – mamy opcje Disk Export i to nawet z GUI. O tyle w przypadku AWS, musimy skorzystać z CLI, za pomocą komendy:

aws ec2 create-instance-export-task –instance-id i-0fe640735a4f8f09b –target-environment microsoft –export-to-s3-task DiskImageFormat=vhd,ContainerFormat=ova,S3Bucket=psensedata,S3Prefix=prefix

Oczywiście wcześniej musimy utworzyć Storage S3 i nadać uprawnienia (The destination bucket must exist and grant WRITE and READ_ACP permissions to the AWS account vm-import-export@amazon.com).

I tutaj możemy spotkać małą niespodziankę, ponieważ może się zdarzyć, iż otrzymamy komunikat:

An error occurred (NotExportable) when calling the CreateInstanceExportTask operation: Only certain instances can be exported. See VM Export documentation for details.

W przypadku Amazon możemy exportować tylko dyski, które wcześniej były zaimportowane do chmury AWS.

Jak sobie poradzić z problemem?

  1. Utworzyć nowy dysk i podłączyć do maszyny wirtualnej
  2. Wykonać kopie sektor po sektorze w przypadku Windows, za pomocą oprogramowania Disk2VHD (omawiane dokładnie tutaj), natomiast w przypadku Linux za pomocą np. dd.
  3. Odegrać tak stworzony image
  4. Zaimportować wspomniany Image (raw) i wyeksportować do jakiegoś bardziej strawnego formatu.

A jak to wykonać po kolei?

Instalujemy AWS CLI i od razu konfigurujemy wydając komendę:

aws configure

AWS Access Key ID oraz AWS Secret Access Key tworzymy na koncie uzytkownika https://console.aws.amazon.com/iam/home#/users w zakładce Security Credentials.

I następnie, wykonujemy komendy – wyświetlenie istniejących instancji typu compute:

aws ec2 describe-instances

Utworzenie nowego dysku o wielkości 32 GB:

aws ec2 create-volume –-size 32 –-region us-east-2 –-availability-zone us-east-2b –-volume-type gp2

Wyświetlenie dysku:

aws ec2 describe-volumes

Przypisanie dysku do instancji:

aws ec2 attach-volume –-volume-id vol-049ddd01841d8c8e4 –-instance-id i-0fe640735a4f8f09b –-device /dev/sdf

Oczywiście volume-id oraz instance-id jest odpowiednio wcześniej odczytane.

Teraz już logujemy się do maszyny wirtualnej i wykonujemy kontrolny restart, a następnie tworzymy partycje, zakładamy filesystem i montujemy (przykład freebsd – dla linux tylko prościej):

geom disk list

gpart show -l

gpart create -s GPT xbd5

gpart add -t freebsd-ufs xbd5

newfs -U /dev/xbd5p1

mount /dev/xbd5p1 /mnt

I wykonujemy kopie sektor po sektorze:

dd if=/dev/ada0 of=/mnt/disk.img

Aby nieco przyspieszyć możemy dodać bs=100M.

Tak przygotowany image musimy dostosować, do bardziej strawnego formatu, aby się z niego zabootować – i tutaj korzystamy albo z Open Stack, albo z VirtualBox.

Poniżej screenshot z uruchomionej instancji pfSense na lokalnym Oracle VirtualBox, z wcześniej wyeksportowanego w wyżej wymieniony sposób dysku.

Import dysku możliwy jest po wykonaniu komendy:

vboxmanage internalcommands createrawvmdk -filename “disk.vmdk” -rawdisk “disk.raw”

Inna opcja migracji z AWS to użycie Azure Site Recovery (co nie raz było omawiane, ale tylko dla wspieranych systemów operacyjnych i nie freebsd) lub nietestowanego jeszcze przeze mnie rozwiązania w AWS typu https://vmplace.eu/Image/azurezilla—physical-virtual-to-azure-converter.

Sometimes we need to move a virtual machine located in the cloud to another cloud provider, or even to on-prem (e.g. for a backup purpose). In Microsoft Azure it is very simple – we have Disk Export in GUI too. In the case of AWS, we need to use the CLI, using the command:

aws ec2 create-instance-export-task –instance-id i-0fe640735a4f8f09b –target-environment microsoft –export-to-s3-task DiskImageFormat=vhd,ContainerFormat=ova,S3Bucket=psensedata,S3Prefix=prefix

Oczywiście wcześniej musimy utworzyć Storage S3 i nadać uprawnienia (The destination bucket must exist and grant WRITE and READ_ACP permissions to the AWS account vm-import-export@amazon.com).

Here we can meet a small surprise, because it may happen that we receive a message:

An error occurred (NotExportable) when calling the CreateInstanceExportTask operation: Only certain instances can be exported. See VM Export documentation for details.

In the case of Amazon, we can only export disks that were previously imported to the AWS cloud.

How to deal with the problem?

  1. Create a new disk and connect to the virtual machine
  2. Make a sector-by-sector copy (for Windows using Disk2VHD), and for Linux, for example, dd.
  3. Copy a created image
  4. Import the mentioned Image (raw) and export to some more digestible format.

Step by Step:

Install AWS CLI and configure it using:

aws configure

AWS Access Key ID and AWS Secret Access Key can be created on the user’s account page https://console.aws.amazon.com/iam/home#/users at the Security Credentials tab.

Next run command to display compute instances:

aws ec2 describe-instances

Create new 32 GB disk:

aws ec2 create-volume –-size 32 –-region us-east-2 –-availability-zone us-east-2b –-volume-type gp2

Display Disk:

aws ec2 describe-volumes

Assign disk to the instance:

aws ec2 attach-volume –-volume-id vol-049ddd01841d8c8e4 –-instance-id i-0fe640735a4f8f09b –-device /dev/sdf

Of course, volume-id and instance-id should be taken from above commands.

Now we can log in to the virtual machine and perform a control restart, then we create partitions, create filesystem and mount it (example freebsd – for linux it is simpler):

geom disk list

gpart show -l

gpart create -s GPT xbd5

gpart add -t freebsd-ufs xbd5

newfs -U /dev/xbd5p1

mount /dev/xbd5p1 /mnt

And make sector by sector copy:

dd if=/dev/ada0 of=/mnt/disk.img

We can add bs=100M to speed up the process.

This image we can upload to Open Stack or use it with Virtual Box. Disk import can be done after:

vboxmanage internalcommands createrawvmdk -filename “disk.vmdk” -rawdisk “disk.raw”

Another option of migrating from AWS is the use of Azure Site Recovery (which has been discussed more than once, but only for supported operating systems and not freebsd) or use untested solution in AWS like https://vmplace.eu/Image/azurezilla—physical-virtual-to-azure-converter.