35

When I use Ubuntu and CentOS, I see /dev/sda and /dev/vda. So I can't understand what is the different between above two?

Braiam
  • 35,991

5 Answers5

32

They're different devices.

/dev/sda is the first disk that's either SCSI or (more likely) providing the SCSI drive API to user land. This includes SATA drives and IDE drives using libata. This can also be an IDE/SATA/SCSI/etc. drive emulated by the hypervisor.

/dev/vda is the first disk using the virtualization-aware disk driver. The performance should be much better, as the hypervisor doesn't have to emulate some hardware interface.

If the disk has been exposed to your VM under both interfaces, you should prefer /dev/vda as it'll almost certainly be faster.

derobert
  • 109,670
  • 2
    What would be the goal of having both /dev/sdX & /dev/vdX devices in a machine? – chromechris Dec 16 '16 at 20:43
  • 1
    @user42076 Only thing I can think of is a hosting company that doesn't know exactly what OS you're running on its VM could offer both interfaces, in case your OS doesn't support the faster /dev/vda. Other than that... no idea why you'd do it. If you're actually seeing it done somewhere, probably worth it's own question. – derobert Dec 16 '16 at 20:46
  • @user42076 Yea, I am going to ask about this . . . . . lol – chromechris Dec 16 '16 at 21:06
  • Just to be sure, are there any cases when I should not name my VM storage devices vdx, when host-OS is newer Linux (e.g. Debian 10) and VM-OS is newer Linux or newer Windows? No matter if VM disk-type is virtio-blk, virtio-scsi, iSCSI, NDB, CD-ROM? – MrCalvin Apr 16 '20 at 08:47
  • Hmm, all the examples I can find when using virtio-scsi is the device is named sdx. But virtio-scsi is also a para-virtualized device, so why not vdx? – MrCalvin Apr 16 '20 at 09:23
  • @MrCalvin this answer is outdated (note the 2014 date), as indeed virtio-scsi uses sdX. There is no real deep meaning to that, just that it uses the existing SCSI stack in the guest kernel. The device name is really just reflecting which drivers/modules are used by the guest. – derobert Apr 16 '20 at 12:22
  • Are you saying it's doesn't matter what you call the device, it's just an alias? And as such I can call all my VM guest devices vdx? – MrCalvin Apr 16 '20 at 13:04
  • @MrCalvin that's true, the name doesn't matter (only the major and minor numbers do, at least to the kernel, random programs may make assumptions, of course)... But this is about why the default names differ. – derobert Apr 16 '20 at 13:06
17

/dev/sdx and /dev/hdx are physical (hard) disk drives or emulated physical (hard) disk drives. When the kernel or some program I/O's to these, it does all sorts of things like bringing the disk to the right spot and doing all sorts of physical-specific "stuff."

/dev/vdx is for virtual (hard) disk drives. All the kernel does when it is I/O'd to is tell the virtualization software that bits need to be read/written and it's done. In general, vdx is faster because the kernel doesn't need to tell the hard drive to do a whole bunch of random junk that really shouldn't be needed because it just needs to tell the VM hypervisor to do stuff.

I apologize for the word "stuff," I couldn't think of a better word :D

5

In the udev rules in /lib/udev/rules.d/* is defined which kind of hardware gets which name from the kernel.

See the rules for /dev/vd*:

# partitions do not have hardware identifiers
ENV{DEVTYPE}!="disk",                   GOTO="persistent_storage_not_a_disk"
# nor do paravirtualized hard disks
KERNEL=="vd*",                          GOTO="persistent_storage_not_a_disk"

...

KERNEL=="vd*",                          ATTRS{serial}=="?*", \
ENV{ID_BUS}="virtio",   ENV{ID_SERIAL}="$attr{serial}"

So /dev/vda is the first, virtual harddrive (no hardware identificators) and a bus id virtio. Looks like a qemu/kvm guest.

The udev rules for /dev/sd* devices can be more different hardware types: scsi, ata, ieee1394, usb, ...

mikeserv
  • 58,310
chaos
  • 48,171
2

/dev/ is the part in the unix directory tree that contains all "device" files -- unix traditionally treats just about everything you can access as a file to read from or write to.

So in essence, the /dev/sda is all device files found in your Harddrive while the /dev/vda is all device files found in the space allocated for your virtual machine.

ryekayo
  • 4,763
0

One fairly fundamental way in which these /dev/.da device files differ that has not yet been mentioned is that they represent different kernel major numbers. If you follow that link you will land on a kernel source documentation page containing a table of mostly all of the allocated device major numbers. This same file is also very likely on your harddisk somewhere.

Here's what a quick CTRL-F has surfaced for me regarding your question:

112 block IBM iSeries virtual disk
      0 = /dev/iseries/vda  First virtual disk, whole disk
      8 = /dev/iseries/vdb  Second virtual disk, whole disk
        ...
    200 = /dev/iseries/vdz  26th virtual disk, whole disk
    208 = /dev/iseries/vdaa 27th virtual disk, whole disk
        ...
    248 = /dev/iseries/vdaf 32nd virtual disk, whole disk

    Partitions are handled in the same way as for IDE
    disks (see major number 3) except that the limit on
    partitions is 7.

The 112 number listed at top indicates the allocated major number and the various 0...8...200 numbers listed before each line indicate the possible minor number ranges. These numbers, among many other things, are used by the linux kernel to pair driver modules with physical devices - they identify a device type.

The above list accounts for 3 of 4 matches found with CTRL-F - here's the fourth:

202 block   Xen Virtual Block Device
      0 = /dev/xvda       First Xen VBD whole disk
      16 = /dev/xvdb      Second Xen VBD whole disk
      32 = /dev/xvdc      Third Xen VBD whole disk
        ...
      240 = /dev/xvdp     Sixteenth Xen VBD whole disk

            Partitions are handled in the same way as for IDE
            disks (see major number 3) except that the limit on
            partitions is 15.

There definitely seems to be a trend among vda block types. Note that these may not be completely up to date in the documentation - even as linked at kernel.org. The devs are, after all, only human. But another possible source of discrepancy is where your distribution's maintainers chose to locate the devices. A great number of the paths listed amount to little more than suggestions - there are many ways to relocate them.

And here's sda:

8 block SCSI disk devices (0-15)
      0 = /dev/sda      First SCSI disk whole disk
     16 = /dev/sdb      Second SCSI disk whole disk
     32 = /dev/sdc      Third SCSI disk whole disk
        ...
    240 = /dev/sdp      Sixteenth SCSI disk whole disk

    Partitions are handled in the same way as for IDE
    disks (see major number 3) except that the limit on
    partitions is 15.

As already noted - these paths are eminently configurable - but the major/minor numbers themselves serve as pretty explicit identifiers. You can check these with a stat call to a device file.

stat /dev/sda

  File: ‘/dev/sda’
  Size: 0               Blocks: 0          IO Block: 4096   block special file
Device: 5h/5d   Inode: 7598        Links: 1     Device type: 8,0
Access: (0660/brw-rw----)  Uid: (    0/    root)   Gid: (    6/    disk)
Access: 2014-07-18 11:10:13.112495427 -0700
Modify: 2014-07-16 18:59:41.313323401 -0700
Change: 2014-07-16 18:59:41.313323401 -0700
 Birth: -

That's my /dev/sda. There's a lot of information there, but to the right you can see that its Device Type is specified as major number 8, minor number 0. You can id pretty much any device file that way by comparing its maj/min to the table linked.

mikeserv
  • 58,310