Resize img file linux

How do I resize a partition on an img file?

I have a puppy linux img that is 8gb but I need it to fit on a 6gb drive. How do I resize the ext2 partition on the img?

2 Answers 2

NOTE Make a backup, anything can go wrong . I ran all those as root :

  1. bind a loop device to the image: losetup /dev/loop10 $image_file
  2. refresh partitions: partprobe /dev/loop10
  3. adjust partition size: gparted /dev/loop10
  4. undo the loop: losetup -d /dev/loop10
  5. remove unwanted space: truncate -s -$G $image_file (you might want to calculate the exact size based on values from fdisk / cfdisk * sector size )

NOTE You might need to double check the partitions’ UUIDs, mine seem to have changed and I had to update fstab

Isn’t the truncate command eg. like this: truncate -s 6GB /yourimagefile.img or truncate -s 6442450944 /yourimagefile.img ? But even though I took the end sector and multiplied by the sector size it must somehow have been wrong, because afterwards parted told me «Can’t have partition outside the disk» and the image doesn’t work.

Also, the resizepart command in parted was very quick (I used parted instead of gparted ). Does it just cut of the partition at the given size? What if files are «physically» located outside the partition boundary — will they just be discarded?

for fdisk, taking into account only the end sector is not exact. The right calculation is: ( start_sector + partition_sector_count) * sector_size. You use this amount A (in bytes) for truncate: truncate —size= $image_file

This answer works great, but the truncate line kept throwing an error. So I added the approximate sizes of the partitions plus a 10M safety margin (eg. truncate -s 250M my.img) and that worked great and left a little unallocated space at the end of the last partition.

Источник

Can I expand the size of a file based disk image?

I created an empty disk image using dd, then I used mkfs to make it a real filesystem image. I am mounting and using it fine. What I need is to be able to expand or shrink this file based disk image when needed. Is it possible to increase the size of an disk image that way? Is there a way to make this file based disk image have a dynamic resizing feature like that is found with Virtual machine drives.

3 Answers 3

First you have to create an image file:

# dd if=/dev/zero of=./binary.img bs=1M count=1000 1000+0 records in 1000+0 records out 1048576000 bytes (1.0 GB) copied, 10.3739 s, 101 MB/s 

Then, you have to create a partition on it — you can use whatever tool you want, fdisk , parted , gparted , I prefer parted , so:

You have to create a partition table first and then one big partition:

(parted) mktable New disk label type? msdos (parted) mkpartfs WARNING: you are attempting to use parted to operate on (mkpartfs) a file system. parted's file system manipulation code is not as robust as what you'll find in dedicated, file-system-specific packages like e2fsprogs. We recommend you use parted only to manipulate partition tables, whenever possible. Support for performing most operations on most types of file systems will be removed in an upcoming release. Partition type? primary/extended? primary File system type? [ext2]? fat32 Start? 1 End? 1049M 
(parted) print Model: (file) Disk /media/binary.img: 1049MB Sector size (logical/physical): 512B/512B Partition Table: msdos Number Start End Size Type File system Flags 1 1049kB 1049MB 1048MB primary fat32 lba 

You want to enlarge it, so fist add some zeros to the image using dd:

# dd if=/dev/zero bs=1M count=400 >> ./binary.img 400+0 records in 400+0 records out 419430400 bytes (419 MB) copied, 2.54333 s, 165 MB/s root:/media# ls -al binary.img -rw-r--r-- 1 root root 1.4G Dec 26 06:47 binary.img 

That added 400M to the image:

# parted binary.img GNU Parted 2.3 Using /media/binary.img Welcome to GNU Parted! Type 'help' to view a list of commands. (parted) print Model: (file) Disk /media/binary.img: 1468MB Sector size (logical/physical): 512B/512B Partition Table: msdos Number Start End Size Type File system Flags 1 1049kB 1049MB 1048MB primary fat32 lba 

As you can see, the size of the image is different (1468MB). Parted can also show you free space in the image. If you want to see it just type print free instead of print . Now you have to add the extra space to the filesystem:

(parted) resize 1 WARNING: you are attempting to use parted to operate on (resize) a file system. parted's file system manipulation code is not as robust as what you'll find in dedicated, file-system-specific packages like e2fsprogs. We recommend you use parted only to manipulate partition tables, whenever possible. Support for performing most operations on most types of file systems will be removed in an upcoming release. Start? [1049kB]? End? [1049MB]? 1468M 
(parted) print Model: (file) Disk /media/binary.img: 1468MB Sector size (logical/physical): 512B/512B Partition Table: msdos Number Start End Size Type File system Flags 1 1049kB 1468MB 1467MB primary fat32 lba 

Pretty nice. If you want to shrink it, just do similar thing:

(parted) resize 1 WARNING: you are attempting to use parted to operate on (resize) a file system. parted's file system manipulation code is not as robust as what you'll find in dedicated, file-system-specific packages like e2fsprogs. We recommend you use parted only to manipulate partition tables, whenever possible. Support for performing most operations on most types of file systems will be removed in an upcoming release. Start? [1049kB]? End? [1468MB]? 500M 

Now you can check if the partition is smaller:

(parted) print Model: (file) Disk /media/binary.img: 1468MB Sector size (logical/physical): 512B/512B Partition Table: msdos Number Start End Size Type File system Flags 1 1049kB 500MB 499MB primary fat32 lba 

If you try to resize the partition when data is on it, you have to pay attention to the size of the data because when you shrink it too much, you will get an error:

Error: Unable to satisfy all constraints on the partition 

After shrinking the file system, you also have to cut some of the file off. But this is tricky. You could take the value from parted 500M (END):

# dd if=./binary.img of=./binary.img.new bs=1M count=500 

But this leaves some space at the end of the file. I’m not sure why, but the image works.

Читайте также:  Malware on linux systems

And there’s one thing about mounting such image — you have to know an offset to pass to the mount command. You can get the offset from, for instance, fdisk:

# fdisk -l binary.img Disk binary.img: 1468 MB, 1468006400 bytes 4 heads, 32 sectors/track, 22400 cylinders, total 2867200 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: 0x000f0321 Device Boot Start End Blocks Id System binary.img1 2048 2867198 1432575+ c W95 FAT32 (LBA) 

2048 (start) x 512 (sector size) = 1048576 , so you have to use the following command in order to mount the image:

# mount -o loop,offset=1048576 binary.img /mnt 

When you write «But this leaves some space at the end of the file. I’m not sure why, but the image works.» I think that it is caused by the fact that you are using different units of measurement. In parted, you have 500MB (in parted 1MB is 1000 * 1000 bytes), so 500000000 bytes; in dd you copy 1Mx500=500M, where M stands for 1024*1024 bytes, so you copy 524288000 bytes. The difference should be the space you left at the end of the file. To fix, you could do with truncate as described here softwarebakery.com/shrinking-images-on-linux or change the units of meas. in parted or in dd

Now it’s irrelevant to use parted with resize : «Error: The resize command has been removed in parted 3.0»

Yes, this is possible — it works just like a partition. I tried the following, which worked:

Make the original file, mount it, check, unmount it

dd if=/dev/zero of=test.file count=102400 mkfs.ext3 test.file mount test.file /m4 -o loop df umount /m4 

Grow it

dd if=/dev/zero count=102400 >> test.file mount test.file /m4 -o loop df resize2fs /dev/loop0 df 

There is no reason why shrinking a file would not work similarly, but shrinking a file is always more difficult then growing a file (and, of-course, needs to be done when the block device is not mounted etc)

Читайте также:  Сгенерировать ssh key linux

Have a look at this link which talks about using qemu-nbd to mount qcow2 images

Great, this is exactly the recipe I needed. However, I needed to run e2fsck -f test.file (on my image file) before resizing. Another thing: adding 70G on a drive image stored on an NTFS partition through dd . >> drive_image can take some 10 minutes.

Sparse files are a good choice for dynamic grow/resize disk images.

This will create a 1024M sparse file:

# dd if=/dev/zero of=sparse.img bs=1M count=0 seek=1024 0+0 records in 0+0 records out 0 bytes (0 B) copied, 0.000565999 s, 0.0 kB/s 

The image is not using any disk space,

# du -m sparse.img 0 sparse.img 

but has the apparent size of 1024M.

# ls -l sparse.img -rw-rw-r--. 1 root root 1073741824 Sep 22 14:22 sparse.img # du -m --apparent-size sparse.img 1024 sparse.img 

You can format and mount it as a regular disk image:

# parted sparse.img GNU Parted 2.1 Using /tmp/sparse.img Welcome to GNU Parted! Type 'help' to view a list of commands. (parted) mktable New disk label type? msdos (parted) mkpartfs WARNING: you are attempting to use parted to operate on (mkpartfs) a file system. parted's file system manipulation code is not as robust as what you'll find in dedicated, file-system-specific packages like e2fsprogs. We recommend you use parted only to manipulate partition tables, whenever possible. Support for performing most operations on most types of file systems will be removed in an upcoming release. Partition type? primary/extended? primary File system type? [ext2]? fat32 Start? 1 End? 1024M (parted) print Model: (file) Disk /tmp/sparse.img: 1074MB Sector size (logical/physical): 512B/512B Partition Table: msdos Number Start End Size Type File system Flags 1 1049kB 1024MB 1023MB primary fat32 lba # du -m sparse.img 2 sparse.img 

Now, resize using the same command to create just changing the seek parameter with the new size of image:

dd if=/dev/zero of=sparse.img bs=1M count=0 seek=2048 

As you can see, the image is now 2048M and you can enlarge the partition using parted or other tool of your choice.

# du -m --apparent-size sparse.img 2048 sparse.img # parted sparse.img GNU Parted 2.1 Using /tmp/sparse.img Welcome to GNU Parted! Type 'help' to view a list of commands. (parted) print free Model: (file) Disk /tmp/sparse.img: 2147MB Sector size (logical/physical): 512B/512B Partition Table: msdos Number Start End Size Type File system Flags 16.4kB 1049kB 1032kB Free Space 1 1049kB 1024MB 1023MB primary fat32 lba 1024MB 2147MB 1123MB Free Space (parted) # du -m sparse.img 2 sparse.img 

Источник

Читайте также:  Linux password windows recovery

How to resize an image through the terminal? [duplicate]

It adds two context menu items in nautlius so you can right click and choose «Resize Image».(The other is «Rotate Image»).

You can do a whole directory of images in one go if you like and you don’t even have to open up an application to do so.

imgp is a relatively new utility that does image resize and rotation. It has more features than nautilus-image-converter.

Since Ubuntu ships with Python, you can also use a Python script to achieve this with a little more control over what happens — see this stackoverflow question for example scripts. Those examples use just the standard library.

import os, sys import Image size = 128, 128 for infile in sys.argv[1:]: outfile = os.path.splitext(infile)[0] + ".thumbnail" if infile != outfile: try: im = Image.open(infile) im.thumbnail(size, Image.ANTIALIAS) im.save(outfile, "JPEG") except IOError: print "cannot create thumbnail for '%s'" % infile 

And another example where you only have to specify the width (as the width variable):

from PIL import Image import sys filename = sys.argv[1:] basewidth = 300 img = Image.open(filename) wpercent = (basewidth/float(img.size[0])) hsize = int((float(img.size[1])*float(wpercent))) img = img.resize((basewidth,hsize), Image.ANTIALIAS) img.save(filename) 

Now, how to do this through the terminal.

Paste one of those blocks of code into the text editor. Ctrl+x to exit (say yes to save changes).

python resizescript.py yourfilenamehere.jpg 
python resizescript.py yourfilenamehere.jpg 

You must be in the same directory as the picture files for both of these scripts. The first one shrinks the image to 128×128 pixels. The second script makes it 300 pixels wide and calculates the proportional height. This is more of a Python answer, but it is done all through the terminal technically.

Источник

Оцените статью
Adblock
detector