2 libparted - a library for manipulating disk partitions
3 Copyright (C) 1999 - 2005, 2007 Free Software Foundation, Inc.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
19 #define PROC_DEVICES_BUFSIZ 16384
23 #include <parted/parted.h>
24 #include <parted/debug.h>
25 #include <parted/linux.h>
35 #include <sys/ioctl.h>
37 #include <sys/types.h>
38 #include <sys/utsname.h> /* for uname() */
39 #include <scsi/scsi.h>
40 #ifdef ENABLE_DEVICE_MAPPER
41 #include <libdevmapper.h>
48 # define _(String) dgettext (PACKAGE, String)
50 # define _(String) (String)
51 #endif /* ENABLE_NLS */
53 #define KERNEL_VERSION(a,b,c) (((a) << 16) + ((b) << 8) + (c))
56 #define __NR__llseek 140
59 #ifndef SCSI_IOCTL_SEND_COMMAND
60 #define SCSI_IOCTL_SEND_COMMAND 1
63 /* from <linux/hdreg.h> */
64 #define HDIO_GETGEO 0x0301 /* get device geometry */
65 #define HDIO_GET_IDENTITY 0x030d /* get IDE identification info */
67 #if defined(O_DIRECT) && (!defined(__s390__) || !defined(__s390x__))
68 #define RD_MODE (O_RDONLY | O_DIRECT)
69 #define WR_MODE (O_WRONLY | O_DIRECT)
70 #define RW_MODE (O_RDWR | O_DIRECT)
72 #define RD_MODE (O_RDONLY)
73 #define WR_MODE (O_WRONLY)
74 #define RW_MODE (O_RDWR)
79 unsigned char sectors;
80 unsigned short cylinders;
84 struct ata7_sectinfo {
91 /* structure returned by HDIO_GET_IDENTITY, as per ANSI ATA2 rev.2f spec */
93 unsigned short config; /* lots of obsolete bit flags */
94 unsigned short cyls; /* "physical" cyls */
95 unsigned short reserved2; /* reserved (word 2) */
96 unsigned short heads; /* "physical" heads */
97 unsigned short track_bytes; /* unformatted bytes per track */
98 unsigned short sector_bytes; /* unformatted bytes per sector */
99 unsigned short sectors; /* "physical" sectors per track */
100 unsigned short vendor0; /* vendor unique */
101 unsigned short vendor1; /* vendor unique */
102 unsigned short vendor2; /* vendor unique */
103 unsigned char serial_no[20]; /* 0 = not_specified */
104 unsigned short buf_type;
105 unsigned short buf_size; /* 512 byte increments;
107 unsigned short ecc_bytes; /* for r/w long cmds;
109 unsigned char fw_rev[8]; /* 0 = not_specified */
110 char model[40]; /* 0 = not_specified */
111 unsigned char max_multsect; /* 0=not_implemented */
112 unsigned char vendor3; /* vendor unique */
113 unsigned short dword_io; /* 0=not_implemented; 1=implemented */
114 unsigned char vendor4; /* vendor unique */
115 unsigned char capability; /* bits 0:DMA 1:LBA 2:IORDYsw
117 unsigned short reserved50; /* reserved (word 50) */
118 unsigned char vendor5; /* vendor unique */
119 unsigned char tPIO; /* 0=slow, 1=medium, 2=fast */
120 unsigned char vendor6; /* vendor unique */
121 unsigned char tDMA; /* 0=slow, 1=medium, 2=fast */
122 unsigned short field_valid; /* bits 0:cur_ok 1:eide_ok */
123 unsigned short cur_cyls; /* logical cylinders */
124 unsigned short cur_heads; /* logical heads */
125 unsigned short cur_sectors; /* logical sectors per track */
126 unsigned short cur_capacity0; /* logical total sectors on drive */
127 unsigned short cur_capacity1; /* (2 words, misaligned int) */
128 unsigned char multsect; /* current multiple sector count */
129 unsigned char multsect_valid; /* when (bit0==1) multsect is ok */
130 unsigned int lba_capacity; /* total number of sectors */
131 unsigned short dma_1word; /* single-word dma info */
132 unsigned short dma_mword; /* multiple-word dma info */
133 unsigned short eide_pio_modes; /* bits 0:mode3 1:mode4 */
134 unsigned short eide_dma_min; /* min mword dma cycle time (ns) */
135 unsigned short eide_dma_time; /* recommended mword dma cycle
137 unsigned short eide_pio; /* min cycle time (ns), no IORDY */
138 unsigned short eide_pio_iordy; /* min cycle time (ns), with IORDY */
139 unsigned short words69_70[2]; /* reserved words 69-70 */
140 /* HDIO_GET_IDENTITY currently returns only words 0 through 70 */
141 unsigned short words71_74[4]; /* reserved words 71-74 */
142 unsigned short queue_depth; /* */
143 unsigned short words76_79[4]; /* reserved words 76-79 */
144 unsigned short major_rev_num; /* */
145 unsigned short minor_rev_num; /* */
146 unsigned short command_set_1; /* bits 0:Smart 1:Security 2:Removable
148 unsigned short command_set_2; /* bits 14:Smart Enabled 13:0 zero */
149 unsigned short cfsse; /* command set-feature supported
151 unsigned short cfs_enable_1; /* command set-feature enabled */
152 unsigned short cfs_enable_2; /* command set-feature enabled */
153 unsigned short csf_default; /* command set-feature default */
154 unsigned short dma_ultra; /* */
155 unsigned short word89; /* reserved (word 89) */
156 unsigned short word90; /* reserved (word 90) */
157 unsigned short CurAPMvalues; /* current APM values */
158 unsigned short word92; /* reserved (word 92) */
159 unsigned short hw_config; /* hardware config */
160 unsigned short words94_105[12];/* reserved words 94-105 */
161 struct ata7_sectinfo ata7_sectinfo; /* ATAPI/ATA7 physical and logical
163 unsigned short words107_116[10];/* reserved words 107-116 */
164 unsigned int logical_sectsize;/* ATAPI/ATA7 logical sector size */
165 unsigned short words119_125[7];/* reserved words 119-125 */
166 unsigned short last_lun; /* reserved (word 126) */
167 unsigned short word127; /* reserved (word 127) */
168 unsigned short dlf; /* device lock function
170 * 8 security level 1:max 0:high
179 unsigned short csfo; /* current set features options
186 unsigned short words130_155[26];/* reserved vendor words 130-155 */
187 unsigned short word156;
188 unsigned short words157_159[3]; /* reserved vendor words 157-159 */
189 unsigned short words160_255[95];/* reserved words 160-255 */
192 /* from <linux/fs.h> */
193 #define BLKRRPART _IO(0x12,95) /* re-read partition table */
194 #define BLKGETSIZE _IO(0x12,96) /* return device size */
195 #define BLKFLSBUF _IO(0x12,97) /* flush buffer cache */
196 #define BLKSSZGET _IO(0x12,104) /* get block device sector size */
197 #define BLKGETLASTSECT _IO(0x12,108) /* get last sector of block device */
198 #define BLKSETLASTSECT _IO(0x12,109) /* set last sector of block device */
200 /* return device size in bytes (u64 *arg) */
201 #define BLKGETSIZE64 _IOR(0x12,114,size_t)
203 struct blkdev_ioctl_param {
205 size_t content_length;
206 char * block_contents;
209 /* from <linux/major.h> */
211 #define IDE1_MAJOR 22
212 #define IDE2_MAJOR 33
213 #define IDE3_MAJOR 34
214 #define IDE4_MAJOR 56
215 #define IDE5_MAJOR 57
216 #define SCSI_CDROM_MAJOR 11
217 #define SCSI_DISK0_MAJOR 8
218 #define SCSI_DISK1_MAJOR 65
219 #define SCSI_DISK2_MAJOR 66
220 #define SCSI_DISK3_MAJOR 67
221 #define SCSI_DISK4_MAJOR 68
222 #define SCSI_DISK5_MAJOR 69
223 #define SCSI_DISK6_MAJOR 70
224 #define SCSI_DISK7_MAJOR 71
225 #define COMPAQ_SMART2_MAJOR 72
226 #define COMPAQ_SMART2_MAJOR1 73
227 #define COMPAQ_SMART2_MAJOR2 74
228 #define COMPAQ_SMART2_MAJOR3 75
229 #define COMPAQ_SMART2_MAJOR4 76
230 #define COMPAQ_SMART2_MAJOR5 77
231 #define COMPAQ_SMART2_MAJOR6 78
232 #define COMPAQ_SMART2_MAJOR7 79
233 #define COMPAQ_SMART_MAJOR 104
234 #define COMPAQ_SMART_MAJOR1 105
235 #define COMPAQ_SMART_MAJOR2 106
236 #define COMPAQ_SMART_MAJOR3 107
237 #define COMPAQ_SMART_MAJOR4 108
238 #define COMPAQ_SMART_MAJOR5 109
239 #define COMPAQ_SMART_MAJOR6 110
240 #define COMPAQ_SMART_MAJOR7 111
241 #define DAC960_MAJOR 48
242 #define ATARAID_MAJOR 114
243 #define I2O_MAJOR1 80
244 #define I2O_MAJOR2 81
245 #define I2O_MAJOR3 82
246 #define I2O_MAJOR4 83
247 #define I2O_MAJOR5 84
248 #define I2O_MAJOR6 85
249 #define I2O_MAJOR7 86
250 #define I2O_MAJOR8 87
252 #define DASD_MAJOR 94
253 #define VIODASD_MAJOR 112
254 #define SX8_MAJOR1 160
255 #define SX8_MAJOR2 161
257 #define SCSI_BLK_MAJOR(M) ( \
258 (M) == SCSI_DISK0_MAJOR \
259 || (M) == SCSI_CDROM_MAJOR \
260 || ((M) >= SCSI_DISK1_MAJOR && (M) <= SCSI_DISK7_MAJOR))
262 static char* _device_get_part_path (PedDevice* dev, int num);
263 static int _partition_is_mounted_by_path (const char* path);
266 _is_ide_major (int major)
283 _is_cpqarray_major (int major)
285 return ((COMPAQ_SMART2_MAJOR <= major && major <= COMPAQ_SMART2_MAJOR7)
286 || (COMPAQ_SMART_MAJOR <= major && major <= COMPAQ_SMART_MAJOR7));
290 _is_i2o_major (int major)
292 return (I2O_MAJOR1 <= major && major <= I2O_MAJOR8);
296 _is_sx8_major (int major)
298 return (SX8_MAJOR1 <= major && major <= SX8_MAJOR2);
301 #ifdef ENABLE_DEVICE_MAPPER
303 readFD (int fd, char **buf)
306 size_t size = PROC_DEVICES_BUFSIZ;
309 *buf = malloc (size * sizeof (char));
315 p = &(*buf) [filesize];
316 s = read (fd, p, PROC_DEVICES_BUFSIZ);
317 /* exit if there is an error or EOF is reached */
322 *buf = realloc (*buf, size);
325 if (filesize == 0 && s < 0) {
330 /* there is always some excess memory left unused */
331 *buf = realloc (*buf, filesize+1);
332 (*buf)[filesize] = '\0';
339 _is_dm_major (int major)
348 fd = open ("/proc/devices", O_RDONLY);
352 if (readFD(fd, &buf) < 0) {
358 end = strchr(line, '\n');
367 if (!strncmp(line, "Block devices:", 14))
372 name = strrchr(line, ' ');
373 if (!name || strcmp(name+1, "device-mapper"))
376 maj = strtol(line, &name, 10);
386 end = strchr(line, '\n');
395 _device_stat (PedDevice* dev, struct stat * dev_stat)
397 PED_ASSERT (dev != NULL, return 0);
398 PED_ASSERT (!dev->external_mode, return 0);
401 if (!stat (dev->path, dev_stat)) {
404 if (ped_exception_throw (
406 PED_EXCEPTION_RETRY_CANCEL,
407 _("Could not stat device %s - %s."),
410 != PED_EXCEPTION_RETRY)
417 _device_probe_type (PedDevice* dev)
419 struct stat dev_stat;
423 if (!_device_stat (dev, &dev_stat))
426 if (!S_ISBLK(dev_stat.st_mode)) {
427 dev->type = PED_DEVICE_FILE;
431 dev_major = major (dev_stat.st_rdev);
432 dev_minor = minor (dev_stat.st_rdev);
434 if (SCSI_BLK_MAJOR (dev_major) && (dev_minor % 0x10 == 0)) {
435 dev->type = PED_DEVICE_SCSI;
436 } else if (_is_ide_major (dev_major) && (dev_minor % 0x40 == 0)) {
437 dev->type = PED_DEVICE_IDE;
438 } else if (dev_major == DAC960_MAJOR && (dev_minor % 0x8 == 0)) {
439 dev->type = PED_DEVICE_DAC960;
440 } else if (dev_major == ATARAID_MAJOR && (dev_minor % 0x10 == 0)) {
441 dev->type = PED_DEVICE_ATARAID;
442 } else if (dev_major == DASD_MAJOR && (dev_minor % 0x4 == 0)) {
443 dev->type = PED_DEVICE_DASD;
444 } else if (dev_major == VIODASD_MAJOR && (dev_minor % 0x8 == 0)) {
445 dev->type = PED_DEVICE_VIODASD;
446 } else if (_is_sx8_major(dev_major) && (dev_minor % 0x20 == 0)) {
447 dev->type = PED_DEVICE_SX8;
448 } else if (_is_i2o_major (dev_major) && (dev_minor % 0x10 == 0)) {
449 dev->type = PED_DEVICE_I2O;
450 } else if (_is_cpqarray_major (dev_major) && (dev_minor % 0x10 == 0)) {
451 dev->type = PED_DEVICE_CPQARRAY;
452 } else if (dev_major == UBD_MAJOR && (dev_minor % 0x10 == 0)) {
453 dev->type = PED_DEVICE_UBD;
454 #ifdef ENABLE_DEVICE_MAPPER
455 } else if (_is_dm_major(dev_major)) {
456 dev->type = PED_DEVICE_DM;
459 dev->type = PED_DEVICE_UNKNOWN;
466 _get_linux_version ()
468 static int kver = -1;
480 if (sscanf (uts.release, "%u.%u.%u", &major, &minor, &teeny) != 3)
483 return kver = KERNEL_VERSION (major, minor, teeny);
489 static int have_devfs = -1;
492 if (have_devfs != -1)
495 /* the presence of /dev/.devfsd implies that DevFS is active */
496 if (stat("/dev/.devfsd", &sb) < 0)
497 return have_devfs = 0;
499 return have_devfs = S_ISCHR(sb.st_mode) ? 1 : 0;
503 _device_set_sector_size (PedDevice* dev)
505 LinuxSpecific* arch_specific = LINUX_SPECIFIC (dev);
508 dev->sector_size = PED_SECTOR_SIZE_DEFAULT;
509 dev->phys_sector_size = PED_SECTOR_SIZE_DEFAULT;
511 PED_ASSERT (dev->open_count, return);
513 if (_get_linux_version() < KERNEL_VERSION (2,3,0)) {
514 dev->sector_size = PED_SECTOR_SIZE_DEFAULT;
518 if (ioctl (arch_specific->fd, BLKSSZGET, §or_size)) {
519 ped_exception_throw (
520 PED_EXCEPTION_WARNING,
522 _("Could not determine sector size for %s: %s.\n"
523 "Using the default sector size (%lld)."),
524 dev->path, strerror (errno), PED_SECTOR_SIZE_DEFAULT);
526 dev->sector_size = (long long)sector_size;
529 /* Return PED_SECTOR_SIZE_DEFAULT for DASDs. */
530 if (dev->type == PED_DEVICE_DASD) {
531 dev->sector_size = PED_SECTOR_SIZE_DEFAULT;
534 if (dev->sector_size != PED_SECTOR_SIZE_DEFAULT) {
535 ped_exception_throw (
536 PED_EXCEPTION_WARNING,
538 _("Device %s has a logical sector size of %lld. Not "
539 "all parts of GNU Parted support this at the moment, "
540 "and the working code is HIGHLY EXPERIMENTAL.\n"),
541 dev->path, dev->sector_size);
546 _kernel_has_blkgetsize64(void)
548 int version = _get_linux_version();
550 if (version >= KERNEL_VERSION (2,5,4)) return 1;
551 if (version < KERNEL_VERSION (2,5,0) &&
552 version >= KERNEL_VERSION (2,4,18)) return 1;
556 /* TODO: do a binary search if BLKGETSIZE doesn't work?! */
558 _device_get_length (PedDevice* dev)
561 LinuxSpecific* arch_specific = LINUX_SPECIFIC (dev);
565 PED_ASSERT (dev->open_count > 0, return 0);
566 PED_ASSERT (dev->sector_size % PED_SECTOR_SIZE_DEFAULT == 0, return 0);
568 if (_kernel_has_blkgetsize64()) {
569 if (ioctl(arch_specific->fd, BLKGETSIZE64, &bytes) == 0) {
570 return bytes / dev->sector_size;
574 if (ioctl (arch_specific->fd, BLKGETSIZE, &size)) {
575 ped_exception_throw (
577 PED_EXCEPTION_CANCEL,
578 _("Unable to determine the size of %s (%s)."),
588 _device_probe_geometry (PedDevice* dev)
590 LinuxSpecific* arch_specific = LINUX_SPECIFIC (dev);
591 struct stat dev_stat;
592 struct hd_geometry geometry;
594 if (!_device_stat (dev, &dev_stat))
596 PED_ASSERT (S_ISBLK (dev_stat.st_mode), return 0);
598 _device_set_sector_size (dev);
600 dev->length = _device_get_length (dev);
604 /* The GETGEO ioctl is no longer useful (as of linux 2.6.x). We could
605 * still use it in 2.4.x, but this is contentious. Perhaps we should
607 dev->bios_geom.sectors = 63;
608 dev->bios_geom.heads = 255;
609 dev->bios_geom.cylinders
610 = dev->length / (63 * 255);
612 /* FIXME: what should we put here? (TODO: discuss on linux-kernel) */
613 if (!ioctl (arch_specific->fd, HDIO_GETGEO, &geometry)
614 && geometry.sectors && geometry.heads) {
615 dev->hw_geom.sectors = geometry.sectors;
616 dev->hw_geom.heads = geometry.heads;
617 dev->hw_geom.cylinders
618 = dev->length / (dev->hw_geom.heads
619 * dev->hw_geom.sectors);
621 dev->hw_geom = dev->bios_geom;
628 strip_name(char* str)
633 for (i = 0; str[i] != 0; i++) {
634 if (!isspace (str[i])
635 || (isspace (str[i]) && !isspace (str[i+1]) && str[i+1])) {
645 init_ide (PedDevice* dev)
647 LinuxSpecific* arch_specific = LINUX_SPECIFIC (dev);
648 struct stat dev_stat;
650 struct hd_driveid hdi;
651 PedExceptionOption ex_status;
653 int sector_multiplier = 0;
655 if (!_device_stat (dev, &dev_stat))
658 dev_major = major (dev_stat.st_rdev);
660 if (!ped_device_open (dev))
663 if (ioctl (arch_specific->fd, HDIO_GET_IDENTITY, &hdi)) {
664 ex_status = ped_exception_throw (
665 PED_EXCEPTION_WARNING,
666 PED_EXCEPTION_IGNORE_CANCEL,
667 _("Could not get identity of device %s - %s"),
668 dev->path, strerror (errno));
670 case PED_EXCEPTION_CANCEL:
671 goto error_close_dev;
673 case PED_EXCEPTION_UNHANDLED:
674 ped_exception_catch ();
675 case PED_EXCEPTION_IGNORE:
676 dev->model = strdup(_("Generic IDE"));
679 PED_ASSERT (0, (void) 0);
683 /* hdi.model is not guaranteed to be NULL terminated */
684 memcpy (hdi_buf, hdi.model, 40);
686 dev->model = strip_name (hdi_buf);
688 if (!hdi.ata7_sectinfo.valid1 && hdi.ata7_sectinfo.valid2)
689 sector_multiplier = hdi.ata7_sectinfo.multiplier;
691 sector_multiplier = 1;
693 if (sector_multiplier != 1) {
694 ex_status = ped_exception_throw (
695 PED_EXCEPTION_WARNING,
696 PED_EXCEPTION_IGNORE_CANCEL,
697 _("Device %s has multiple (%d) logical sectors "
698 "per physical sector.\n"
699 "GNU Parted supports this EXPERIMENTALLY for "
700 "some special disk label/file system "
701 "combinations, e.g. GPT and ext2/3.\n"
702 "Please consult the web site for up-to-date "
704 dev->path, sector_multiplier);
707 case PED_EXCEPTION_CANCEL:
708 goto error_close_dev;
710 case PED_EXCEPTION_UNHANDLED:
711 ped_exception_catch ();
712 case PED_EXCEPTION_IGNORE:
715 PED_ASSERT (0, (void) 0);
720 /* XXX sector_size has not been set yet! */
721 /* dev->phys_sector_size = dev->sector_size
722 * sector_multiplier;*/
723 dev->phys_sector_size = PED_SECTOR_SIZE_DEFAULT;
726 if (!_device_probe_geometry (dev))
727 goto error_close_dev;
729 ped_device_close (dev);
733 ped_device_close (dev);
738 /* This function reads the /sys entry named "file" for device "dev". */
740 read_device_sysfs_file (PedDevice *dev, const char *file)
746 snprintf (name_buf, 127, "/sys/block/%s/device/%s",
747 basename (dev->path), file);
749 if ((f = fopen (name_buf, "r")) == NULL)
752 if (fgets (buf, 255, f) == NULL)
756 return strip_name (buf);
759 /* This function sends a query to a SCSI device for vendor and product
760 * information. It uses the deprecated SCSI_IOCTL_SEND_COMMAND to
764 scsi_query_product_info (PedDevice* dev, char **vendor, char **product)
766 /* The following are defined by the SCSI-2 specification. */
767 typedef struct _scsi_inquiry_cmd
770 uint8_t lun; /* bits 5-7 denote the LUN */
773 uint8_t alloc_length;
775 } __attribute__((packed)) scsi_inquiry_cmd_t;
777 typedef struct _scsi_inquiry_data
779 uint8_t peripheral_info;
781 uint8_t version_info;
783 uint8_t additional_length;
787 uint8_t vendor_id[8];
788 uint8_t product_id[16];
789 uint8_t product_revision[4];
790 uint8_t vendor_specific[20];
791 uint8_t _reserved3[40];
792 } __attribute__((packed)) scsi_inquiry_data_t;
801 scsi_inquiry_data_t out;
802 scsi_inquiry_cmd_t in;
806 LinuxSpecific* arch_specific = LINUX_SPECIFIC (dev);
812 memset (&arg, 0x00, sizeof(struct scsi_arg));
814 arg.outlen = sizeof(scsi_inquiry_data_t);
815 arg.data.in.op = INQUIRY;
816 arg.data.in.lun = dev->host << 5;
817 arg.data.in.alloc_length = sizeof(scsi_inquiry_data_t);
818 arg.data.in.page_code = 0;
819 arg.data.in.reserved = 0;
820 arg.data.in.control = 0;
822 if (ioctl (arch_specific->fd, SCSI_IOCTL_SEND_COMMAND, &arg) < 0)
825 memcpy (buf, arg.data.out.vendor_id, 8);
827 *vendor = strip_name (buf);
829 memcpy (buf, arg.data.out.product_id, 16);
831 *product = strip_name (buf);
836 /* This function provides the vendor and product name for a SCSI device.
837 * It supports both the modern /sys interface and direct queries
838 * via the deprecated ioctl, SCSI_IOCTL_SEND_COMMAND.
841 scsi_get_product_info (PedDevice* dev, char **vendor, char **product)
843 *vendor = read_device_sysfs_file (dev, "vendor");
844 *product = read_device_sysfs_file (dev, "model");
845 if (*vendor && *product)
848 return scsi_query_product_info (dev, vendor, product);
852 init_scsi (PedDevice* dev)
857 uint32_t host_unique_id;
860 LinuxSpecific* arch_specific = LINUX_SPECIFIC (dev);
864 if (!ped_device_open (dev))
867 if (ioctl (arch_specific->fd, SCSI_IOCTL_GET_IDLUN, &idlun) < 0) {
870 if (ped_exception_throw (
871 PED_EXCEPTION_ERROR, PED_EXCEPTION_IGNORE_CANCEL,
872 _("Error initialising SCSI device %s - %s"),
873 dev->path, strerror (errno))
874 != PED_EXCEPTION_IGNORE)
875 goto error_close_dev;
876 if (!_device_probe_geometry (dev))
877 goto error_close_dev;
878 ped_device_close (dev);
882 dev->host = idlun.host_unique_id;
883 dev->did = idlun.dev_id;
885 dev->model = (char*) ped_malloc (8 + 16 + 2);
887 goto error_close_dev;
889 if (scsi_get_product_info (dev, &vendor, &product)) {
890 sprintf (dev->model, "%.8s %.16s", vendor, product);
894 strcpy (dev->model, "Generic SCSI");
897 if (!_device_probe_geometry (dev))
898 goto error_close_dev;
900 ped_device_close (dev);
904 ped_device_close (dev);
910 init_file (PedDevice* dev)
912 struct stat dev_stat;
914 if (!_device_stat (dev, &dev_stat))
916 if (!ped_device_open (dev))
919 if (S_ISBLK(dev_stat.st_mode))
920 dev->length = _device_get_length (dev);
922 dev->length = dev_stat.st_size / 512;
923 if (dev->length <= 0) {
924 ped_exception_throw (
926 PED_EXCEPTION_CANCEL,
927 _("The device %s has zero length, and can't possibly "
928 "store a file system or partition table. Perhaps "
929 "you selected the wrong device?"),
931 goto error_close_dev;
934 ped_device_close (dev);
936 dev->bios_geom.cylinders = dev->length / 4 / 32;
937 dev->bios_geom.heads = 4;
938 dev->bios_geom.sectors = 32;
939 dev->hw_geom = dev->bios_geom;
940 dev->sector_size = PED_SECTOR_SIZE_DEFAULT;
941 dev->phys_sector_size = PED_SECTOR_SIZE_DEFAULT;
942 dev->model = strdup ("");
947 ped_device_close (dev);
953 init_dasd (PedDevice* dev, char* model_name)
955 struct stat dev_stat;
956 struct hd_geometry geo;
959 if (!_device_stat (dev, &dev_stat))
962 if (!ped_device_open (dev))
965 LinuxSpecific* arch_specific = LINUX_SPECIFIC (dev);
967 PED_ASSERT (S_ISBLK (dev_stat.st_mode), return 0);
969 _device_set_sector_size (dev);
970 if (!dev->sector_size)
971 goto error_close_dev;
973 dev->length = _device_get_length (dev);
975 goto error_close_dev;
977 if (!ioctl (arch_specific->fd, HDIO_GETGEO, &geo)) {
978 dev->hw_geom.sectors = geo.sectors;
979 dev->hw_geom.heads = geo.heads;
980 dev->hw_geom.cylinders = dev->length
981 / (dev->hw_geom.heads * dev->hw_geom.sectors)
982 / (dev->sector_size / PED_SECTOR_SIZE_DEFAULT);
983 dev->bios_geom = dev->hw_geom;
985 dev->bios_geom.sectors = 12;
986 dev->bios_geom.heads = 15;
987 dev->bios_geom.cylinders = dev->length
988 / (dev->hw_geom.heads * dev->hw_geom.sectors)
989 / (dev->sector_size / PED_SECTOR_SIZE_DEFAULT);
990 dev->hw_geom = dev->bios_geom;
993 dev->model = strdup (model_name);
995 ped_device_close (dev);
998 ped_exception_throw ( PED_EXCEPTION_ERROR,
999 PED_EXCEPTION_IGNORE_CANCEL,
1003 ped_device_close (dev);
1009 init_generic (PedDevice* dev, char* model_name)
1011 struct stat dev_stat;
1012 PedExceptionOption ex_status;
1014 if (!_device_stat (dev, &dev_stat))
1017 if (!ped_device_open (dev))
1020 ped_exception_fetch_all ();
1021 if (_device_probe_geometry (dev)) {
1022 ped_exception_leave_all ();
1024 /* hack to allow use of files, for testing */
1025 ped_exception_catch ();
1026 ped_exception_leave_all ();
1028 ex_status = ped_exception_throw (
1029 PED_EXCEPTION_WARNING,
1030 PED_EXCEPTION_IGNORE_CANCEL,
1031 _("Unable to determine geometry of "
1032 "file/device %s. You should not use Parted "
1033 "unless you REALLY know what you're doing!"),
1035 switch (ex_status) {
1036 case PED_EXCEPTION_CANCEL:
1037 goto error_close_dev;
1039 case PED_EXCEPTION_UNHANDLED:
1040 ped_exception_catch ();
1041 case PED_EXCEPTION_IGNORE:
1044 PED_ASSERT (0, (void) 0);
1048 /* what should we stick in here? */
1049 dev->length = dev_stat.st_size / PED_SECTOR_SIZE_DEFAULT;
1050 dev->bios_geom.cylinders = dev->length / 4 / 32;
1051 dev->bios_geom.heads = 4;
1052 dev->bios_geom.sectors = 32;
1053 dev->sector_size = PED_SECTOR_SIZE_DEFAULT;
1054 dev->phys_sector_size = PED_SECTOR_SIZE_DEFAULT;
1057 dev->model = strdup (model_name);
1059 ped_device_close (dev);
1063 ped_device_close (dev);
1069 linux_new (const char* path)
1073 PED_ASSERT (path != NULL, return NULL);
1075 dev = (PedDevice*) ped_malloc (sizeof (PedDevice));
1079 dev->path = strdup (path);
1081 goto error_free_dev;
1084 = (LinuxSpecific*) ped_malloc (sizeof (LinuxSpecific));
1085 if (!dev->arch_specific)
1086 goto error_free_path;
1088 dev->open_count = 0;
1090 dev->external_mode = 0;
1092 dev->boot_dirty = 0;
1094 if (!_device_probe_type (dev))
1095 goto error_free_arch_specific;
1097 switch (dev->type) {
1098 case PED_DEVICE_IDE:
1099 if (!init_ide (dev))
1100 goto error_free_arch_specific;
1103 case PED_DEVICE_SCSI:
1104 if (!init_scsi (dev))
1105 goto error_free_arch_specific;
1108 case PED_DEVICE_DAC960:
1109 if (!init_generic (dev, _("DAC960 RAID controller")))
1110 goto error_free_arch_specific;
1113 case PED_DEVICE_SX8:
1114 if (!init_generic (dev, _("Promise SX8 SATA Device")))
1115 goto error_free_arch_specific;
1118 case PED_DEVICE_DASD:
1119 if (!init_dasd (dev, _("IBM S390 DASD drive")))
1120 goto error_free_arch_specific;
1123 case PED_DEVICE_VIODASD:
1124 if (!init_generic (dev, _("IBM iSeries Virtual DASD")))
1125 goto error_free_arch_specific;
1128 case PED_DEVICE_CPQARRAY:
1129 if (!init_generic (dev, _("Compaq Smart Array")))
1130 goto error_free_arch_specific;
1133 case PED_DEVICE_ATARAID:
1134 if (!init_generic (dev, _("ATARAID Controller")))
1135 goto error_free_arch_specific;
1138 case PED_DEVICE_I2O:
1139 if (!init_generic (dev, _("I2O Controller")))
1140 goto error_free_arch_specific;
1143 case PED_DEVICE_UBD:
1144 if (!init_generic (dev, _("User-Mode Linux UBD")))
1145 goto error_free_arch_specific;
1148 case PED_DEVICE_FILE:
1149 if (!init_file (dev))
1150 goto error_free_arch_specific;
1153 #ifdef ENABLE_DEVICE_MAPPER
1155 if (!init_generic (dev, _("Linux device-mapper")))
1156 goto error_free_arch_specific;
1160 case PED_DEVICE_UNKNOWN:
1161 if (!init_generic (dev, _("Unknown")))
1162 goto error_free_arch_specific;
1166 ped_exception_throw (PED_EXCEPTION_NO_FEATURE,
1167 PED_EXCEPTION_CANCEL,
1168 _("ped_device_new() Unsupported device type"));
1169 goto error_free_arch_specific;
1173 error_free_arch_specific:
1174 ped_free (dev->arch_specific);
1176 ped_free (dev->path);
1184 linux_destroy (PedDevice* dev)
1186 ped_free (dev->arch_specific);
1187 ped_free (dev->path);
1188 ped_free (dev->model);
1193 linux_is_busy (PedDevice* dev)
1198 if (_partition_is_mounted_by_path (dev->path))
1201 for (i = 0; i < 32; i++) {
1204 part_name = _device_get_part_path (dev, i);
1207 status = _partition_is_mounted_by_path (part_name);
1208 ped_free (part_name);
1217 /* we need to flush the master device, and all the partition devices,
1218 * because there is no coherency between the caches.
1219 * We should only flush unmounted partition devices, because:
1220 * - there is never a need to flush them (we're not doing IO there)
1221 * - flushing a device that is mounted causes unnecessary IO, and can
1222 * even screw journaling & friends up. Even cause oopsen!
1225 _flush_cache (PedDevice* dev)
1227 LinuxSpecific* arch_specific = LINUX_SPECIFIC (dev);
1234 ioctl (arch_specific->fd, BLKFLSBUF);
1236 for (i = 1; i < 16; i++) {
1240 name = _device_get_part_path (dev, i);
1243 if (!_partition_is_mounted_by_path (name)) {
1244 fd = open (name, WR_MODE, 0);
1246 ioctl (fd, BLKFLSBUF);
1255 linux_open (PedDevice* dev)
1257 LinuxSpecific* arch_specific = LINUX_SPECIFIC (dev);
1260 arch_specific->fd = open (dev->path, RW_MODE);
1262 if (arch_specific->fd == -1) {
1263 char* rw_error_msg = strerror (errno);
1265 arch_specific->fd = open (dev->path, RD_MODE);
1267 if (arch_specific->fd == -1) {
1268 if (ped_exception_throw (
1269 PED_EXCEPTION_ERROR,
1270 PED_EXCEPTION_RETRY_CANCEL,
1271 _("Error opening %s: %s"),
1272 dev->path, strerror (errno))
1273 != PED_EXCEPTION_RETRY) {
1279 ped_exception_throw (
1280 PED_EXCEPTION_WARNING,
1282 _("Unable to open %s read-write (%s). %s has "
1283 "been opened read-only."),
1284 dev->path, rw_error_msg, dev->path);
1297 linux_refresh_open (PedDevice* dev)
1303 linux_close (PedDevice* dev)
1305 LinuxSpecific* arch_specific = LINUX_SPECIFIC (dev);
1309 close (arch_specific->fd);
1314 linux_refresh_close (PedDevice* dev)
1321 #if SIZEOF_OFF_T < 8
1323 static _syscall5(int,_llseek,
1325 unsigned long, offset_high,
1326 unsigned long, offset_low,
1328 unsigned int, origin)
1331 llseek (unsigned int fd, loff_t offset, unsigned int whence)
1336 retval = _llseek(fd,
1337 ((unsigned long long)offset) >> 32,
1338 ((unsigned long long)offset) & 0xffffffff,
1341 return (retval==-1 ? (loff_t) retval : result);
1344 #endif /* SIZEOF_OFF_T < 8 */
1347 _device_seek (const PedDevice* dev, PedSector sector)
1349 LinuxSpecific* arch_specific;
1351 PED_ASSERT (dev->sector_size % PED_SECTOR_SIZE_DEFAULT == 0, return 0);
1352 PED_ASSERT (dev != NULL, return 0);
1353 PED_ASSERT (!dev->external_mode, return 0);
1355 arch_specific = LINUX_SPECIFIC (dev);
1357 #if SIZEOF_OFF_T < 8
1358 if (sizeof (off_t) < 8) {
1359 loff_t pos = (loff_t)(sector * dev->sector_size);
1360 return llseek (arch_specific->fd, pos, SEEK_SET) == pos;
1364 off_t pos = sector * dev->sector_size;
1365 return lseek (arch_specific->fd, pos, SEEK_SET) == pos;
1370 _read_lastoddsector (const PedDevice* dev, void* buffer)
1372 LinuxSpecific* arch_specific;
1373 struct blkdev_ioctl_param ioctl_param;
1375 PED_ASSERT(dev != NULL, return 0);
1376 PED_ASSERT(buffer != NULL, return 0);
1378 arch_specific = LINUX_SPECIFIC (dev);
1381 ioctl_param.block = 0; /* read the last sector */
1382 ioctl_param.content_length = dev->sector_size;
1383 ioctl_param.block_contents = buffer;
1385 if (ioctl(arch_specific->fd, BLKGETLASTSECT, &ioctl_param) == -1) {
1386 PedExceptionOption opt;
1387 opt = ped_exception_throw (
1388 PED_EXCEPTION_ERROR,
1389 PED_EXCEPTION_RETRY_IGNORE_CANCEL,
1390 _("%s during read on %s"),
1391 strerror (errno), dev->path);
1393 if (opt == PED_EXCEPTION_CANCEL)
1395 if (opt == PED_EXCEPTION_RETRY)
1403 linux_read (const PedDevice* dev, void* buffer, PedSector start,
1406 LinuxSpecific* arch_specific = LINUX_SPECIFIC (dev);
1407 PedExceptionOption ex_status;
1410 PED_ASSERT (dev->sector_size % PED_SECTOR_SIZE_DEFAULT == 0, return 0);
1412 if (_get_linux_version() < KERNEL_VERSION (2,6,0)) {
1413 /* Kludge. This is necessary to read/write the last
1414 block of an odd-sized disk, until Linux 2.5.x kernel fixes.
1416 if (dev->type != PED_DEVICE_FILE && (dev->length & 1)
1417 && start + count - 1 == dev->length - 1)
1418 return ped_device_read (dev, buffer, start, count - 1)
1419 && _read_lastoddsector (
1420 dev, (char *) buffer + (count-1) * 512);
1423 if (_device_seek (dev, start))
1426 ex_status = ped_exception_throw (
1427 PED_EXCEPTION_ERROR,
1428 PED_EXCEPTION_RETRY_IGNORE_CANCEL,
1429 _("%s during seek for read on %s"),
1430 strerror (errno), dev->path);
1432 switch (ex_status) {
1433 case PED_EXCEPTION_IGNORE:
1436 case PED_EXCEPTION_RETRY:
1439 case PED_EXCEPTION_UNHANDLED:
1440 ped_exception_catch ();
1441 case PED_EXCEPTION_CANCEL:
1444 PED_ASSERT (0, (void) 0);
1449 size_t read_length = count * dev->sector_size;
1450 if (posix_memalign (&diobuf, dev->sector_size, read_length) != 0)
1454 ssize_t status = read (arch_specific->fd, diobuf, read_length);
1456 memcpy(buffer, diobuf, status);
1457 if (status == (ssize_t) read_length)
1460 read_length -= status;
1461 buffer = (char *) buffer + status;
1465 ex_status = ped_exception_throw (
1466 PED_EXCEPTION_ERROR,
1467 PED_EXCEPTION_RETRY_IGNORE_CANCEL,
1468 _("%s during read on %s"),
1472 switch (ex_status) {
1473 case PED_EXCEPTION_IGNORE:
1477 case PED_EXCEPTION_RETRY:
1480 case PED_EXCEPTION_UNHANDLED:
1481 ped_exception_catch ();
1482 case PED_EXCEPTION_CANCEL:
1486 PED_ASSERT (0, (void) 0);
1496 _write_lastoddsector (PedDevice* dev, const void* buffer)
1498 LinuxSpecific* arch_specific;
1499 struct blkdev_ioctl_param ioctl_param;
1501 PED_ASSERT(dev != NULL, return 0);
1502 PED_ASSERT(buffer != NULL, return 0);
1504 arch_specific = LINUX_SPECIFIC (dev);
1507 ioctl_param.block = 0; /* write the last sector */
1508 ioctl_param.content_length = dev->sector_size;
1509 ioctl_param.block_contents = (void*) buffer;
1511 if (ioctl(arch_specific->fd, BLKSETLASTSECT, &ioctl_param) == -1) {
1512 PedExceptionOption opt;
1513 opt = ped_exception_throw (
1514 PED_EXCEPTION_ERROR,
1515 PED_EXCEPTION_RETRY_IGNORE_CANCEL,
1516 _("%s during write on %s"),
1517 strerror (errno), dev->path);
1519 if (opt == PED_EXCEPTION_CANCEL)
1521 if (opt == PED_EXCEPTION_RETRY)
1529 linux_write (PedDevice* dev, const void* buffer, PedSector start,
1532 LinuxSpecific* arch_specific = LINUX_SPECIFIC (dev);
1533 PedExceptionOption ex_status;
1537 PED_ASSERT(dev->sector_size % PED_SECTOR_SIZE_DEFAULT == 0, return 0);
1539 if (dev->read_only) {
1540 if (ped_exception_throw (
1541 PED_EXCEPTION_ERROR,
1542 PED_EXCEPTION_IGNORE_CANCEL,
1543 _("Can't write to %s, because it is opened read-only."),
1545 != PED_EXCEPTION_IGNORE)
1551 if (_get_linux_version() < KERNEL_VERSION (2,6,0)) {
1552 /* Kludge. This is necessary to read/write the last
1553 block of an odd-sized disk, until Linux 2.5.x kernel fixes.
1555 if (dev->type != PED_DEVICE_FILE && (dev->length & 1)
1556 && start + count - 1 == dev->length - 1)
1557 return ped_device_write (dev, buffer, start, count - 1)
1558 && _write_lastoddsector (
1559 dev, (char*) buffer + (count-1) * 512);
1562 if (_device_seek (dev, start))
1565 ex_status = ped_exception_throw (
1566 PED_EXCEPTION_ERROR, PED_EXCEPTION_RETRY_IGNORE_CANCEL,
1567 _("%s during seek for write on %s"),
1568 strerror (errno), dev->path);
1570 switch (ex_status) {
1571 case PED_EXCEPTION_IGNORE:
1574 case PED_EXCEPTION_RETRY:
1577 case PED_EXCEPTION_UNHANDLED:
1578 ped_exception_catch ();
1579 case PED_EXCEPTION_CANCEL:
1582 PED_ASSERT (0, (void) 0);
1588 printf ("ped_device_write (\"%s\", %p, %d, %d)\n",
1589 dev->path, buffer, (int) start, (int) count);
1591 size_t write_length = count * dev->sector_size;
1593 if (posix_memalign(&diobuf, dev->sector_size, write_length) != 0)
1595 memcpy(diobuf, buffer, write_length);
1596 diobuf_start = diobuf;
1598 ssize_t status = write (arch_specific->fd, diobuf, write_length);
1599 if (status == write_length) break;
1601 write_length -= status;
1602 diobuf = (char *) diobuf + status;
1606 ex_status = ped_exception_throw (
1607 PED_EXCEPTION_ERROR,
1608 PED_EXCEPTION_RETRY_IGNORE_CANCEL,
1609 _("%s during write on %s"),
1610 strerror (errno), dev->path);
1612 switch (ex_status) {
1613 case PED_EXCEPTION_IGNORE:
1617 case PED_EXCEPTION_RETRY:
1620 case PED_EXCEPTION_UNHANDLED:
1621 ped_exception_catch ();
1622 case PED_EXCEPTION_CANCEL:
1626 PED_ASSERT (0, (void) 0);
1631 #endif /* !READ_ONLY */
1635 /* returns the number of sectors that are ok.
1638 linux_check (PedDevice* dev, void* buffer, PedSector start, PedSector count)
1640 LinuxSpecific* arch_specific = LINUX_SPECIFIC (dev);
1645 PED_ASSERT(dev != NULL, return 0);
1647 if (!_device_seek (dev, start))
1650 if (posix_memalign(&diobuf, PED_SECTOR_SIZE_DEFAULT,
1651 count * PED_SECTOR_SIZE_DEFAULT) != 0)
1654 for (done = 0; done < count; done += status / dev->sector_size) {
1655 status = read (arch_specific->fd, diobuf,
1656 (size_t) ((count-done) * dev->sector_size));
1658 memcpy(buffer, diobuf, status);
1668 _do_fsync (PedDevice* dev)
1670 LinuxSpecific* arch_specific = LINUX_SPECIFIC (dev);
1672 PedExceptionOption ex_status;
1675 status = fsync (arch_specific->fd);
1676 if (status >= 0) break;
1678 ex_status = ped_exception_throw (
1679 PED_EXCEPTION_ERROR,
1680 PED_EXCEPTION_RETRY_IGNORE_CANCEL,
1681 _("%s during write on %s"),
1682 strerror (errno), dev->path);
1684 switch (ex_status) {
1685 case PED_EXCEPTION_IGNORE:
1688 case PED_EXCEPTION_RETRY:
1691 case PED_EXCEPTION_UNHANDLED:
1692 ped_exception_catch ();
1693 case PED_EXCEPTION_CANCEL:
1696 PED_ASSERT (0, (void) 0);
1704 linux_sync (PedDevice* dev)
1706 PED_ASSERT (dev != NULL, return 0);
1707 PED_ASSERT (!dev->external_mode, return 0);
1711 if (!_do_fsync (dev))
1718 linux_sync_fast (PedDevice* dev)
1720 PED_ASSERT (dev != NULL, return 0);
1721 PED_ASSERT (!dev->external_mode, return 0);
1725 if (!_do_fsync (dev))
1727 /* no cache flush... */
1732 _compare_digit_state (char ch, int need_digit)
1734 return !!isdigit (ch) == need_digit;
1737 /* matches the regexp "[^0-9]+[0-9]+[^0-9]+[0-9]+$".
1738 * Motivation: accept devices looking like /dev/rd/c0d0, but
1739 * not looking like /dev/hda1 and /dev/rd/c0d0p1
1742 _match_rd_device (const char* name)
1747 /* exclude directory names from test */
1748 pos = strrchr(name, '/') ?: name;
1756 for (state = 0; state < 4; state++) {
1757 int want_digits = (state % 2 == 1);
1761 if (!_compare_digit_state (*pos, want_digits))
1764 } while (_compare_digit_state (*pos, want_digits));
1771 _probe_proc_partitions ()
1773 FILE* proc_part_file;
1774 int major, minor, size;
1776 char part_name [256];
1777 char dev_name [256];
1779 proc_part_file = fopen ("/proc/partitions", "r");
1780 if (!proc_part_file)
1783 if (fgets (buf, 256, proc_part_file) == NULL)
1786 if (fgets (buf, 256, proc_part_file) == NULL)
1789 while (fgets (buf, 512, proc_part_file)
1790 && sscanf (buf, "%d %d %d %255s", &major, &minor, &size,
1792 /* Heuristic for telling partitions and devices apart
1793 * Probably needs to be improved
1795 if (!_match_rd_device (part_name)
1796 && isdigit (part_name [strlen (part_name) - 1]))
1799 strcpy (dev_name, "/dev/");
1800 strcat (dev_name, part_name);
1801 _ped_device_probe (dev_name);
1804 fclose (proc_part_file);
1814 _skip_entry (const char *name)
1817 static struct _entry entries[] = {
1818 { ".", sizeof (".") - 1 },
1819 { "..", sizeof ("..") - 1 },
1820 { "dm-", sizeof ("dm-") - 1 },
1821 { "loop", sizeof ("loop") - 1 },
1822 { "ram", sizeof ("ram") - 1 },
1826 for (i = entries; i->name != 0; i++) {
1827 if (strncmp (name, i->name, i->len) == 0)
1838 struct dirent *dirent;
1839 char dev_name [256];
1842 if (!(blockdir = opendir ("/sys/block")))
1844 while ((dirent = readdir (blockdir))) {
1845 if (_skip_entry (dirent->d_name))
1848 if (strlen (dirent->d_name) > sizeof (dev_name) - 6)
1849 continue; /* device name too long! */
1851 strcpy (dev_name, "/dev/");
1852 strcat (dev_name, dirent->d_name);
1853 /* in /sys/block, '/'s are replaced with '!' or '.' */
1854 for (ptr = dev_name; *ptr != '\0'; ptr++) {
1855 if (*ptr == '!' || *ptr == '.')
1858 _ped_device_probe (dev_name);
1861 closedir (blockdir);
1866 _probe_standard_devices ()
1868 _ped_device_probe ("/dev/hda");
1869 _ped_device_probe ("/dev/hdb");
1870 _ped_device_probe ("/dev/hdc");
1871 _ped_device_probe ("/dev/hdd");
1872 _ped_device_probe ("/dev/hde");
1873 _ped_device_probe ("/dev/hdf");
1874 _ped_device_probe ("/dev/hdg");
1875 _ped_device_probe ("/dev/hdh");
1877 _ped_device_probe ("/dev/sda");
1878 _ped_device_probe ("/dev/sdb");
1879 _ped_device_probe ("/dev/sdc");
1880 _ped_device_probe ("/dev/sdd");
1881 _ped_device_probe ("/dev/sde");
1882 _ped_device_probe ("/dev/sdf");
1890 /* we should probe the standard devs too, even with /proc/partitions,
1891 * because /proc/partitions might return devfs stuff, and we might not
1892 * have devfs available
1894 _probe_standard_devices ();
1896 /* /sys/block is more reliable and consistent; fall back to using
1897 * /proc/partitions if the former is unavailable, however.
1899 if (!_probe_sys_block ())
1900 _probe_proc_partitions ();
1904 _device_get_part_path (PedDevice* dev, int num)
1906 int path_len = strlen (dev->path);
1907 int result_len = path_len + 16;
1910 result = (char*) ped_malloc (result_len);
1914 /* Check for devfs-style /disc => /partN transformation
1915 unconditionally; the system might be using udev with devfs rules,
1916 and if not the test is harmless. */
1917 if (!strcmp (dev->path + path_len - 5, "/disc")) {
1918 /* replace /disc with /path%d */
1919 strcpy (result, dev->path);
1920 snprintf (result + path_len - 5, 16, "/part%d", num);
1921 } else if (dev->type == PED_DEVICE_DAC960
1922 || dev->type == PED_DEVICE_CPQARRAY
1923 || dev->type == PED_DEVICE_ATARAID
1924 #ifdef ENABLE_DEVICE_MAPPER
1925 || dev->type == PED_DEVICE_DM
1927 || isdigit (dev->path[path_len - 1]))
1928 snprintf (result, result_len, "%sp%d", dev->path, num);
1930 snprintf (result, result_len, "%s%d", dev->path, num);
1936 linux_partition_get_path (const PedPartition* part)
1938 return _device_get_part_path (part->disk->dev, part->num);
1942 _partition_get_part_dev (const PedPartition* part)
1944 struct stat dev_stat;
1945 int dev_major, dev_minor;
1947 if (!_device_stat (part->disk->dev, &dev_stat))
1949 dev_major = major (dev_stat.st_rdev);
1950 dev_minor = minor (dev_stat.st_rdev);
1951 return (dev_t)makedev (dev_major, dev_minor + part->num);
1955 _mount_table_search (const char* file_name, dev_t dev)
1957 struct stat part_stat;
1959 char part_name[512];
1963 file = fopen (file_name, "r");
1966 while (fgets (line, 512, file)) {
1967 junk = sscanf (line, "%s", part_name);
1968 if (stat (part_name, &part_stat) == 0) {
1969 if (part_stat.st_rdev == dev) {
1980 _partition_is_mounted_by_dev (dev_t dev)
1982 return _mount_table_search( "/proc/mounts", dev)
1983 || _mount_table_search( "/proc/swaps", dev)
1984 || _mount_table_search( "/etc/mtab", dev);
1988 _partition_is_mounted_by_path (const char *path)
1990 struct stat part_stat;
1991 if (stat (path, &part_stat) != 0)
1993 if (!S_ISBLK(part_stat.st_mode))
1995 return _partition_is_mounted_by_dev (part_stat.st_rdev);
1999 _partition_is_mounted (const PedPartition *part)
2002 if (!ped_partition_is_active (part))
2004 dev = _partition_get_part_dev (part);
2005 return _partition_is_mounted_by_dev (dev);
2009 linux_partition_is_busy (const PedPartition* part)
2013 PED_ASSERT (part != NULL, return 0);
2015 if (_partition_is_mounted (part))
2017 if (part->type == PED_PARTITION_EXTENDED) {
2018 for (walk = part->part_list; walk; walk = walk->next) {
2019 if (linux_partition_is_busy (walk))
2027 _blkpg_part_command (PedDevice* dev, struct blkpg_partition* part, int op)
2029 LinuxSpecific* arch_specific = LINUX_SPECIFIC (dev);
2030 struct blkpg_ioctl_arg ioctl_arg;
2033 ioctl_arg.flags = 0;
2034 ioctl_arg.datalen = sizeof (struct blkpg_partition);
2035 ioctl_arg.data = (void*) part;
2037 return ioctl (arch_specific->fd, BLKPG, &ioctl_arg) == 0;
2041 _blkpg_add_partition (PedDisk* disk, PedPartition* part)
2043 struct blkpg_partition linux_part;
2044 const char* vol_name;
2047 PED_ASSERT(disk != NULL, return 0);
2048 PED_ASSERT(disk->dev->sector_size % PED_SECTOR_SIZE_DEFAULT == 0,
2051 if (ped_disk_type_check_feature (disk->type,
2052 PED_DISK_TYPE_PARTITION_NAME))
2053 vol_name = ped_partition_get_name (part);
2057 dev_name = _device_get_part_path (disk->dev, part->num);
2061 memset (&linux_part, 0, sizeof (linux_part));
2062 linux_part.start = part->geom.start * disk->dev->sector_size;
2063 /* see fs/partitions/msdos.c:msdos_partition(): "leave room for LILO" */
2064 if (part->type & PED_PARTITION_EXTENDED)
2065 linux_part.length = part->geom.length == 1 ? 512 : 1024;
2067 linux_part.length = part->geom.length * disk->dev->sector_size;
2068 linux_part.pno = part->num;
2069 strncpy (linux_part.devname, dev_name, BLKPG_DEVNAMELTH);
2071 strncpy (linux_part.volname, vol_name, BLKPG_VOLNAMELTH);
2073 ped_free (dev_name);
2075 if (!_blkpg_part_command (disk->dev, &linux_part,
2076 BLKPG_ADD_PARTITION)) {
2077 return ped_exception_throw (
2078 PED_EXCEPTION_ERROR,
2079 PED_EXCEPTION_IGNORE_CANCEL,
2080 _("Error informing the kernel about modifications to "
2081 "partition %s -- %s. This means Linux won't know "
2082 "about any changes you made to %s until you reboot "
2083 "-- so you shouldn't mount it or use it in any way "
2084 "before rebooting."),
2088 == PED_EXCEPTION_IGNORE;
2095 _blkpg_remove_partition (PedDisk* disk, int n)
2097 struct blkpg_partition linux_part;
2099 memset (&linux_part, 0, sizeof (linux_part));
2101 return _blkpg_part_command (disk->dev, &linux_part,
2102 BLKPG_DEL_PARTITION);
2106 _disk_sync_part_table (PedDisk* disk)
2109 int last = PED_MAX (ped_disk_get_last_partition_num (disk), 16);
2110 int* rets = ped_malloc(sizeof(int) * last);
2111 int* errnums = ped_malloc(sizeof(int) * last);
2114 for (i = 1; i <= last; i++) {
2115 rets[i - 1] = _blkpg_remove_partition (disk, i);
2116 errnums[i - 1] = errno;
2119 for (i = 1; i <= last; i++) {
2122 part = ped_disk_get_partition (disk, i);
2124 /* busy... so we won't (can't!) disturb ;) Prolly
2125 * doesn't matter anyway, because users shouldn't be
2126 * changing mounted partitions anyway...
2128 if (!rets[i - 1] && errnums[i - 1] == EBUSY)
2131 /* add the (possibly modified or new) partition */
2132 if (!_blkpg_add_partition (disk, part))
2140 #ifdef ENABLE_DEVICE_MAPPER
2142 _dm_remove_map_name(char *name)
2144 struct dm_task *task = NULL;
2147 task = dm_task_create(DM_DEVICE_REMOVE);
2151 dm_task_set_name (task, name);
2153 rc = dm_task_run(task);
2154 dm_task_update_nodes();
2155 dm_task_destroy(task);
2163 _dm_is_part (struct dm_info *this, char *name)
2165 struct dm_task* task = NULL;
2166 struct dm_info* info = alloca(sizeof *info);
2167 struct dm_deps* deps = NULL;
2171 task = dm_task_create(DM_DEVICE_DEPS);
2175 dm_task_set_name(task, name);
2176 rc = dm_task_run(task);
2183 memset(info, '\0', sizeof *info);
2184 dm_task_get_info(task, info);
2188 deps = dm_task_get_deps(task);
2193 for (i = 0; i < deps->count; i++) {
2194 unsigned int ma = major(deps->device[i]),
2195 mi = minor(deps->device[i]);
2197 if (ma == this->major && mi == this->minor)
2202 dm_task_destroy(task);
2207 _dm_remove_parts (PedDevice* dev)
2209 struct stat dev_stat;
2210 struct dm_task* task = NULL;
2211 struct dm_info* info = alloca(sizeof *info);
2212 struct dm_names* names = NULL;
2213 unsigned int next = 0;
2216 if (!_device_stat (dev, &dev_stat))
2219 task = dm_task_create(DM_DEVICE_LIST);
2223 dm_task_set_major (task, major (dev_stat.st_rdev));
2224 dm_task_set_minor (task, minor (dev_stat.st_rdev));
2226 rc = dm_task_run(task);
2230 memset(info, '\0', sizeof *info);
2231 dm_task_get_info(task, info);
2235 names = dm_task_get_names(task);
2241 names = (void *)names + next;
2243 if (_dm_is_part(info, names->name))
2244 rc += _dm_remove_map_name(names->name);
2249 dm_task_update_nodes();
2250 dm_task_destroy(task);
2257 dm_task_destroy(task);
2258 ped_exception_throw (PED_EXCEPTION_WARNING, PED_EXCEPTION_IGNORE,
2259 _("parted was unable to re-read the partition "
2260 "table on %s (%s). This means Linux won't know "
2261 "anything about the modifications you made. "),
2262 dev->path, strerror (errno));
2267 _dm_add_partition (PedDisk* disk, PedPartition* part)
2269 struct stat dev_stat;
2270 struct dm_task* task = NULL;
2272 char* vol_name = NULL;
2273 char* dev_name = NULL;
2274 char* params = NULL;
2276 dev_name = _device_get_part_path (disk->dev, part->num);
2280 vol_name = strrchr (dev_name, '/');
2281 if (vol_name && *vol_name && *(++vol_name))
2282 vol_name = strdup (vol_name);
2284 vol_name = strdup (dev_name);
2288 if (!_device_stat (disk->dev, &dev_stat))
2291 if (asprintf (¶ms, "%d:%d %lld", major (dev_stat.st_rdev),
2292 minor (dev_stat.st_rdev), part->geom.start) == -1)
2298 task = dm_task_create (DM_DEVICE_CREATE);
2302 dm_task_set_name (task, vol_name);
2303 dm_task_add_target (task, 0, part->geom.length,
2305 rc = dm_task_run(task);
2307 //printf("0 %ld linear %s\n", part->geom.length, params);
2308 dm_task_update_nodes();
2309 dm_task_destroy(task);
2314 _dm_remove_map_name(vol_name);
2317 dm_task_update_nodes();
2319 dm_task_destroy (task);
2327 _dm_reread_part_table (PedDisk* disk)
2330 int last = PED_MAX (ped_disk_get_last_partition_num (disk), 16);
2334 if (!_dm_remove_parts(disk->dev))
2337 for (i = 1; i <= last; i++) {
2340 part = ped_disk_get_partition (disk, i);
2344 if (!_dm_add_partition (disk, part))
2352 _kernel_reread_part_table (PedDevice* dev)
2354 LinuxSpecific* arch_specific = LINUX_SPECIFIC (dev);
2355 int retry_count = 5;
2358 while (ioctl (arch_specific->fd, BLKRRPART)) {
2362 ped_exception_throw (
2363 PED_EXCEPTION_WARNING,
2364 PED_EXCEPTION_IGNORE,
2365 _("The kernel was unable to re-read the partition "
2366 "table on %s (%s). This means Linux won't know "
2367 "anything about the modifications you made "
2368 "until you reboot. You should reboot your computer "
2369 "before doing anything with %s."),
2370 dev->path, strerror (errno), dev->path);
2381 static int have_blkpg = -1;
2384 if (have_blkpg != -1)
2387 kver = _get_linux_version();
2388 return have_blkpg = kver >= KERNEL_VERSION (2,4,0) ? 1 : 0;
2392 linux_disk_commit (PedDisk* disk)
2394 #ifdef ENABLE_DEVICE_MAPPER
2395 if (disk->dev->type == PED_DEVICE_DM)
2396 return _dm_reread_part_table (disk);
2398 if (disk->dev->type != PED_DEVICE_FILE) {
2399 /* The ioctl() command BLKPG_ADD_PARTITION does not notify
2400 * the devfs system; consequently, /proc/partitions will not
2401 * be up to date, and the proper links in /dev are not
2402 * created. Therefore, if using DevFS, we must get the kernel
2403 * to re-read and grok the partition table.
2405 /* Work around kernel dasd problem so we really do BLKRRPART */
2406 if (disk->dev->type != PED_DEVICE_DASD &&
2407 _have_blkpg () && !_have_devfs ()) {
2408 if (_disk_sync_part_table (disk))
2412 return _kernel_reread_part_table (disk->dev);
2418 static PedDeviceArchOps linux_dev_ops = {
2420 destroy: linux_destroy,
2421 is_busy: linux_is_busy,
2423 refresh_open: linux_refresh_open,
2425 refresh_close: linux_refresh_close,
2430 sync_fast: linux_sync_fast,
2431 probe_all: linux_probe_all
2434 PedDiskArchOps linux_disk_ops = {
2435 partition_get_path: linux_partition_get_path,
2436 partition_is_busy: linux_partition_is_busy,
2437 disk_commit: linux_disk_commit
2440 PedArchitecture ped_linux_arch = {
2441 dev_ops: &linux_dev_ops,
2442 disk_ops: &linux_disk_ops