Create bootable partition in a virtual disk [PARTLY SOLVED]?

For discussions about programming, programming questions/advice, and projects that don't really have anything to do with Puppy.
Post Reply
Message
Author
User avatar
Argolance
Posts: 3767
Joined: Sun 06 Jan 2008, 22:57
Location: PORT-BRILLET (Mayenne - France)
Contact:

#4 Post by Argolance »

Bonjour,
As the title of this thread may seem ambiguous, here are some explanations.
Nowadays, there is no doubt that the best way to distribute Puppy is no longer an ISO file to burn to CD/DVD, but an image file to dump directly to USB stick or SD card.
I made this choice for ToOpPy in June 2017. To this end, did18 developed :arrow: Img2Key and I made a dumping wizard called 2Pd2f using gdiskdump, available to ToOpPy users.

So far I have created such images from a 4GB hardware USB stick while the system weighs only about 300MB. The target drive must therefore have at least the same capacity as the source. Dumping takes about 20 minutes. With a 300 MB image, within 1 to 3 minutes.

So I initiated this thread to learn how to:
  • - (1) create ex nihilo a smaller capacity virtual disk with a primary "bootable" partition containing the system files and then save its image.
    - (2) reduce an existing image file. This image can be as well an image created ex nihilo as the image of an existing USB stick, the interest being finally to reduce its size as much as possible.
[EDIT]: Actually, the first one runs smoothly without complaining about anything, but the USB stick burnt from the image file... does not boot! :cry:
- The second one produces an operational image file and the USB stick burnt from it boots normally.

:idea: As soon as the commands are known and do the job well, it's quite simple to make a wizard, or a graphical user interface like the :arrow: Disk Image Creator of mistfire (or better yet, to complete it for that purpose).
It was just an idea but while searching the net I didn't find anything like that, although it could be very useful.

**********************************
(1) Creating a disk image ex nihilo
**********************************

Create a working directory:

Code: Select all

# mkdir $HOME/virtual_disk
Then open a terminal and type:

Code: Select all

# dd if=/dev/zero of=$HOME/virtual_disk/VD.img bs=1M count=512
  • Code: Select all

    512+0 records in
    512+0 records out
    536870912 bytes (537 MB) copied, 1,25517 s, 428 MB/s
Create a loopback device for the image using the loopback-functionality (GParted operating on devices, not simple files like images)
Enable loopback if it wasn't already enabled:

Code: Select all

# modprobe loop
Request a new (free) loopback device:

Code: Select all

# losetup -f
Create a device of the image:

Code: Select all

# losetup /dev/loop3 $HOME/virtual_disk/VD.img
The device /dev/loop3 represents VD.img that can be loaded using GParted:

Code: Select all

# gparted /dev/loop3
  • Code: Select all

    ======================
    libparted : 3.1
    ======================
    1 - Create a table partition
    2 - Create a primary partition
    3 - Format it to whatever you want
    4 - Give it a label (optional)
    5 - Make it "bootable"
    6 - Possibly check it
    7 - Quit gparted
Unload the loopback-device that is not needed anymore:

Code: Select all

# losetup -d /dev/loop3
Calculate the offset of where the partition starts to mount the partition inside the disk image.

Code: Select all

# parted $HOME/virtual_disk/VD.img
  • Code: Select all

    GNU Parted 3.1
    Utilisation de $HOME/virtual_disk/VD.img
    Bienvenue sur GNU Parted ! Tapez 'help' pour voir la liste des commandes.
    (parted) unit                                                             
    Unité ?  [compact]? B                                                     
    (parted) print                                                            
    Modèle:  (file)
    Disque $HOME/virtual_disk/VD.img : 536870912B
    Taille des secteurs (logiques/physiques): 512B/512B
    Table de partitions : msdos
    Disk Flags: 
    
    Numéro  Début     Fin         Taille      Type     Système de fichiers  Fanions
     1      1048576B  536870911B  535822336B  primary  ntfs                 démarrage
    
    (parted) q (type q to exit from parted).
To mount/fill/modify the partition, create a mounting directory:

Code: Select all

# mkdir /mnt/VD
and run something like (the offset value "1048576" being the "Start" value given above):

Code: Select all

# mount -o loop,offset=1048576 $HOME/virtual_disk/VD.img /mnt/VD
**********************************
(2) Shrinking (existing) disk image
**********************************

Create a device of the image:

Code: Select all

$ losetup /dev/loop3 myimage.img
Access the partition on the image:

Code: Select all

$ partprobe /dev/loop3
This should give the device /dev/loop3p1, which represents the partition in myimage.img. The device is not directly directly needed, but GParted requires it.
Load the device using GParted:

Code: Select all

$ gparted /dev/loop3
Resize this partition so that is fits it content, but not more than that.
  • 1 - Select the partition and click Resize/Move.
    2 - Drag the right bar to the left as much as possible
    3 - Press Apply
    3 - Possibly check it
    4 - Close GParted).
Sometimes GParted needs a few MB extra to place some filesystem-related data. Press the up-arrow at the New size-box a few times to do so. For example, 10 times (=10MiB) for FAT32 to work. Might not be needed to at all for NTFS.
The part of the disk unallocated that will not be used by the partition can be shaved. GParted is a tool for disks, so it doesn't shrink images, only partitions.
Unload the loopback-device being not needed anymore:

Code: Select all

$ losetup -d /dev/loop3
Where the partition ends and where the unallocated part begins?

Code: Select all

$ fdisk -l myimage.img
  • Code: Select all

    Disk myimage.img: 6144 MB, 6144000000 bytes, 12000000 sectors
    Units = sectors of 1 * 512 = 512 bytes
    Sector size (logical/physical): 512 bytes / 512 bytes
    I/O size (minimum/optimal): 512 bytes / 512 bytes
    Disk identifier: 0x000ea37d
    
          Device Boot      Start         End      Blocks   Id  System
    myimage.img1            2048     9181183     4589568    b  W95 FAT32
Note the partition ends on block 9181183 (shown under End)
So, next the image-file is shrunk to a size that can just contain the partition. For this, use the dd command. The size of the file is needed. The last block was 9181183 and block-numbers start at 0. That means the "count" value is equal to 9181183+1. This is important, else the partition will not fit the image:

Code: Select all

$ dd if=myimage.img of=myimage_shrunk.img bs=512 count=9181184
The new image should act exactly the same as the "big" one (to mount any image of this type, do as described above at the end of the "Creating a disk image ex nihilo") section.
**********************************

Adapted from :arrow: shrinking-images-on-linux by FrozenCow

Cordialement.

User avatar
Argolance
Posts: 3767
Joined: Sun 06 Jan 2008, 22:57
Location: PORT-BRILLET (Mayenne - France)
Contact:

#5 Post by Argolance »

:arrow: remote directory of latest compressed ToOpPy image files to be dumped to a USB flash drive, shrunk/optimized as described above: time required within 1 and 2 instead of 20 minutes... :)

Post Reply