Resizing NTFS partitions via PXE under Linux
Using PXE to install Windows and Linux on machines greatly reduces the time required to get machines ready for users. We normally install windows first, via RIS, then reduce the size of the C: drive before installing Linux.
The re-partitioning was traditionally performed using Partition Magic, although this is a reliable method it can be a little operator intensive and become tedious when lots of machines are involved. What we really wanted was a quick resize utility that could be used for this specific purpose. After investigating various possible options we came up with our solution; using a modified Linux Rescue mode and ntfsresize utility from the ntfsprogs RPM.
The method works uses a slightly modified stage2.img to contain the ntfsresize utility and shell scripts to simplify the resize process. The machine can be easily booted via PXE and if specified with a kickstart file will automatically mount the modified image to drop the user into a shell, where the resize script can be executed.
Building a modified stage2.img
Extracting the original image
- Locate stage2.img from the your Linux distribution and copy it somewhere accessible eg. /var/tmp - For RedHat Advanced Server distributions it lives in RedHat/base
- Mount the image using the loop device: mount -o loop /var/tmp/stage2.img /mnt
- Make a directory somewhere to extract the image into eg. /export/cramfs
- Extract the image into the above directory preserving everthing - links,
permissions etc. (Ideally cp -rpd would be used, however, this gave
some freaky errors about failing to hard-link a couple of directories which
I didn't have time to investigate so we used tar instead!)
cd /mnt ; tar cf - .buildstamp * | ( cd /export/cramfs ; tar xpf - )
- unmount the image: umount /mnt
Modifiying the image contents
- You will need to install ntfsprogs in order to extract the necessary components
- Copy the ntfsprogs utilities and libraries to your image
cd / ; tar cf - ./usr/bin/ntfs* ./usr/sbin/ntfs* ./usr/lib/libntfs* | ( cd /export/cramfs ; tar xpf -)
- Copy the resizer and partition-zap scripts to /export/cramfs/usr/sbin - Please note these scripts may do serious harm to your filesystems and partition table!
Rebuilding the image
- mkcramfs /export/cramfs /export/stage2.img
- Copy the modified image back to the distribution tree. You probably want to make a copy of the original first!
Booting the modified image
Add the following to your pxelinux.cfg config file (ensure you add the rescue option othewise you'll actually install Linux!)
label resizer kernel RedHat-4AS/i386/vmlinuz append load_ramdisk=1 prompt_ramdisk=0 initrd=RedHat-4AS/i386/initrd.img rescue ks=nfs:ks-host:/export/install/kickstart/client.cfg ksdevice=eth0
Set your machine to boot from PXE and boot the resizer image. Assuming the kickstart file contains an nfs or url install option the machine will automatically mount the distribution and start booting your modified stage2 image. When you are asked if you wish to search the machine for existing Linux installations press Skip and you will drop through to a shell.
From here you can run the resizer command. At present the command searches devices /dev/sda /dev/sdb /dev/hda /dev/hdb for NTFS partitions. The user is then prompted to select the NTFS partition to resize. The current size of the NTFS partition is displayed followed by the user being asked to enter the required size. At present it is only possible to reduce the size of a partition. Once the user has confirmed they wish to continue the NTFS filesystem is reduced to the new value following which the disk partition table is updated.