[klibc] Custom kinit to find device by "label"

Wayne Sherman wsherman at gmail.com
Thu Aug 23 19:33:43 PDT 2007


Max,

Thanks for your suggestions.  I finally got everything working by 
translating my "FindRootDeviceByLabel" pascal function to C and 
incorporating it into kinit.c.  If anyone is interested, here are the 
most interesting parts of the code:

   http://systemdesignworks.com/new_kinit_functions.c

Note:  My application has very specific needs, which I used to simplify 
the code.  For example, I only need to search filesystem labels on the 
second partition of USB memory devices (/dev/sda2 to /dev/sdp2) and only 
need to support finding labels on EXT2/EXT3 filesystems.

Best Regards,

Wayne Sherman


maximilian attems wrote:
> On Tue, 24 Jul 2007, noneya biz wrote:
> 
>> function FindRootDeviceByLabel(const RootFSLabel: string): boolean;
>> const
>>   DEV_MAJOR: dev_t = 8;
>>   DEV_MODE: mode_t = S_IFBLK or &0660;
>> var
>>   ActualFSLabel: string;
>>   FileDescriptor: cint;
>>   ReadBytes: TsSize;
>>   DeviceFound: boolean;
>>   dev_minor: dev_t;
>> begin
>>   Result := False;
>>   SetLength(ActualFSLabel, Length(RootFSLabel));
>>
>>   //loop through all devices that might have our root filesystem sda2 to sdp2
>>   dev_minor := 2;
>>   while dev_minor < 243 do begin // last one is 242
>>     if mknod(DEVICENAME, DEV_MODE, (DEV_MAJOR shl 8) or dev_minor) = 0 then
>>     begin
>>       WriteLn('Created device node "', DEVICENAME, '". dev_minor = ',
>> dev_minor);
>>     end
>>     else begin
>>       WriteError('mknod with dev_minor:  ' + IntToStr(dev_minor));
>>       EXIT; //exit w/ error
>>     end;
>>     dev_minor := dev_minor + 16;
>>
>>     FileDescriptor := FpOpen(DEVICENAME, 0, 0);
>>     if FileDescriptor >= 0 then begin
>>       WriteLn('Opened device:  ', DEVICENAME);
>>       ReadBytes := FpPRead(FileDescriptor,
>>         PChar(ActualFSLabel), Length(ActualFSLabel), $478);
>>       if (ReadBytes = Length(ActualFSLabel)) then begin
>>         if (ActualFSLabel = RootFSLabel) then begin
>>           Result := True;
>>           DeviceFound := True;
>>         end;
>>       end
> 
> that can be done easier vol_id from udev will print your the UUID
> or the LABEL of the fs. also udev directly sets up the symlinks
> under /dev/disk/. :)
> 
> i don't have a big kinit love so i don't know where it fails,
> but from the resume binary irc it would be in name_to_dev_t()
> need to dig anyway if we support resume with boot_arg UUID=$fun_hash, ah
> 
> case $resume in
>         LABEL=*)
>         resume="/dev/disk/by-label/${resume#LABEL=}"
>         ;;
>         UUID=*)
>         resume="/dev/disk/by-uuid/${resume#UUID=}"
>         ;;
> esac
> ..
> /bin/resume ${resume}
> 
> need to check if that really works.
> 
> 
>>       else WriteError('Reading from device:  ' + DEVICENAME);
>>
>>       WriteLn('Closing device:  ', DEVICENAME);
>>       if FpClose(FileDescriptor) <> 0 then
>>         WriteError('Closing device:  ' + DEVICENAME);
>>
>>       if DeviceFound then BREAK;
>>     end
>>     else WriteError('Opening device:  ' + DEVICENAME);
>>
>>     //Delete the device so we can try again
>>     WriteLn('Deleting device we created:  ', DEVICENAME);
>>     if Fpunlink(DEVICENAME) <> 0 then
>>       WriteError('Deleting device:  ' + DEVICENAME);
>>   end; // while dev_minor < 243
>> end;
> 
> 
> regards
> 



More information about the klibc mailing list