How to Swap /Dev/Sda with /Dev/Sdb

How do you swap /dev/sda with /dev/sdb?

These days, the Linux kernel dynamically populates /dev/ according to UDEV rules.

Let me first explain how device files work. Each device file, typically a block device file, has a major and a minor number. These numbers actually describe what device the file points to. The name does not play any role in this. Let's have a look at our specific case of disks:

# ls -l sd*
brw-rw---- 1 root disk 8, 0 Aug 22 15:45 sda
brw-rw---- 1 root disk 8, 1 Aug 22 15:45 sda1
brw-rw---- 1 root disk 8, 2 Aug 22 15:45 sda2
brw-rw---- 1 root disk 8, 3 Aug 22 15:45 sda3
brw-rw---- 1 root disk 8, 5 Aug 22 15:45 sda5
brw-rw---- 1 root disk 8, 6 Aug 22 15:45 sda6

Here you see that my first disk has various partitions and that I booted on Aug 22, at 3pm, which is when the kernel created the files according to rules. You can also see that the major number is 8 and the minor numbers are used to access partitions (0 pointing to the whole disk). The 'b' in the beginning of each line tells that each of these is a special "block device" file.

As I said, the kernel creates the files dynamically "these days". It was not always like that and it is not like that on other Unix systems. There, files would be created statically, and, the user would create or manipulate these files.

It is perfectly possible to create your own device files, with your own name, and major/minor numbers. See mknod (man mknod) for that. However, after you boot again, your custom files will disappear.

The second possibility is to change UDEV rules. The rules will be processed during system boot, and guarantee you a permanently consistent behavior. A good guide on these rules ca be found here: http://www.reactivated.net/writing_udev_rules.html

You will see that it is possible to define a rule that creates "sda*" given specific hardware info that matches your device. You will need to replace the original rules that would create sda with yours. How this works depends on your distribution.

As I think this is a dangerous business for the novice, I will not explain you specific steps; the document I linked above will give you all information you need, and you should indeed read it all.

I have two disks on azure linux VM, sda and sdb

Azure A_v2-Series VMs have additional temporary disk attached(in your case it is /dev/sdb)

The temporary disk provides short-term storage for applications and processes, and is intended to only store data such as page or swap files.

Data on the temporary disk may be lost during a maintenance event, shutdown or when you redeploy a VM. During a successful standard reboot of the VM, data on the temporary disk will persist.

https://azure.microsoft.com/en-us/blog/new-av2-series-vm-sizes/

https://learn.microsoft.com/en-us/azure/virtual-machines/managed-disks-overview#temporary-disk

Immutable names in /dev/disk

In short: you can use by-label or by-uuid to keep names immutable.

In detail:

Disk names (/dev/sdX) are given by kernel based on controller priority (master/slave) disk attached to. If you are moving disk from one USB port to another, for kernel it is like switching a controller. This is why names are changed from /dev/sda to /dev/sdb.

The directory /dev/disk is related to filesystem located on the disk. Label and uuid are filesystem attributes which are given on filesystem creation and can be changed after.

They are immutable and can survive:

  • disk migration from one computer to another.
  • disk migration from one controller to another on same computer.

However by-label and by-uuid will not survive if you destroy the partition, but the same label, uuid names can be given upon filesystem creation. So newly created filesystem will be mounted at same mount point.

I personally prefer to use by-label as it supported by many filesystems, short and descriptive.

More information about persistent block device naming.

how in c check what exactly is under /dev/sda and what is under /dev/sdb when both are USB drives

udev should be solving that for you by creating unique names under /dev/disk which you can use instead of /dev/sda and /dev/sdb. The links under /dev/disk/by-id also include manufacturer name.



Related Topics



Leave a reply



Submit