Puppy Linux Discussion Forum Forum Index Puppy Linux Discussion Forum
Puppy HOME page : puppylinux.com
"THE" alternative forum : puppylinux.info
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

The time now is Thu 27 Feb 2020, 14:06
All times are UTC - 4
 Forum index » Off-Topic Area » Programming
Create bootable partition in a virtual disk [PARTLY SOLVED]?
Post new topic   Reply to topic View previous topic :: View next topic
Page 1 of 1 [5 Posts]  
Author Message
Argolance


Joined: 06 Jan 2008
Posts: 3734
Location: PORT-BRILLET (Mayenne - France)

PostPosted: Sun 27 Jan 2019, 10:59    Post subject:  

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! Crying or Very sad
- 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:
# mkdir $HOME/virtual_disk

Then open a terminal and type:
Code:
# dd if=/dev/zero of=$HOME/virtual_disk/VD.img bs=1M count=512
    Code:
    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:
# modprobe loop

Request a new (free) loopback device:
Code:
# losetup -f
    Code:
    /dev/loop3

Create a device of the image:
Code:
# losetup /dev/loop3 $HOME/virtual_disk/VD.img

The device /dev/loop3 represents VD.img that can be loaded using GParted:
Code:
# gparted /dev/loop3
    Code:
    ======================
    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:
# losetup -d /dev/loop3

Calculate the offset of where the partition starts to mount the partition inside the disk image.
Code:
# parted $HOME/virtual_disk/VD.img
    Code:
    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:
# mkdir /mnt/VD

and run something like (the offset value "1048576" being the "Start" value given above):
Code:
# mount -o loop,offset=1048576 $HOME/virtual_disk/VD.img /mnt/VD

**********************************
(2) Shrinking (existing) disk image
**********************************

Create a device of the image:
Code:
$ losetup /dev/loop3 myimage.img

Access the partition on the image:
Code:
$ 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:
$ 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:
$ losetup -d /dev/loop3

Where the partition ends and where the unallocated part begins?
Code:
$ fdisk -l myimage.img
    Code:
    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:
$ 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.

_________________

A second life for your PCs...
Back to top
View user's profile Send private message Visit poster's website 
Argolance


Joined: 06 Jan 2008
Posts: 3734
Location: PORT-BRILLET (Mayenne - France)

PostPosted: Sat 02 Feb 2019, 06:25    Post subject:  

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... Smile
_________________

A second life for your PCs...
Back to top
View user's profile Send private message Visit poster's website 
Display posts from previous:   Sort by:   
Page 1 of 1 [5 Posts]  
Post new topic   Reply to topic View previous topic :: View next topic
 Forum index » Off-Topic Area » Programming
Jump to:  

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.0441s ][ Queries: 11 (0.0058s) ][ GZIP on ]