0

I have an HDD with installed Ubuntu 16.04 on it, and now I need to install Windows as a second OS, without loosing any data in Linux. If I understood correctly, it can be done by creating a partition and installing Windows on the new one. However, I don't know how to create a partition table in ext4 file system, especially when it's the system disk.

GParted screen

I also tried to create the new partition table with booting from the flash drive and unmounting the sda3 in order to create the partition, but I did't reach success.

How can I create the partition table without loosing any data & stability of Linux? Also alternative ways are welcome.

1
  • You need to shrink the 900 GB partition and then create a partition in the empty space.
    – Mokubai
    Commented Jan 29, 2017 at 11:50

2 Answers 2

0

You can not create partition on ext4 filesystem. At the start of your disk there is a partition table (legacy BIOS type or GTP) which describes how many and how large partitions you have on the disk. Filesystems are created on those partitions. (NTFS filesystem for windows, EXT4 or several other types for linux.

In your case you have to do:

  1. Backup your important data first. It's crucial.
  2. Boot linux from pendrive (or any source, except your system disk)
  3. Resize your ext4 partition (reduce). You can not reduce mounted partitions that's why you have to boot from external source.
  4. Resize your system-virtual partition matching with the reduced ext4 size. I suggest you using a little bit bigger size than the ext4 size for safety shake. Because you are using lvm, you can do this with lvresize. You can even do step 3.+4. at once with lvresize --resizefs option which is the safest option.
  5. Now you have to reduce the physical volume with pvresize. Choose the size you will need for all future linux partitions (you can make partitions on the fly with lvm).
  6. Now delete+recreate your underlying GPT partition. If you delete+recreate your partition you MUST start the new partition at the same sector position than your deleted partition otherwise you will loose your lvm data (and most possibly your filesystem).
  7. Now you can create a new partition for windows (or you can let it make). (Before that I would check if I can boot linux).

The key commands are (all as root, or start with sudo) :

# to list your logical volumes with phyisical pv-s
lvs -o +devices
# if you want 20G linux  system disk 
lvresize -r /dev/ubuntu-gnome-vg/your-system-lv-name 20G
# if you want preserve 500G for linux (you can use this space for anything)
pvresize --setphysicalvolumesize 500G /dev/sda3
# now resize the 3rd partition on disk1 to 500G (maybe a bit bigger for safety)
fdisk/parted/gparted
# after a reboot you can match up your pv exactly with the physical partition with (this is useful if you used the safe method and created a bit bigger partition eg.510G)
pvresize /dev/sda3

While it's perfectly working (I did it several times) somewhat advanced method, not for beginners. If you are not very familiar with lvm/gpt it might be easier to back up your linux filesystem (mount it from your boot cd, tar+gzip your whole filesystem and copy on a pendrive/external hdd) Then create two partitions (three in fact, keep the small efi, one partition for win and one for linux), install windows, and recreate your linux filesystem from the tar.gz. You should back up anyway, so even if you choose the former method this can be your backup plan!

Misc note: Your windows might (and most possibly will) overwrite your boot manager (eg. grub) and your Linux will be unbootable. In case of UEFI/GPT you can select your os, on legacy systems you have to reinstall grub. That's easy, just boot from usb, chroot to the system filesystem and grub-install /dev/yourharddiskdevname. Many install usb (debian, ubuntu) can boot from your disk (you don't have to chroot then).

3
  • windows almost certainly will overwrite the bootloader
    – Journeyman Geek
    Commented Jan 29, 2017 at 12:11
  • no it won't, because in UEFI systems it can install its own alongside the Linux one Commented Jan 29, 2017 at 13:36
  • Yes, in case of UEFI there should be no problem. boot flag indicates it is an EFI based install. Anyway overwritten boot sector can be easily replaced. I now see the system is using lvm I upgrade the answer to reflect this.
    – goteguru
    Commented Jan 29, 2017 at 18:19
0

From my experience it's easier to install Windows first, then Linux.

However, Windows usually needs a NTFS partition. Windows overrides the grub2 bootmanager with it's own windows bootloader. While grub2 can handle to boot windows and linux (selection menu and default boot after < n > seconds), the windows bootloader just loads the windows OS.

  1. Backup system
  2. resize your linux partition
  3. create new partition and format it with NTFS filesystem
  4. boot from windows installer media (dvd/usb) and install windows on NTFS partition
  5. boot linux live installer media
  6. chroot your linux partition and fix grub2 (update/reinstall)

done

1
  • for 6. one can use the repair mode of the install media. Both ubuntu and debian install media can do that (at least the server distros). It's easier because it will do the bind mounts for you, or it can boot it's kernel directly with your system partition and you don't need to chroot.
    – goteguru
    Commented Jan 29, 2017 at 18:50

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .