Skip to content

Commit 04c355d

Browse files
authored
Merge pull request #197413 from cynthn/prepare-linux
Linux prepare doc
2 parents 16774b4 + 4426bfe commit 04c355d

File tree

4 files changed

+199
-49
lines changed

4 files changed

+199
-49
lines changed

articles/virtual-machines/generalize.md

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ ms.service: virtual-machines
66
ms.subservice: imaging
77
ms.workload: infrastructure-services
88
ms.topic: how-to
9-
ms.date: 03/24/2022
9+
ms.date: 05/13/2022
1010
ms.author: cynthn
1111
ms.custom: portal
1212

@@ -21,6 +21,20 @@ Generalizing removes machine specific information so the image can be used to cr
2121

2222
## Linux
2323

24+
Distribution specific instructions for preparing Linux images for Azure are available here:
25+
- [Generic steps](./linux/create-upload-generic.md)
26+
- [CentOS](./linux/create-upload-centos.md)
27+
- [Debian](./linux/debian-create-upload-vhd.md)
28+
- [Flatcar](./linux/flatcar-create-upload-vhd.md)
29+
- [FreeBSD](./linux/freebsd-intro-on-azure.md)
30+
- [Oracle Linux](./linux/oracle-create-upload-vhd.md)
31+
- [OpenBSD](./linux/create-upload-openbsd.md)
32+
- [Red Hat](./linux/redhat-create-upload-vhd.md)
33+
- [SUSE](./linux/suse-create-upload-vhd.md)
34+
- [Ubuntu](./linux/create-upload-ubuntu.md)
35+
36+
The following instructions only cover setting the VM to generalized. We recommend you follow the distro specific instructions for production workloads.
37+
2438
First you'll deprovision the VM by using the Azure VM agent to delete machine-specific files and data. Use the `waagent` command with the `-deprovision+user` parameter on your source Linux VM. For more information, see the [Azure Linux Agent user guide](./extensions/agent-linux.md). This process can't be reversed.
2539

2640
1. Connect to your Linux VM with an SSH client.
@@ -51,7 +65,7 @@ az vm generalize \
5165
--name myVM
5266
```
5367

54-
## Windows
68+
## Windows
5569

5670
Sysprep removes all your personal account and security information, and then prepares the machine to be used as an image. For information about Sysprep, see [Sysprep overview](/windows-hardware/manufacture/desktop/sysprep--system-preparation--overview).
5771

articles/virtual-machines/image-version.md

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ ms.service: virtual-machines
66
ms.subservice: gallery
77
ms.topic: how-to
88
ms.workload: infrastructure
9-
ms.date: 04/26/2022
9+
ms.date: 05/13/2022
1010
ms.author: saraic
1111
ms.reviewer: cynthn
1212
ms.custom:
@@ -43,6 +43,24 @@ Allowed characters for the image version are numbers and periods. Numbers must b
4343

4444
When working through this article, replace the resource names where needed.
4545

46+
For [generalized](generalize.md) images, see the OS specific guidance before capturing the image:
47+
48+
- **Linux**
49+
- [Generic steps](./linux/create-upload-generic.md)
50+
- [CentOS](./linux/create-upload-centos.md)
51+
- [Debian](./linux/debian-create-upload-vhd.md)
52+
- [Flatcar](./linux/flatcar-create-upload-vhd.md)
53+
- [FreeBSD](./linux/freebsd-intro-on-azure.md)
54+
- [Oracle Linux](./linux/oracle-create-upload-vhd.md)
55+
- [OpenBSD](./linux/create-upload-openbsd.md)
56+
- [Red Hat](./linux/redhat-create-upload-vhd.md)
57+
- [SUSE](./linux/suse-create-upload-vhd.md)
58+
- [Ubuntu](./linux/create-upload-ubuntu.md)
59+
60+
- **Windows**
61+
62+
If you plan to run Sysprep before uploading your virtual hard disk (VHD) to Azure for the first time, make sure you have [prepared your VM](./windows/prepare-for-upload-vhd-image.md).
63+
4664
## Community gallery (preview)
4765

4866
> [!IMPORTANT]
@@ -54,7 +72,7 @@ When working through this article, replace the resource names where needed.
5472
5573
If you will be sharing your images using a [community gallery (preview)](azure-compute-gallery.md#community), make sure that you create your gallery, image definitions, and image versions in the same region.
5674

57-
When users search for community gallery images, only the latest version of an image are shown.
75+
When users search for community gallery images, only the latest version of an image is shown.
5876

5977

6078
## Create an image
@@ -110,7 +128,7 @@ You can also capture an existing VM as an image, from the portal. For more infor
110128

111129
Image definitions create a logical grouping for images. They are used to manage information about the image versions that are created within them.
112130

113-
Create an image definition in a gallery using [az sig image-definition create](/cli/azure/sig/image-definition#az-sig-image-definition-create). Make sure your image definition is the right type. If you have generalized the VM (using Sysprep for Windows, or waagent -deprovision for Linux) then you should create a generalized image definition using `--os-state generalized`. If you want to use the VM without removing existing user accounts, create a specialized image definition using `--os-state specialized`.
131+
Create an image definition in a gallery using [az sig image-definition create](/cli/azure/sig/image-definition#az-sig-image-definition-create). Make sure your image definition is the right type. If you have [generalized](generalize.md) the VM (using `waagent -deprovision` for Linux, or Sysprep for Windows) then you should create a generalized image definition using `--os-state generalized`. If you want to use the VM without removing existing user accounts, create a specialized image definition using `--os-state specialized`.
114132

115133
For more information about the parameters you can specify for an image definition, see [Image definitions](shared-image-galleries.md#image-definitions).
116134

articles/virtual-machines/linux/create-upload-generic.md

Lines changed: 161 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
---
2-
title: Create and upload a Linux VHD
3-
description: Learn to create and upload an Azure virtual hard disk (VHD) that contains a Linux operating system.
2+
title: Prepare Linux for imaging
3+
description: Learn to prepare a Linux system to be used for an image in Azure.
44
author: srijang
55
ms.service: virtual-machines
66
ms.collection: linux
77
ms.topic: how-to
8-
ms.date: 11/17/2021
8+
ms.date: 05/13/2022
99
ms.author: srijangupta
1010

1111
---
12-
# Information for Non-endorsed Distributions
12+
# Information for community supported and non-endorsed distributions
1313

1414
**Applies to:** :heavy_check_mark: Linux VMs :heavy_check_mark: Flexible scale sets
1515

@@ -18,17 +18,7 @@ The Azure platform SLA applies to virtual machines running the Linux OS only whe
1818
* [Linux on Azure - Endorsed Distributions](endorsed-distros.md)
1919
* [Support for Linux images in Microsoft Azure](https://support.microsoft.com/kb/2941892)
2020

21-
All distributions running on Azure have a number of prerequisites. This article can't be comprehensive, as every distribution is different. Even if you meet all the criteria below, you may need to significantly tweak your Linux system for it to run properly.
22-
23-
We recommend that you start with one of the [Linux on Azure Endorsed Distributions](endorsed-distros.md). The following articles show you how to prepare the various endorsed Linux distributions that are supported on Azure:
24-
25-
- [CentOS-based Distributions](create-upload-centos.md)
26-
- [Debian Linux](debian-create-upload-vhd.md)
27-
- [Flatcar Container Linux](flatcar-create-upload-vhd.md)
28-
- [Oracle Linux](oracle-create-upload-vhd.md)
29-
- [Red Hat Enterprise Linux](redhat-create-upload-vhd.md)
30-
- [SLES & openSUSE](suse-create-upload-vhd.md)
31-
- [Ubuntu](create-upload-ubuntu.md)
21+
All other, non-Azure Marketplace, distributions running on Azure have a number of prerequisites. This article can't be comprehensive, as every distribution is different. Even if you meet all the criteria below, you may need to significantly tweak your Linux system for it to run properly.
3222

3323
This article focuses on general guidance for running your Linux distribution on Azure.
3424

@@ -47,10 +37,9 @@ This article focuses on general guidance for running your Linux distribution on
4737
7. Don't configure a swap partition on the OS disk. The Linux agent can be configured to create a swap file on the temporary resource disk, as described in the following steps.
4838

4939
8. All VHDs on Azure must have a virtual size aligned to 1 MB (1024 × 1024 bytes). When converting from a raw disk to VHD you must ensure that the raw disk size is a multiple of 1 MB before conversion, as described in the following steps.
40+
9. Use the most up-to-date distribution version, packages, and software.
41+
8. Remove users and system accounts, public keys, sensitive data, unnecessary software and application.
5042

51-
> [!NOTE]
52-
> Make sure **'udf'** (cloud-init >= 21.2) and **'vfat'** modules are enable. Blocklisting the udf module will cause a provisioning failure and backlisting vfat module will cause both provisioning and boot failures. **_Cloud-init < 21.2 are not affected and does not require this change._**
53-
>
5443

5544

5645
### Installing kernel modules without Hyper-V
@@ -73,9 +62,9 @@ The mechanism for rebuilding the initrd or initramfs image may vary depending on
7362
7463
### Resizing VHDs
7564
VHD images on Azure must have a virtual size aligned to 1 MB. Typically, VHDs created using Hyper-V are aligned correctly. If the VHD isn't aligned correctly, you may receive an error message similar to the following when you try to create an image from your VHD.
76-
77-
* The VHD http:\//\<mystorageaccount>.blob.core.windows.net/vhds/MyLinuxVM.vhd has an unsupported virtual size of 21475270656 bytes. The size must be a whole number (in MBs).
78-
65+
```outout
66+
The VHD http:\//\<mystorageaccount>.blob.core.windows.net/vhds/MyLinuxVM.vhd has an unsupported virtual size of 21475270656 bytes. The size must be a whole number (in MBs).
67+
```
7968
In this case, resize the VM using either the Hyper-V Manager console or the [Resize-VHD](/powershell/module/hyper-v/resize-vhd) PowerShell cmdlet. If you aren't running in a Windows environment, we recommend using `qemu-img` to convert (if needed) and resize the VHD.
8069

8170
> [!NOTE]
@@ -160,42 +149,171 @@ The [Azure Linux Agent](../extensions/agent-linux.md) `waagent` provisions a Lin
160149
* In some cases, the Azure Linux Agent may not be compatible with NetworkManager. Many of the RPM/deb packages provided by distributions configure NetworkManager as a conflict to the waagent package. In these cases, it will uninstall NetworkManager when you install the Linux agent package.
161150
* The Azure Linux Agent must be at or above the [minimum supported version](https://support.microsoft.com/en-us/help/4049215/extensions-and-virtual-machine-agent-minimum-version-support).
162151
152+
> [!NOTE]
153+
> Make sure **'udf'** (cloud-init >= 21.2) and **'vfat'** modules are enable. Blocklisting the udf module will cause a provisioning failure and backlisting vfat module will cause both provisioning and boot failures. **_Cloud-init < 21.2 are not affected and does not require this change._**
154+
>
155+
163156
## General Linux System Requirements
164157
165158
1. Modify the kernel boot line in GRUB or GRUB2 to include the following parameters, so that all console messages are sent to the first serial port. These messages can assist Azure support with debugging any issues.
166159
```
167-
console=ttyS0,115200n8 earlyprintk=ttyS0,115200 rootdelay=300
160+
GRUB_CMDLINE_LINUX="rootdelay=300 console=ttyS0 earlyprintk=ttyS0 net.ifnames=0"
168161
```
169162
We also recommend *removing* the following parameters if they exist.
170163
```
171164
rhgb quiet crashkernel=auto
172165
```
173166
Graphical and quiet boot isn't useful in a cloud environment, where we want all logs sent to the serial port. The `crashkernel` option may be left configured if needed, but note that this parameter reduces the amount of available memory in the VM by at least 128 MB, which may be problematic for smaller VM sizes.
174167
168+
2. After you are done editing /etc/default/grub, run the following command to rebuild the grub configuration:
169+
```bash
170+
sudo grub2-mkconfig -o /boot/grub2/grub.cfg
171+
```
172+
1. Add Hyper-V modules both initrd and initramfs instructions (Dracut).
173+
1. Rebuild initrd or initramfs
174+
**Initramfs**
175+
```bash
176+
cp /boot/initramfs-$(uname -r).img /boot/initramfs-[latest kernel version ].img.bak
177+
dracut -f -v /boot/initramfs-[latest kernel version ].img [depending on the version of grub]
178+
grub-mkconfig -o /boot/grub/grub.cfg
179+
grub2-mkconfig -o /boot/grub2/grub.cfg
180+
```
181+
182+
**Initrd**
183+
```bash
184+
mv /boot/[initrd kernel] /boot/[initrd kernel]-old
185+
mkinitrd /boot/initrd.img-[initrd kernel]-generic /boot/[initrd kernel]-generic-old
186+
update-initramfs -c -k [initrd kernel]
187+
update-grub
188+
```
189+
1. Ensure that the SSH server is installed, and configured to start at boot time. This configuration is usually the default.
175190
1. Install the Azure Linux Agent.
176-
177-
The Azure Linux Agent is required for provisioning a Linux image on Azure. Many distributions provide the agent as an RPM or .deb package (the package is typically called WALinuxAgent or walinuxagent). The agent can also be installed manually by following the steps in the [Linux Agent Guide](../extensions/agent-linux.md).
178-
179-
1. Ensure that the SSH server is installed, and configured to start at boot time. This configuration is usually the default.
180-
181-
1. Don't create swap space on the OS disk.
182-
183-
The Azure Linux Agent can automatically configure swap space using the local resource disk that is attached to the VM after provisioning on Azure. The local resource disk is a *temporary* disk, and might be emptied when the VM is deprovisioned. After installing the Azure Linux Agent (step 2 above), modify the following parameters in /etc/waagent.conf as needed.
184-
```
185-
ResourceDisk.Format=y
186-
ResourceDisk.Filesystem=ext4
187-
ResourceDisk.MountPoint=/mnt/resource
188-
ResourceDisk.EnableSwap=y
189-
ResourceDisk.SwapSizeMB=2048 ## NOTE: Set this to your desired size.
191+
The Azure Linux Agent is required for provisioning a Linux image on Azure. Many distributions provide the agent as an RPM or .deb package (the package is typically called WALinuxAgent or walinuxagent). The agent can also be installed manually by following the steps in the [Linux Agent Guide](../extensions/agent-linux.md).
192+
193+
Install the Azure Linux Agent, cloud-init and other necessary utilities by running the following command:
194+
195+
**Redhat/Centos**
196+
```bash
197+
sudo yum install -y [waagent] cloud-init cloud-utils-growpart gdisk hyperv-daemons
198+
```
199+
**Ubuntu/Debian**
200+
```bash
201+
sudo apt install walinuxagent cloud-init cloud-utils-growpart gdisk hyperv-daemons
202+
```
203+
**Suse**
204+
```bash
205+
sudo zypper install python-azure-agent cloud-init cloud-utils-growpart gdisk hyperv-daemons
206+
```
207+
Then enable the agent and cloud-init on all distributions using:
208+
```bash
209+
sudo systemctl enable waagent.service
210+
sudo systemctl enable cloud-init.service
211+
```
212+
213+
214+
1. Don't create swap space on the OS disk. The Azure Linux Agent can automatically configure swap space using the local resource disk that is attached to the VM after provisioning on Azure. The local resource disk is a temporary disk, and might be emptied when the VM is deprovisioned. After installing the Azure Linux Agent, modify the following parameters in /etc/waagent.conf as needed.
190215
```
191-
1. Run the following commands to deprovision the virtual machine.
192-
216+
ResourceDisk.Format=y
217+
ResourceDisk.Filesystem=ext4
218+
ResourceDisk.MountPoint=/mnt/resource
219+
ResourceDisk.EnableSwap=y
220+
ResourceDisk.SwapSizeMB=2048 ## NOTE: Set this to your desired size.
221+
```
222+
223+
9. Configure cloud-init to handle the provisioning:
224+
1. Configure waagent for cloud-init:
225+
```bash
226+
sed -i 's/Provisioning.Agent=auto/Provisioning.Agent=cloud-init/g' /etc/waagent.conf
227+
sed -i 's/ResourceDisk.Format=y/ResourceDisk.Format=n/g' /etc/waagent.conf
228+
sed -i 's/ResourceDisk.EnableSwap=y/ResourceDisk.EnableSwap=n/g' /etc/waagent.conf
229+
```
230+
If you are migrating a specific virtual machine and do not wish to create a generalized image, set `Provisioning.Agent=disabled` in the `/etc/waagent.conf` config.
231+
1. Configure mounts:
232+
```
233+
echo "Adding mounts and disk_setup to init stage"
234+
sed -i '/ - mounts/d' /etc/cloud/cloud.cfg
235+
sed -i '/ - disk_setup/d' /etc/cloud/cloud.cfg
236+
sed -i '/cloud_init_modules/a\\ - mounts' /etc/cloud/cloud.cfg
237+
sed -i '/cloud_init_modules/a\\ - disk_setup' /etc/cloud/cloud.cfg
238+
2. Configure Azure datasource:
239+
```
240+
echo "Allow only Azure datasource, disable fetching network setting via IMDS"
241+
cat > /etc/cloud/cloud.cfg.d/91-azure_datasource.cfg <<EOF
242+
datasource_list: [ Azure ]
243+
datasource:
244+
Azure:
245+
apply_network_config: False
246+
EOF
247+
```
248+
3. If configured, remove existing swapfile:
249+
```
250+
if [[ -f /mnt/resource/swapfile ]]; then
251+
echo "Removing swapfile" #RHEL uses a swapfile by defaul
252+
swapoff /mnt/resource/swapfile
253+
rm /mnt/resource/swapfile -f
254+
fi
255+
```
256+
4. Configure cloud-init logging:
257+
```
258+
echo "Add console log file"
259+
cat >> /etc/cloud/cloud.cfg.d/05_logging.cfg <<EOF
260+
261+
# This tells cloud-init to redirect its stdout and stderr to
262+
# 'tee -a /var/log/cloud-init-output.log' so the user can see output
263+
# there without needing to look on the console.
264+
output: {all: '| tee -a /var/log/cloud-init-output.log'}
265+
EOF
266+
```
267+
268+
10. Swap configuration. Do not create swap space on the operating system disk.
269+
Previously, the Azure Linux Agent automatically configured swap space by using the local resource disk that is attached to the virtual machine after the virtual machine is provisioned on Azure. However, this is now handled by cloud-init, you must not use the Linux Agent to format the resource disk create the swap file, modify the following parameters in /etc/waagent.conf appropriately:
270+
```
271+
ResourceDisk.Format=n
272+
ResourceDisk.EnableSwap=n
193273
```
194-
sudo waagent -force -deprovision
195-
export HISTSIZE=0
196-
logout
197-
```
274+
If you want to mount, format and create swap you can either:
275+
1. Pass this in as a cloud-init config every time you create a VM through `customdata`. This is the recommended method.
276+
2. Use a cloud-init directive baked into the image that will do this every time the VM is created.
277+
```
278+
echo 'DefaultEnvironment="CLOUD_CFG=/etc/cloud/cloud.cfg.d/00-azure-swap.cfg"' >> /etc/systemd/system.conf
279+
cat > /etc/cloud/cloud.cfg.d/00-azure-swap.cfg << EOF
280+
#cloud-config
281+
# Generated by Azure cloud image build
282+
disk_setup:
283+
ephemeral0:
284+
table_type: mbr
285+
layout: [66, [33, 82]]
286+
overwrite: True
287+
fs_setup:
288+
- device: ephemeral0.1
289+
filesystem: ext4
290+
- device: ephemeral0.2
291+
filesystem: swap
292+
mounts:
293+
- ["ephemeral0.1", "/mnt"]
294+
- ["ephemeral0.2", "none", "swap", "sw,nofail,x-systemd.requires=cloud-init.service,x-systemd.device-timeout=2", "0", "0"]
295+
EOF
296+
```
297+
298+
1. Deprovision.
299+
> [!CAUTION]
300+
> If you are migrating a specific virtual machine and do not wish to create a generalized image, skip the deprovision step. Running the command waagent -force -deprovision+user will render the source machine unusable, this step is intended only to create a generalized image.
301+
302+
Run the following commands to deprovision the virtual machine.
303+
304+
```
305+
# sudo rm -f /var/log/waagent.log
306+
# sudo cloud-init clean
307+
# waagent -force -deprovision+user
308+
# rm -f ~/.bash_history
309+
# export HISTSIZE=0
310+
# logout
311+
```
312+
198313
> [!NOTE]
199314
> On Virtualbox you may see the following error after running `waagent -force -deprovision` that says `[Errno 5] Input/output error`. This error message is not critical and can be ignored.
200315
201-
* Shut down the virtual machine and upload the VHD to Azure.
316+
1. Shut down the virtual machine and upload the VHD to Azure.
317+
318+
## Next Steps
319+
[Create a Linux VM from a custom disk with the Azure CLI](upload-vhd.md).

0 commit comments

Comments
 (0)