Bug#632832: ufsutils: growfs.ufs do not work
Hello all,
The " bogus sectorsize: 0" error is because the get_dev_size() function
in growfs.c only works when using the FreeBSD kernel. The sectorsize and
mediasize local variables get initialized only if HAVE_BSD_DISKLABEL is
defined, and this gets defined only when compiling the source under
Debian GNU/kFreeBSD.
Please find attached a patch that makes the function work under Linux
too. In order to accomplish this, the patch will add a
get_block_device_sectors() function under libport/blockdev.c. The
get_dev_size() function will call the latter if HAVE_BSD_DISKLABEL is
not defined.
The patch also adds a get_sector_size() function that returns the value
of the hard-coded sector_size variable that was already present in
libprot/blockdev.c. This function is currently not being used, so feel
free to remove it if you want.
I'm not sure that hard-coding the sector_size is the right thing to do.
You can always ask the kernel for the sector size value by triggering an
appropriate ioctl system call:
ioctl(fd, DIOCGSECTORSIZE, §orsize) (for freeBSD)
ioctl(fd, BLKSSZGET, §orsize) (for Linux)
but I haven't changed this to make the patch as less intrusive as possible.
Regards,
Nikos Skalkotos
--- a/growfs.ufs/growfs.c
+++ b/growfs.ufs/growfs.c
@@ -1926,20 +1926,22 @@
static void
get_dev_size(int fd, int *size)
{
+#ifdef HAVE_BSD_DISKLABEL
int sectorsize;
off_t mediasize;
-#ifdef HAVE_BSD_DISKLABEL
if (ioctl(fd, DIOCGSECTORSIZE, §orsize) == -1)
err(1,"DIOCGSECTORSIZE");
if (ioctl(fd, DIOCGMEDIASIZE, &mediasize) == -1)
err(1,"DIOCGMEDIASIZE");
-#endif
if (sectorsize <= 0)
errx(1, "bogus sectorsize: %d", sectorsize);
*size = mediasize / sectorsize;
+#else
+ *size = get_block_device_sectors(fd);
+#endif
}
/* ************************************************************** main ***** */
--- a/include/port/blockdev.h
+++ b/include/port/blockdev.h
@@ -4,6 +4,8 @@
#include <sys/types.h>
int64_t get_block_device_size(int fd);
+int64_t get_block_device_sectors(int fd);
+int get_sector_size(int fd);
#endif
--- a/libport/blockdev.c
+++ b/libport/blockdev.c
@@ -48,6 +48,16 @@
static const int sector_size = 512;
+int
+get_sector_size(int fd)
+{
+ /* In Linux you can ask the OS for the sector size with this syscall:
+ * ioctl(fd, BLKSSZGET, §or_size)
+ */
+ return sector_size;
+}
+
+
int64_t
get_block_device_size(int fd)
{
@@ -90,3 +100,9 @@
return size;
}
+
+int64_t
+get_block_device_sectors(int fd)
+{
+ return get_block_device_size(fd) / sector_size;
+}
--- a/growfs.ufs/Makefile
+++ b/growfs.ufs/Makefile
@@ -16,8 +16,8 @@
endif
WARNS = 6
-LDADD += -L../libufs -lufs
-INCLUDES =
+LDADD += -L../libufs -lufs -L../libport -lport
+INCLUDES = -I../include -include port/blockdev.h
include ../Makefile.common
Reply to: