Skip to content

Commit 96893f4

Browse files
authored
Merge pull request #232717 from divargas-msft/patch-3
[Doc-a-thon] Updating disk-encryption-sample-scripts
2 parents 4077187 + 2bfce6b commit 96893f4

File tree

1 file changed

+86
-46
lines changed

1 file changed

+86
-46
lines changed

articles/virtual-machines/linux/disk-encryption-sample-scripts.md

Lines changed: 86 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,10 @@ ms.subservice: disks
77
ms.collection: linux
88
ms.topic: how-to
99
ms.author: mbaldwin
10-
ms.date: 08/06/2019
10+
ms.date: 03/29/2023
1111
ms.custom: seodec18, devx-track-azurepowershell
1212
---
1313

14-
1514
# Azure Disk Encryption sample scripts for Linux VMs
1615

1716
**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
4645
```
4746

4847
### Using the Azure Disk Encryption prerequisites PowerShell script
48+
4949
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.
5050

5151
The following table shows which parameters can be used in the PowerShell script:
5252

53-
5453
|Parameter|Description|Mandatory?|
5554
|------|------|------|
5655
|$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:
7069
### Encrypt or decrypt VMs with an Azure AD app (previous release)
7170

7271
- [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-
7572
- [Disable encryption on a running Linux VM](https://github.com/Azure/azure-quickstart-templates/tree/master/quickstarts/microsoft.compute/decrypt-running-linux-vm)
7673
- Disabling encryption is only allowed on Data volumes for Linux VMs.
77-
78-
7974
- [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)
8075
- Creates a new encrypted managed disk provided a pre-encrypted VHD and its corresponding encryption settings
8176

8277
## Encrypting an OS drive on a running Linux VM
8378

8479
### Prerequisites for OS disk encryption
8580

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)
8782
* 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.
8984
* (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.
9085
* After you disable SELinux, reboot the VM at least once.
9186

9287
### Steps
93-
1. Create a VM by using one of the distributions specified previously.
9488

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:
96-
97-
```powershell
98-
Set-AzVMSourceImage -VM $VirtualMachine -PublisherName "OpenLogic" -Offer "CentOS" -Skus "7.2n" -Version "latest"
99-
```
89+
1. Create a VM by using one of the distributions specified previously.
10090
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.
10191

10292
> [!NOTE]
10393
> 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-
10594
3. Sign out of the SSH sessions.
106-
10795
4. To encrypt the OS, specify volumeType as **All** or **OS** when you enable encryption.
10896

10997
> [!NOTE]
11098
> 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.
11199
112100
5. Periodically monitor the progress of encryption by using the instructions in the [next section](#monitoring-os-encryption-progress).
113-
114101
6. After Get-AzVmDiskEncryptionStatus shows "VMRestartPending", restart your VM either by signing in to it or by using the portal, PowerShell, or CLI.
115-
```powershell
102+
103+
```azurepowershell-interactive
116104
C:\> Get-AzVmDiskEncryptionStatus -ResourceGroupName $ResourceGroupName -VMName $VMName
117105
-ExtensionName $ExtensionName
106+
```
118107
108+
```output
119109
OsVolumeEncrypted : VMRestartPending
120110
DataVolumesEncrypted : NotMounted
121111
OsVolumeEncryptionSettings : Microsoft.Azure.Management.Compute.Models.DiskEncryptionSettings
122112
ProgressMessage : OS disk successfully encrypted, reboot the VM
123113
```
114+
124115
Before you reboot, we recommend that you save [boot diagnostics](https://azure.microsoft.com/blog/boot-diagnostics-for-virtual-machines-v2/) of the VM.
125116
126117
## Monitoring OS encryption progress
118+
127119
You can monitor OS encryption progress in three ways:
128120
129121
* Use the `Get-AzVmDiskEncryptionStatus` cmdlet and inspect the ProgressMessage field:
130-
```powershell
122+
123+
```azurepowershell-interactive
124+
Get-AzVMDiskEncryptionStatus -ResourceGroupName $_.ResourceGroupName -VMName $_.Name
125+
```
126+
127+
```output
131128
OsVolumeEncrypted : EncryptionInProgress
132129
DataVolumesEncrypted : NotMounted
133130
OsVolumeEncryptionSettings : Microsoft.Azure.Management.Compute.Models.DiskEncryptionSettings
134131
ProgressMessage : OS disk encryption started
135132
```
133+
136134
After the VM reaches "OS disk encryption started", it takes about 40 to 50 minutes on a Premium-storage backed VM.
137135
138136
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:
164162
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.
165163
166164
## 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.
168165
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+
170170
Configure encryption during the distribution installation by doing the following steps:
171171
172172
1. Select **Configure encrypted volumes** when you partition the disks.
@@ -193,7 +193,7 @@ Configure encryption during the distribution installation by doing the following
193193
194194
Configure encryption to work with Azure by doing the following steps:
195195
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.
197197
198198
```bash
199199
#!/bin/sh
@@ -231,22 +231,27 @@ Configure encryption to work with Azure by doing the following steps:
231231
```
232232

233233
2. Change the crypt config in */etc/crypttab*. It should look like this:
234-
```
234+
235+
```config
235236
xxx_crypt uuid=xxxxxxxxxxxxxxxxxxxxx none luks,discard,keyscript=/usr/local/sbin/azure_crypt_key.sh
236237
```
237238
238239
4. Add executable permissions to the script:
240+
241+
```bash
242+
sudo chmod +x /usr/local/sbin/azure_crypt_key.sh
239243
```
240-
chmod +x /usr/local/sbin/azure_crypt_key.sh
241-
```
242-
5. Edit */etc/initramfs-tools/modules* by appending lines:
243-
```
244+
245+
5. Edit `/etc/initramfs-tools/modules` by appending lines:
246+
247+
```config
244248
vfat
245249
ntfs
246250
nls_cp437
247251
nls_utf8
248252
nls_iso8859-1
249253
```
254+
250255
6. Run `update-initramfs -u -k all` to update the initramfs to make the `keyscript` take effect.
251256

252257
7. Now you can deprovision the VM.
@@ -255,8 +260,10 @@ Configure encryption to work with Azure by doing the following steps:
255260

256261
8. Continue to the next step and upload your VHD into Azure.
257262

258-
### openSUSE 13.2
263+
# [openSUSE](#tab/opensuse)
264+
259265
To configure encryption during the distribution installation, do the following steps:
266+
260267
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.
261268

262269
![openSUSE 13.2 Setup - Encrypt Volume Group](./media/disk-encryption/opensuse-encrypt-fig1.png)
@@ -268,11 +275,15 @@ To configure encryption during the distribution installation, do the following s
268275
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.
269276

270277
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
273282
add_drivers+=" vfat ntfs nls_cp437 nls_iso8859-1"
274283
```
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`:
286+
276287
```bash
277288
# inst_multiple -o \
278289
# $systemdutildir/system-generators/systemd-cryptsetup-generator \
@@ -285,19 +296,25 @@ To configure encryption to work with Azure, do the following steps:
285296
# inst_script "$moddir"/crypt-run-generator.sh /sbin/crypt-run-generator
286297
```
287298

288-
3. Append the following line at the beginning of the file /usr/lib/dracut/modules.d/90crypt/parse-crypt.sh:
299+
3. Append the following line at the beginning of the file `/usr/lib/dracut/modules.d/90crypt/parse-crypt.sh`:
300+
289301
```bash
290302
DRACUT_SYSTEMD=0
291303
```
304+
292305
And change all occurrences of:
306+
293307
```bash
294308
if [ -z "$DRACUT_SYSTEMD" ]; then
295309
```
310+
296311
to:
312+
297313
```bash
298314
if [ 1 ]; then
299315
```
300-
4. Edit /usr/lib/dracut/modules.d/90crypt/cryptroot-ask.sh and append it to "# Open LUKS device":
316+
317+
4. Edit `/usr/lib/dracut/modules.d/90crypt/cryptroot-ask.sh` and append it to "# Open LUKS device":
301318
302319
```bash
303320
MountPoint=/tmp-keydisk-mount
@@ -319,13 +336,15 @@ To configure encryption to work with Azure, do the following steps:
319336
fi
320337
done
321338
```
339+
322340
5. Run `/usr/sbin/dracut -f -v` to update the initrd.
323341
324342
6. Now you can deprovision the VM and upload your VHD into Azure.
325343
326-
### CentOS 7 and RHEL 7
344+
# [CentOS 7 and RHEL 7](#tab/rhel)
327345
328346
To configure encryption during the distribution installation, do the following steps:
347+
329348
1. Select **Encrypt my data** when you partition disks.
330349
331350
![CentOS 7 Setup -Installation destination](./media/disk-encryption/centos-encrypt-fig1.png)
@@ -349,11 +368,13 @@ To configure encryption during the distribution installation, do the following s
349368
To configure encryption to work with Azure, do the following steps:
350369
351370
1. Edit the /etc/dracut.conf, and add the following line:
352-
```
371+
372+
```config
353373
add_drivers+=" vfat ntfs nls_cp437 nls_iso8859-1"
354374
```
355375
356376
2. Comment out these lines by the end of the file /usr/lib/dracut/modules.d/90crypt/module-setup.sh:
377+
357378
```bash
358379
# inst_multiple -o \
359380
# $systemdutildir/system-generators/systemd-cryptsetup-generator \
@@ -367,18 +388,25 @@ To configure encryption to work with Azure, do the following steps:
367388
```
368389
369390
3. Append the following line at the beginning of the file /usr/lib/dracut/modules.d/90crypt/parse-crypt.sh:
391+
370392
```bash
371393
DRACUT_SYSTEMD=0
372394
```
395+
373396
And change all occurrences of:
397+
374398
```bash
375399
if [ -z "$DRACUT_SYSTEMD" ]; then
376400
```
401+
377402
to
403+
378404
```bash
379405
if [ 1 ]; then
380406
```
381-
4. Edit /usr/lib/dracut/modules.d/90crypt/cryptroot-ask.sh and append the following after the "# Open LUKS device":
407+
408+
4. Edit `/usr/lib/dracut/modules.d/90crypt/cryptroot-ask.sh` and append the following after the "# Open LUKS device":
409+
382410
```bash
383411
MountPoint=/tmp-keydisk-mount
384412
KeyFileName=LinuxPassPhraseFileName
@@ -399,19 +427,26 @@ To configure encryption to work with Azure, do the following steps:
399427
fi
400428
done
401429
```
402-
5. Run the "/usr/sbin/dracut -f -v" to update the initrd.
430+
431+
5. Run the `/usr/sbin/dracut -f -v` to update the initrd.
403432
404433
![CentOS 7 Setup - run /usr/sbin/dracut -f -v](./media/disk-encryption/centos-encrypt-fig5.png)
405434
435+
---
436+
406437
## Upload encrypted VHD to an Azure storage account
438+
407439
After DM-Crypt encryption is enabled, the local encrypted VHD needs to be uploaded to your storage account.
440+
408441
```powershell
409442
Add-AzVhd [-Destination] <Uri> [-LocalFilePath] <FileInfo> [[-NumberOfUploaderThreads] <Int32> ] [[-BaseImageUriToPatch] <Uri> ] [[-OverWrite]] [ <CommonParameters>]
410443
```
444+
411445
## Upload the secret for the pre-encrypted VM to your key vault
446+
412447
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.
413448
414-
```powershell
449+
```azurepowershell-interactive
415450
$AadClientId = "My-AAD-Client-Id"
416451
$AadClientSecret = "My-AAD-Client-Secret"
417452

@@ -422,9 +457,10 @@ When encrypting using an Azure AD app (previous release), the disk-encryption se
422457
```
423458
424459
### Disk encryption secret not encrypted with a KEK
460+
425461
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.
426462
427-
```powershell
463+
```azurepowershell-interactive
428464

429465
# This is the passphrase that was provided for encryption during the distribution installation
430466
$passphrase = "contoso-password"
@@ -438,13 +474,13 @@ To set up the secret in your key vault, use [Set-AzKeyVaultSecret](/powershell/m
438474
$secretUrl = $secret.Id
439475
```
440476
441-
442477
Use the `$secretUrl` in the next step for [attaching the OS disk without using KEK](#without-using-a-kek).
443478
444479
### Disk encryption secret encrypted with a KEK
480+
445481
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.
446482
447-
```powershell
483+
```azurepowershell-interactive
448484
# This is the passphrase that was provided for encryption during the distribution installation
449485
$passphrase = "contoso-password"
450486
@@ -534,11 +570,12 @@ Before you upload the secret to the key vault, you can optionally encrypt it by
534570
535571
Use `$KeyEncryptionKey` and `$secretUrl` in the next step for [attaching the OS disk using KEK](#using-a-kek).
536572
537-
## Specify a secret URL when you attach an OS disk
573+
## Specify a secret URL when you attach an OS disk
538574
539-
### Without using a KEK
575+
### Without using a KEK
540576
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
542579
Set-AzVMOSDisk `
543580
-VM $VirtualMachine `
544581
-Name $OSDiskName `
@@ -549,9 +586,12 @@ While you're attaching the OS disk, you need to pass `$secretUrl`. The URL was g
549586
-DiskEncryptionKeyVaultId $KeyVault.ResourceId `
550587
-DiskEncryptionKeyUrl $SecretUrl
551588
```
589+
552590
### Using a KEK
591+
553592
When you attach the OS disk, pass `$KeyEncryptionKey` and `$secretUrl`. The URL was generated in the "Disk encryption secret encrypted with a KEK" section.
554-
```powershell
593+
594+
```azurepowershell-interactive
555595
Set-AzVMOSDisk `
556596
-VM $VirtualMachine `
557597
-Name $OSDiskName `

0 commit comments

Comments
 (0)