|
| 1 | +--- |
| 2 | +title: Recover Linux VMs using chroot where LVM (Logical Volume Manager) is used - Azure VMs |
| 3 | +description: Recovery of Linux VMs with LVMs. |
| 4 | +services: virtual-machines-linux |
| 5 | +documentationcenter: '' |
| 6 | +author: vilibert |
| 7 | +manager: spogge |
| 8 | +editor: '' |
| 9 | +tags: Linux chroot LVM |
| 10 | + |
| 11 | +ms.service: virtual-machines-linux |
| 12 | +ms.devlang: na |
| 13 | +ms.topic: troubleshooting |
| 14 | +ms.tgt_pltfrm: vm-linux |
| 15 | +ms.workload: infrastructure-services |
| 16 | +ms.date: 11/24/2019 |
| 17 | +ms.author: vilibert |
| 18 | +--- |
| 19 | + |
| 20 | +# Troubleshooting a Linux VM when there is no access to the Azure serial console and the disk layout is using LVM (Logical Volume Manager) |
| 21 | + |
| 22 | +This troubleshooting guide is of benefit for scenarios where a Linux VM is not booting,ssh is not possible and the underlying file system layout is configured with LVM (Logical Volume Manager). |
| 23 | + |
| 24 | +## Take snapshot of the failing VM |
| 25 | + |
| 26 | +Take a snapshot of the affected VM. |
| 27 | + |
| 28 | +The snapshot will then be attached to a **rescue** VM. |
| 29 | +Follow instructions [here](https://docs.microsoft.com/azure/virtual-machines/linux/snapshot-copy-managed-disk#use-azure-portal) on how to take a **snapshot**. |
| 30 | + |
| 31 | +## Create a rescue VM |
| 32 | +Usually a rescue VM of the same or similar Operating system version is recommended. Use the same **region** and **resource group** of the affected VM |
| 33 | + |
| 34 | +## Connect to the rescue VM |
| 35 | +Connect using ssh into the **rescue** VM. Elevate privileges and become super user using |
| 36 | + |
| 37 | +`sudo su -` |
| 38 | + |
| 39 | +## Attach the disk |
| 40 | +Attach a disk to the **rescue** VM made from the snapshot taken previously. |
| 41 | + |
| 42 | +Azure portal -> select the **rescue** VM -> **Disks** |
| 43 | + |
| 44 | + |
| 45 | + |
| 46 | +Populate the fields. |
| 47 | +Assign a name to your new disk, select the same Resource Group as the snapshot, affected VM, and Rescue VM. |
| 48 | + |
| 49 | +The **Source type** is **Snapshot** . |
| 50 | +The **Source snapshot** is the name of the **snapshot** previously created. |
| 51 | + |
| 52 | + |
| 53 | + |
| 54 | +Create a mount point for the attached disk. |
| 55 | + |
| 56 | +`mkdir /rescue` |
| 57 | + |
| 58 | +Run the **fdisk -l** command to verify the snapshot disk has been attached and list all devices and partitions available |
| 59 | + |
| 60 | +`fdisk -l` |
| 61 | + |
| 62 | +Most scenarios, the attached snapshot disk will be seen as **/dev/sdc** displaying two partitions **/dev/sdc1** and **/dev/sdc2** |
| 63 | + |
| 64 | + |
| 65 | + |
| 66 | +The **\*** indicates a boot partition, both partitions are to be mounted. |
| 67 | + |
| 68 | +Run the command **lsblk** to see the LVMs of the affected VM |
| 69 | + |
| 70 | +`lsblk` |
| 71 | + |
| 72 | + |
| 73 | + |
| 74 | + |
| 75 | +Verify if LVMs from the affected VM are displayed. |
| 76 | +If not, use the below commands to enable them and rerun **lsblk**. |
| 77 | +Ensure to have the LVMs from the attached disk visible before proceeding. |
| 78 | + |
| 79 | +``` |
| 80 | +vgscan --mknodes |
| 81 | +vgchange -ay |
| 82 | +lvscan |
| 83 | +mount –a |
| 84 | +lsblk |
| 85 | +``` |
| 86 | + |
| 87 | +Locate the path to mount the Logical Volume that contains the / (root) partition. It has the configuration files such as /etc/default/grub |
| 88 | + |
| 89 | +In this example, taking the output from the previous **lsblk** command **rootvg-rootlv** is the correct **root** LV to mount and can be used in the next command. |
| 90 | + |
| 91 | +The output of the next command will show the path to mount for the **root** LV |
| 92 | + |
| 93 | +`pvdisplay -m | grep -i rootlv` |
| 94 | + |
| 95 | + |
| 96 | + |
| 97 | +Proceed to mount this device on the directory /rescue |
| 98 | + |
| 99 | +`mount /dev/rootvg/rootlv /rescue` |
| 100 | + |
| 101 | +Mount the partition that has the **Boot flag** set on /rescue/boot |
| 102 | + |
| 103 | +` |
| 104 | +mount /dev/sdc1 /rescue/boot |
| 105 | +` |
| 106 | + |
| 107 | +Verify the file systems of the attached disk are now correctly mounted using the **lsblk** command |
| 108 | + |
| 109 | + |
| 110 | + |
| 111 | +or the **df -Th** command |
| 112 | + |
| 113 | + |
| 114 | + |
| 115 | +## Gaining chroot access |
| 116 | + |
| 117 | +Gain **chroot** access, which will enable you to perform various fixes, slight variations exist for each Linux distribution. |
| 118 | + |
| 119 | +``` |
| 120 | + cd /rescue |
| 121 | + mount -t proc proc proc |
| 122 | + mount -t sysfs sys sys/ |
| 123 | + mount -o bind /dev dev/ |
| 124 | + mount -o bind /dev/pts dev/pts/ |
| 125 | + chroot /rescue |
| 126 | +``` |
| 127 | + |
| 128 | +If an error is experienced such as: |
| 129 | + |
| 130 | +**chroot: failed to run command ‘/bin/bash’: No such file or directory** |
| 131 | + |
| 132 | +attempt to mount the **usr** Logical Volume |
| 133 | + |
| 134 | +` |
| 135 | +mount /dev/mapper/rootvg-usrlv /rescue/usr |
| 136 | +` |
| 137 | + |
| 138 | +> [!TIP] |
| 139 | +> When executing commands in a **chroot** environment, note they are run against the attached OS Disk and not the local **rescue** VM. |
| 140 | +
|
| 141 | +Commands can be used to install, remove and update software. Troubleshoot VMs in order to fix errors. |
| 142 | + |
| 143 | + |
| 144 | +Execute the lsblk command and the /rescue is now / and /rescue/boot is /boot |
| 145 | + |
| 146 | + |
| 147 | +## Perform Fixes |
| 148 | + |
| 149 | +### Example 1 - configure the VM to boot from a different kernel |
| 150 | + |
| 151 | +A common scenario is to force a VM to boot from a previous kernel as the current installed kernel may have become corrupt or an upgrade did not complete correctly. |
| 152 | + |
| 153 | + |
| 154 | +``` |
| 155 | +cd /boot/grub2 |
| 156 | +
|
| 157 | +grep -i linux grub.cfg |
| 158 | +
|
| 159 | +grub2-editenv list |
| 160 | +
|
| 161 | +grub2-set-default "CentOS Linux (3.10.0-1062.1.1.el7.x86_64) 7 (Core)" |
| 162 | +
|
| 163 | +grub2-editenv list |
| 164 | +
|
| 165 | +grub2-mkconfig -o /boot/grub2/grub.cfg |
| 166 | +``` |
| 167 | + |
| 168 | +*walkthrough* |
| 169 | + |
| 170 | +The **grep** command lists the kernels that **grub.cfg** is aware of. |
| 171 | + |
| 172 | + |
| 173 | +**grub2-editenv list** displays which kernel will be loaded at next boot |
| 174 | + |
| 175 | + |
| 176 | +**grub2-set-default** is used to change to another kernel |
| 177 | + |
| 178 | + |
| 179 | +**grub2-editenv** list displays which kernel will be loaded at next boot |
| 180 | + |
| 181 | + |
| 182 | +**grub2-mkconfig** rebuilds grub.cfg using the versions required |
| 183 | + |
| 184 | + |
| 185 | + |
| 186 | + |
| 187 | +### Example 2 - upgrade packages |
| 188 | + |
| 189 | +A failed kernel upgrade can render the VM non-bootable. |
| 190 | +Mount all the Logical Volumes to allow packages to be removed or reinstalled |
| 191 | + |
| 192 | +Run the **lvs** command to verify which **LVs** are available for mounting, every VM, which has been migrated or comes from another Cloud Provider will vary in configuration. |
| 193 | + |
| 194 | +Exit the **chroot** environment mount the required **LV** |
| 195 | + |
| 196 | + |
| 197 | + |
| 198 | +Now access the **chroot** environment again by running |
| 199 | + |
| 200 | +`chroot /rescue` |
| 201 | + |
| 202 | +All LVs should be visible as mounted partitions |
| 203 | + |
| 204 | + |
| 205 | + |
| 206 | +Query the installed **kernel** |
| 207 | + |
| 208 | + |
| 209 | + |
| 210 | +If needed upgrade the **kernel** |
| 211 | + |
| 212 | + |
| 213 | + |
| 214 | +### Example 3 - enable Serial Console |
| 215 | +If access has not been possible to the Azure serial console, verify GRUB configuration parameters for your Linux VM and correct them. DEtailed information can be found [in this doc](https://docs.microsoft.com/azure/virtual-machines/troubleshooting/serial-console-grub-proactive-configuration) |
| 216 | + |
| 217 | + |
| 218 | +## Exit chroot and swap the OS disk |
| 219 | + |
| 220 | +After repairing the issue, proceed to unmount and detach the disk from the rescue VM allowing it to be swapped with the affected VM OS disk. |
| 221 | + |
| 222 | +``` |
| 223 | +exit |
| 224 | +cd / |
| 225 | +umount /rescue/proc/ |
| 226 | +umount /rescue/sys/ |
| 227 | +umount /rescue/dev/pts |
| 228 | +umount /rescue/dev/ |
| 229 | +umount /rescue/boot |
| 230 | +umount /rescue |
| 231 | +``` |
| 232 | + |
| 233 | +Detach the disk from the rescue VM and perform a Disk Swap. |
| 234 | + |
| 235 | +Select the VM from the portal **Disks** and select **detach** |
| 236 | + |
| 237 | + |
| 238 | +Save the changes |
| 239 | + |
| 240 | + |
| 241 | +The disk will now become available allowing it to be swapped with the original OS disk of the affected VM. |
| 242 | + |
| 243 | +Navigate in the Azure portal to the failing VM and select **Disks** -> **Swap OS Disk** |
| 244 | + |
| 245 | + |
| 246 | +Complete the fields the **Choose disk** is the snapshot disk just detached in the previous step. The VM name of the affected VM is also required then select **OK** |
| 247 | + |
| 248 | + |
| 249 | + |
| 250 | +If the VM is running the Disk Swap will shut it down, reboot the VM once the disk swap operation has completed. |
| 251 | + |
| 252 | + |
| 253 | +## Next steps |
| 254 | +Learn more about [Azure Serial Console]( https://docs.microsoft.com/azure/virtual-machines/troubleshooting/serial-console-linux) |
0 commit comments