diff --git a/block/raw-posix.c b/block/raw-posix.c
index 72fb8ce..444fb2d 100644
--- a/block/raw-posix.c
+++ b/block/raw-posix.c
@@ -62,6 +62,12 @@
 #include <sys/disklabel.h>
 #include <sys/dkio.h>
 #endif
+#if defined(__NetBSD__)
+#include <sys/ioctl.h>
+#include <sys/disklabel.h>
+#include <sys/dkio.h>
+#include <sys/disk.h>
+#endif
 
 #ifdef __DragonFly__
 #include <sys/ioctl.h>
@@ -595,6 +601,28 @@ static int64_t raw_getlength(BlockDriverState *bs)
     } else
         return st.st_size;
 }
+#elif defined(__NetBSD__)
+static int64_t  raw_getlength(BlockDriverState *bs)
+{
+	int fd = ((BDRVRawState*)bs->opaque)->fd;
+	struct stat st;
+	if (fstat(fd, &st))
+		return -1;
+	if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode)) {
+		struct dkwedge_info dkw;
+		if (ioctl(fd, DIOCGWEDGEINFO, &dkw) != -1) {
+			/* NetBSD still supports only 512 bytes for disk sector */
+			return dkw.dkw_size * 512;
+		} else {
+			struct disklabel dl;
+			if(ioctl(fd, DIOCGDINFO, &dl))
+				return -1;
+			return (uint64_t)dl.d_secsize *
+			    dl.d_partitions[DISKPART(st.st_rdev)].p_size;
+		}
+	} else
+		return st.st_size;
+}
 #elif defined(__sun__)
 static int64_t raw_getlength(BlockDriverState *bs)
 {