Jump to content











Photo
- - - - -

Command Line Tool to Create IMG Floppy Image


  • Please log in to reply
16 replies to this topic

#1 Alexander Ceed

Alexander Ceed

    Frequent Member

  • Advanced user
  • 185 posts

Posted 15 June 2016 - 07:40 AM

Hi guys! I'm looking for a way to automate the creation of a .img floppy image from a folder containing some files. 

I basically have folder X and I'd like to create an img image of 1440K size with a FAT partition containing the files from X folder. This will not be a one-time thing, it will be done very often so it needs to be automated.

 

Is there a command line tool for this job that you can share with me? 

Thank you



#2 Icecube

Icecube

    Gold Member

  • Team Reboot
  • 1061 posts
  •  
    Belgium

Posted 15 June 2016 - 08:48 AM

You can use tools from the mtools suite.

 

A version compiled for Windows can be found here (with also some links to some examples I wrote):

http://reboot.pro/topic/17993-mtools/

 

The official site:

http://www.gnu.org/software/mtools/



#3 Zharif

Zharif

    Frequent Member

  • .script developer
  • 123 posts
  • Location:Germany
  •  
    Germany

Posted 15 June 2016 - 08:50 AM

You may want to use Imdisk.

 

This is a very short example script that might be useful for a start-up.

Save code as a *.cmd file.

Don't run it first! Instead, open the file via text editor and set your source directory.

 

This script creates an imagedrv in memory and copies files from folder X to the image mountpoint.

Source is: please adjust

Mountpoint is A:

FileSystem is: FAT

Label is : MyFloppy

Size is: 1440k  Edited

@ECHO OFF
SETLOCAL ENABLEEXTENSIONS
ECHO.

:: adjust settings here
Set Src=DrvLetter:\Directory
SET MPoint=A:
SET FS=FAT
SET Lbl=MyFloppy
SET Size=1440k

:: create imagedrv in memory
IMDISK -a -t vm -m %MPoint% -o fd -s %SIZE% -p "/fs:%FS% /v:%LBL% /q /y"

:: copy source files to imagedrv/mountpoint
XCOPY /E /I "%Src%" "%MPoint%"

PAUSE>NUL&EXIT 


#4 Alexander Ceed

Alexander Ceed

    Frequent Member

  • Advanced user
  • 185 posts

Posted 15 June 2016 - 10:26 AM

@Zharif:

It works but how do you save it as an img afterwards, through the command line?



#5 Zharif

Zharif

    Frequent Member

  • .script developer
  • 123 posts
  • Location:Germany
  •  
    Germany

Posted 15 June 2016 - 10:39 AM

Use rawcopy (also from Olof) to save as Image or iso file.

Don't forget to dismount the drv afterwards (via imdisk again).

 

Add:

rawcopy -ml \\.\Mountpoint DestDir\DestFile.img 

 

...and the appropriate imdisk command to unmount the imagedrv.

Type imdisk /? to get the command line Syntax.



#6 Wonko the Sane

Wonko the Sane

    The Finder

  • Advanced user
  • 13334 posts
  • Location:The Outside of the Asylum (gate is closed)
  •  
    Italy

Posted 15 June 2016 - 01:30 PM

UNless:

1) IMDISK is already installed (for *any* reason)

AND

2) there is an actual need for a "bootable" image (i.e. one that has been formatted by the Windows Format.com which is what IMDISK uses and thus has boot code in the PBR)

 

Using imdisk is more complicated than really needed, and mtools will be faster and "less intrusive".

 

Here (unrelated thread):
http://reboot.pro/to...e-9#entry193659

you can find an example of a batch that makes use of the mtools to create a floppy image with detailed parameters (BTW it is anyway possible to to write a given PBR code).

 

BUT depending on what the actual specific need is, if the image is that of a standard 1.44 Mb floppy you can use good ol' BFI alright and even the mtools are more complex than needed:
https://web.archive....p://nu2.nu/bfi/

 

:duff:

Wonko



#7 Alexander Ceed

Alexander Ceed

    Frequent Member

  • Advanced user
  • 185 posts

Posted 15 June 2016 - 01:33 PM

I'll take a look at mtools tomorrow. 

I've went with the IMDISK batch for now because I already have it installed.

 

Thank you all for your replies.



#8 Alexander Ceed

Alexander Ceed

    Frequent Member

  • Advanced user
  • 185 posts

Posted 15 June 2016 - 06:40 PM

Well, mtools were way too complex for this type of job.

IMDISK batch was easier but as you said, dependent on the installment of IMDISK.

 

I went with bfi as all I needed was a standard floppy image.

So thank you all for your help.



#9 Olof Lagerkvist

Olof Lagerkvist

    Gold Member

  • Developer
  • 1331 posts
  • Location:Borås, Sweden
  •  
    Sweden

Posted 15 June 2016 - 08:35 PM

If you use ImDisk, you could simplify the whole thing a bit by skipping the rawcopy operation and instead mount the image file directly.

 

IMDISK -a -f DestDir\DestFile.img -m %MPoint% -o fd -s %SIZE% -p "/fs:%FS% /v:%LBL% /q /y"
XCOPY /E /I "%Src%" "%MPoint%"
IMDISK -d -m %MPoint%


#10 Wonko the Insane

Wonko the Insane

    Frequent Member

  • Advanced user
  • 458 posts
  • Location:The Inside of the Asylum (gate is wide open)
  • Interests:Oh, so you hate me too? Well, join the club! There are weekly meetings at the corner of Fuck You St. and Kiss My Ass Blvd.
  •  
    United States

Posted 17 June 2016 - 12:14 AM

I would also like to create an IMG, but something much bigger than a floppy image. It will be empty initially (no partition table, filesystems, etc), and must be fully Linux compatible (capable of both being read and written to from either Windows or Linux, etc.). A rooted Android must also also be able to handle it, my primary purpose is to use this IMG to contain a Linux install that will be booted from within Android in a chroot jail environment. What is best for this, mtools or ImDisk)?



#11 Wonko the Sane

Wonko the Sane

    The Finder

  • Advanced user
  • 13334 posts
  • Location:The Outside of the Asylum (gate is closed)
  •  
    Italy

Posted 17 June 2016 - 07:18 AM

Apart the initial status of "empty", does it need to be a partitioned, i.e. a "hd-like" image or does it need to be non-partitioned, i.e. a "superfloppy"?

And which filesystem would be later used?

 

:duff:

Wonko



#12 wendy

wendy

    Frequent Member

  • Lady
  • 263 posts
  • Location:one mile from the QR main line
  • Interests:Operating systems, Weights and Measures, Geometry
  •  
    Australia

Posted 17 June 2016 - 07:25 AM

BFI allows you to use various boot sectors.  I have a directory with a supply of boot sectors, and then when creating a msdos, win9x or pcdos disk, simply tell the command to 'copy' a boot sector + directory to a diskette image.  so, eg dosdisk pcdos502.144 c:\mydisks\disk1 disk1.dsk.



#13 Wonko the Sane

Wonko the Sane

    The Finder

  • Advanced user
  • 13334 posts
  • Location:The Outside of the Asylum (gate is closed)
  •  
    Italy

Posted 17 June 2016 - 11:51 AM

BFI allows you to use various boot sectors.  I have a directory with a supply of boot sectors, and then when creating a msdos, win9x or pcdos disk, simply tell the command to 'copy' a boot sector + directory to a diskette image.  so, eg dosdisk pcdos502.144 c:\mydisks\disk1 disk1.dsk.

Sure it does :).

The "difficult" part is maybe to "procure" the supply of boot sectors.

And of course BFI is (by design) limited to a couple "standard formats"

 

It all depends on the OS running and on the expected result, if you are running (say) XP and you want a "standard" 1.44 MS-DOS disk, the simplest would be to extract the image from diskcopy.dll:
http://www.911cd.net...showtopic=16745

 

IMDISK allows for non-standard images, but because it uses the format.com it may have some limitations in customizing the image, whilst the mtools allow full modification/tweaking of all parameters of the image, besides size number of fats, etc..

 

OT :w00t: :ph34r:, but not much (and shameless plug ;)), here is some discussion on floppy (and superfloppy) formats:

http://www.msfn.org/...d-their-images/

 and a small worksheet to calculate size of fats, etc. for most known floppy size formats and "optimized" for custom ones:
http://www.msfn.org/...#comment-988732

 

and a (nifty? :unsure:) little batch to view a floppy bootsector contents:
http://www.msfn.org/...comment-1001712

 

:duff:

Wonko



#14 Zharif

Zharif

    Frequent Member

  • .script developer
  • 123 posts
  • Location:Germany
  •  
    Germany

Posted 17 June 2016 - 02:29 PM

Edited: Thanks for mention bfi. I nearly forgot this tool.
Just for sake of completeness - the code below is a batch proposal that checks for bfi, imdisk and rawcopy.
Considering their limiations, bfi will be used to create the floppy image if it has been detected.
Otherwise, Imdisk is used.
This batch should be flexible enough to avoid errors and provides the ability
to batch floppy image operations with different settings.
Please note that this code is based on my personal coding preferences.
Feel free to remove or change any unwanted/unneeded code.

As always, adjust settings before running this batch.
 

@ECHO OFF
SETLOCAL ENABLEEXTENSIONS

:: set filepathes here - will be checked by _FileExists
CALL:_FileExists "%CD%\bfi.exe" _BFI
IF NOT DEFINED _BFI CALL:_FileExists "imdisk.exe" _ImDsk
IF DEFINED _ImDsk CALL:_FileExists "%CD%\rawcopy.exe" _RawC
IF NOT DEFINED _BFI IF NOT DEFINED _IMDSK ECHO BFI and Imdisk not found ...exit batch.&PAUSE>NUL&EXIT

:: Use BFI prior to Imdisk - pass your parameters to call
:: Syntax: ImageFileIN  ImageLabelIN  [DirPathInjectIN]  [ImageBootSectorFileIN] [ImageTypeIN]
:: 1st and 2nd parm are needed - 3rd, 4th and 5th parm are optional
:: Any parm should be surrounded by dblquotes.
IF DEFINED _BFI (
  CALL:_Nu2BFI "%CD%\MyFloppy.img" "MyFloppy" "D:\test" "%CD%\FloppyEmulation.bin"
  CALL:_Nu2BFI "%CD%\MyFloppy1.img" "MyFloppy" "D:\test" "%CD%\FloppyEmulation.bin" "7"
  CALL:_Nu2BFI "%CD%\MyFloppy2.img" "MyFloppy" "D:\test" "%CD%\FloppyEmulation.bin" 4
  PAUSE>NUL&EXIT
)

:: use Imdisk - pass your parameters to call
:: Syntax: ImageMountpointIN  ImageSizeIN  ImageFileSystemIN  ImageLabelIN  [ImageFileIN]  [DirPathInjectIN]
:: 1st to 4th parm are needed - 5th and 6th parm are optional
:: Any parm should be surrounded by dblquotes.
IF DEFINED _ImDsk (
  CALL:_Imdisk "A:" "1440k" "FAT" "MyFloppy" "%CD%\MyFloppy.img" "D:\test"
  CALL:_Imdisk "A:" "2880k" "FAT" "MyFloppy" "%CD%\MyFloppy1.img" "D:\test"
  CALL:_Imdisk "A:" "720k" "FAT" "MyFloppy" "%CD%\MyFloppy2.img" "D:\test"
)
PAUSE>NUL&EXIT


:_Nu2BFI ImageFileIN ImageLabelIN [DirPathInjectIN] [ImageBootSectorFileIN] [ImageTypeIN]
SETLOCAL
IF NOT "%~3"=="" PUSHD %3 >NUL 2>&1 && SET _Nu2Inj=%3&& POPD
IF NOT "%~4"=="" CALL:_FileExists %4 _Nu2Boot
IF DEFINED _Nu2Boot SET "_Nu2Boot=-b=%4"
SET _Nu2Type=-t=6& IF NOT "%~5"=="" SET "_Nu2Type=-t=%~5"

ECHO.&SET /P "=BFI: Creating a FAT12 floppy image: " <NUL
%_BFI% -f=%1 -l=%2 %_Nu2Boot% %_Nu2Type% %_Nu2Inj%>NUL 2>&1 && ECHO ok||ECHO failed
ECHO      Image File: %~s1
ECHO      Image Label: %~2
ECHO      Image DirToInject: %_Nu2Inj%
ECHO      Image BootSector: %_Nu2Boot%
ECHO      Image Type: %_Nu2Type:*t=%
ENDLOCAL
GOTO:EOF

:_ImDisk ImageMountpointIN ImageSizeIN ImageFileSystemIN ImageLabelIN [ImageFileIN] [DirPathInjectIN]
SETLOCAL
ECHO.&SET /P "=ImDisk: Creating image drive: " <NUL
%_IMDSK% -a -t vm -m %1 -o fd -s %2 -p "/fs:%3 /v:%4 /q /y" >NUL 2>&1 && ECHO ok||ECHO failed

CALL:_FileExists %5 _ImdF
IF DEFINED _ImdF SET "_ImdF=%5"
IF NOT "%~6"=="" PUSHD %6 >NUL 2>&1 && SET _ImdInj=%6&& POPD

IF DEFINED _ImdInj (
    SET /P "=Imdisk: Copying files: " <NUL
    XCOPY /E /I /Q %6 %1 >NUL 2>&1 && ECHO ok||ECHO failed
)
IF DEFINED _ImdF IF DEFINED _RAWC (
    SET /P "=Imdisk: Saving image drive: " <NUL
    %_RAWC% -mli \\.\%~1 %5 >NUL 2>&1 && ECHO ok||ECHO failed
)
SET /P "=Imdisk: Forcing dismount image drive: " <NUL
%_IMDSK% -D -m %1 >NUL 2>&1 && ECHO ok||ECHO failed
ECHO         Image Mountpoint: %~1
ECHO         Image Size: %~2
ECHO         Image FileSystem: %~3
ECHO         Image Label: %~4
ECHO         Image File: %_ImdF%
ECHO         Image DirToInject: %_ImdInj%
ENDLOCAL
GOTO:EOF

:_FileExists  FilePath-FileNameToSearchIN  PathVarNameOUT
SET "_FileExists="
IF "%~1"=="" GOTO:EOF
IF "%~2"=="" GOTO:EOF
SET "%~2="

:: map directory part of filepath, dir filename, save full filepath, unmap
(PUSHD "%~dp1" 2>NUL && DIR /B "%~nx1" >NUL 2>&1 && SET "%~2=%~1") & POPD
IF DEFINED %~2 SET _FileExists=1& GOTO:EOF

:: loop through PATH variable, map directories, dir filename,
:: save directory path [last match only]
FOR %%A IN ("%PATH:;=" "%") DO (
    PUSHD "%%A" 2>NUL
    DIR /B "%~1" >NUL 2>&1 && SET "%~2=%%~A"
  POPD
)
:: VarOUT is not defined - quit, set tempvar
IF NOT DEFINED %~2 GOTO:EOF
CALL SET "_OUT=%%%~2%%"

:: remove trailing bkslsh from directory path
IF "%_OUT:~-1%"=="\" SET "%~2=%_OUT:~0,-1%" 2>NUL
:: add filename to directory path and set function TRUE
SET "%~2=%_OUT%\%~1" 2>NUL && SET _FileExists=1
SET "_OUT="
GOTO:EOF


#15 Wonko the Sane

Wonko the Sane

    The Finder

  • Advanced user
  • 13334 posts
  • Location:The Outside of the Asylum (gate is closed)
  •  
    Italy

Posted 17 June 2016 - 05:00 PM

@zharif
With all due respect :), I thought that the idea was about having something "simple".

JFYI, sometimes it is easier to actually attempt to run an executable, rather than "looking for it", *like*:
 
 

@ECHO OFF
SETLOCAL

imdisk.exe --version >nul 2>&1 &&ECHO IMDISK found *somewhere*

bfi.exe -f=nul && ECHO BFI found *somewhere*

rawcopy.exe 0 0 nul nul && ECHO RAWCOPY found *somewhere*

PAUSE

::Another way

imdisk /? >nul 2>&1
IF %ERRORLEVEL%.==-1. ECHO IMDISK found

bfi /? >nul 2>&1
IF %ERRORLEVEL%.==255. ECHO BFI found

rawcopy /? >nul 2>&1
IF %ERRORLEVEL%.==-1. ECHO Rawcopy found

::Since Rawcopy is a "common enough" name, this might be better:
rawcopy /? 2>&1|FIND "Lagerkvist">nul&&ECHO Rawcopy by Olof Lagerkvist found.
after all, you don't really need to know where the tool is, only if it is available to the batch ;).

:duff:
Wonko

#16 Wonko the Insane

Wonko the Insane

    Frequent Member

  • Advanced user
  • 458 posts
  • Location:The Inside of the Asylum (gate is wide open)
  • Interests:Oh, so you hate me too? Well, join the club! There are weekly meetings at the corner of Fuck You St. and Kiss My Ass Blvd.
  •  
    United States

Posted 17 June 2016 - 05:35 PM

@ Wonko: It will be partitioned, like an HDD, and will have at least one partition (Linux EXT4), maybe also a swap partition. My plan is to use something like rsync to restore the root filesystem from a backup into an empty EXT4 inside the new IMG, thereby making a near-"clone" (at a filesystem level) of a current IMG which is already bootable. The reason for all this is that I haven't had any real luck syncing up an IMG to either my Dropbox or MEGA accounts, since every time a tiny bit of the file changes, the entire file is resynced (over 8GB!), rather than just the changed bytes. This must be due to the nature of the file, its' structure, etc. So I decided that backing up/restoring (online) at FS level might be more efficient. I also figured that syncing/backing up/restoring from IMG to IMG would also be an effective strategy.

 

My app, Linux Deploy, appears to use the dd command to create the IMG (on Android, not PC). From the logs when creating an example 2048MB/2GB IMG:

 

+ echo done
+ [ file = file ]
+ local file_size=0
+ [ -f /storage/sdcard0/linux.img ]
+ [ 2048 -eq 0 ]
+ let file_size=0/1048576
+ [ 2048 != 0 ]
+ msg -n Making new disk image (2048 MB) ...
+ echo -n Making new disk image (2048 MB) ...
+ expr 2048 - 1
Making new disk image (2048 MB) ... + dd if=/dev/zero of=/storage/sdcard0/linux.img bs=1048576 seek=2047 count=1
1+0 records in
1+0 records out
1048576 bytes (1.0MB) copied, 0.017460 seconds, 57.3MB/s
done

 

However, it doesn't allow me to just create an empty IMG then stop, instead it continues with the installation. I'm not sure how it determines the bs (blocksize), count, and seek values. So I figure it's best for now to just find a program that semi-automates the process, with me inputting just the desired size.



#17 Wonko the Sane

Wonko the Sane

    The Finder

  • Advanced user
  • 13334 posts
  • Location:The Outside of the Asylum (gate is closed)
  •  
    Italy

Posted 17 June 2016 - 07:59 PM

I am not sure to understand your overall plan, however both IMDISK and MTOOLS are not the "right" tools for a partitioned image.
Using some workarounds of course both can be used, but you will better use something capable of managing a hard disk like (partitioned) image.

If you were not using an EXT2/3/4 filesystem I would have proposed you my good ol' batches:
http://reboot.pro/to...atch-001-alpha/

But you would have to modify it/them for using a non-MS format utility capable of creating the filesystem of your choice.
At this point it would be probably easier to use a recent disk driver (the Arsenal Image Mounter, also by Olof, is excellent) and whatever filesystem driver for windows (with the format capabilities or or mke2fs port) you use for EXT2/3/4.

All in all it seems to me better/easier to do the whole thing under Linux.

:duff:
Wonko




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users