You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: articles/virtual-machines/linux/disk-encryption-sample-scripts.md
+86-46Lines changed: 86 additions & 46 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -7,11 +7,10 @@ ms.subservice: disks
7
7
ms.collection: linux
8
8
ms.topic: how-to
9
9
ms.author: mbaldwin
10
-
ms.date: 08/06/2019
10
+
ms.date: 03/29/2023
11
11
ms.custom: seodec18, devx-track-azurepowershell
12
12
---
13
13
14
-
15
14
# Azure Disk Encryption sample scripts for Linux VMs
16
15
17
16
**Applies to:**:heavy_check_mark: Linux VMs :heavy_check_mark: Flexible scale sets
@@ -46,11 +45,11 @@ This article provides sample scripts for preparing pre-encrypted VHDs and other
46
45
```
47
46
48
47
### Using the Azure Disk Encryption prerequisites PowerShell script
48
+
49
49
If you're already familiar with the prerequisites for Azure Disk Encryption, you can use the [Azure Disk Encryption prerequisites PowerShell script](https://raw.githubusercontent.com/Azure/azure-powershell/master/src/Compute/Compute/Extension/AzureDiskEncryption/Scripts/AzureDiskEncryptionPreRequisiteSetup.ps1). For an example of using this PowerShell script, see the [Encrypt a VM Quickstart](disk-encryption-powershell-quickstart.md). You can remove the comments from a section of the script, starting at line 211, to encrypt all disks for existing VMs in an existing resource group.
50
50
51
51
The following table shows which parameters can be used in the PowerShell script:
52
52
53
-
54
53
|Parameter|Description|Mandatory?|
55
54
|------|------|------|
56
55
|$resourceGroupName| Name of the resource group to which the KeyVault belongs to. A new resource group with this name will be created if one doesn't exist.| True|
@@ -70,69 +69,68 @@ The following table shows which parameters can be used in the PowerShell script:
70
69
### Encrypt or decrypt VMs with an Azure AD app (previous release)
71
70
72
71
-[Enable disk encryption on an existing or running Linux VM](https://github.com/Azure/azure-quickstart-templates/tree/master/quickstarts/microsoft.compute/encrypt-running-linux-vm)
73
-
74
-
75
72
-[Disable encryption on a running Linux VM](https://github.com/Azure/azure-quickstart-templates/tree/master/quickstarts/microsoft.compute/decrypt-running-linux-vm)
76
73
- Disabling encryption is only allowed on Data volumes for Linux VMs.
77
-
78
-
79
74
-[Create a new encrypted managed disk from a pre-encrypted VHD/storage blob](https://github.com/Azure/azure-quickstart-templates/tree/master/quickstarts/microsoft.compute/create-encrypted-managed-disk)
80
75
- Creates a new encrypted managed disk provided a pre-encrypted VHD and its corresponding encryption settings
81
76
82
77
## Encrypting an OS drive on a running Linux VM
83
78
84
79
### Prerequisites for OS disk encryption
85
80
86
-
* The VM must be using a distribution compatible with OS disk encryption as listed in the [Azure Disk Encryption supported operating systems](disk-encryption-overview.md#supported-vms)
81
+
* The VM must be using a distribution compatible with OS disk encryption as listed in the [Azure Disk Encryption supported operating systems](/azure/virtual-machines/linux/disk-encryption-overview#supported-operating-systems)
87
82
* The VM must be created from the Marketplace image in Azure Resource Manager.
88
-
* Azure VM with at least 4 GB of RAM (recommended size is 7 GB).
83
+
* Azure VM with at least 4 GB of RAM (recommended size is 7 GB). See [Memory requirements](/azure/virtual-machines/linux/disk-encryption-overview#memory-requirements) for further information.
89
84
* (For RHEL and CentOS) Disable SELinux. To disable SELinux, see "4.4.2. Disabling SELinux" in the [SELinux User's and Administrator's Guide](https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/7/html/selinux_users_and_administrators_guide/sect-security-enhanced_linux-working_with_selinux-changing_selinux_modes#sect-Security-Enhanced_Linux-Enabling_and_Disabling_SELinux-Disabling_SELinux) on the VM.
90
85
* After you disable SELinux, reboot the VM at least once.
91
86
92
87
### Steps
93
-
1. Create a VM by using one of the distributions specified previously.
94
88
95
-
For CentOS 7.2, OS disk encryption is supported via a special image. To use this image, specify "7.2n" as the SKU when you create the VM:
1. Create a VM by using one of the distributions specified previously.
100
90
2. Configure the VM according to your needs. If you're going to encrypt all the (OS + data) drives, the data drives need to be specified and mountable from /etc/fstab.
101
91
102
92
> [!NOTE]
103
93
> Use UUID=... to specify data drives in /etc/fstab instead of specifying the block device name (for example, /dev/sdb1). During encryption, the order of drives changes on the VM. If your VM relies on a specific order of block devices, it will fail to mount them after encryption.
104
-
105
94
3. Sign out of the SSH sessions.
106
-
107
95
4. To encrypt the OS, specify volumeType as **All** or **OS** when you enable encryption.
108
96
109
97
> [!NOTE]
110
98
> All user-space processes that are not running as `systemd` services should be killed with a `SIGKILL`. Reboot the VM. When you enable OS disk encryption on a running VM, plan on VM downtime.
111
99
112
100
5. Periodically monitor the progress of encryption by using the instructions in the [next section](#monitoring-os-encryption-progress).
113
-
114
101
6. After Get-AzVmDiskEncryptionStatus shows "VMRestartPending", restart your VM either by signing in to it or by using the portal, PowerShell, or CLI.
ProgressMessage : OS disk successfully encrypted, reboot the VM
123
113
```
114
+
124
115
Before you reboot, we recommend that you save [boot diagnostics](https://azure.microsoft.com/blog/boot-diagnostics-for-virtual-machines-v2/) of the VM.
125
116
126
117
## Monitoring OS encryption progress
118
+
127
119
You can monitor OS encryption progress in three ways:
128
120
129
121
* Use the `Get-AzVmDiskEncryptionStatus` cmdlet and inspect the ProgressMessage field:
After the VM reaches "OS disk encryption started", it takes about 40 to 50 minutes on a Premium-storage backed VM.
137
135
138
136
Because of [issue #388](https://github.com/Azure/WALinuxAgent/issues/388) in WALinuxAgent, `OsVolumeEncrypted` and `DataVolumesEncrypted` show up as `Unknown` in some distributions. With WALinuxAgent version 2.1.5 and later, this issue is fixed automatically. If you see `Unknown` in the output, you can verify disk-encryption status by using the Azure Resource Explorer.
@@ -164,9 +162,11 @@ You can monitor OS encryption progress in three ways:
164
162
We recommend that you don't sign-in to the VM while OS encryption is in progress. Copy the logs only when the other two methods have failed.
165
163
166
164
## Prepare a pre-encrypted Linux VHD
167
-
The preparation for pre-encrypted VHDs can vary depending on the distribution. Examples on preparing Ubuntu 16, openSUSE 13.2, and CentOS 7 are available.
168
165
169
-
### Ubuntu 16
166
+
The preparation for pre-encrypted VHDs can vary depending on the distribution. Examples on preparing Ubuntu, openSUSE, and CentOS 7 are available.
167
+
168
+
# [Ubuntu](#tab/ubuntu)
169
+
170
170
Configure encryption during the distribution installation by doing the following steps:
171
171
172
172
1. Select **Configure encrypted volumes** when you partition the disks.
@@ -193,7 +193,7 @@ Configure encryption during the distribution installation by doing the following
193
193
194
194
Configure encryption to work with Azure by doing the following steps:
195
195
196
-
1. Create a file under /usr/local/sbin/azure_crypt_key.sh, with the content in the following script. Pay attention to the KeyFileName, because it's the passphrase file name used by Azure.
196
+
1. Create a file under `/usr/local/sbin/azure_crypt_key.sh`, with the content in the following script. Pay attention to the KeyFileName, because it's the passphrase file name used by Azure.
197
197
198
198
```bash
199
199
#!/bin/sh
@@ -231,22 +231,27 @@ Configure encryption to work with Azure by doing the following steps:
231
231
```
232
232
233
233
2. Change the crypt config in */etc/crypttab*. It should look like this:
5. Edit */etc/initramfs-tools/modules* by appending lines:
243
-
```
244
+
245
+
5. Edit `/etc/initramfs-tools/modules` by appending lines:
246
+
247
+
```config
244
248
vfat
245
249
ntfs
246
250
nls_cp437
247
251
nls_utf8
248
252
nls_iso8859-1
249
253
```
254
+
250
255
6. Run `update-initramfs -u -k all` to update the initramfs to make the `keyscript` take effect.
251
256
252
257
7. Now you can deprovision the VM.
@@ -255,8 +260,10 @@ Configure encryption to work with Azure by doing the following steps:
255
260
256
261
8. Continue to the next step and upload your VHD into Azure.
257
262
258
-
### openSUSE 13.2
263
+
# [openSUSE](#tab/opensuse)
264
+
259
265
To configure encryption during the distribution installation, do the following steps:
266
+
260
267
1. When you partition the disks, select **Encrypt Volume Group**, and then enter a password. This is the password that you'll upload to your key vault.
@@ -268,11 +275,15 @@ To configure encryption during the distribution installation, do the following s
268
275
3. Prepare the VM for uploading to Azure by following the instructions in [Prepare a SLES or openSUSE virtual machine for Azure](./suse-create-upload-vhd.md?toc=/azure/virtual-machines/linux/toc.json#prepare-opensuse-152). Don't run the last step (deprovisioning the VM) yet.
269
276
270
277
To configure encryption to work with Azure, do the following steps:
271
-
1. Edit the /etc/dracut.conf, and add the following line:
272
-
```
278
+
279
+
1. Edit the `/etc/dracut.conf`, and add the following line:
280
+
281
+
```config
273
282
add_drivers+=" vfat ntfs nls_cp437 nls_iso8859-1"
274
283
```
275
-
2. Comment out these lines by the end of the file /usr/lib/dracut/modules.d/90crypt/module-setup.sh:
284
+
285
+
2. Comment out these lines by the end of the file `/usr/lib/dracut/modules.d/90crypt/module-setup.sh`:
## Upload the secret for the pre-encrypted VM to your key vault
446
+
412
447
When encrypting using an Azure AD app (previous release), the disk-encryption secret that you obtained previously must be uploaded as a secret in your key vault. The key vault needs to have disk encryption and permissions enabled for your Azure AD client.
413
448
414
-
```powershell
449
+
```azurepowershell-interactive
415
450
$AadClientId = "My-AAD-Client-Id"
416
451
$AadClientSecret = "My-AAD-Client-Secret"
417
452
@@ -422,9 +457,10 @@ When encrypting using an Azure AD app (previous release), the disk-encryption se
422
457
```
423
458
424
459
### Disk encryption secret not encrypted with a KEK
460
+
425
461
To set up the secret in your key vault, use [Set-AzKeyVaultSecret](/powershell/module/az.keyvault/set-azkeyvaultsecret). The passphrase is encoded as a base64 string and then uploaded to the key vault. In addition, make sure that the following tags are set when you create the secret in the key vault.
426
462
427
-
```powershell
463
+
```azurepowershell-interactive
428
464
429
465
# This is the passphrase that was provided for encryption during the distribution installation
430
466
$passphrase = "contoso-password"
@@ -438,13 +474,13 @@ To set up the secret in your key vault, use [Set-AzKeyVaultSecret](/powershell/m
438
474
$secretUrl = $secret.Id
439
475
```
440
476
441
-
442
477
Use the `$secretUrl`in the next step for [attaching the OS disk without using KEK](#without-using-a-kek).
443
478
444
479
### Disk encryption secret encrypted with a KEK
480
+
445
481
Before you upload the secret to the key vault, you can optionally encrypt it by using a key encryption key. Use the wrap [API](/rest/api/keyvault/keys/wrap-key) to first encrypt the secret using the key encryption key. The output of this wrap operation is a base64 URL encoded string, which you can then upload as a secret by using the [`Set-AzKeyVaultSecret`](/powershell/module/az.keyvault/set-azkeyvaultsecret) cmdlet.
446
482
447
-
```powershell
483
+
```azurepowershell-interactive
448
484
# This is the passphrase that was provided for encryption during the distribution installation
449
485
$passphrase = "contoso-password"
450
486
@@ -534,11 +570,12 @@ Before you upload the secret to the key vault, you can optionally encrypt it by
534
570
535
571
Use `$KeyEncryptionKey` and `$secretUrl`in the next step for [attaching the OS disk using KEK](#using-a-kek).
536
572
537
-
## Specify a secret URL when you attach an OS disk
573
+
## Specify a secret URL when you attach an OS disk
538
574
539
-
### Without using a KEK
575
+
### Without using a KEK
540
576
While you're attaching the OS disk, you need to pass `$secretUrl`. The URL was generated in the "Disk-encryption secret not encrypted with a KEK" section.
541
-
```powershell
577
+
578
+
```powershazurepowershell-interactiveell
542
579
Set-AzVMOSDisk `
543
580
-VM $VirtualMachine `
544
581
-Name $OSDiskName `
@@ -549,9 +586,12 @@ While you're attaching the OS disk, you need to pass `$secretUrl`. The URL was g
549
586
-DiskEncryptionKeyVaultId $KeyVault.ResourceId `
550
587
-DiskEncryptionKeyUrl $SecretUrl
551
588
```
589
+
552
590
### Using a KEK
591
+
553
592
When you attach the OS disk, pass `$KeyEncryptionKey` and `$secretUrl`. The URL was generated in the "Disk encryption secret encrypted with a KEK" section.
0 commit comments